#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvTIMER.h"
#include "Driver\DrvPWM.h"
#define Run_Led 2 //2----LED1 3----LED2 4----LED3 5----LED4
volatile uint8_t PWM0_Duty_Cycle = 50; //PWM占空比
/***************
** 函数声明 **
***************/
void Init_System (void);
void Init_Uart (void);
void Init_PWM0(void);
void UART_INT_HANDLE(uint32_t u32IntStatus);
void Init_System(void)
{
/* Unlock the locked registers before access */
UNLOCKREG(x); //寄存器锁定键地址寄存器(RegLockAddr) :有些系统控制寄存器需要被保护起来,以防止误操作而影响芯片运行,
//这些寄存器在上电复位到用户解锁定之前是锁定的。用户可以连续依次写入“59h”, “16h” “88h”到0x5000_0100解锁定.
/* Enable the 12MHz oscillator oscillation */
//确定时钟
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); //SYSCLK->PWRCON.XTL12M_EN = 1;
/* Waiting for 12M Xtal stable */
//while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1); //SYSCLK->CLKSTATUS.XTL12M_STB
/*eClkSrc - [in] E_SYS_XTL12M / E_SYS_XTL32K / E_SYS_OSC22M / E_SYS_OSC10K / E_SYS_PLL */
// Note: Only some of NuMicro NUC100 Series support this function.
DrvSYS_Delay(5000);
LOCKREG(x);
//向“0x5000_0100”写入任何值,就可以重锁保护寄存器
}
void Init_PWM0(void)
{
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; /*自动重载模式 DRVPWM_ONE_SHOT_MODE为单触发模式*/
sPt.u32Frequency = 400; /*PWM 频率 为400HZ即2500us为一周期*/
sPt.u8HighPulseRatio = PWM0_Duty_Cycle; /* 高脉冲宽度 : 总脉冲宽度 = 1 : 100 */
sPt.i32Inverter = 0; /*反向关闭*/
/* Enable PWM clock */
DrvPWM_Open();//使能PWM clock时钟函数
/* Select PWM engine clock */
DrvPWM_SelectClockSource(DRVPWM_TIMER0, DRVPWM_EXT_12M);
/* Set PWM Timer0 Configuration */
DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt); //将配置好的参数赋值寄存器函数
/* Enable Output for PWM Timer0 */
DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1);//使能定时器输出
/* Set PWM pins */
DrvGPIO_InitFunction(E_FUNC_PWM01);//选出PWM的输出脚
/* Enable the PWM Timer 0 */
DrvPWM_Enable(DRVPWM_TIMER0, 1);//使能PWM定时器0
}
void delay_1s()
{
uint32_t i;
for(i = 0; i < 200; i++)
{
DrvSYS_Delay(1000);
}
}
int main (void)
{
Init_System(); //初始化系统
Init_PWM0();//初始化PWM0
DrvGPIO_Open(E_GPA,Run_Led, E_IO_OUTPUT);//程序运行指示,设置LED1OUTPUT
DrvGPIO_ClrBit(E_GPA,Run_Led);//点亮LED1
while(1)
{
uint8_t i;
for(i = 0; i < 10; i++)
{
PWM0_Duty_Cycle += 10;//每隔一秒时间加10从而改变占空比
PWMA->CMR0 = ((PWMA->CNR0))*PWM0_Duty_Cycle/100-1;//配置比较寄存器改变占空比
delay_1s();//每隔一秒钟占空比改变一次
}
}
}
上面就是实现过程了
下面就上图了:
|