├── README.md ├── main.c ├── custom_board.h ├── config.h ├── LIS3DH.h ├── TWI.c ├── I2C_Test.uvoptx └── I2C_Test.uvprojx /README.md: -------------------------------------------------------------------------------- 1 | # CJMCU-8223 2 | NRF51822+LIS3DH 3 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Madhavagg/CJMCU-8223/HEAD/main.c -------------------------------------------------------------------------------- /custom_board.h: -------------------------------------------------------------------------------- 1 | #ifndef CUSTOM_BOARD_H 2 | #define CUSTOM_BOARD_H 3 | 4 | #define LEDS_NUMBER 2 5 | 6 | #define LED_START 21 7 | #define LED_1 23 8 | #define LED_2 24 9 | 10 | #define LED_STOP 24 11 | 12 | #define LEDS_LIST { LED_1, LED_2} 13 | 14 | #define BSP_LED_0 LED_1 15 | #define BSP_LED_1 LED_2 16 | 17 | #define BSP_LED_0_MASK (1< Bit 0 = 5 | 6 | static const uint8_t REG_STATUS_AUX = 0x07; 7 | static const uint8_t REG_OUT_ADC1_L = 0x08; 8 | static const uint8_t REG_OUT_ADC1_H = 0x09; 9 | static const uint8_t REG_OUT_ADC2_L = 0x0a; 10 | static const uint8_t REG_OUT_ADC2_H = 0x0b; 11 | static const uint8_t REG_OUT_ADC3_L = 0x0c; 12 | static const uint8_t REG_OUT_ADC3_H = 0x0d; 13 | static const uint8_t REG_INT_COUNTER_REG = 0x0e; 14 | static const uint8_t REG_WHO_AM_I = 0x0f; 15 | static const uint8_t REG_TEMP_CFG_REG = 0x1f; 16 | static const uint8_t REG_CTRL_REG1 = 0x20; 17 | static const uint8_t REG_CTRL_REG2 = 0x21; 18 | static const uint8_t REG_CTRL_REG3 = 0x22; 19 | static const uint8_t REG_CTRL_REG4 = 0x23; 20 | static const uint8_t REG_CTRL_REG5 = 0x24; 21 | static const uint8_t REG_CTRL_REG6 = 0x25; 22 | static const uint8_t REG_REFERENCE = 0x26; 23 | static const uint8_t REG_STATUS_REG = 0x27; 24 | static const uint8_t REG_OUT_X_L = 0x28; 25 | static const uint8_t REG_OUT_X_H = 0x29; 26 | static const uint8_t REG_OUT_Y_L = 0x2a; 27 | static const uint8_t REG_OUT_Y_H = 0x2b; 28 | static const uint8_t REG_OUT_Z_L = 0x2c; 29 | static const uint8_t REG_OUT_Z_H = 0x2d; 30 | static const uint8_t REG_FIFO_CTRL_REG = 0x2e; 31 | static const uint8_t REG_FIFO_SRC_REG = 0x2f; 32 | static const uint8_t REG_INT1_CFG = 0x30; 33 | static const uint8_t REG_INT1_SRC = 0x31; 34 | static const uint8_t REG_INT1_THS = 0x32; 35 | static const uint8_t REG_INT1_DURATION = 0x33; 36 | static const uint8_t REG_CLICK_CFG = 0x38; 37 | static const uint8_t REG_CLICK_SRC = 0x39; 38 | static const uint8_t REG_CLICK_THS = 0x3a; 39 | static const uint8_t REG_TIME_LIMIT = 0x3b; 40 | static const uint8_t REG_TIME_LATENCY = 0x3c; 41 | static const uint8_t REG_TIME_WINDOW = 0x3d; 42 | static const uint8_t REG_ACT_THS = 0x3e; 43 | static const uint8_t REG_ACT_DUR = 0x3f; 44 | 45 | #endif //LIS3DH.h 46 | 47 | -------------------------------------------------------------------------------- /TWI.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "config.h" 7 | #include "nrf_drv_twi.h" 8 | #include "app_error.h" 9 | #include "nrf.h" 10 | #include "bsp.h" 11 | #include "app_util_platform.h" 12 | #include "nrf_drv_config.h" 13 | #include "custom_board.h" 14 | #include "LIS3DH.h" 15 | 16 | 17 | extern nrf_drv_twi_t m_twi_master; 18 | extern ret_code_t twi_master_init(void); 19 | extern uint8_t LIS3DH_read(uint8_t addr); 20 | extern ret_code_t LIS3DH_write(uint8_t addr, uint8_t wdata); 21 | 22 | /** 23 | * @brief Initialize the master TWI 24 | * 25 | * Function used to initialize master TWI interface that would communicate with LIS3DH. 26 | * 27 | * @return NRF_SUCCESS or the reason of failure 28 | */ 29 | 30 | ret_code_t twi_master_init(void) 31 | { 32 | ret_code_t ret; 33 | const nrf_drv_twi_config_t config = 34 | { 35 | .scl = TWI_SCL_PIN, 36 | .sda = TWI_SDA_PIN, 37 | .frequency = NRF_TWI_FREQ_100K, 38 | .interrupt_priority = APP_IRQ_PRIORITY_HIGH 39 | }; 40 | 41 | do 42 | { 43 | ret = nrf_drv_twi_init(&m_twi_master, &config, NULL, NULL); 44 | if(NRF_SUCCESS != ret) 45 | { 46 | break; 47 | } 48 | nrf_drv_twi_enable(&m_twi_master); 49 | }while(0); 50 | return ret; 51 | } 52 | /** 53 | * @brief Read data from LIS3DH 54 | * 55 | * Function uses TWI interface to read data from LIS3DH. 56 | * 57 | * @param addr Register address to read 58 | * 59 | * @return Read value. 60 | */ 61 | 62 | uint8_t LIS3DH_read(uint8_t addr) 63 | { 64 | ret_code_t ret; 65 | static uint8_t receive_data; 66 | do 67 | { 68 | 69 | ret = nrf_drv_twi_tx(&m_twi_master, LIS3DH_SLAVE_ADDRESS, &addr, 1, true); 70 | if(NRF_SUCCESS != ret) 71 | { 72 | break; 73 | } 74 | ret = nrf_drv_twi_rx(&m_twi_master, LIS3DH_SLAVE_ADDRESS, &receive_data, 1); 75 | }while(0); 76 | return receive_data; 77 | } 78 | /** 79 | * @brief Write data to LIS3DH 80 | * 81 | * Function uses TWI interface to write data into LIS3DH. 82 | * 83 | * @param addr Register address to write 84 | * @param[in] wdata Data to send 85 | * 86 | * @return NRF_SUCCESS or reason of error. 87 | * 88 | */ 89 | 90 | ret_code_t LIS3DH_write(uint8_t addr, uint8_t wdata) 91 | { 92 | ret_code_t ret; 93 | 94 | uint8_t tx_data[2] = {addr, wdata}; 95 | ret=nrf_drv_twi_tx(&m_twi_master, LIS3DH_SLAVE_ADDRESS, tx_data, sizeof(tx_data), false); 96 | 97 | return ret; 98 | } 99 | -------------------------------------------------------------------------------- /I2C_Test.uvoptx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1.0 5 | 6 |
### uVision Project, (C) Keil Software
7 | 8 | 9 | *.c 10 | *.s*; *.src; *.a* 11 | *.obj; *.o 12 | *.lib 13 | *.txt; *.h; *.inc 14 | *.plm 15 | *.cpp 16 | 0 17 | 18 | 19 | 20 | 0 21 | 0 22 | 23 | 24 | 25 | Target 1 26 | 0x4 27 | ARM-ADS 28 | 29 | 12000000 30 | 31 | 0 32 | 1 33 | 0 34 | 1 35 | 0 36 | 37 | 38 | 1 39 | 65535 40 | 0 41 | 0 42 | 0 43 | 44 | 45 | 79 46 | 66 47 | 8 48 | .\ 49 | 50 | 51 | 1 52 | 1 53 | 1 54 | 0 55 | 1 56 | 1 57 | 0 58 | 1 59 | 0 60 | 0 61 | 0 62 | 0 63 | 64 | 65 | 1 66 | 1 67 | 1 68 | 1 69 | 1 70 | 1 71 | 1 72 | 0 73 | 0 74 | 75 | 76 | 0 77 | 0 78 | 1 79 | 80 | 5 81 | 82 | 0 83 | 1 84 | 1 85 | 1 86 | 1 87 | 1 88 | 1 89 | 1 90 | 1 91 | 1 92 | 1 93 | 1 94 | 1 95 | 1 96 | 0 97 | 1 98 | 1 99 | 1 100 | 1 101 | 0 102 | 0 103 | 1 104 | 0 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | BIN\UL2CM3.DLL 116 | 117 | 118 | 119 | 0 120 | UL2CM3 121 | UL2CM3(-S0 -C0 -P0 -FD20000000 -FC4000 -FN1 -FF0nrf51xxx -FS00 -FL0200000 -FP0($$Device:nRF51822_xxAA$Flash\nrf51xxx.flm)) 122 | 123 | 124 | 125 | 126 | 0 127 | 128 | 129 | 0 130 | 1 131 | 0 132 | 0 133 | 0 134 | 0 135 | 0 136 | 0 137 | 0 138 | 0 139 | 0 140 | 0 141 | 0 142 | 0 143 | 0 144 | 0 145 | 0 146 | 0 147 | 0 148 | 0 149 | 0 150 | 0 151 | 0 152 | 0 153 | 154 | 155 | 156 | 0 157 | 0 158 | 0 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | Source Group 1 168 | 0 169 | 0 170 | 0 171 | 0 172 | 173 | 174 |
175 | -------------------------------------------------------------------------------- /I2C_Test.uvprojx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 2.1 5 | 6 |
### uVision Project, (C) Keil Software
7 | 8 | 9 | 10 | Target 1 11 | 0x4 12 | ARM-ADS 13 | 14 | 15 | nRF51822_xxAA 16 | Nordic Semiconductor 17 | NordicSemiconductor.nRF_DeviceFamilyPack.8.13.0 18 | http://developer.nordicsemi.com/nRF5_SDK/pieces/nRF_DeviceFamilyPack/ 19 | IRAM(0x20000000,0x4000) IROM(0x00000000,0x40000) CPUTYPE("Cortex-M0") CLOCK(12000000) ELITTLE 20 | 21 | 22 | UL2CM3(-S0 -C0 -P0 -FD20000000 -FC4000 -FN1 -FF0nrf51xxx -FS00 -FL0200000 -FP0($$Device:nRF51822_xxAA$Flash\nrf51xxx.flm)) 23 | 0 24 | $$Device:nRF51822_xxAA$Device\Include\nrf.h 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | $$Device:nRF51822_xxAA$SVD\nrf51.svd 35 | 0 36 | 0 37 | 38 | 39 | 40 | 41 | 42 | 43 | 0 44 | 0 45 | 0 46 | 0 47 | 1 48 | 49 | .\ 50 | I2C_Test 51 | 1 52 | 0 53 | 0 54 | 1 55 | 1 56 | .\ 57 | 1 58 | 0 59 | 0 60 | 61 | 0 62 | 0 63 | 64 | 65 | 0 66 | 0 67 | 0 68 | 0 69 | 70 | 71 | 0 72 | 0 73 | 74 | 75 | 0 76 | 0 77 | 0 78 | 0 79 | 80 | 81 | 0 82 | 0 83 | 84 | 85 | 0 86 | 0 87 | 0 88 | 0 89 | 90 | 0 91 | 92 | 93 | 94 | 0 95 | 0 96 | 0 97 | 0 98 | 0 99 | 1 100 | 0 101 | 0 102 | 0 103 | 0 104 | 3 105 | 106 | 107 | 1 108 | 109 | 110 | SARMCM3.DLL 111 | 112 | DARMCM1.DLL 113 | -pCM0 114 | SARMCM3.DLL 115 | 116 | TARMCM1.DLL 117 | -pCM0 118 | 119 | 120 | 121 | 1 122 | 0 123 | 0 124 | 0 125 | 16 126 | 127 | 128 | 0 129 | 1 130 | 1 131 | 1 132 | 1 133 | 1 134 | 1 135 | 1 136 | 0 137 | 1 138 | 139 | 140 | 1 141 | 1 142 | 1 143 | 1 144 | 1 145 | 1 146 | 0 147 | 1 148 | 1 149 | 1 150 | 151 | 0 152 | 0 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | BIN\UL2CM3.DLL 167 | 168 | 169 | 170 | 171 | 1 172 | 0 173 | 0 174 | 1 175 | 0 176 | -1 177 | 178 | 1 179 | BIN\UL2CM3.DLL 180 | 181 | 182 | 183 | 184 | 185 | 0 186 | 187 | 188 | 189 | 0 190 | 1 191 | 1 192 | 1 193 | 1 194 | 1 195 | 1 196 | 1 197 | 0 198 | 1 199 | 1 200 | 0 201 | 1 202 | 1 203 | 0 204 | 0 205 | 1 206 | 1 207 | 1 208 | 1 209 | 1 210 | 1 211 | 1 212 | 1 213 | 1 214 | 0 215 | 0 216 | "Cortex-M0" 217 | 218 | 0 219 | 0 220 | 0 221 | 1 222 | 1 223 | 0 224 | 0 225 | 0 226 | 0 227 | 0 228 | 8 229 | 0 230 | 0 231 | 0 232 | 0 233 | 3 234 | 3 235 | 0 236 | 0 237 | 0 238 | 0 239 | 0 240 | 0 241 | 0 242 | 0 243 | 0 244 | 0 245 | 1 246 | 0 247 | 0 248 | 0 249 | 0 250 | 1 251 | 0 252 | 253 | 254 | 0 255 | 0x0 256 | 0x0 257 | 258 | 259 | 0 260 | 0x0 261 | 0x0 262 | 263 | 264 | 0 265 | 0x0 266 | 0x0 267 | 268 | 269 | 0 270 | 0x0 271 | 0x0 272 | 273 | 274 | 0 275 | 0x0 276 | 0x0 277 | 278 | 279 | 0 280 | 0x0 281 | 0x0 282 | 283 | 284 | 0 285 | 0x20000000 286 | 0x4000 287 | 288 | 289 | 1 290 | 0x0 291 | 0x40000 292 | 293 | 294 | 0 295 | 0x0 296 | 0x0 297 | 298 | 299 | 0 300 | 0x0 301 | 0x0 302 | 303 | 304 | 0 305 | 0x0 306 | 0x0 307 | 308 | 309 | 0 310 | 0x0 311 | 0x0 312 | 313 | 314 | 1 315 | 0x0 316 | 0x40000 317 | 318 | 319 | 0 320 | 0x0 321 | 0x0 322 | 323 | 324 | 0 325 | 0x0 326 | 0x0 327 | 328 | 329 | 0 330 | 0x0 331 | 0x0 332 | 333 | 334 | 0 335 | 0x0 336 | 0x0 337 | 338 | 339 | 0 340 | 0x20000000 341 | 0x4000 342 | 343 | 344 | 0 345 | 0x0 346 | 0x0 347 | 348 | 349 | 350 | 351 | 352 | 1 353 | 1 354 | 0 355 | 0 356 | 1 357 | 0 358 | 0 359 | 0 360 | 0 361 | 0 362 | 2 363 | 0 364 | 0 365 | 1 366 | 0 367 | 1 368 | 1 369 | 1 370 | 1 371 | 0 372 | 0 373 | 0 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 1 383 | 0 384 | 0 385 | 0 386 | 0 387 | 0 388 | 0 389 | 0 390 | 0 391 | 0 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 0 401 | 0 402 | 0 403 | 0 404 | 1 405 | 0 406 | 0x00000000 407 | 0x20000000 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | Source Group 1 421 | 422 | 423 | 424 | 425 | 426 |
427 | --------------------------------------------------------------------------------