在精英STM32F103 开发板上为RTC配备了纽扣电池座以支持其在断电RTC计时器继续工作,利用OLED屏为显示工具,结合光盘中的例程可以快速实现RTC电子时钟功能,其效果如图1所示。
若以GPIO口来为OLED屏供电,还可将OLED屏直接插入板上的OLED/OV7670连接座,其效果如图2所示。
RTC计时效果1
RTC计时效果2
在图 2的使用方式下,OLED屏与MCU的连接关系为:
VCC -- 3.3V
GND -- PD6
SCLK -- PG14
SDIN -- PG15
相关引脚的定义为:
#define OLED_L PDout(6)
#define OLED_SCLK PGout(14)
#define OLED_SDIN PGout(15)
设置引脚为输出口的语句为:
RCC->APB2ENR|=1<<5; // PORTD
GPIOD->CRL&=0XF0FFFFFF; //PD6 OUT
GPIOD->CRL|=0X03000000;
GPIOD->ODR|=1<<6;
OLED_L=0;
RCC->APB2ENR|=1<<8; //PORTG
GPIOG->CRH&=0X0FFFFFF; //PG14 、PG 15 OUT
GPIOG->CRH|=0X33000000;
GPIOG->ODR|=3<<14;
实现RTC电子时钟的主程序为:
- int main(void)
- {
- u8 t;
- Stm32_Clock_Init(9);
- delay_init(72);
- usmart_dev.init(72);
- LED_Init();
- OLED_Initi();
- OLED_ClearI();
- OLED_ShowStringI(10,0,"STM32F103 RTC",16);
- while(RTC_Init())
- {
- OLED_ShowStringI(10,0,"RTC ERROR!",16);
- delay_ms(800);
- OLED_ShowStringI(10,0,"RTC Trying...",16);
- }
- OLED_ShowStringI(0,4," - - ",16);
- OLED_ShowStringI(0,6," : : ",16);
- while(1)
- {
- if(t!=calendar.sec)
- {
- t=calendar.sec;
- OLED_ShowNumI(8,4,calendar.w_year,4,16);
- OLED_ShowNumI(48,4,calendar.w_month,2,16);
- OLED_ShowNumI(72,4,calendar.w_date,2,16);
- switch(calendar.week)
- {
- case 0:
- OLED_ShowStringI(10,2,"Sunday",16);
- break;
- case 1:
- OLED_ShowStringI(10,2,"Monday",16);
- break;
- case 2:
- OLED_ShowStringI(10,2,"Tuesday",16);
- break;
- case 3:
- OLED_ShowStringI(10,2,"Wednesday",16);
- break;
- case 4:
- OLED_ShowStringI(10,2,"Thursday",16);
- break;
- case 5:
- OLED_ShowStringI(10,2,"Friday",16);
- break;
- case 6:
- OLED_ShowStringI(10,2,"Saturday",16);
- break;
- }
- OLED_ShowNumI(8,6,calendar.hour,2,16);
- OLED_ShowNumI(32,6,calendar.min,2,16);
- OLED_ShowNumI(54,6,calendar.sec,2,16);
- LED0=!LED0;
- }
- delay_ms(10);
- };
- }
复制代码
|