您的位置:首页 > 其它

6、MPU6050例程

2015-08-18 09:38 405 查看
main.c函数中
宏定义->测试串口

quit() && start() 输出字符串
show_error()死循环
test()向串口输入再读取(利用等待读一个字符串的方式)比较两次是否一样进行测试

//#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 */

#define ERROR_PIN                (LED_0)   /*!< gpio pin number to show error if loopback is enabled */
#define MAX_TEST_DATA_BYTES      (15U) /*!< max number of test bytes to be used for tx and rx */

#ifndef ENABLE_LOOPBACK_TEST

/** @brief Function for sending ' Exit!' string to UART.
Execution is blocked until UART peripheral detects all characters have been sent.
*/
 static __INLINE void uart_quit()
 {
simple_uart_putstring((const uint8_t *)" \n\rExit!\n\r");
}

/** @brief Function for sending 'Start: ' string to UART.
Execution is blocked until UART peripheral detects all characters have been sent.
*/
 static __INLINE void uart_start()
 {
simple_uart_putstring((const uint8_t *)" \n\rStart: ");
}

#else

/** @brief Function for setting @ref ERROR_PIN to one and enter an infinite loop. This function is called if any of the
*  nRF6350 functions fail.
*/
 static void show_error(void)
 {
nrf_gpio_pin_write(ERROR_PIN, 1);
while(true)
{
}
}

/** @brief Function for transmitting one char at a time as check if the loopback received data is same as transmitted
*  Just used for testing with loopback setup (i.e, @ref TX_PIN_NUMBER connected to @ref RX_PIN_NUMBER)
*  return true if test passed, else return false
*/
 static void uart_loopback_test()
 {
uint8_t tx_data[] = ("\n\r  LOOPBACK_TEST");
uint8_t rx_data[MAX_TEST_DATA_BYTES] = {0};

// Start sending one byte and see if you get the same
for(uint8_t i = 0; i < MAX_TEST_DATA_BYTES; i++)
{
bool status;
simple_uart_put(tx_data[i]);
if(!simple_uart_get_with_timeout(2, &rx_data[i]))
{
show_error();
}
}

for(uint8_t i = 0; i < MAX_TEST_DATA_BYTES; i++)
{
if ((rx_data[i] != tx_data[i]))
{
show_error();
}
}
return; // Test passed
}

#endif


main.c中
simple_uart_config初始化串口,引脚和属性

twi_master_init();->i2C总线
mpu6050_init(0x69)->传进去的是mpu6050在i2c总线中的地址

bool mpu6050_register_read(uint8_t register_address, uint8_t *destination, uint8_t number_of_bytes)

在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);

/**
* @brief Function for application main entry.
* @return 0. int return type required by ANSI/ISO standard.
*/
int main(void)
{
uint8_t id;
uint8_t t;
int16_t tem1[3], tem2[3];
simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC);

#ifndef ENABLE_LOOPBACK_TEST

uart_start();
twi_master_init();

printf("mpu6050 test\r\n");
if(mpu6050_init(0x69) == false)
{
printf("mpu6050 init fail\r\n");
}
mpu6050_register_read(0x75U, &id, 1);
printf("mpu6050 id is %d \r\n", id);
while(true)
{
27         MPU6050_ReadAcc( &tem1[0], &tem1[1] , &tem1[2] );
28         MPU6050_ReadGyro(&tem2[0] , &tem2[1] , &tem2[2] );

printf("ACC:  %d    %d  %d  ", tem1[0], tem1[1], tem1[2]);
printf("GYRO: %d    %d  %d  \r\n", tem2[0], tem2[1], tem2[2]);

nrf_delay_us(10000);

/*uint8_t cr = simple_uart_get();
simple_uart_put(cr);

if(cr == 'q' || cr == 'Q')
{
uart_quit();
while(1){}
}*/
}

#else
/* This part of the example is just for testing, can be removed if you do not have a loopback setup */

// ERROR_PIN configure as output
nrf_gpio_cfg_output(ERROR_PIN);
while(true)
{
uart_loopback_test();
}
#endif
}


效果:





链接:http://pan.baidu.com/s/1pJMgqCz
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: