本例程实现:输出频率为100HZ占空比为%25的PWM
main函数- /*---------------------------------------------------------------------------------------------------------*/
- /* */
- /* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
- /* */
- /*---------------------------------------------------------------------------------------------------------*/
- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: main
- ** Descriptions: 输出频率为100HZ占空比为%25的PWM
- ** input parameters: 无
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- int main (void)
- {
- Set_System(); //封装一些初始化模块
- while(1)
- {
- }
- }
hw_config函数- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: Set_System
- ** Descriptions: 封装一些初始化模块
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- void Set_System(void)
- {
- RCC_Configuration(); //配置系统时钟
- GPIO_Configuration(); //配置GPIO
- PWM_Configuration(); //配置PWM
- }
- /*************************************************************************************
- ** Function name: RCC_Configuration
- ** Descriptions: 系统时钟源设置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void RCC_Configuration(void)
- {
- UNLOCKREG(); // 对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88,
- DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);//与其 SYSCLK->PWRCON.XTL12M_EN = 1; 等同
- // PWRCON寄存器(这些寄存器在上电复位到用户解锁定之前是锁定的)除了 BIT[6]位其他位都受写保护
- // 解除这些需要向 0x50000 0100写入 0x59,0x16,0x88,
- // 令PWRCON寄存器的BITP[0]为1(即设定12M外部晶振)
- delay_ms(100); //while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1);//等待外部12MHZ晶振就绪
- LOCKREG(); // 向“0x5000_0100”写入任何值,就可以重锁保护寄存器
- }
- /*************************************************************************************
- ** Function name: GPIO_Configuration
- ** Descriptions: GPIO配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void GPIO_Configuration()
- {
- DrvGPIO_Open( E_GPA, 12, E_IO_OUTPUT );//PWM01端口设置为输出
- }
- /*************************************************************************************
- ** Function name: PWM_Configuration
- ** Descriptions: PWM配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void PWM_Configuration()
- {
- S_DRVPWM_TIME_DATA_T sPt;
- /*
- 声明 PWM Timer设置的结构体 位于DRVPWM.H
- 结构体如下:
- typedef struct
- {
- uint8_t u8Mode;
- uint8_t u8HighPulseRatio;
- uint8_t u8ClockSelector;
- uint8_t u8PreScale;
- uint32_t u32Frequency;
- uint32_t u32Duty;
- int32_t i32Inverter;
- }S_DRVPWM_TIME_DATA_T;
- */
- /* PWM Timer property */
- sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE; /*自动重载模式*/
- sPt.u32Frequency = 100; /*PWM 频率 为100HZ即10000us为一周期*/
- sPt.u8HighPulseRatio =25; /* 高脉冲宽度时间所占周期的百分比: 25%*/
- sPt.i32Inverter = 0; /*反向关闭*/
- /* Enable PWM clock */
- DrvPWM_Open(); //打开 PWM 时钟并且复位PWM
- /* Select PWM engine clock */
- //DrvPWM_SelectClockSource(DRVPWM_TIMER0, DRVPWM_EXT_12M);//设置PWM 定时器0 为外部12 MHz crystal 时钟
- DrvSYS_SelectIPClockSource(E_SYS_PWM01_CLKSRC,0); //使用外设时注意必须设置该外设的时钟 设置PWM01的时钟源为外部12MHZ
- /* Set PWM Timer0 Configuration */
- DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt); //配置PWM 定时器0的一些参数 如配置频率/脉冲/模式/逆转功能
- /* Enable Output for PWM Timer0 */
- DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1); //使能或关闭PWM定时器0对应的IO口输出使能
- /* Set PWM pins */
- DrvGPIO_InitFunction(E_FUNC_PWM01); //指定多功能引脚 即 PA12,PA13为PWM0和PWM1
- /* Enable the PWM Timer 0 */
- DrvPWM_Enable(DRVPWM_TIMER0, 1); //使能/关闭PWM定时器0
- }
- /*************************************************************************************
- ** Function name: delay_ms
- ** Descriptions: 1ms(晶振为12MHZ)延时子程序
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- void delay_ms(uint32_t count)
- {
- uint32_t i,j;
- for(i=count;i>0;i--)
- for(j=2395;j>0;j--);
- }
hw_config头文件- #ifndef __HW_CONFIG_H__
- #define __HW_CONFIG_H__
- void Set_System(void);
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void PWM_Configuration(void);
- void delay_ms(uint32_t count);
- #endif
工程
|