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

[复制链接]
 楼主| lixiaoxu2meng 发表于 2011-9-24 11:04 | 显示全部楼层 |阅读模式
本例程实现:输出频率为100HZ占空比为%25的PWM
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: 输出频率为100HZ占空比为%25的PWM
  10. ** input parameters: 无
  11. ** output parameters: 无
  12. ** Returned value: 无
  13. *************************************************************************************/
  14. int main (void)
  15. {
  16. Set_System(); //封装一些初始化模块
  17. while(1)
  18. {
  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. PWM_Configuration(); //配置PWM
  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, 12, E_IO_OUTPUT );//PWM01端口设置为输出
  42. }
  43. /*************************************************************************************
  44. ** Function name: PWM_Configuration
  45. ** Descriptions: PWM配置
  46. ** input parameters: none
  47. ** output parameters: none
  48. ** Returned value: none
  49. *************************************************************************************/
  50. void PWM_Configuration()
  51. {
  52. S_DRVPWM_TIME_DATA_T sPt;
  53. /*
  54. 声明 PWM Timer设置的结构体 位于DRVPWM.H
  55. 结构体如下:
  56. typedef struct
  57. {
  58. uint8_t u8Mode;
  59. uint8_t u8HighPulseRatio;
  60. uint8_t u8ClockSelector;
  61. uint8_t u8PreScale;
  62. uint32_t u32Frequency;
  63. uint32_t u32Duty;
  64. int32_t i32Inverter;
  65. }S_DRVPWM_TIME_DATA_T;
  66. */

  67. /* PWM Timer property */
  68. sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE; /*自动重载模式*/
  69. sPt.u32Frequency = 100; /*PWM 频率 为100HZ即10000us为一周期*/
  70. sPt.u8HighPulseRatio =25; /* 高脉冲宽度时间所占周期的百分比: 25%*/
  71. sPt.i32Inverter = 0; /*反向关闭*/

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

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

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

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

  83. /* Enable the PWM Timer 0 */
  84. DrvPWM_Enable(DRVPWM_TIMER0, 1); //使能/关闭PWM定时器0
  85. }
  86. /*************************************************************************************
  87. ** Function name: delay_ms
  88. ** Descriptions: 1ms(晶振为12MHZ)延时子程序
  89. ** input parameters: count
  90. ** output parameters: 无
  91. ** Returned value: 无
  92. *************************************************************************************/
  93. void delay_ms(uint32_t count)
  94. {
  95. uint32_t i,j;
  96. for(i=count;i>0;i--)
  97. for(j=2395;j>0;j--);
  98. }

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

工程

本帖子中包含更多资源

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

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

本版积分规则

0

主题

1679

帖子

2

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