本帖最后由 elecfans跑堂 于 2015-9-7 09:05 编辑
首记念抗战胜利70周,缅怀一下为了中华民族生死存亡而英勇牺牲的英雄们。
实验目的:
今天学习下FireBLE的IIC,因为FireBLE提供了SDK,所以不用再去为IIC的协议去写什么流程之类的,直接使用SDK,所以没有什么难度,就是配置一下IO,初始化一下需要用到的资源,这里我们用到了UART,IIC,其它没有了。
移植MPU6050也比较简单,把之前仿照mbed定义的抽象API实现两个函数IIC的读写函数就搞定了,MPU6050的驱动代码都不用改动,甚是简单,在这里不得不佩服下mbed的设计思想。
实验电路:
实验代码:
用QBlue生成的配置IO的代码,覆盖掉system.c的相应函数
- static void SystemIOCfg(void)
- {
- // pin mux
- syscon_SetPMCR0(QN_SYSCON, P00_UART0_TXD_PIN_CTRL
- | P01_GPIO_1_PIN_CTRL
- | P02_GPIO_2_PIN_CTRL
- | P03_GPIO_3_PIN_CTRL
- | P04_GPIO_4_PIN_CTRL
- | P05_GPIO_5_PIN_CTRL
- | P06_SW_DAT_PIN_CTRL
- | P07_SW_CLK_PIN_CTRL
- | P10_GPIO_8_PIN_CTRL
- | P11_GPIO_9_PIN_CTRL
- | P12_GPIO_10_PIN_CTRL
- | P13_GPIO_11_PIN_CTRL
- | P14_GPIO_12_PIN_CTRL
- | P15_GPIO_13_PIN_CTRL
- | P16_GPIO_14_PIN_CTRL
- | P17_UART0_RXD_PIN_CTRL
- );
- syscon_SetPMCR1(QN_SYSCON, P20_GPIO_16_PIN_CTRL
- | P21_GPIO_17_PIN_CTRL
- | P22_GPIO_18_PIN_CTRL
- | P23_I2C_SDA_PIN_CTRL
- | P24_I2C_SCL_PIN_CTRL
- | P25_GPIO_21_PIN_CTRL
- | P26_GPIO_22_PIN_CTRL
- | P27_GPIO_23_PIN_CTRL
- | P30_GPIO_24_PIN_CTRL
- | P31_GPIO_25_PIN_CTRL
- | P32_GPIO_26_PIN_CTRL
- | P33_GPIO_27_PIN_CTRL
- | P34_GPIO_28_PIN_CTRL
- | P35_GPIO_29_PIN_CTRL
- | P36_GPIO_30_PIN_CTRL
- );
- /**
- * Pin select
- **** I2C
- * Pin23 <--> I2C_SDA
- * Pin24 <--> I2C_SCL
- */
- syscon_SetPMCR2(QN_SYSCON, SYSCON_MASK_UART1_PIN_SEL | SYSCON_MASK_SPI0_PIN_SEL);
- // driver ability
- syscon_SetPDCR(QN_SYSCON, 0x0); // 0 : low driver, 1 : high driver
- // pin pull ( 00 : High-Z, 01 : Pull-down, 10 : Pull-up, 11 : Reserved )
- syscon_SetPPCR0(QN_SYSCON, 0xAAAA5AAA);
- syscon_SetPPCR1(QN_SYSCON, 0x2AAAAAAA);
- }
复制代码
主程序代码就相当简单了
- /**
- ****************************************************************************************
- *
- * [url=home.php?mod=space&uid=1455510]@file[/url] main.c
- *
- * @brief main program
- *
- * Copyright (C) sunsjw 2015-2018
- *
- * $Rev: 1.0 $
- *
- ****************************************************************************************
- */
- #include "uart.h"
- #include "i2c.h"
- #include "system.h"
- #include "MPU6050.h"
- #include
- //i2c缓冲区
- uint8_t i2cbuffer[104];
- int main(void)
- {
- //系统初始化
- SystemInit();
- //UART0 初始化
- uart_init(QN_UART0, __USART_CLK, UART_115200);
- uart_tx_enable(QN_UART0, MASK_ENABLE);
- uart_rx_enable(QN_UART0, MASK_ENABLE);
- uart_clock_on(QN_UART0);
- //i2c初始化
- i2c_init(I2C_SCL_RATIO(100000),i2cbuffer,104);
-
- //MPU6050初始化
- InitMPU6050();
-
- while(1)
- {
- //读取加速度和角速度值
- int16_t ax = GetAccelValue('x');
- int16_t ay = GetAccelValue('y');
- int16_t az = GetAccelValue('z');
- int16_t gx = GetGyroValue('x');
- int16_t gy = GetGyroValue('y');
- int16_t gz = GetGyroValue('z');
- //输出
- printf("accX:%d accY:%d accZ:%d rn",ax,ay,az);
- printf("gyroX:%d gyroY:%d gyroZ:%d rn",gx,gy,gz);
- }
- }
复制代码
实验结果:上位机打印出来的结果如下
MPU6050的代码太多了就不贴出来,有需要的同学,可以给我发消息。
0
|
|
|
|