按键后没有反应,不知道哪里的问题,急啊!!!那位大神帮帮忙,不胜感激!!!
// Crystal: 8.0000Mhz
#include
#include
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
void time0_init(void)
{
TCCR0=0x0D;
TCNT0=0x00;
OCR0=0x4E;
TIMSK |=0x02;
TIFR |=0x02;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
time0_init();
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
/*************************************************************************
用 途:LCD1602显示
Taget :mega8
crystal :8M
介 绍:用PB做端口,
PC0-D0
PC7-D7
------
PA4-6
PA3-5
PA2-4----456为控制引脚
入口参数:
出口参数:
*************************************/
//延时程序
void delay(unsigned int ms)
{
unsigned int i,j;
for(i=0;i
{for(j=0;j<200;j++);}
}
//送指令子程序
void com_lcd(unsigned char com)
{
PORTA&=~(1<
PORTA&=~(1<
PORTA&=~(1<
PORTC=com;
PORTA|=(1<
delay(5);
PORTA&=~(1<
}
//送数据子程序
void data_lcd(unsigned char data)
{
PORTA&=~(1<
PORTA|=(1<
PORTA&=~(1<
PORTC=data;
PORTA|=(1<
delay(5);
PORTA&=~(1<
}
//初始化
void LCD1602_init()
{
DDRC=0XFF;
DDRA|=0X1C;
PORTA&=~(1<
delay(15);
com_lcd(0x38);
delay(5);
com_lcd(0x38);
delay(5);
com_lcd(0x38);
delay(5);
com_lcd(0x38);//5*7,2行显示
delay(5);
com_lcd(0x08); //清屏
delay(5);
com_lcd(0x01); //清屏
delay(5);
com_lcd(0x06);//文字不动,光标自动右移
delay(5);
com_lcd(0x0C);//开显示
delay(5);
}
//定位x,y(x-列,y-行)
void LCD1602_goxy(unsigned char line,unsigned char row)
{
if (row==0)
line+=0x80;
else
line+=0xc0;
com_lcd(line);//第二行首地址
delay(5);
}
//显示字符串
LCD1602_print(char *str)
{
while(*str)
{
data_lcd(*str);
delay(10);
str++;
}
}
//**************************************************************************
/************************************************
按键初始化
*******************************/
unsigned char get_key0(void)
{
static unsigned char key_flag = 0;
if (key_flag == 0)
{
if((PINA & 0x20)==0x00 )
{
key_flag = 1;
}
}
else if (key_flag == 1)
{
if((PINA & 0x20) == 0x20 )
{
key_flag = 0;
return (1);
}
}
return (0);
}
unsigned char key0_flag =0;
unsigned char nokey_flag =0;
#pragma interrupt_handler timer0_comp_isr:20
void timer0_comp_isr()
{
static unsigned int key_count = 0;//compare occured TCNT0=OCR0
TIFR |=0x02;
TCNT0=0x00;
if (get_key0()==1)
{
key_count = 0;
key0_flag = 1;
}
}
/*********************************************************
主函数
**************************************************/
void main()
{
unsigned char i=23;
init_devices();
LCD1602_init();
LCD1602_goxy(0,0);
LCD1602_print("nokey is pressed");
while(1)
{
if (key0_flag == 1)
{
LCD1602_print("key0 is pressed");
key0_flag = 0;
}
}
}