废话不多说,直接上代码/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "DrvPWM.H"
#define LED_PWM_VAL PWMA->CMR0 //通过比较器调节占空比
int main(void)
{
uint16_t ledpwmval=0;
S_DRVPWM_TIME_DATA_T spt;
DrvPWM_Open();
DrvGPIO_InitFunction(E_FUNC_PWM0); // 设置GPA12(即PWM0)为PWM输出
DrvPWM_SelectClockSource(DRVPWM_TIMER0,DRVPWM_HCLK); //为PWM0,PWM1选择时钟源 DRVPWM_HCLK
spt.u8Mode = DRVPWM_AUTO_RELOAD_MODE; //自动装载模式
spt.u8HighPulseRatio = 1; //占空比
spt.u32Frequency = 1000;
//spt.u8ClockSelector = DRVPWM_CLOCK_DIV_1; //时钟分频 u32Frequency =0 时生效
//spt.u8PreScale = 0; //8位预分频0-255 u32Frequency =0时生效
//spt.u32Duty = 0; //u32Frequency = 0,或工作在捕获模式时有效
spt.i32Inverter = 1;
DrvPWM_SetTimerClk(DRVPWM_TIMER0,&spt);
DrvPWM_SetTimerIO(DRVPWM_TIMER0,ENABLE); //使能IO输出
DrvPWM_Enable(DRVPWM_TIMER0,ENABLE); //使能PWM0
//DrvPWM_SetTimerCounter()
//LED_PWM_VAL = 1;
while (1)
{
DrvSYS_Delay(10000);
ledpwmval ++ ;
if (ledpwmval >= 11000)
{
ledpwmval = 1;
}
LED_PWM_VAL = ledpwmval;
};
}
效果就是板子上得L6渐亮
有个疑问,u32Frequency = 1000 但实际上PWM输出频率测量显示540hz,基本上是设置值的一半
换了几个设置都是如此,不知道问题出在哪
工程包就不上传了 |