6、MPU6050例程
1 //#define ENABLE_LOOPBACK_TEST /*!< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback */ 2 3 #define ERROR_PIN (LED_0) /*!< gpio pin number to show error if loopback is enabled */ 4 #define MAX_TEST_DATA_BYTES (15U) /*!< max number of test bytes to be used for tx and rx */ 5 6 #ifndef ENABLE_LOOPBACK_TEST 7 8 /** @brief Function for sending \' Exit!\' string to UART. 9 Execution is blocked until UART peripheral detects all characters have been sent. 10 */ 11 static __INLINE void uart_quit() 12 { 13 simple_uart_putstring((const uint8_t *)" \n\rExit!\n\r"); 14 } 15 16 /** @brief Function for sending \'Start: \' string to UART. 17 Execution is blocked until UART peripheral detects all characters have been sent. 18 */ 19 static __INLINE void uart_start() 20 { 21 simple_uart_putstring((const uint8_t *)" \n\rStart: "); 22 } 23 24 #else 25 26 /** @brief Function for setting @ref ERROR_PIN to one and enter an infinite loop. This function is called if any of the 27 * nRF6350 functions fail. 28 */ 29 static void show_error(void) 30 { 31 nrf_gpio_pin_write(ERROR_PIN, 1); 32 while(true) 33 { 34 } 35 } 36 37 38 /** @brief Function for transmitting one char at a time as check if the loopback received data is same as transmitted 39 * Just used for testing with loopback setup (i.e, @ref TX_PIN_NUMBER connected to @ref RX_PIN_NUMBER) 40 * return true if test passed, else return false 41 */ 42 static void uart_loopback_test() 43 { 44 uint8_t tx_data[] = ("\n\r LOOPBACK_TEST"); 45 uint8_t rx_data[MAX_TEST_DATA_BYTES] = {0}; 46 47 // Start sending one byte and see if you get the same 48 for(uint8_t i = 0; i < MAX_TEST_DATA_BYTES; i++) 49 { 50 bool status; 51 simple_uart_put(tx_data[i]); 52 if(!simple_uart_get_with_timeout(2, &rx_data[i])) 53 { 54 show_error(); 55 } 56 } 57 58 for(uint8_t i = 0; i < MAX_TEST_DATA_BYTES; i++) 59 { 60 if ((rx_data[i] != tx_data[i])) 61 { 62 show_error(); 63 } 64 } 65 return; // Test passed 66 } 67 68 #endif
simple_uart_config初始化串口,引脚和属性
- 在mpu6050驱动文件中read_acc和read_gyro调用register_read
- i2c驱动函数对外提供:
- bool twi_master_init(void);
- bool twi_master_transfer(uint8_t address, uint8_t *data, uint8_t data_length, bool issue_stop_condition);
1 /** 2 * @brief Function for application main entry. 3 * @return 0. int return type required by ANSI/ISO standard. 4 */ 5 int main(void) 6 { 7 uint8_t id; 8 uint8_t t; 9 int16_t tem1[3], tem2[3]; 10 simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC); 11 12 #ifndef ENABLE_LOOPBACK_TEST 13 14 uart_start(); 15 twi_master_init(); 16 17 18 printf("mpu6050 test\r\n"); 19 if(mpu6050_init(0x69) == false) 20 { 21 printf("mpu6050 init fail\r\n"); 22 } 23 mpu6050_register_read(0x75U, &id, 1); 24 printf("mpu6050 id is %d \r\n", id); 25 while(true) 26 { 27 MPU6050_ReadAcc( &tem1[0], &tem1[1] , &tem1[2] ); 28 MPU6050_ReadGyro(&tem2[0] , &tem2[1] , &tem2[2] ); 29 30 printf("ACC: %d %d %d ", tem1[0], tem1[1], tem1[2]); 31 printf("GYRO: %d %d %d \r\n", tem2[0], tem2[1], tem2[2]); 32 33 nrf_delay_us(10000); 34 35 36 /*uint8_t cr = simple_uart_get(); 37 simple_uart_put(cr); 38 39 if(cr == \'q\' || cr == \'Q\') 40 { 41 uart_quit(); 42 while(1){} 43 }*/ 44 } 45 46 #else 47 /* This part of the example is just for testing, can be removed if you do not have a loopback setup */ 48 49 // ERROR_PIN configure as output 50 nrf_gpio_cfg_output(ERROR_PIN); 51 while(true) 52 { 53 uart_loopback_test(); 54 } 55 #endif 56 }
效果:
链接:http://pan.baidu.com/s/1pJMgqCz