HC32F460内部配有RTC功能,这里我们为它配上OLED屏显示,这样它就构成一个RTC电子时钟。
所用的OLED屏就是随板子自带的,该OLED屏的原理图见图1所示。
芯片破解
芯片破解
本想以I2C通讯来驱动OLED屏,但将其移到RTC的程序中,比较难协调,最终还是以模拟I2C的方式来轻松地将两者结合在一起。
由于SDA和SCL 仍占用原位置,故定义其输出高低电平的语句如下:
#define OLED_SCLK_Set() PORT_SetBits(PortD, Pin00)
#define OLED_SCLK_Clr() PORT_ResetBits(PortD, Pin00)
#define OLED_SDIN_Set() PORT_SetBits(PortD, Pin01)
#define OLED_SDIN_Clr() PORT_ResetBits(PortD, Pin01)
所用的字符显示函数为:
- 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++;
- }
- }
复制代码
所用的数值显示函数为:
- void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size2)
- {
- uint8_t t,temp;
- uint8_t enshow=0;
- for(t=0;t<len;t++)
- {
- temp=(num/oled_pow(10,len-t-1))%10;
- if(enshow==0&&t<(len-1))
- {
- if(temp==0)
- {
- OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
- continue;
- }else enshow=1;
- }
- OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2);
- }
- }