[Cortex-M0技术交流] 菜鸟学习M0第三帖——TIMER

[复制链接]
 楼主| lixiaoxu2meng 发表于 2011-8-14 16:09 | 显示全部楼层 |阅读模式
本函数实现led4每0.5s闪烁一次。
main
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /* */
  3. /* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
  4. /* */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. #include "includes.h" //包含所需的头文件
  7. /*************************************************************************************
  8. ** Function name: main
  9. ** Descriptions: timer0 (led4每0.5是闪烁一次)
  10. ** input parameters: 无
  11. ** output parameters: 无
  12. ** Returned value: 无
  13. *************************************************************************************/
  14. int main (void)
  15. {
  16. Set_System(); //封装一些初始化模块
  17. while(1)
  18. {}
  19. }


hw_config.c
  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. GPIO_Configuration(); //配置GPIO

  13. TIMER_Configuration(); //配置TIMER

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

  53. DrvSYS_SelectIPClockSource(E_SYS_TMR0_CLKSRC,0); //设定TIMER0的时钟源为外部12MHZ

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

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

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

  57. DrvTIMER_Start(E_TMR0); //定时器timer0开始计数 //TIMER0->TCSR.CEN = 1;
  58. }
  59. /*************************************************************************************
  60. ** Function name: Timer0_Callback
  61. ** Descriptions: 定时处理事件,小灯每0.5秒闪烁一次
  62. ** input parameters: none
  63. ** output parameters: none
  64. ** Returned value: none
  65. *************************************************************************************/
  66. void Timer0_Callback (void)
  67. {
  68. if (DrvGPIO_GetBit(E_GPA,5))
  69. DrvGPIO_ClrBit(E_GPA,5);
  70. else
  71. DrvGPIO_SetBit(E_GPA,5);
  72. }

  73. /*************************************************************************************
  74. ** Function name: delay_ms
  75. ** Descriptions: 1ms(晶振为12MHZ)延时子程序
  76. ** input parameters: count
  77. ** output parameters: 无
  78. ** Returned value: 无
  79. *************************************************************************************/
  80. void delay_ms(uint32_t count)
  81. {
  82. uint32_t i,j;
  83. for(i=count;i>0;i--)
  84. for(j=2395;j>0;j--);
  85. }

hw_config.h
  1. void Set_System(void);
  2. void RCC_Configuration(void);
  3. void GPIO_Configuration(void);
  4. void TIMER_Configuration(void);
  5. void Timer0_Callback (void);
  6. void delay_ms(uint32_t count);

includes.h
  1. #include <stdio.h>
  2. #include "NUC1xx.h"
  3. #include "variables.h"
  4. #include "hw_config.h"
  5. #include "Driver\DrvSYS.h"
  6. #include "Driver\DrvGPIO.h"
  7. #include "Driver\DrvTIMER.h"

工程

本帖子中包含更多资源

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

×
hotpower 发表于 2011-8-15 00:49 | 显示全部楼层
按照惯例,发裤子一条鼓励。
 楼主| lixiaoxu2meng 发表于 2011-8-15 07:18 | 显示全部楼层
多谢菜农老师:$
Swallow_0322 发表于 2011-8-15 10:44 | 显示全部楼层
:P继续努力,加油!
 楼主| lixiaoxu2meng 发表于 2011-8-15 11:01 | 显示全部楼层
:handshake 谢谢
296895536 发表于 2013-3-10 10:55 | 显示全部楼层
求解,這个程序下到我的单片机里,它的引脚一直都是高电平,为啥呀?
a437916817 发表于 2013-4-9 15:34 | 显示全部楼层
296895536 发表于 2013-3-10 10:55
求解,這个程序下到我的单片机里,它的引脚一直都是高电平,为啥呀?

不会吧。。
西行侠客 发表于 2013-4-18 10:25 | 显示全部楼层
先尝试下,不过楼主头像真心亮
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

1679

帖子

2

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

0

主题

1679

帖子

2

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