/*avr mega16 11.0592M*/
#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int
//定义液晶的 CS SID SCLK分别是PD4 PD5 PD6
#define CS PD4
#define SID PD5
#define SCLK PD6
#define S_CS PORTD|=BIT(PD4)
#define R_CS PORTD&=~BIT(PD4)
#define S_SID PORTD|=BIT(PD5)
#define R_SID PORTD&=~BIT(PD5)
#define S_SCLK PORTD|=BIT(PD6)
#define R_SCLK PORTD&=~BIT(PD6)
uchar key;
uchar flag;
void delay_us(uchar x)//延时50us
{
uchar z,y;
for(z=0;z<x;z++)
for(y=0;y<1415;y++);
}
void delay()//延时2us
{
uchar z;
for(z=0;z<1;z++)
_NOP();
}
void delay_ms(uint x)//延时1ms
{
uint z,y;
for(z=0;z<x;z++)
for(y=0;y<1570;y++);
}
void wcom_data(uchar code)//把数据或指令给液晶
{
uchar i;
for(i=0;i<8;i++)
{
if((code&0X80)==0X80)
S_SID;
else
R_SID;
code<<=1;
R_SCLK;
delay();
S_SCLK;
}
delay();
}
void write_data(uchar data)//写数据--》液晶
{
S_CS;
delay_us(10);
wcom_data(0XFA);
wcom_data(data&0XF0);
wcom_data((data<<4)&0XF0);
R_CS;
delay_us(5);
}
void write_com(uchar com) //写指令--》液晶
{
S_CS;
delay(3);
wcom_data(0XF8);
wcom_data(com&0XF0);
wcom_data((com<<4)&0XF0);
R_CS;
delay_us(5);
}
void lcd_init(void) //初始化液晶
{
write_com(0x30); //基本指令集
write_com(0x01); //清屏
delay_ms(3);
write_com(0x06); //光标右移
//write_com(0x14);
write_com(0x0F); //开显示
write_com(0x02); //地址归位
}
void port_init()
{
PORTC=0XF0;
DDRC=0X0F;
PORTA=0XF0;
DDRA=0X0F;
PORTD=0X00;
DDRD=0XFF;
PORTB&=~BIT(PB2);
DDRB&=~BIT(PB2);
}
void init_devices()//器件初始化
{
CLI(); //disable all interrupts
MCUCR=0X00;
MCUCSR&=~BIT(6);//下降沿触发
GICR|=BIT(5);//使能INT2中断
GIFR|=BIT(5);//清除INT2中断标志
SEI(); //使能全局中断
}
void keyscan()//键盘扫描
{
//CLI();//disable all interrupt
uchar i=0X7F,j;
delay_us(2);
do//检测是哪一个键被按下
{
i=(i<<1|i>>7);
PORTA=i;
_NOP();
delay();
key=PINA;
j=key&0XF0;
}while(j==0XF0);
switch(key)
{
case 0xEE: key=0x41; break;
case 0xDE: key=0x42; break;
case 0xBE: key=0x43; break;
case 0x7E: key=0x44; break;
case 0xED: key=0x45; break;
case 0xDD: key=0x46; break;
case 0xBD: key=0x47; break;
case 0x7D: key=0x48; break;
case 0xEB: key=0x49; break;
case 0xDB: key=0x4a; break;
case 0xBB: key=0x4b; break;
case 0x7B: key=0x4c; break;
case 0xE7: key=0x4d; break;
case 0xD7: key=0x4e; break;
case 0xB7: key=0x4f; break;
case 0x77: key=0x50; break;
default: break;
}
}
#pragma interrupt_handler int2_isr:iv_INT2
void int2_isr()
{
GICR&=~BIT(5);
delay_us(20);
keyscan();
flag=1;
//while((PINA&0XF0)!=0XF0);
port_init();
GIFR|=BIT(5);
GICR|=BIT(5);
}
void main()
{
flag=0;
port_init();
init_devices();
lcd_init();
delay_ms(60);
write_data(0x41);
while(1)
{
if(flag==1)
{
flag=0;
write_data(0x41);
delay_ms(500);
}
}
}
[
本帖最后由 郭小乐 于 2011-4-3 20:19 编辑 ]
此帖出自
小平头技术问答
一周热门 更多>