refactor xbee complet
Fork of Repo_Noeud_Mobile by
Embed:
(wiki syntax)
Show/hide line numbers
MMA8452Q.cpp
00001 #include "MMA8452Q.h" 00002 Serial a_pc(USBTX, USBRX); 00003 I2C i2c(p28, p27); 00004 00005 Accel::Accel() 00006 { 00007 } 00008 00009 uint8_t Accel::get_WHO_AM_I() 00010 { 00011 char cmd = WHO_AM_I; 00012 char data[2] = {0x00, 0x00}; 00013 nack = i2c.write(W_ADDR, &cmd, 1, true); //send a request to WHO_AM_I register, we expect 0x2a, or 42, the answer to the universe. 00014 wait(SPEED); 00015 if (!nack) { 00016 nack = i2c.read(R_ADDR, data, 1); 00017 wait(SPEED); 00018 if (!nack) { 00019 if(data[0] == CTRL_REG1) { 00020 a_pc.printf("I2C communication succes: Module MMA8452 online\r\n"); 00021 } 00022 } else { 00023 a_pc.printf("I2C communication fail: Module MMA8452 offline\r\n"); 00024 a_pc.getc(); 00025 return 1; 00026 } 00027 } 00028 return 0; 00029 } 00030 00031 uint8_t Accel::set_CTRL_REG1() 00032 { 00033 char cmd[2]; 00034 cmd[0] = CTRL_REG1; 00035 cmd[1] = 0x01; //byte to enable the device 00036 00037 nack = i2c.write(W_ADDR, cmd, 2, true); //comand to enable the device 00038 wait(SPEED); 00039 if (!nack) { 00040 a_pc.printf("I2C communication success: CTRL_REG1 configured\r\n"); 00041 } else { 00042 a_pc.printf("I2C communication fail: Could not configure CTRL_REG1\r\n"); 00043 a_pc.getc(); 00044 return 1; 00045 } 00046 return 0; 00047 } 00048 00049 00050 uint8_t Accel::init_MMA8452() 00051 { 00052 Serial a_pc(USBTX, USBRX); 00053 a_pc.printf("\r\n\r\n================================================\r\n\r\n"); 00054 a_pc.printf("Accelerometer I2C interface: Initialization sequence starting...\r\n"); 00055 00056 00057 get_WHO_AM_I(); 00058 wait_ms(50); 00059 set_CTRL_REG1(); 00060 00061 00062 return 0; 00063 } 00064 00065 00066 accel_t Accel::get_axis_values() 00067 { 00068 char cmd = X_OUT_MSB; 00069 char data[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 00070 accel_t a_data; 00071 00072 00073 nack = i2c.write(W_ADDR, &cmd, 1, true); //request the data in the 6 out registers 00074 wait(SPEED); 00075 if (!nack) { 00076 nack = i2c.read(R_ADDR, data, 6); //receive the data from the 6 out registers 00077 wait(SPEED); 00078 a_data.x = (data[0] << 4) + (data[1] >> 4); 00079 a_data.y = (data[2] << 4) + (data[3] >> 4); 00080 a_data.z = (data[4] << 4) + (data[5] >> 4); 00081 00082 00083 if (!nack) { 00084 } 00085 } else { 00086 a_pc.printf("I2C communcation fail: Command %#X at address %#X\n", cmd, W_ADDR); 00087 a_pc.getc(); 00088 } 00089 return a_data; 00090 } 00091 00092 00093 uint8_t Accel::set_STANDBY_MODE() 00094 { 00095 char cmd[2]; 00096 cmd[0] = CTRL_REG1; 00097 cmd[1] = 0x18; //byte to put the device in standby mode 00098 00099 nack = i2c.write(W_ADDR, cmd, 2, true); //command to put the device in standby mode 00100 wait(SPEED); 00101 if (!nack) { 00102 a_pc.printf("I2C communication success: Device now in STANDBY mode\r\n"); 00103 } else { 00104 a_pc.printf("I2C communication fail: Could not put device in STANDBY mode\r\n"); 00105 a_pc.getc(); 00106 return 1; 00107 } 00108 return 0; 00109 } 00110 00111 uint8_t Accel::set_TRANSIENT_MODE(char axis, char count, char debounce) 00112 { 00113 set_STANDBY_MODE(); 00114 char cmd[2]; 00115 cmd[0] = TRANSIENT_CFG; 00116 cmd[1] = axis; 00117 nack = i2c.write(W_ADDR, cmd, 2, true); //command to configure the TRANSIENT_CFG Register 00118 wait(SPEED); 00119 if (!nack) { 00120 a_pc.printf("I2C communication success: Device configured for TRANSIENT_DETECTION mode on selected axis\r\n"); 00121 } else { 00122 a_pc.printf("I2C communication fail: Could not put device in TRANSIENT_DETECTION mode on selected axis\r\n"); 00123 a_pc.getc(); 00124 return 1; 00125 } 00126 00127 cmd[0] = THRESHOLD_REG; 00128 cmd[1] = count; 00129 nack = i2c.write(W_ADDR, cmd, 2, true); //command to configure the TRANSIENT_CFG Register 00130 wait(SPEED); 00131 if (!nack) { 00132 a_pc.printf("I2C communication success: Device configured for THRESHOLD_REG mode on threshold count\r\n"); 00133 } else { 00134 a_pc.printf("I2C communication fail: Could not put device in THRESHOLD_REG mode on threshold count \r\n"); 00135 a_pc.getc(); 00136 return 1; 00137 } 00138 00139 cmd[0] = DEBOUNCE_CFG; 00140 cmd[1] = debounce; 00141 nack = i2c.write(W_ADDR, cmd, 2, true); //command to configure the TRANSIENT_CFG Register 00142 wait(SPEED); 00143 if (!nack) { 00144 a_pc.printf("I2C communication success: Device configured for DEBOUNCE_CFG mode on debounce count\r\n"); 00145 } else { 00146 a_pc.printf("I2C communication fail: Could not put device in DEBOUNCE_CFG mode on debounce count \r\n"); 00147 a_pc.getc(); 00148 return 1; 00149 } 00150 cmd[0] = CTRL_REG4; 00151 cmd[1] = 0x20; 00152 nack = i2c.write(W_ADDR, cmd, 2, true); //command to configure the TRANSIENT_CFG Register 00153 wait(SPEED); 00154 if (!nack) { 00155 a_pc.printf("I2C communication success: Device configured for CTRL_REG4 \r\n"); 00156 } else { 00157 a_pc.printf("I2C communication fail: Could not put device in CTRL_REG4\r\n"); 00158 a_pc.getc(); 00159 return 1; 00160 } 00161 00162 cmd[0] = CTRL_REG5; 00163 cmd[1] = 0x20; 00164 nack = i2c.write(W_ADDR, cmd, 2, true); //command to configure the TRANSIENT_CFG Register 00165 wait(SPEED); 00166 if (!nack) { 00167 a_pc.printf("I2C communication success: Device configured for CTRL_REG5\r\n"); 00168 } else { 00169 a_pc.printf("I2C communication fail: Could not put device in CTRL_REG5 \r\n"); 00170 a_pc.getc(); 00171 return 1; 00172 } 00173 00174 set_CTRL_REG1(); 00175 return 0; 00176 } 00177 00178 uint8_t Accel::clear_TRANSIENT_INTERRUPT() 00179 { 00180 char cmd = TRANSIENT_VALUE; 00181 char data[2] = {0x00, 0x00}; 00182 nack = i2c.write(W_ADDR, &cmd, 1, true); //command to put the device in standby mode 00183 wait(SPEED); 00184 if (!nack) { 00185 nack = i2c.read(R_ADDR, data, 1); 00186 if(!nack) { 00187 a_pc.printf("I2C communication success: Cleared TRANSIENT_REG\r\n"); 00188 } else { 00189 a_pc.printf("I2C communication fail: Could not clear TRANSIENT_REG\r\n"); 00190 a_pc.getc(); 00191 return 1; 00192 00193 } 00194 } 00195 return 0; 00196 }
Generated on Thu Jul 21 2022 13:34:51 by
1.7.2
