本帖最后由 hotpower 于 2012-9-22 12:54 编辑
根据助学园地的PWM例子改编而成,园地的例子使用PWM过程中没有改变占空比,于是学习了一下并且加入改变占空比部分,实现PWM调光功能。。。
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include "includes.h" //°üº¬ËùÐèµÄÍ·Îļþ
uint8_t duty=99;
S_DRVPWM_TIME_DATA_T sP_PWM;
int8_t flag;
void TMR0_Callback(void)
{
if(duty == 0)
duty=99;
duty--;
PWM->CMR2 = (duty * PWM->CNR2) / 100;//change duty
flag = ~flag;
if(flag)
{
DrvGPIO_SetBit(E_PORT3, 1); //LED0-OFF
DrvGPIO_SetBit(E_PORT3, 6); //LED1-OFF
DrvGPIO_SetBit(E_PORT5, 2); //LED2-OFF
DrvGPIO_SetBit(E_PORT2, 6); //LED3-OFF
}
else
{
DrvGPIO_ClrBit(E_PORT3, 1); //LED0-ON
DrvGPIO_ClrBit(E_PORT3, 6); //LED1-ON
DrvGPIO_ClrBit(E_PORT5, 2); //LED2-ON
DrvGPIO_ClrBit(E_PORT2, 6); //LED3-ON
}
}
int main (void)
{
Set_System();
DrvGPIO_InitFunction(FUNC_PWM2);
DrvPWM_Open();
sP_PWM.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
sP_PWM.u8HighPulseRatio = 99;
sP_PWM.u32Frequency =100;
sP_PWM.i32Inverter = 0;
DrvPWM_SetTimerClk(DRVPWM_TIMER2, &sP_PWM);//use method1
DrvPWM_SetTimerIO(DRVPWM_TIMER2, 1);
DrvPWM_Enable(DRVPWM_TIMER2);
while(1)
{
;
}
}
#include "includes.h" //°üº¬ËùÐèµÄÍ·Îļþ
void timer_init(void);
/*************************************************************************************
** Function name: Set_System
** Descriptions: ·âװһЩ³õʼ»¯Ä£¿é
** input parameters: none
** output parameters: none
** Returned value: none
*************************************************************************************/
void Set_System(void)
{
RCC_Configuration(); //ÅäÖÃϵͳʱÖÓ
GPIO_Configuration(); //ÅäÖÃGPIO
timer_init();
}
/*************************************************************************************
** 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_Open(XTL_CLK); // Enable high external clock and use it as system clock (HCLK)
while (DrvSYS_GetChipClockSourceStatus(XTL_CLK) != 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_PORT3, E_PIN1, E_IO_OUTPUT); //LED0
DrvGPIO_Open(E_PORT3, E_PIN6, E_IO_OUTPUT); //LED1
DrvGPIO_Open(E_PORT5, E_PIN2, E_IO_OUTPUT); //LED2
DrvGPIO_Open(E_PORT2, E_PIN6, E_IO_OUTPUT); //LED3
}
/*************************************************************************************
** Function name: delay_ms
** Descriptions: 1ms(¾§ÕñΪ12MHZ)ÑÓʱ×Ó³ÌÐò
** input parameters: count
** output parameters: none
** Returned value: none
*************************************************************************************/
void delay_ms(uint32_t count)
{
uint32_t i,j;
for(i=count;i>0;i--)
for(j=2395;j>0;j--);
}
void timer_init(void)
{
DrvTIMER_Init();
DrvTIMER_Open(E_TMR0, 1000, E_PERIODIC_MODE); //1000/s
DrvTIMER_SetTimerEvent(E_TMR0, 100, (TIMER_CALLBACK)TMR0_Callback, 0);//100ms
DrvTIMER_EnableInt(E_TMR0);
DrvTIMER_Start(E_TMR0);
}
|