本帖最后由 jinglixixi 于 2021-10-20 19:28 编辑
星空派开发板提供了使用I2C_OLED屏的接口,此外该接口还与AT24C02共用该接口,其电路原理图见图1所示。
图1 OLED屏接口 使用该接口本人所驱动的是一个0.91寸的单色OLED屏,其连接方式与显示效果如图2所示。
图2 显示效果 为了驱动该显示屏,这里采用的是以GPIO口来模拟I2C通讯。 按驱动的要求,其引脚功能配置的初始化函数为:
- void IIC_Init(void)
- {
- rcu_periph_clock_enable(RCU_GPIOB);
- gpio_init(GPIOB, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
- }
复制代码
使相应引脚输出高低电平的语句定义为: #define OLED_SCLK_Clr() gpio_bit_reset(GPIOB, GPIO_PIN_6) #define OLED_SCLK_Set() gpio_bit_set(GPIOB, GPIO_PIN_6) #define OLED_SDIN_Clr() gpio_bit_reset(GPIOB, GPIO_PIN_7) #define OLED_SDIN_Set() gpio_bit_set(GPIOB, GPIO_PIN_7) 对应的显示屏初始化函数为:
- void OLED_Init(void)
- {
- Write_IIC_Command(0xAE); //display off
- Write_IIC_Command(0x40); //--set start line address
- Write_IIC_Command(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
- Write_IIC_Command(0xc8); //Set COM Output Scan Direction
- Write_IIC_Command(0x81); //--set contrast control register
- Write_IIC_Command(0xff);
- Write_IIC_Command(0xa1); //--set segment re-map 0 to 127
- Write_IIC_Command(0xa6); //--set normal display
- Write_IIC_Command(0xa8); //--set multiplex ratio(1 to 64)
- Write_IIC_Command(0x1F);
- Write_IIC_Command(0xd3); //-set display offset
- Write_IIC_Command(0x00); //-not offset
- Write_IIC_Command(0xd5); //--set display clock divide atio/oscillator frequency
- Write_IIC_Command(0xf0); //--set divide ratio
- Write_IIC_Command(0xd9); //--set pre-charge period
- Write_IIC_Command(0x22);
- Write_IIC_Command(0xda); //--set com pins hardware configuration
- Write_IIC_Command(0x02);
- Write_IIC_Command(0x8d); //--set DC-DC enable
- Write_IIC_Command(0x14);
- Write_IIC_Command(0xdb); //--set vcomh
- Write_IIC_Command(0x49); //0x20,0.77xVcc
- Write_IIC_Command(0xaf); //--turn on oled panel
- }
复制代码
实现显示屏的清屏函数为:
- void OLED_Clear(void)
- {
- uint8_t i,n;
- for(i=0;i<8;i++)
- {
- OLED_WR_Byte(0xb0+i,OLED_CMD);
- OLED_WR_Byte(0x00,OLED_CMD);
- OLED_WR_Byte(0x10,OLED_CMD);
- for(n=0;n<128;n++) OLED_WR_Byte(0,OLED_DATA);
- }
- }
复制代码
为实现显示效果,其字符串显示函数为:
- void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
- {
- unsigned char j=0;
- while (chr[j]!='\0')
- {
- OLED_ShowChar(x,y,chr[j],Char_Size);
- x+=8;
- if(x>120)
- {
- x=0;
- y+=2;
- }
- j++;
- }
- }
复制代码
实现图2显示效果的主程序为:
- int main(void)
- {
- /* configure systick */
- systick_config();
- IIC_Init();
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"GD32F303 TEST",16);
- OLED_ShowString(0,2,"OLED DISPLAY",16);
- while(1);
- }
复制代码
在GD32F303片内配有RTC计时器,将它与OLED屏相配合可以快速地实现电子时钟的功能。为保证RTC的持续工作,在开发板上配置了2种维持工作的供电方式,其电路原理图见图3所示。
图3 后备供电方式 在未配置OLED屏的情况下,RTC的使用方式如图4所示。在首次运行时,会要求设置初始时间,输入方式是通过串口来依次方式时分秒的值。随后RTC开始计时,并在10秒后启动闹钟功能来点亮LED。
图4 RTC运行效果 在配置OLED屏的显示功能后,其显示效果如图5所示。
图5 电子时钟显示效果 实现电子时钟的主程序为:
- int main(void)
- {
- key_init();
- IIC_Init();
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"GD32F303 TEST",16);
- OLED_ShowString(0,2,"OLED & RTC ",16);
- while(SET == key_state_get());
- OLED_Clear();
- OLED_ShowString(8,0,"RTC CLOCK ",16);
- OLED_ShowString(0,2," : :",16);
- /* NVIC config */
- nvic_configuration();
- printf( "rn This is a RTC demo...... rn" );
- if (bkp_read_data(BKP_DATA_0) != 0xA5A5){
- /* backup data register value is not correct or not yet programmed
- (when the first time the program is executed) */
- printf("rnThis is a RTC demo!rn");
- printf("rnn RTC not yet configured....");
- /* RTC configuration */
- rtc_configuration();
- printf("rn RTC configured....");
- /* adjust time by values entred by the user on the hyperterminal */
- time_adjust();
- bkp_write_data(BKP_DATA_0, 0xA5A5);
- }else{
- /* check if the power on reset flag is set */
- if (rcu_flag_get(RCU_FLAG_PORRST) != RESET){
- printf("rnn Power On Reset occurred....");
- }else if (rcu_flag_get(RCU_FLAG_SWRST) != RESET){
- /* check if the pin reset flag is set */
- printf("rnn External Reset occurred....");
- }
- printf("rn No need to configure RTC....");
- /* wait for RTC registers synchronization */
- rtc_register_sync_wait();
- rtc_lwoff_wait();
- /* enable the RTC second */
- rtc_interrupt_enable(RTC_INT_SECOND);
- rtc_interrupt_enable(RTC_INT_ALARM);
- /* wait until last write operation on RTC registers has finished */
- rtc_lwoff_wait();
- }
- #ifdef RTCCLOCKOUTPUT_ENABLE
- /* enable PMU and BKPI clocks */
- rcu_periph_clock_enable(RCU_BKPI);
- rcu_periph_clock_enable(RCU_PMU);
- /* allow access to BKP domain */
- pmu_backup_write_enable();
- /* disable the tamper pin */
- bkp_tamper_detection_disable();
- /* enable RTC clock output on tamper Pin */
- bkp_rtc_calibration_output_enable();
- #endif
- /* clear reset flags */
- rcu_all_reset_flag_clear();
- /* display time in infinite loop */
- time_showv();
- while (1){
- }
- }
复制代码
实现显示效果的相关函数为:
- void time_displayv(uint32_t timevar)
- {
- uint32_t thh = 0, tmm = 0, tss = 0;
- thh = timevar / 3600;
- // compute minutes
- tmm = (timevar % 3600) / 60;
- // compute seconds
- tss = (timevar % 3600) % 60;
- printf(" Time: %0.2d:%0.2d:%0.2drn", thh, tmm, tss);
- OLED_ShowNum(0,2,thh,2,16);
- OLED_ShowNum(24,2,tmm,2,16);
- OLED_ShowNum(48,2,tss,2,16);
- }
- void time_showv(void)
- {
- printf("nr");
- // infinite loop
- while (1){
- // if 1s has paased
- if (timedisplay == 1){
- // display current time
- time_displayv(rtc_counter_get());
- timedisplay = 0;
- }
- }
- }
复制代码
0
|
|
|
|