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

[复制链接]
 楼主| lixiaoxu2meng 发表于 2011-9-24 11:26 | 显示全部楼层 |阅读模式
本工程实现系统自带的嘀嗒定时器  该定时器可以用来作为RTOS的时钟及测量时间等
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: 实现系统自带的24为SysTick定时器 以后可以作为RTOS的系统时钟
  10. 本函数实现 led4每0.1s闪烁一次
  11. ** input parameters: 无
  12. ** output parameters: 无
  13. ** Returned value: 无
  14. *************************************************************************************/
  15. int main (void)
  16. {
  17. Set_System(); //封装一些初始化模块
  18. while(1)
  19. {}
  20. }


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

  13. SysTick_Configuration(); //配置系统滴答定时器SysTick
  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, 2, E_IO_OUTPUT ); //led端口设置为输出
  42. DrvGPIO_Open( E_GPA, 3, E_IO_OUTPUT );
  43. DrvGPIO_Open( E_GPA, 4, E_IO_OUTPUT );
  44. DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );
  45. }
  46. /*************************************************************************************
  47. ** Function name: SysTick_Configuration
  48. ** Descriptions: 配置系统滴答定时器SysTick
  49. ** input parameters: none
  50. ** output parameters: none
  51. ** Returned value: none
  52. *************************************************************************************/
  53. void SysTick_Configuration()
  54. {
  55. DrvSYS_SelectSysTickSource(0); //0: External 12M clock 1: External 32K clock 2: External 12M clock / 2
  56. //3: HCLK / 2 4~7: Internal 22M clock / 2

  57. SysTick->CTRL = 0X03; //[SYST_CSR]向下计数到0将引起SysTick 异常而挂起(进入中断) 计数器运行
  58. SysTick->VAL = 0x00; //[SYST_CVR]该向SYST_CVR寄存器写入0样确保定时器以SYST_RVR中的值计数,而非任意值
  59. SysTick->LOAD = 6000000; //[SYST_RVR]6000000/12000000 = 0.5s
  60. }
  61. /*************************************************************************************
  62. ** Function name: SysTick_Handler
  63. ** Descriptions: 系统滴答定时器SysTick中断函数
  64. ** input parameters: none
  65. ** output parameters: none
  66. ** Returned value: none
  67. *************************************************************************************/
  68. void SysTick_Handler(void)
  69. {
  70. static uint8_t count = 0;
  71. SysTick->CTRL |= (1 << 16);//向SYST_CSR.COUNTFLAG中写1 以清除标志
  72. count++;
  73. if(count==3)
  74. count = 1;
  75. if(count == 1)
  76. DrvGPIO_ClrBit(E_GPA,5); //LED4 点亮
  77. if(count == 2)
  78. DrvGPIO_SetBit(E_GPA,5); //LED4 熄灭
  79. }
  80. /*************************************************************************************
  81. ** Function name: delay_ms
  82. ** Descriptions: 1ms(晶振为12MHZ)延时子程序
  83. ** input parameters: count
  84. ** output parameters: 无
  85. ** Returned value: 无
  86. *************************************************************************************/
  87. void delay_ms(uint32_t count)
  88. {
  89. uint32_t i,j;
  90. for(i=count;i>0;i--)
  91. for(j=2395;j>0;j--);
  92. }

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 SysTick_Configuration(void);
  7. void delay_ms(uint32_t count);
  8. #endif

测试通过 OK
工程

本帖子中包含更多资源

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

×
wangjia2000 发表于 2011-9-30 09:02 | 显示全部楼层
谢谢
mbydyjj 发表于 2011-9-30 13:12 | 显示全部楼层
我是1514 发表于 2012-3-17 21:58 | 显示全部楼层
下下来看一下,哈哈。最近在看这个,不理解啊。
kongkongyu 发表于 2012-6-19 11:11 | 显示全部楼层
为啥我使用了一下根本无法进入中断啊- -
liguang02 发表于 2012-8-25 13:24 | 显示全部楼层
不是1.5s中断一次么,led4怎么会每0.1s闪烁一次,不应该是0.5s么
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

1679

帖子

2

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