[Cortex-M0技术交流] 菜鸟学习M0第十八帖——RTC

[复制链接]
 楼主| lixiaoxu2meng 发表于 2011-9-24 16:06 | 显示全部楼层 |阅读模式
RTC, IO, TI, gp, AD
本函数实现:RTC功能及闹钟 (同时也包括:AD温度模块 按键模块 串口模块  定时器模块 GPIO模块 LED数码管显示模块) 相当一个大杂烩吧
main()函数
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /* */
  3. /* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
  4. /* */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. #include "includes.h" //包含所需的头文件
  7. S_DRVRTC_TIME_DATA_T Curren_Time;
  8. int32_t AD_Value;
  9. /*************************************************************************************
  10. ** Function name: main
  11. ** Descriptions: 本函数实现:RTC功能及闹钟 (同时也包括:AD温度模块 按键模块 串口模块 定时器模块 GPIO模块 LED数码管显示模块)
  12. 相当一个大杂烩吧
  13. ** input parameters: 无
  14. ** output parameters: 无
  15. ** Returned value: 无
  16. *************************************************************************************/
  17. int main (void)
  18. {
  19. Set_System(); //封装一些初始化模块
  20. printf("======RTC实验=====\n");
  21. while(1)
  22. {
  23. if(ID)//查询是否去RTC时间
  24. {
  25. ID = 0;
  26. DrvRTC_Read(DRVRTC_CURRENT_TIME, &Curren_Time);
  27. DrvADC_StartConvert(); // 开启ADC转换
  28. while(DrvADC_IsConversionDone()==FALSE); // 判断ADC是否转换结束 并送串口显示
  29. AD_Value = DrvADC_GetConversionData(7);
  30. AD_Value = AD_Value*3300/4095- 730; // 将电压值转换成温度 经过校准后 确认0度为730mv左右
  31. AD_Value = AD_Value/-2;

  32. printf("当前日期为: %d年%d月%d日 星期%d\n",Curren_Time.u32Year,Curren_Time.u32cMonth,Curren_Time.u32cDay,Curren_Time.u32cDayOfWeek);
  33. printf("当前时间为: %d:%d:%d \n",Curren_Time.u32cHour,Curren_Time.u32cMinute,Curren_Time.u32cSecond);
  34. printf("当前温度为: %d度\n",AD_Value);
  35. }

  36. KeyCode = GetKey();
  37. switch(KeyCode)
  38. {
  39. case KEY1_DOWN_USER:
  40. {
  41. DrvGPIO_ClrBit(E_GPB,10); //关闭蜂鸣器
  42. }
  43. break;
  44. case KEY2_DOWN_USER:
  45. {
  46. DrvGPIO_SetBit(E_GPB,10); //开启蜂鸣器
  47. }
  48. break;
  49. default:break;
  50. }
  51. }
  52. }


hw_config函数
  1. #include "includes.h" //包含所需的头文件

  2. /*************************************************************************************
  3. ** Function name: Set_System
  4. ** Descriptions: 封装一些初始化模块
  5. ** input parameters: count
  6. ** output parameters: 无
  7. ** Returned value: 无
  8. *************************************************************************************/
  9. void Set_System(void)
  10. {
  11. RCC_Configuration(); //配置系统时钟

  12. InitButtonVar(); //按键初始化

  13. GPIO_Configuration(); //配置GPIO

  14. USART_Configuration(); //配置USART

  15. ADC_Configuration(); //配置ADC

  16. TIMER_Configuration(); //配置TIMER

  17. RTC_Configuration(); //配置RTC
  18. }
  19. /*************************************************************************************
  20. ** Function name: RCC_Configuration
  21. ** Descriptions: 系统时钟源设置
  22. ** input parameters: none
  23. ** output parameters: none
  24. ** Returned value: none
  25. *************************************************************************************/
  26. void RCC_Configuration(void)
  27. {
  28. UNLOCKREG(); // 对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88,
  29. DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);// 与其 SYSCLK->PWRCON.XTL12M_EN = 1; 等同
  30. // PWRCON寄存器(这些寄存器在上电复位到用户解锁定之前是锁定的)除了 BIT[6]位其他位都受写保护
  31. // 解除这些需要向 0x50000 0100写入 0x59,0x16,0x88,
  32. // 令PWRCON寄存器的BITP[0]为1(即设定12M外部晶振)
  33. delay_ms(100); // while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1);//等待外部12MHZ晶振就绪
  34. LOCKREG(); // 向“0x5000_0100”写入任何值,就可以重锁保护寄存器
  35. }
  36. /*************************************************************************************
  37. ** Function name: GPIO_Configuration
  38. ** Descriptions: GPIO配置
  39. ** input parameters: none
  40. ** output parameters: none
  41. ** Returned value: none
  42. *************************************************************************************/
  43. void GPIO_Configuration()
  44. {
  45. DrvGPIO_Open( E_GPB, 4, E_IO_INPUT ); //RX
  46. DrvGPIO_Open( E_GPB, 5, E_IO_OUTPUT ); //TX

  47. DrvGPIO_Open( E_GPB, 14, E_IO_INPUT ); //按键端口设置为输入
  48. DrvGPIO_Open( E_GPB, 15, E_IO_INPUT );

  49. DrvGPIO_Open( E_GPB, 10, E_IO_OUTPUT );//蜂鸣器引脚设置为输出
  50. DrvGPIO_ClrBit(E_GPB,10); //关闭蜂鸣器

  51. DrvGPIO_Open( E_GPA, 2, E_IO_OUTPUT );//数码管段选
  52. DrvGPIO_Open( E_GPA, 3, E_IO_OUTPUT );
  53. DrvGPIO_Open( E_GPA, 4, E_IO_OUTPUT );
  54. DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );
  55. DrvGPIO_Open( E_GPA, 6, E_IO_OUTPUT );
  56. DrvGPIO_Open( E_GPA, 7, E_IO_OUTPUT );
  57. DrvGPIO_Open( E_GPA, 8, E_IO_OUTPUT );
  58. DrvGPIO_Open( E_GPA, 9, E_IO_OUTPUT );
  59. DrvGPIO_Open( E_GPC, 14, E_IO_OUTPUT );//数码管位选
  60. DrvGPIO_Open( E_GPC, 15, E_IO_OUTPUT );
  61. DrvGPIO_Open( E_GPC, 6, E_IO_OUTPUT );
  62. DrvGPIO_Open( E_GPC, 7, E_IO_OUTPUT );
  63. }
  64. /*************************************************************************************
  65. ** Function name: USART_Configuration
  66. ** Descriptions: USART配置
  67. ** input parameters: none
  68. ** output parameters: none
  69. ** Returned value: none
  70. *************************************************************************************/
  71. void USART_Configuration()
  72. {
  73. STR_UART_T param;
  74. DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0); //使用外设时注意必须设置该外设的时钟 设置USART的时钟源为外部12MHZ
  75. DrvGPIO_InitFunction(E_FUNC_UART1); // 复用功能引脚设置
  76. param.u32BaudRate = 9600; // 波特率 9600
  77. param.u8cDataBits = DRVUART_DATABITS_8; // 数据位
  78. param.u8cStopBits = DRVUART_STOPBITS_1; // 停止位
  79. param.u8cParity = DRVUART_PARITY_NONE; // 校验位
  80. param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; // FIFO存储深度 1 字节
  81. param.u8TimeOut = 0; // FIFO超时设定
  82. DrvUART_Open(UART_PORT1, ¶m); // 串口usart0开启、结构体整体赋值
  83. }
  84. /*************************************************************************************
  85. ** Function name: ADC_Configuration
  86. ** Descriptions: 配置USART
  87. ** input parameters: none
  88. ** output parameters: none
  89. ** Returned value: none
  90. *************************************************************************************/
  91. void ADC_Configuration()
  92. {
  93. DrvSYS_SelectIPClockSource(E_SYS_ADC_CLKSRC, 0); //使用外设时注意必须设置该外设的时钟 设置ADC的时钟源为外部12MHZ
  94. DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_OP, 0, EXTERNAL_12MHZ, 3); // ADC_SINGLE_END AD为单端输入模式
  95. // ADC_SINGLE_OP 单一转换
  96. // 1 GA0作为输入 模式输入通道使能
  97. // EXTERNAL_12MHZ ADC时钟为 外部12MHZ
  98. // 3 AD时钟频率 = ADC时钟/(3+1) = 3MHZ
  99. DrvADC_SetADCChannel(0x10,ADC_SINGLE_END); // 8 设置为模拟通道7
  100. // ADC_SINGLE_END AD为单端输入模式
  101. ADC->ADCR.DMOF = 0; //转化结果无符号
  102. ADC->ADCHER.PRESEL = 0x02; //内部温度传感器
  103. ADC->ADCHER.CHEN = 0x80; //模拟输入通道7使能
  104. SYS->TEMPCR = 0x01; //使能温度传感器

  105. }
  106. /*************************************************************************************
  107. ** Function name: TIMER_Configuration
  108. ** Descriptions: TIMER配置
  109. ** input parameters: none
  110. ** output parameters: none
  111. ** Returned value: none
  112. *************************************************************************************/
  113. void TIMER_Configuration()
  114. {
  115. DrvTIMER_Init(); //初始化定时器

  116. DrvSYS_SelectIPClockSource(E_SYS_TMR0_CLKSRC,0); //使用外设时注意必须设置该外设的时钟 设置TIMER0的时钟源为外部12MHZ

  117. DrvTIMER_Open(E_TMR0,1000,E_PERIODIC_MODE); //设定定时器timer0的tick周期,并且启动定时器:定时器通道 TMR0 每秒1000次 周期模式

  118. DrvTIMER_SetTimerEvent(E_TMR0,5,(TIMER_CALLBACK) Timer0_Callback,0); //安装一个定时处理事件到 timer0通道

  119. DrvTIMER_EnableInt(E_TMR0); //使能定时器中断 //TIMER0->TCSR.IE = 1

  120. DrvTIMER_Start(E_TMR0); //定时器timer0开始计数 //TIMER0->TCSR.CEN = 1;
  121. }
  122. /*************************************************************************************
  123. ** Function name: Timer0_Callback
  124. ** Descriptions: 定时处理事件,LED动态扫描
  125. ** input parameters: none
  126. ** output parameters: none
  127. ** Returned value: none
  128. *************************************************************************************/
  129. void Timer0_Callback (void)
  130. {

  131. static uint8_t count= 0;
  132. static uint8_t count1= 0;
  133. static uint8_t i,xx[4];
  134. uint32_t data;
  135. uint16_t ValueBuff ;
  136. ValueBuff = value;
  137. KeyPro(); //按键扫描 该函数在 button.c 中实现
  138. count++;
  139. count1++;
  140. if(count1>=100)
  141. {
  142. count1 = 0;
  143. ID = 1; //每500ms 将获取RTC时间的标志置一
  144. }
  145. if(count >= 5)
  146. count = 1;
  147. for(i=0;i<4;i++)
  148. {
  149. xx[i] = ValueBuff%10;
  150. ValueBuff = ValueBuff/10;
  151. }
  152. switch(count)
  153. {
  154. case 1:
  155. DrvGPIO_SetBit(E_GPC,14);
  156. DrvGPIO_ClrBit(E_GPC,15);
  157. DrvGPIO_ClrBit(E_GPC,6);
  158. DrvGPIO_ClrBit(E_GPC,7);
  159. data = Table[xx[0]]<<2;
  160. GPIOA->DOUT = data;
  161. break;
  162. case 2:
  163. DrvGPIO_SetBit(E_GPC,15);
  164. DrvGPIO_ClrBit(E_GPC,14);
  165. DrvGPIO_ClrBit(E_GPC,6);
  166. DrvGPIO_ClrBit(E_GPC,7);
  167. data = Table[xx[1]]<<2;
  168. GPIOA->DOUT = data;
  169. break;
  170. case 3:
  171. DrvGPIO_SetBit(E_GPC,7);
  172. DrvGPIO_ClrBit(E_GPC,14);
  173. DrvGPIO_ClrBit(E_GPC,15);
  174. DrvGPIO_ClrBit(E_GPC,6);
  175. data = Table[xx[2]]<<2;
  176. GPIOA->DOUT = data;
  177. break;
  178. case 4:
  179. DrvGPIO_SetBit(E_GPC,6);
  180. DrvGPIO_ClrBit(E_GPC,14);
  181. DrvGPIO_ClrBit(E_GPC,15);
  182. DrvGPIO_ClrBit(E_GPC,7);
  183. data = Table[xx[3]]<<2;
  184. GPIOA->DOUT = data;
  185. //DrvGPIO_ClrBit(E_GPA,9); //显示小数点
  186. break;
  187. default:break;
  188. }
  189. }
  190. /*************************************************************************************
  191. ** Function name: RTC_Configuration
  192. ** Descriptions: RTC配置
  193. ** input parameters: none
  194. ** output parameters: none
  195. ** Returned value: none
  196. *************************************************************************************/
  197. void RTC_Configuration()
  198. {
  199. UNLOCKREG();
  200. /* Step 1. Enable and Select RTC clock source */
  201. SYSCLK->PWRCON.XTL32K_EN = 1;//Enable 32Khz for RTC clock source
  202. SYSCLK->APBCLK.RTC_EN =1;//Enable RTC clock source
  203. LOCKREG();
  204. /* Step 2. Initiate and unlock RTC module */
  205. START_RTC();
  206. /* Step 3. Initiate Time and Calendar setting */
  207. RTC->TSSR.HR24 =1;//Set 24hour mode
  208. RTC->DWR.DWR =6;//Set Sunday
  209. //Set time and calendar for loading register, Calendar: 11/9/18, Time: 12:58:00
  210. Set_CLR(1,1,0,9,1,7);//设置日期载入寄存器CLR
  211. Set_TLR(1,2,5,8,0,0);//设置时间载入寄存器TLR

  212. /* Step 4. Set alarm interrupt */
  213. //Set time and calendar for alarm register , Calendar: 11/9/18, Time: 13:10:00
  214. Set_CAR(1,1,0,9,1,7);//设置日历闹钟寄存器CAR
  215. Set_TAR(1,3,0,0,0,0);//设置时间闹钟寄存器TAR
  216. DrvRTC_EnableInt(DRVRTC_ALARM_INT, RTC_AlarmCallBackfn); //闹钟中断使能 并设置中断回调函数
  217. }
  218. /*************************************************************************************
  219. ** Function name: RTC_AlarmCallBackfn
  220. ** Descriptions: 闹钟中断函数
  221. ** input parameters: none
  222. ** output parameters: none
  223. ** Returned value: none
  224. *************************************************************************************/
  225. void RTC_AlarmCallBackfn()
  226. {
  227. DrvGPIO_SetBit(E_GPB,10); //开启蜂鸣器
  228. }
  229. /*************************************************************************************
  230. ** Function name: START_RTC
  231. ** Descriptions: 启动RTC
  232. ** input parameters: none
  233. ** output parameters: none
  234. ** Returned value: none
  235. *************************************************************************************/
  236. void START_RTC(void)
  237. {
  238. while(1)
  239. {
  240. RTC->INIR = 0xa5eb1357; //RTC初始化
  241. if(inpw(&RTC->INIR)==1)
  242. break;
  243. }
  244. while(1)
  245. {
  246. RTC->AER.AER = 0xA965; //RTC读/写使能
  247. if(inpw(&RTC->AER)&0x10000)// AER bit
  248. break;
  249. }
  250. }
  251. /*************************************************************************************
  252. ** Function name: Set_TLR
  253. ** Descriptions: 设置RTC的TLR寄存器
  254. ** input parameters: int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f
  255. ** output parameters: none
  256. ** Returned value: none
  257. *************************************************************************************/
  258. void Set_TLR (int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f)
  259. {
  260. outpw(&RTC->TLR, a<<20|b<<16|c<<12|d<<8|e<<4|f) ;
  261. }
  262. /*************************************************************************************
  263. ** Function name: Set_CLR
  264. ** Descriptions: 设置RTC的CLR寄存器
  265. ** input parameters: int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f
  266. ** output parameters: none
  267. ** Returned value: none
  268. *************************************************************************************/
  269. void Set_CLR (int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f)
  270. {
  271. outpw(&RTC->CLR, a<<20|b<<16|c<<12|d<<8|e<<4|f) ;
  272. }
  273. /*************************************************************************************
  274. ** Function name: Set_TAR
  275. ** Descriptions: 设置RTC的TAR寄存器
  276. ** input parameters: int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f
  277. ** output parameters: none
  278. ** Returned value: none
  279. *************************************************************************************/
  280. void Set_TAR(int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f)
  281. {
  282. outpw(&RTC->TAR, a<<20|b<<16|c<<12|d<<8|e<<4|f) ;
  283. }
  284. /*************************************************************************************
  285. ** Function name: Set_CAR
  286. ** Descriptions: 设置RTC的CAR寄存器
  287. ** input parameters: int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f
  288. ** output parameters: none
  289. ** Returned value: none
  290. *************************************************************************************/
  291. void Set_CAR (int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f)
  292. {
  293. outpw(&RTC->CAR, a<<20|b<<16|c<<12|d<<8|e<<4|f) ;
  294. }
  295. /*************************************************************************************
  296. ** Function name: delay_ms
  297. ** Descriptions: 1ms(晶振为12MHZ)延时子程序
  298. ** input parameters: count
  299. ** output parameters: 无
  300. ** Returned value: 无
  301. *************************************************************************************/
  302. void delay_ms(uint32_t count)
  303. {
  304. uint32_t i,j;
  305. for(i=count;i>0;i--)
  306. for(j=2395;j>0;j--);
  307. }

hw_config头文件
  1. #ifndef __HW_CONFIG_H__
  2. #define __HW_CONFIG_H__
  3. void Set_System(void);
  4. void RCC_Configuration(void);
  5. void GPIO_Configuration(void);
  6. void USART_Configuration(void);
  7. void ADC_Configuration(void);
  8. void TIMER_Configuration(void);
  9. void Timer0_Callback (void);
  10. void RTC_Configuration(void);
  11. void START_RTC(void);
  12. void Set_TLR (int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f);
  13. void Set_CLR (int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f);
  14. void Set_TAR (int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f);
  15. void Set_CAR (int32_t a,int32_t b,int32_t c,int32_t d,int32_t e,int32_t f);
  16. void RTC_TickCallBackfn(void);
  17. void RTC_AlarmCallBackfn(void);
  18. void delay_ms(uint32_t count);
  19. #endif

调试截图
工程

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| lixiaoxu2meng 发表于 2011-9-24 16:07 | 显示全部楼层
由于2000的限制 Button代码没有贴  在工程里了 直接下载就可以了:victory:
zhaor 发表于 2011-9-25 16:39 | 显示全部楼层
不错!学习!
hotpower 发表于 2011-9-25 17:56 | 显示全部楼层
楼主用了半主机。

但是俺要问问你们做过RTC的,设置时间星期如何处理???
 楼主| lixiaoxu2meng 发表于 2011-9-26 16:44 | 显示全部楼层
4# hotpower
晚上回去 弄弄 用串口输入校准的时间
hotpower 发表于 2011-9-26 17:41 | 显示全部楼层
好!
hotpower 发表于 2011-9-26 17:51 | 显示全部楼层
看见hotisp的虚拟控制台了吧?
我在考虑在虚拟控制台上键入“time()”,“date()”即可将pc时间或日期传入rtc例程。
goodboy3021 发表于 2011-9-26 18:05 | 显示全部楼层
7# hotpower
想过用VB做个上位机,通过串口设置时间、日期,这两天一直忙于学习I2S想让助学板出声,所以没时间做。可几天I2S都没成功,好难啊!
wangjia2000 发表于 2011-9-30 09:03 | 显示全部楼层
RTC有中断功能吗
zzyaizll 发表于 2013-2-27 15:38 | 显示全部楼层
串口接收不到数据呀
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

1679

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部