本帖最后由 jinglixixi 于 2020-6-17 23:59 編輯
在NV32F100的內部也配有RTC功能,但它不同與常規的RTC 它在例程中并沒有通過RTC初試值的設置與讀取方法,為了實現RTC的計時功能,我們可以模擬其計時的進制關系來實現。此外,通過一個I2C接口的雙色OLED屏可以將計時器顯示出來,其效果如圖所示。[size=13.3333px]
51hei截圖16370701393724.png (560.49 KB, 下載次數: 63)
下載附件
2020-6-17 23:38 上傳
OLED屏與NV32F100的連接關系為: VCC -- 5V/3.3V GND -- GND SCL -- PE1 SDA -- PE0
相應引腳輸出高低電平的語句定義為: #define OLED_SCLK_Clr() GPIO_PinClear(GPIO_PTE1); #define OLED_SCLK_Set() GPIO_PinSet(GPIO_PTE1);
#define OLED_SDIN_Clr() GPIO_PinClear(GPIO_PTE0); #defineOLED_SDIN_Set() GPIO_PinSet(GPIO_PTE0);
實現RTC時鐘的程序代碼如下:
- #include "common.h"
- #include "ics.h"
- #include "rtc.h"
- #include "uart.h"
- #include "gpio.h"
- #include "sysinit.h"
- #include "start.h"
- #include "oledfont.h"
- #include "oled.h"
- void cd11()
- {
- OLED_Clear();
- OLED_ShowString(24,0,"RTC CLOCK",16);
- OLED_ShowString(24,4,"jinglixixi",16);
- }
- void RTC_Task(void);
- int main (void)
- {
- sysinit();
- cpu_identify();
-
- RTC_ConfigType sRTCConfig;
- RTC_ConfigType *pRTCConfig = &sRTCConfig;
-
- pRTCConfig->u16ModuloValue = 9;
- pRTCConfig->bInterruptEn = RTC_INTERRUPT_ENABLE;
- pRTCConfig->bClockSource = RTC_CLKSRC_1KHZ;
- pRTCConfig->bClockPresaler = RTC_CLK_PRESCALER_100;
-
- RTC_SetCallback(RTC_Task);
- RTC_Init(pRTCConfig);
-
- GPIO_Init(GPIOB, GPIO_PTE7_MASK, GPIO_PinOutput);
- GPIO_Init(GPIOB, GPIO_PTE7_MASK, GPIO_PinOutput);
- GPIO_Init(GPIOB, GPIO_PTE0_MASK, GPIO_PinOutput);
- GPIO_Init(GPIOB, GPIO_PTE1_MASK, GPIO_PinOutput);
- OLED_Init();
- OLED_Clear();
- cd11();
- OLED_ShowString(24,6,"2018.6.30",16);
-
- while (1)
- {
- OLED_ShowNum(24,2,hh,2,16);
- OLED_ShowChar(40,2,':',16);
- OLED_ShowNum(48,2,mm,2,16);
- OLED_ShowChar(64,2,':',16);
- OLED_ShowNum(72,2,ss,2,16);
- if(ss==60)
- {
- ss=0;
- mm=mm+1;
- }
- if(mm==60)
- {
- mm=0;
- hh=hh+1;
- }
- if(hh==24)
- {
- hh=0;
- }
- }
- }
- void RTC_Task(void)
- {
- GPIO_Toggle(GPIOB, GPIO_PTE7_MASK);
- ss++;
- }
復制代碼
OLED 屏的相關顯示函數有: - void OLED_ShowChar(unsigned char x,unsigned char y,unsigned char chr,unsigned char Char_Size)
- {
- unsigned char c=0,i=0;
- c=chr-' ';
- if(x>Max_Column-1){x=0;y=y+2;}
- if(Char_Size ==16)
- {
- OLED_Set_Pos(x,y);
- for(i=0;i<8;i++)
- OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
- OLED_Set_Pos(x,y+1);
- for(i=0;i<8;i++)
- OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
- }
- else
- {
- OLED_Set_Pos(x,y);
- for(i=0;i<6;i++)
- OLED_WR_Byte(F6x8[c][i],OLED_DATA);
- }
- }
- uint32_t oled_pow(unsigned char m,unsigned char n)
- {
- uint32_t result=1;
- while(n--)result*=m;
- return result;
- }
-
- void OLED_ShowNum(unsigned char x,unsigned char y,uint32_t num,unsigned char len,unsigned char size2)
- {
- unsigned char t,temp;
- unsigned char 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);
- }
- }
- void OLED_ShowString(unsigned char x,unsigned char y,unsigned char *chr,unsigned char 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++;
- }
- }
復制代碼
|