本帖最后由 cecwxf 于 2011-8-30 23:09 编辑
最近一直很忙 手持机 、专利的事情。晚上写了个呼吸灯的小程序,改变CMR0的值以改变占空比达到呼吸灯的效果,中断回调函数中亮灭LED3。
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
/*---------------------------------------------------------------------------------------------------------*/
/******************************************************************************
文件名: NUC120_PWM.c
说 明: 本文件为“NUC120RE3AN用PWM0控制PA12的小灯。UART的波特率为115200。
申明: 本程序参考新唐BSP例程,中断回调函数中使LED3亮灭。谢谢。
开 始: 2011.8.30
修 改: V1.0
******************************************************************************/
#include <stdio.h>
#include "NUC1xx.h"
#include "DrvUART.h"
#include "DrvSYS.h"
#include "DrvPWM.h"
#include "DrvGPIO.h"
#include "global.h"
int main()
{
uint32_t tmp;
uint32_t i;
SYSTEM_Config();
UART_Config();
GPIO_Config();
printf("\n\n");
printf("+----------------------------------------------------------------------+\n");
printf("| 菜农老师助学板之PWM呼吸灯 |\n");
printf("| |\n");
printf("+----------------------------------------------------------------------+\n");
printf("\n");
/*PWM01配置函数*/
PWM_Config();
/* Enable the PWM Timer 0 */
DrvPWM_Enable(DRVPWM_TIMER0, 1);
while(1)
{
for( i=0;i<50;i++)
{
tmp = ((PWMA->CNR0)+1)*(i*2)/100-1;
PWMA->CMR0 = tmp;
DrvSYS_Delay(10000);
}
i=0;
}
}
#include <stdio.h>
#include "NUC1xx.h"
#include "DrvSYS.h"
#include "DrvGPIO.h"
#include "DrvPWM.h"
/*---------------------------------------------------------------------------------------------------------*/
/* PWM Timer Callback function */
/*---------------------------------------------------------------------------------------------------------*/
void DRVPWM_PwmIRQHandler()
{
uint8_t E_GPA_STA=DrvGPIO_GetBit(E_GPA , 2);
if(E_GPA_STA==1);
{
DrvGPIO_ClrBit(E_GPA,2);
}
if(E_GPA_STA==0)
{
DrvGPIO_SetBit(E_GPA,2);
}
}
void PWM_Config()
{
S_DRVPWM_TIME_DATA_T sPt;//定义一个PWM配置的结构体
/*
结构体如下:
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;
*/
sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE;//自动重载模式
sPt.u8HighPulseRatio=50;
// spt.u8ClockSelector=
// spt.u8PreScale=
sPt.u32Frequency=400;
// spt.u32Duty=
sPt.i32Inverter=0;
/* Enable PWM clock */
DrvPWM_Open();
/* Select PWM engine clock */
DrvPWM_SelectClockSource(DRVPWM_TIMER0, DRVPWM_EXT_12M); //设置外部12M时钟为时钟源
/* Set PWM Timer0 Configuration */
DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt);
/* Enable Output for PWM Timer0 */
DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1);
/* Enable Interrupt Sources of PWM Timer0 and install call back function */
DrvPWM_EnableInt(DRVPWM_TIMER0, 0, DRVPWM_PwmIRQHandler);
} |