好几天没有发贴了,今天发贴来说下FireBLE的OLED移植,就是把官方的OLED应用到自己的项目中来,虽然坛子里有FireBLE的OLED的文章,但是没有讲清楚,只不过是在官方的例子中自己加了个字库,换了个文字而已。好了废话不多说了,开始吧。
实验目的:
把官方OLED移植到自己的项目中来。(我是移植到MPU6050项目中,实时显示 开发板的姿态数据)
实验电路:
实验步骤:
1、首先把官方的OLED三个文件复制到自己的项目中来(oled.h、oled.c、oledfont.h)
2、在keil的工程选项中添加两个宏定义:FB_OLED,FB_SPI_OLED
3、用Qblue配置IO
实验代码:
system.c
- /**
- ****************************************************************************************
- *
- * [url=home.php?mod=space&uid=1455510]@file[/url] system.c
- *
- * @brief User system setup and initial configuration source file.
- *
- * Copyright (C) Quintic 2012-2013
- *
- * $Rev: 1.0 $
- *
- ****************************************************************************************
- */
- #include "system.h"
- #include "uart.h"
- #include "gpio.h"
- #include "spi.h"
- #include "timer.h"
- #include "pwm.h"
- #include "dma.h"
- #include "serialflash.h"
- #include "adc.h"
- #include "analog.h"
- #include "calibration.h"
- #include "i2c.h"
- uint8_t i2cbuffer[104];
- /**
- ****************************************************************************************
- * @brief GPIO functionn configuration.
- *
- * Set Pins as gpio pin or function pin
- *****************************************************************************************
- */
- static void SystemIOCfg(void)
- {
- // P0.0 -> UART0 [TX] Pull Up Drive ability: Low
- // P0.3 -> GPIO Pull Up Drive ability: Low
- // P0.6 -> SWD [DAT] Pull Up Drive ability: Low
- // P0.7 -> SWD [CLK] Pull Up Drive ability: Low
- // P1.0 -> GPIO Pull Up Drive ability: Low
- // P1.1 -> SPI1 [DOUT] Pull Up Drive ability: Low
- // P1.2 -> GPIO Pull Up Drive ability: Low
- // P1.3 -> SPI1 [CLK] Pull Up Drive ability: Low
- // P1.7 -> UART0 [RX] Pull Up Drive ability: Low
- // P2.3 -> I2C [SDA] Pull Up Drive ability: Low
- // P2.4 -> I2C [SCL] Pull Up Drive ability: Low
- // P2.6 -> GPIO Pull Up Drive ability: Low
- // P2.7 -> GPIO Pull Up Drive ability: Low
- // P3.0 -> GPIO Pull Up Drive ability: Low
- // P3.1 -> GPIO Pull Up Drive ability: Low
- //Pin Mux Control Register
- syscon_SetPMCR0(QN_SYSCON, P00_UART0_TXD_PIN_CTRL
- | P03_GPIO_3_PIN_CTRL
- | P06_SW_DAT_PIN_CTRL
- | P07_SW_CLK_PIN_CTRL
- | P10_GPIO_8_PIN_CTRL
- | P11_SPI1_DAT_PIN_CTRL
- | P12_GPIO_10_PIN_CTRL
- | P13_SPI1_CLK_PIN_CTRL
- | P17_UART0_RXD_PIN_CTRL);
- //Pin Mux Control Register
- syscon_SetPMCR1(QN_SYSCON, P23_I2C_SDA_PIN_CTRL
- | P24_I2C_SCL_PIN_CTRL
- | P26_GPIO_22_PIN_CTRL
- | P27_GPIO_23_PIN_CTRL
- | P30_GPIO_24_PIN_CTRL
- | P31_GPIO_25_PIN_CTRL);
- //Pin Mux Control Register
- syscon_SetPMCR2(QN_SYSCON, 0);
- //Pull Control Register
- syscon_SetPPCR0(QN_SYSCON, 0xAAAAAAAA);
- //Pull Control Register
- syscon_SetPPCR1(QN_SYSCON, 0xAAAAAAAA);
- //Drive ability Control Register
- syscon_SetPDCR(QN_SYSCON, 0x00000000);
- }
- /**
- ****************************************************************************************
- * @brief Setup the microcontroller system.
- *
- * Initialize the system clock and pins.
- *****************************************************************************************
- */
- void SystemInit(void)
- {
- /*
- **************************
- * Sub module clock setting
- **************************
- */
- syscon_SetIvrefX32WithMask(QN_SYSCON, SYSCON_MASK_DVDD12_SW_EN, MASK_ENABLE);
- syscon_set_sysclk_src(CLK_XTAL, __XTAL);
-
- #if __XTAL == XTAL_32MHz
- calibration_init(XTAL_32M);
- #else
- calibration_init(XTAL_16M);
- #endif
- ref_pll_calibration();
- clk32k_enable(__32K_TYPE);
- #if QN_32K_RCO
- rco_calibration();
- #endif
- // Disable all peripheral clock, will be enabled in the driver initilization.
- timer_clock_off(QN_TIMER0);
- timer_clock_off(QN_TIMER1);
- timer_clock_off(QN_TIMER2);
- timer_clock_off(QN_TIMER3);
- uart_clock_off(QN_UART0);
- uart_clock_off(QN_UART1);
- spi_clock_off(QN_SPI0);
- usart_reset((uint32_t) QN_SPI1);
- spi_clock_off(QN_SPI1);
- flash_clock_off();
- gpio_clock_off();
- adc_clock_off();
- dma_clock_off();
- pwm_clock_off();
-
- // calibration will change system clock setting
- // Configure sytem clock.
- syscon_set_sysclk_src(CLK_XTAL, __XTAL);
- syscon_set_ahb_clk(__AHB_CLK);
- syscon_set_apb_clk(__APB_CLK);
- syscon_set_ble_clk(__BLE_CLK);
- syscon_set_timer_clk(__TIMER_CLK);
- syscon_set_usart_clk((uint32_t)QN_UART0, __USART_CLK);
- syscon_set_usart_clk((uint32_t)QN_UART1, __USART_CLK);
-
- /*
- **************************
- * IO configuration
- **************************
- */
- SystemIOCfg();
- /*
- **************************
- * Peripheral setting
- **************************
- */
- //Initialize UART
- 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);
-
- //Initialize Timer
- timer_init(QN_TIMER0, NULL);
- timer_config(QN_TIMER0, TIMER_PSCAL_DIV, TIMER_COUNT_US(1000, TIMER_PSCAL_DIV));
- timer_enable(QN_TIMER0, MASK_ENABLE);
-
- //Initialize I2C master
- i2c_init(I2C_SCL_RATIO(100000), i2cbuffer, 104);
-
- spi_init(QN_SPI1, SPI_BITRATE(1000000), SPI_8BIT, SPI_MASTER_MOD);
- spi_clock_on(QN_SPI1);
-
- gpio_clock_on();
- }
- /// @} SYSTEM_CONTROLLER
复制代码
main.c代码片段
- /* Main Program */
- int main (void)
- {
- SystemInit();
-
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,(uint8_t*)" MPU6050 DEMO");
-
-
- int result = mpu_init(NULL);
- if(result == MPU_OK)
- {
- printf("mpu initialization complete......n "); //mpu_set_sensor
- if(mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL) == MPU_OK)
- printf("mpu_set_sensor complete ......n");
- else
- printf("mpu_set_sensor come across error ......n");
- if(mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL) == MPU_OK) //mpu_configure_fifo
- printf("mpu_configure_fifo complete ......n");
- else
- printf("mpu_configure_fifo come across error ......n");
- if(mpu_set_sample_rate(DEFAULT_MPU_HZ) == MPU_OK) //mpu_set_sample_rate
- printf("mpu_set_sample_rate complete ......n");
- else
- printf("mpu_set_sample_rate error ......n");
- if(dmp_load_motion_driver_firmware() == MPU_OK) //dmp_load_motion_driver_firmvare
- printf("dmp_load_motion_driver_firmware complete ......n");
- else
- printf("dmp_load_motion_driver_firmware come across error ......n");
- if(dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_orientation)) == MPU_OK) //dmp_set_orientation
- printf("dmp_set_orientation complete ......n");
- else
- printf("dmp_set_orientation come across error ......n");
- if(dmp_enable_feature(DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_TAP |
- DMP_FEATURE_ANDROID_ORIENT | DMP_FEATURE_SEND_RAW_ACCEL | DMP_FEATURE_SEND_CAL_GYRO |
- DMP_FEATURE_GYRO_CAL) == MPU_OK) //dmp_enable_feature
- printf("dmp_enable_feature complete ......n");
- else
- printf("dmp_enable_feature come across error ......n");
- if(dmp_set_fifo_rate(DEFAULT_MPU_HZ) == MPU_OK) //dmp_set_fifo_rate
- printf("dmp_set_fifo_rate complete ......n");
- else
- printf("dmp_set_fifo_rate come across error ......n");
- run_self_test();
- if(mpu_set_dmp_state(1) == MPU_OK)
- printf("mpu_set_dmp_state complete ......n");
- else
- printf("mpu_set_dmp_state come across error ......n");
- }
-
-
- while (1) /* Loop forever */
- {
- float q0=1.0f,q1=0.0f,q2=0.0f,q3=0.0f;
-
- float Pitch,Roll,Yaw;
- unsigned long sensor_timestamp;
- short gyro[3], accel[3], sensors;
- unsigned char more;
- long quat[4];
-
- dmp_read_fifo(gyro, accel, quat, &sensor_timestamp, &sensors,&more);
- /* Gyro and accel data are written to the FIFO by the DMP in chip
- * frame and hardware units. This behavior is convenient because it
- * keeps the gyro and accel outputs of dmp_read_fifo and
- * mpu_read_fifo consistent.
- */
- /* Unlike gyro and accel, quaternions are written to the FIFO in
- * the body frame, q30. The orientation is set by the scalar passed
- * to dmp_set_orientation during initialization.
- */
- if (sensors & INV_WXYZ_QUAT )
- {
- q0=quat[0] / q30;
- q1=quat[1] / q30;
- q2=quat[2] / q30;
- q3=quat[3] / q30;
- Pitch = asin(2 * q1 * q3 - 2 * q0* q2)* 57.3; // pitch
- Roll = atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2* q2 + 1)* 57.3; // roll
- Yaw = atan2(2*(q1*q2 + q0*q3),q0*q0+q1*q1-q2*q2-q3*q3) * 57.3;
- usart1_report_imu(accel[0],
- accel[1],
- accel[2],
- gyro[0],
- gyro[1],
- gyro[2],
- (short)(Roll*100),
- (short)(Pitch*100),
- (short)(Yaw*10));
-
- sprintf(oledBuffer,"Roll:%d",(short)(Roll*100));
- OLED_ShowString(0,2,oledBuffer);
- sprintf(oledBuffer,"Pitch:%d",(short)(Pitch*100));
- OLED_ShowString(0,4,&oledBuffer[0]);
- sprintf(oledBuffer,"Yaw:%d",(short)(Yaw*10));
- OLED_ShowString(0,6,&oledBuffer[0]);
-
- }
- //delay(5000*10);
- }
- }
复制代码
实验视频:
0
|
|
|
|