//AVR定时器2的PWM工作方式 By AnalogMaster
#include<mega8.h>
#include<DELAY.h>
#define SEI SREG|=0x80
#define uchar unsigned char
volatile char tempk;
void IOVA_Init(void)
{
DDRC.2=0;
PORTC.2=1;
DDRB.3=1;
PORTB.3=0;
DDRD.4=1;
PORTD.4=0;
DDRD.5=1;
PORTD.5=0;
DDRD.6=1;
PORTD.6=0;
DDRD.7=1;
PORTD.7=0;
tempk="0";
}
void Timer2PWM_Init(void)
{
TCCR2=0;
TCNT2=0;
OCR2=0;
TIMSK|=0x3f;
}
void Key_Scan(void)
{
if (!PINC.2)
{
delay_ms(10);
if (!PINC.2)
{
if (tempk++>=5){tempk=0;};
for(;;){if (PINC.2){delay_ms(10);if (PINC.2){break;}}}
}
}
}
void OCR2_Change(void)
{
if (tempk==0){PORTD.4=0;PORTD.5=0;PORTD.6=0;PORTD.7=0;TCCR2=0;}
else if (tempk==1){PORTD.4=0;PORTD.5=0;PORTD.6=0;PORTD.7=1;OCR2=56;TCCR2=0x7a;} //快速PWM,匹配置1;
else if (tempk==2){PORTD.4=0;PORTD.5=0;PORTD.6=1;PORTD.7=0;OCR2=100;}
else if (tempk==3){PORTD.4=0;PORTD.5=1;PORTD.6=0;PORTD.7=0;OCR2=10;TCCR2=0x6a;}//快速PWM,匹配清0;
else if (tempk==4){PORTD.4=1;PORTD.5=0;PORTD.6=0;PORTD.7=0;OCR2=200;}
}
void main(void)
{
IOVA_Init();
Timer2PWM_Init();
while(1)
{
Key_Scan();
OCR2_Change();
}
}
文章评论(0条评论)
登录后参与讨论