#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
unsigned int Flash=0;
void Port_init() //端口初始化
{
PORTB=0X0F;
DDRB=0X00;
PORTD=0X00;
DDRD=0X20;
}
void keyscan() //按键扫描
{
if(PINB!=0x0f)
{
_delay_ms(10);
if((PINB&0X01)==0)
{
Flash+=2;
if(Flash>1023)
Flash=0;
while(!(PINB&0X01));
}
else if((PINB&0X02)==0)
{
Flash-=2;
if(Flash<=0)
Flash=1023;
while(!(PINB&0X02));
}
else if((PINB&0X04)==0)
{
Flash=0;
while(!(PINB&0X04));
}
else if((PINB&0X08)==0)
{
Flash=1023;
while(!(PINB&0X08));
}
}
}
void Fast_PWM_init() //比较匹配初始化
{
TCNT1=0;
TIMSK|=_BV(OCIE1A);
TCCR1A|=_BV(COM1A1)|_BV(COM1A0)|_BV(WGM11)|_BV(WGM10);
TCCR1B|=_BV(WGM12)|_BV(CS10);
}
int main(void)
{
Port_init();
Fast_PWM_init();
sei(); //开启总中断
while(1)
{
keyscan();
OCR1A=Flash; //将Flash设置为OCR1A值
_delay_ms(1);
}
}
此帖出自
小平头技术问答
一周热门 更多>