基于LPC1114 DevKit口袋开发板,系统上电,系统默认工作频率为48MHz,在Keil MDK 4.74上编写一段程序:设置16位定时器1工作在PWM模式,PIO1_9设置为PWM输出引脚,利用另外一个定时器定时(例如32位定时器0,设置每隔0.01s,或者更小)增大或者减小16位定时器1输出PWM的占空比(占空比改变的步长与32位定时器0的定时时间相配合,确定呼吸频率),实现PIO1_9上的LED灯渐亮渐灭的呼吸灯效果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #include <LPC11xx.h>
#define step 0.005
int flag = 0; float freCoe = 0;
void TMR16B1_PWM_Init(void); void TMR32B0_Init(void); void TIMER32_0_IRQHandler(void);
int main(){ TMR16B1_PWM_Init(); TMR32B0_Init(); while(1){}; }
void TMR16B1_PWM_Init(void){ LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 8); LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 16); LPC_IOCON->PIO1_9 |= 0x01; LPC_TMR16B1->PR = 0; LPC_TMR16B1->PWMC= 0x01; LPC_TMR16B1->MCR = 0x02 <<9; LPC_TMR16B1->MR3 = SystemCoreClock; LPC_TMR16B1->MR0 = LPC_TMR16B1->MR3/2; LPC_TMR16B1->TCR = 0x01; }
void TMR32B0_Init(void){ LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 9); LPC_TMR32B0->IR = 0x1F; LPC_TMR32B0->PR = 0; LPC_TMR32B0->MCR = 3; LPC_TMR32B0->MR0 = SystemCoreClock/100; LPC_TMR32B0->TCR = 0x01; NVIC_EnableIRQ(TIMER_32_0_IRQn); }
void TIMER32_0_IRQHandler(void){ if((LPC_TMR32B0->IR|=0x01)==1){ if (freCoe > 1 || freCoe < 0){ flag = ~flag; } if(flag==0){ LPC_TMR16B1->MR0 = freCoe*LPC_TMR16B1->MR3; freCoe += step; } else{ LPC_TMR16B1->MR0 = freCoe*LPC_TMR16B1->MR3; freCoe -= step; } } }
|