ICCAVR:按键控制灯亮度实验
//ICC-AVR application builder : 2008-5-30 17:46:59// Target : M16
// Crystal: 1.0000Mhz
#include <iom16v.h>
#include <macros.h>
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0x00;
PORTB = 0xFF;
DDRB = 0xFF;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0xFF;
}
/****************************************
* 函数名称: void delay_ms(uint i) *
* 函数功能:延时函数 *
* 晶振频率:1MHZ *
* 入口参数:i mS *
****************************************/
void delay_ms(unsigned int i)
{
unsigned int a;
for(;i;i--)
{
for(a=142;a;a--)
{;}
}
}
//TIMER0 initialize - prescale:1
void timer0_init(void)
{
TCCR0 = 0x79; //快速PWM,匹配时置位,1分频
}
void main(void)
{
unsigned char a;
port_init();
timer0_init();
OCR0 = 1; //初始亮度
while(1)
{
a = (PINA&(1<<6)); //按键1接于PA6:增加亮度
if(!a)
{
delay_ms(20); //延时消抖
a = (PINA&(1<<6)); //再读按键
if(!a)
{
OCR0 +=1; //增加亮度
if(OCR0==255)OCR0=254;
}
}
a = (PINA&(1<<5)); //按键2接于PA5:减少亮度
if(!a)
{
delay_ms(20); //延时消抖
a = (PINA&(1<<5)); //再读按键
if(!a)
{
OCR0 -=1; //减少亮度
if(OCR0==0)OCR0=1;
}
}
}
} 先占个沙发再细看:lol 有电路图吗? 应该吧电路图也放出来,这个我们看的会明白一些!!
页:
[1]