新手刚接触单片机,用的是PIC16f1828芯片,参考着做了个呼吸灯,用PWM输出,在中断部分的程序没看明白,可以帮我解释下吗?而且在灯渐亮和渐暗过程中不是很流畅,在暗亮之间的停顿时间过短。大家能帮我看看该怎么调整吗?如果配置完PWM后不用中断,想让占空比通过逐渐增加和减少来实现渐亮到渐灭来实现呼吸灯功能又该怎么写?谢谢以下是我参考所写的程序:
- #include <pic16f1828.h>
- #define uint8 unsigned char
- #define uint16 unsigned int
- uint8 gDutycount = 0;
- uint16 count=0;
- const char SINETABLE[38]={50,55,60,65,70,75,80,85,90,95,100,95,90,85,80,75,70,60,60,55,50,45,40,35,30,25,20,15,10,5,10,15,20,25,30,35,40,45};
- void PWM_Init(void)
- {
- OSCCONbits.IRCF=1110;//8M晶振
- CCPR1L=0x32;
- CCP1CON=0b00001100;
- TRISC5=0;
- }
- void Time2_Init(void)
- {
- T2CON=0x05;
- PR2=99;
- TMR2IF=0;
- GIE=1;
- PEIE=1;
- TMR2IE=1;
- }
- void main(void)
- {
- PWM_Init();
- Time2_Init();
- while(1)
- {
- }
- }
- void interrupt Tmer2_isr(void)
- {
- if(TMR2IF)
- {
- ++count;
- if(count==35)
- {
- ++gDutycount;
- if(gDutycount==75)
- {
- gDutycount=0;
- }
- CCPR1L=SINETABLE[gDutycount];
- count=0;
- }
- TMR2IF=0;
- }
- }
复制代码 |