[菜农助学交流] (第四批)tendence第二帖:GPIO+TIMER

[复制链接]
2565|1
 楼主| tendence 发表于 2011-11-18 17:49 | 显示全部楼层 |阅读模式
感谢菜农,感谢lixiaoxu2meng的指导,完成了我的第二贴。
我的程序主要参考lixiaoxu2meng,学习他将程序进行了重新布局。将系统时钟频率,GPIO设置,和TIMER设置独立出来,共建一个hw_config.c文件,并使用Set_System进行调用。另一个简化就是将所有的头文件写在一起,建立includes。h文件。话不多说直接上程序。
includes.h
  1. #include <stdio.h>
  2. #include "NUC1xx.h"
  3. #include "variables.h"
  4. #include "hw_config.h"
  5. #include "Driver\DrvTIMER.h"
  6. #include "Driver\DrvGPIO.h"
  7. #include "Driver\DrvSYS.h"



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

  2. void Timer0_Callback( void );

  3. void TIMER_Configuration(void);

  4. uint8_t count;
  5. /*************************************************************************************
  6. ** Function name: Set_System
  7. ** Descriptions: 系统
  8. ** input parameters: 无
  9. ** output parameters: 无
  10. ** Returned value: 无
  11. *************************************************************************************/
  12. void Set_System(void)
  13. {
  14. RCC_Configuration(); //配置系统时钟

  15. GPIO_Configuration(); //配置GPIO

  16. TIMER_Configuration(); //配置TIMER

  17. }

  18. void TIMER_Configuration()
  19. {
  20. DrvTIMER_Init(); //初始化定时器

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

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

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

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

  25. DrvTIMER_Start(E_TMR0); //定时器timer0开始计数 //TIMER0->TCSR.CEN = 1;
  26. }



  27. void Timer0_Callback()
  28. {
  29. if(count>=4)
  30. count=0;
  31. count++;

  32. switch(count)
  33. {
  34. case 1:
  35. DrvGPIO_SetBit(E_GPA,5);
  36. DrvGPIO_ClrBit(E_GPA,2);
  37. break;
  38. case 2:
  39. DrvGPIO_SetBit(E_GPA,2);
  40. DrvGPIO_ClrBit(E_GPA,3);
  41. break;
  42. case 3:
  43. DrvGPIO_SetBit(E_GPA,3);
  44. DrvGPIO_ClrBit(E_GPA,4);
  45. break;
  46. case 4:
  47. DrvGPIO_SetBit(E_GPA,4);
  48. DrvGPIO_ClrBit(E_GPA,5);
  49. break ;
  50. }
  51. }

  52. /*************************************************************************************
  53. ** Function name: RCC_Configuration
  54. ** Descriptions: 系统时钟源设置
  55. ** input parameters: none
  56. ** output parameters: none
  57. ** Returned value: none
  58. *************************************************************************************/
  59. void RCC_Configuration(void)
  60. {
  61. UNLOCKREG(); // 对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88,
  62. DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);//与其 SYSCLK->PWRCON.XTL12M_EN = 1; 等同
  63. // PWRCON寄存器(这些寄存器在上电复位到用户解锁定之前是锁定的)除了 BIT[6]位其他位都受写保护
  64. // 解除这些需要向 0x50000 0100写入 0x59,0x16,0x88,
  65. // 令PWRCON寄存器的BITP[0]为1(即设定12M外部晶振)
  66. delay_ms(100); //while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1);//等待外部12MHZ晶振就绪
  67. LOCKREG(); // 向“0x5000_0100”写入任何值,就可以重锁保护寄存器
  68. }
  69. /*************************************************************************************
  70. ** Function name: GPIO_Configuration
  71. ** Descriptions: 端口配置
  72. ** input parameters: none
  73. ** output parameters: none
  74. ** Returned value: none
  75. *************************************************************************************/
  76. void GPIO_Configuration()
  77. {
  78. DrvGPIO_Open( E_GPA, 2, E_IO_OUTPUT );
  79. DrvGPIO_Open( E_GPA, 3, E_IO_OUTPUT );
  80. DrvGPIO_Open( E_GPA, 4, E_IO_OUTPUT );
  81. DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );
  82. }


  83. /*************************************************************************************
  84. ** Function name: delay_ms
  85. ** Descriptions: 1ms(晶振为12MHZ)延时子程序
  86. ** input parameters: count
  87. ** output parameters: 无
  88. ** Returned value: 无
  89. *************************************************************************************/
  90. void delay_ms(uint32_t count)
  91. {
  92. uint32_t i,j;
  93. for(i=count;i>0;i--)
  94. for(j=2395;j>0;j--);
  95. }

mian.
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /* */
  3. /* 流水灯,每一秒改变一次,由定时器严格定时 */
  4. /* */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. #include "includes.h" //包含所需的头文件
  7. /*************************************************************************************
  8. ** Function name: main
  9. ** Descriptions: 4个LED交替点亮
  10. ** input parameters: 无
  11. ** output parameters: 无
  12. ** Returned value: 无
  13. *************************************************************************************/
  14. int main (void)
  15. {
  16. Set_System();

  17. while(1)
  18. {
  19. }
  20. }
 楼主| tendence 发表于 2011-11-18 18:00 | 显示全部楼层
这是工程文件

本帖子中包含更多资源

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

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

本版积分规则

个人签名:把技术记在心里

1

主题

164

帖子

0

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