#include <mega8.h>
#include<DELAY.h>
#define SEI SREG|=0x80
#define uchar unsigned char
volatile char tempk,tempn;
void IOVA_Init(void)
{
DDRC.2=0;
PORTC.2=1;
DDRB.2=1;
PORTB.2=1;
DDRB.7=1;
PORTB.7=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 Timer1PWM_Init(void)
{
TCCR1B=0; //先停止Timer1
TCNT1H=255; //设置TCNT1初值
TCNT1L=0;
OCR1AH=0; //设置OC1A初值
OCR1AL=250;
OCR1BH=0; //设置OC1B初值
OCR1BL=125;
TCCR1A=0x21;//00100001 PWM模式九 相频可调PWM 高电平时间:2*OCR1B;低电平时间:2*(OCR1A-OCR1B)
TCCR1B=0x12;//00010010 八分频
TIMSK|=0x00;//00001000
}
void Key_Scan(void)
{
if (!PINC.2)
{
delay_ms(10);
if (!PINC.2)
{
if (tempk++>=6){tempk=0;};
for(;;){if (PINC.2){delay_ms(5);if (PINC.2){break;}}}
}
}
}
void L1_Light(void)
{
PORTD.4=1;
PORTD.5=0;
PORTD.6=0;
PORTD.7=0;
PORTB.7=0;
}
void L2_Light(void)
{
PORTD.4=0;PORTD.5=1;PORTD.6=0;PORTD.7=0;PORTB.7=0;
}
void L3_Light(void)
{
PORTD.4=0;PORTD.5=0;PORTD.6=1;PORTD.7=0;PORTB.7=0;
}
void L4_Light(void)
{
PORTD.4=0;PORTD.5=0;PORTD.6=0;PORTD.7=1;PORTB.7=0;
}
void L5_Light(void)
{
PORTD.4=0;PORTD.5=0;PORTD.6=0;PORTD.7=0;PORTB.7=1;
}
void OCR1_Change(void)
{
if (tempk==1){L1_Light();OCR1BH=0;OCR1BL=30;}
else if (tempk==2){L2_Light();OCR1BH=0;OCR1BL=100;}
else if (tempk==3){L3_Light();OCR1BH=0;OCR1BL=150;}
else if (tempk==4){L4_Light();OCR1BH=0;OCR1BL=250;}
else if (tempk==5){L5_Light();OCR1BH=0;OCR1BL=0;}
}
void main(void)
{
IOVA_Init();
Timer1PWM_Init();
while(1)
{
Key_Scan();
OCR1_Change();
}
}
文章评论(0条评论)
登录后参与讨论