This is test purpose program only for GR-PEACH. This program only run one hour 11 minutes!

Dependencies:   L3GD20 LIS3DH TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * mbed Application program for the mbed
00003  *      Test program for GR-PEACH
00004  *
00005  * Copyright (c) 2014 Kenji Arai / JH1PJL
00006  *  http://www.page.sannet.ne.jp/kenjia/index.html
00007  *  http://mbed.org/users/kenjiArai/
00008  *      Created: November  29th, 2014
00009  *      Revised: January   10th, 2015
00010  *
00011  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
00012  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
00013  * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00014  * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00015  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00016  */
00017 
00018 //  Include ---------------------------------------------------------------------------------------
00019 #include    "mbed.h"
00020 #include    "L3GD20.h"
00021 #include    "LIS3DH.h"
00022 #include    "TextLCD.h"
00023 
00024 //  Definition ------------------------------------------------------------------------------------
00025 #define USE_COM         // use Communication with PC(UART)
00026 #define USE_I2C_LCD
00027 #define USE_I2C_SENSOR
00028  
00029 // Com
00030 #ifdef  USE_COM
00031 #define BAUD(x)             pcx.baud(x)
00032 #define GETC(x)             pcx.getc(x)
00033 #define PUTC(x)             pcx.putc(x)
00034 #define PRINTF(...)         pcx.printf(__VA_ARGS__)
00035 #define READABLE(x)         pcx.readable(x)
00036 #else
00037 #define BAUD(x)             {;}
00038 #define GETC(x)             {;}
00039 #define PUTC(x)             {;}
00040 #define PRINTF(...)         {;}
00041 #define READABLE(x)         {;}
00042 #endif
00043 
00044 //  Object ----------------------------------------------------------------------------------------
00045 // LED's
00046 DigitalOut LEDs[4] = {
00047     DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4)
00048 };
00049 // Swiches
00050 DigitalIn   USER_SWITCH[2] = {
00051     #if defined(TARGET_RZ_A1H)
00052     DigitalIn(P6_0),  DigitalIn(P6_1)
00053     #elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE)\
00054      || defined(TARGET_NUCLEO_L152RE)
00055     DigitalIn(PC_13),  DigitalIn(A0)
00056     #elif defined(TARGET_LPC1768)
00057     DigitalIn(A0),  DigitalIn(A1)
00058     #elif defined(TARGET_K64F)
00059     DigitalIn(PTA4),  DigitalIn(PTC6)
00060     #endif
00061 };
00062 // com
00063 #ifdef USE_COM
00064 Serial      pcx(USBTX, USBRX);      // Communication with Host
00065 #endif
00066 I2C i2c(D14,D15);
00067 // Gyro
00068 L3GX_GYRO   gyro(i2c, L3GD20_V_CHIP_ADDR, L3GX_DR_95HZ, L3GX_BW_HI, L3GX_FS_250DPS);
00069 // Acc
00070 LIS3DH      acc(i2c, LIS3DH_G_CHIP_ADDR, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_8G);
00071 #ifdef USE_I2C_LCD
00072 // LCD
00073 TextLCD_I2C_N lcd0(&i2c, 0x7c, TextLCD::LCD16x2);  // LCD(Akizuki AQM0802A)
00074 #endif
00075 
00076 //  RAM -------------------------------------------------------------------------------------------
00077 float fa[3];    // Acc  0:X, 1:Y, 2:Z
00078 float fg[3];    // Gyro 0:X, 1:Y, 2:Z
00079 
00080 uint8_t show_flag;
00081 uint32_t count;
00082 
00083 //  ROM / Constant data ---------------------------------------------------------------------------
00084 
00085 //  Function prototypes ---------------------------------------------------------------------------
00086 
00087 //  Function prototypes ---------------------------------------------------------------------------
00088 
00089 //-------------------------------------------------------------------------------------------------
00090 //  Control Program
00091 //-------------------------------------------------------------------------------------------------
00092 void blink(void const *n) {
00093     LEDs[(int)n] = !LEDs[(int)n];
00094 }
00095 
00096 // Read switch status
00097 int read_sw(uint8_t n){
00098     if (USER_SWITCH[n] == 0){   return 1;
00099     } else {                    return 0;}
00100 }
00101 
00102 // Update sensor data
00103 void update_angle(void){
00104 #ifdef USE_I2C_SENSOR
00105     // read acceleration data from sensor
00106     acc.read_data(fa);
00107     // read gyroscope data from sensor
00108     gyro.read_data(fg);
00109 #else
00110     fa[0] = fa[1] = fa[2] = 1.11f;
00111     fg[0] = fg[1] = fg[2] = 1.11f;
00112 #endif
00113 }
00114 
00115 // Update sensor data
00116 void display(void){
00117 #ifdef USE_I2C_LCD
00118     lcd0.locate(0, 0);    // 1st line top
00119     lcd0.printf(" G=%4.1f ", sqrt(fa[0]*fa[0] + fa[1]*fa[1] + fa[2]*fa[2]));
00120     lcd0.locate(0, 1);    // 2nd line top
00121     lcd0.printf("%8d",count++);
00122 #endif
00123 }
00124 
00125 void send_pc(void){
00126     PRINTF("G:%+6.1f,%+6.1f,%+6.1f,  ", fg[0], fg[1], fg[2]);
00127     PRINTF("A:%+6.1f,%+6.1f,%+6.1f \r\n", fa[0], fa[1], fa[2]);
00128 }
00129 
00130 int main(void) {
00131     // I2C LCD
00132 #ifdef USE_I2C_LCD
00133     lcd0.locate(0, 0);    // 1st line top
00134     lcd0.printf("I2C test");
00135     lcd0.locate(0, 1);    // 2nd line top
00136     lcd0.puts(" JH1PJL ");
00137     lcd0.setContrast(0x14);
00138 #endif
00139     count = 0; 
00140     while (true) {
00141         update_angle();
00142         display();
00143         send_pc();
00144         //wait(0.2);
00145         blink((void *)0);
00146         //wait(0.2);
00147         blink((void *)1);
00148         //wait(0.2);
00149         blink((void *)2);
00150         //wait(0.2);
00151         blink((void *)3);
00152         //wait(0.2);
00153     }
00154 }