找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2204|回复: 0
打印 上一主题 下一主题
收起左侧

基于NV32F100的RTC计时时钟

[复制链接]
跳转到指定楼层
楼主
本帖最后由 jinglixixi 于 2020-6-17 23:59 编辑

在NV32F100的内部也配有RTC功能,但它不同与常规的RTC 它在例程中并没有通过RTC初试值的设置与读取方法,为了实现RTC的计时功能,我们可以模拟其计时的进制关系来实现。此外,通过一个I2C接口的双色OLED屏可以将计时器显示出来,其效果如图所示。[size=13.3333px]





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时钟的程序代码如下:
  1. #include "common.h"
  2. #include "ics.h"
  3. #include "rtc.h"
  4. #include "uart.h"
  5. #include "gpio.h"
  6. #include "sysinit.h"
  7. #include "start.h"
  8. #include "oledfont.h"
  9. #include "oled.h"

  10. void cd11()
  11. {  
  12.         OLED_Clear();
  13.         OLED_ShowString(24,0,"RTC CLOCK",16);
  14.         OLED_ShowString(24,4,"jinglixixi",16);
  15. }

  16. void RTC_Task(void);

  17. int main (void)
  18. {
  19.     sysinit();
  20.     cpu_identify();

  21.     RTC_ConfigType  sRTCConfig;
  22.     RTC_ConfigType  *pRTCConfig = &sRTCConfig;

  23.     pRTCConfig->u16ModuloValue = 9;                                    
  24.     pRTCConfig->bInterruptEn   = RTC_INTERRUPT_ENABLE;   
  25.     pRTCConfig->bClockSource   = RTC_CLKSRC_1KHZ;        
  26.     pRTCConfig->bClockPresaler = RTC_CLK_PRESCALER_100;   
  27.    
  28.     RTC_SetCallback(RTC_Task);
  29.     RTC_Init(pRTCConfig);

  30.     GPIO_Init(GPIOB, GPIO_PTE7_MASK, GPIO_PinOutput);
  31.     GPIO_Init(GPIOB, GPIO_PTE7_MASK, GPIO_PinOutput);
  32.     GPIO_Init(GPIOB, GPIO_PTE0_MASK, GPIO_PinOutput);
  33.     GPIO_Init(GPIOB, GPIO_PTE1_MASK, GPIO_PinOutput);
  34.     OLED_Init();
  35.     OLED_Clear();
  36.     cd11();
  37.     OLED_ShowString(24,6,"2018.6.30",16);
  38.    
  39.     while (1)
  40.     {
  41.            OLED_ShowNum(24,2,hh,2,16);
  42.            OLED_ShowChar(40,2,':',16);
  43.            OLED_ShowNum(48,2,mm,2,16);
  44.            OLED_ShowChar(64,2,':',16);
  45.            OLED_ShowNum(72,2,ss,2,16);
  46.            if(ss==60)
  47.            {
  48.                   ss=0;
  49.                   mm=mm+1;
  50.            }
  51.            if(mm==60)
  52.            {
  53.                  mm=0;
  54.                  hh=hh+1;
  55.            }
  56.            if(hh==24)
  57.            {
  58.                 hh=0;
  59.            }
  60.     }
  61. }

  62. void RTC_Task(void)
  63. {
  64.          GPIO_Toggle(GPIOB, GPIO_PTE7_MASK);
  65.          ss++;
  66. }
复制代码

OLED 屏的相关显示函数有:
  1. void OLED_ShowChar(unsigned char x,unsigned char y,unsigned char chr,unsigned char Char_Size)
  2. {              
  3.         unsigned char c=0,i=0;        
  4.         c=chr-' ';               
  5.         if(x>Max_Column-1){x=0;y=y+2;}
  6.         if(Char_Size ==16)
  7.         {
  8.                  OLED_Set_Pos(x,y);        
  9.                  for(i=0;i<8;i++)
  10.                  OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
  11.                  OLED_Set_Pos(x,y+1);
  12.                  for(i=0;i<8;i++)
  13.                  OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
  14.         }
  15.         else
  16.         {        
  17.                  OLED_Set_Pos(x,y);
  18.                  for(i=0;i<6;i++)
  19.                  OLED_WR_Byte(F6x8[c][i],OLED_DATA);
  20.         }
  21. }

  22. uint32_t oled_pow(unsigned char m,unsigned char n)
  23. {
  24.         uint32_t result=1;         
  25.         while(n--)result*=m;   
  26.         return result;
  27. }                                 
  28.                   
  29. void OLED_ShowNum(unsigned char x,unsigned char y,uint32_t num,unsigned char len,unsigned char size2)
  30. {                 
  31.         unsigned char t,temp;
  32.         unsigned char enshow=0;                                                   
  33.         for(t=0;t<len;t++)
  34.         {
  35.                 temp=(num/oled_pow(10,len-t-1))%10;
  36.                 if(enshow==0&&t<(len-1))
  37.                 {
  38.                         if(temp==0)
  39.                         {
  40.                                 OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
  41.                                 continue;
  42.                         }else enshow=1;
  43.                           
  44.                 }
  45.                  OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2);
  46.         }
  47. }

  48. void OLED_ShowString(unsigned char x,unsigned char y,unsigned char *chr,unsigned char Char_Size)
  49. {
  50.         unsigned char j=0;
  51.         while (chr[j]!='\0')
  52.         {               
  53.                 OLED_ShowChar(x,y,chr[j],Char_Size);
  54.                 x+=8;
  55.                 if(x>120){x=0;y+=2;}
  56.                 j++;
  57.         }
  58. }

复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表