[Cortex-M0技术交流] 菜鸟学习M0第十一帖——Button-PWM

[复制链接]
 楼主| lixiaoxu2meng 发表于 2011-9-24 11:09 | 显示全部楼层 |阅读模式
本工程实现按键控制频率及占空比
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: Key1键按下 输出周期为 12M/2/(39*+1) = 150KHZ 占空比为25%
  10. Key2键按下 输出周期为 12M/2/(99*+1) = 60KHZ 占空比为50%
  11. ** input parameters: 无
  12. ** output parameters: 无
  13. ** Returned value: 无
  14. *************************************************************************************/
  15. int main (void)
  16. {
  17. Set_System(); //封装一些初始化模块
  18. while(1)
  19. {
  20. KeyCode = GetKey();
  21. switch(KeyCode)
  22. {
  23. case KEY1_DOWN_USER:
  24. {
  25. PWMA->CNR0 = 39;
  26. PWMA->CMR0 = 10;
  27. }
  28. break;
  29. case KEY2_DOWN_USER:
  30. {
  31. PWMA->CNR0 = 99;
  32. PWMA->CMR0 = 50;
  33. }
  34. break;
  35. default:break;
  36. }
  37. }
  38. }


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. InitButtonVar(); //按键初始化

  12. RCC_Configuration(); //配置系统时钟

  13. GPIO_Configuration(); //配置GPIO

  14. TIMER_Configuration(); //配置TIMER

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

  47. DrvGPIO_Open( E_GPA, 12, E_IO_OUTPUT );//PWM01端口设置为输出

  48. DrvGPIO_Open( E_GPB, 14, E_IO_INPUT ); //按键端口设置为输入
  49. DrvGPIO_Open( E_GPB, 15, E_IO_INPUT );
  50. }
  51. /*************************************************************************************
  52. ** Function name: TIMER_Configuration
  53. ** Descriptions: TIMER配置
  54. ** input parameters: none
  55. ** output parameters: none
  56. ** Returned value: none
  57. *************************************************************************************/
  58. void TIMER_Configuration()
  59. {
  60. DrvTIMER_Init(); //初始化定时器

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

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

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

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

  65. DrvTIMER_Start(E_TMR0); //定时器timer0开始计数 //TIMER0->TCSR.CEN = 1;
  66. }
  67. /*************************************************************************************
  68. ** Function name: Timer0_Callback
  69. ** Descriptions: 定时处理事件 用来做按键扫描
  70. ** input parameters: none
  71. ** output parameters: none
  72. ** Returned value: none
  73. *************************************************************************************/
  74. void Timer0_Callback (void)
  75. {
  76. KeyPro(); //按键扫描 该函数在 button.c 中实现
  77. }
  78. /*************************************************************************************
  79. ** Function name: PWM_Configuration
  80. ** Descriptions: PWM配置
  81. ** input parameters: none
  82. ** output parameters: none
  83. ** Returned value: none
  84. *************************************************************************************/
  85. void PWM_Configuration()
  86. {
  87. S_DRVPWM_TIME_DATA_T sPt;
  88. /*
  89. 声明 PWM Timer设置的结构体 位于DRVPWM.H
  90. 结构体如下:
  91. typedef struct
  92. {
  93. uint8_t u8Mode;
  94. uint8_t u8HighPulseRatio;
  95. uint8_t u8ClockSelector;
  96. uint8_t u8PreScale;
  97. uint32_t u32Frequency;
  98. uint32_t u32Duty;
  99. int32_t i32Inverter;
  100. }S_DRVPWM_TIME_DATA_T;
  101. */

  102. /* PWM Timer property */
  103. sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE; /*自动重载模式*/
  104. sPt.u32Frequency = 100; /*PWM 频率 为400HZ即2500us为一周期*/
  105. sPt.u8HighPulseRatio =25; /* 高脉冲宽度时间所占周期的百分比: 50%*/
  106. sPt.i32Inverter = 0; /*反向关闭*/

  107. /* Enable PWM clock */
  108. DrvPWM_Open(); //打开 PWM 时钟并且复位PWM

  109. /* Select PWM engine clock */
  110. //DrvPWM_SelectClockSource(DRVPWM_TIMER0, DRVPWM_EXT_12M);//设置PWM 定时器0 为外部12 MHz crystal 时钟
  111. DrvSYS_SelectIPClockSource(E_SYS_PWM01_CLKSRC,0); //使用外设时注意必须设置该外设的时钟 设置PWM01的时钟源为外部12MHZ
  112. /* Set PWM Timer0 Configuration */
  113. DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt); //配置PWM 定时器0的一些参数 如配置频率/脉冲/模式/逆转功能

  114. /* Enable Output for PWM Timer0 */
  115. DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1); //使能或关闭PWM定时器0对应的IO口输出使能

  116. /* Set PWM pins */
  117. DrvGPIO_InitFunction(E_FUNC_PWM01); //指定多功能引脚 即 PA12,PA13为PWM0和PWM1

  118. /* Enable the PWM Timer 0 */
  119. DrvPWM_Enable(DRVPWM_TIMER0, 1); //使能/关闭PWM定时器0
  120. }
  121. /*************************************************************************************
  122. ** Function name: delay_ms
  123. ** Descriptions: 1ms(晶振为12MHZ)延时子程序
  124. ** input parameters: count
  125. ** output parameters: 无
  126. ** Returned value: 无
  127. *************************************************************************************/
  128. void delay_ms(uint32_t count)
  129. {
  130. uint32_t i,j;
  131. for(i=count;i>0;i--)
  132. for(j=2395;j>0;j--);
  133. }

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 TIMER_Configuration(void);
  7. void Timer0_Callback (void);
  8. void PWM_Configuration(void);
  9. void delay_ms(uint32_t count);
  10. #endif

至于按键的函数就不贴了 工程里有
测试过 OK
工程

本帖子中包含更多资源

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

×
qq124469142 发表于 2011-10-5 16:25 | 显示全部楼层
学习一下~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

1679

帖子

2

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