用mega16L单片机驱动,PA口为数据口,
/**********************************************
功能:液晶显示字符
工作频率:8.000MHz
**********************************************/
#include<iom16v.h>
#define Busy 0x80
#define uchar unsigned char
char e[]=""; //要显示的字符
//data
#define DATA PORTA
//RS=PC5
#define RS_H asm("sbi 0x15,5")
#define RS_L asm("cbi 0x15,5")
//RW=PC6
#define RW_H asm("sbi 0x15,6")
#define RW_L asm("cbi 0x15,6")
//EN=PC7
#define EN_H asm("sbi 0x15,7")
#define EN_L asm("cbi 0x15,7")
void delay()
{
asm("nop");
}
void LcdWriteCommand(char cmd)
{RS_L;RW_H;EN_H;
while(PINA&Busy);
EN_L;RW_L;
DDRA=0XFF;
PORTA=cmd;
EN_H;
delay();delay();delay();
EN_L;
DDRA=0X00;
PORTA=0X00;
}
void LocateXY(char posx,char posy)
{char temp;
if(posy==0)
temp="posx"+0x80;
else
temp="posx"+0xc0;
LcdWriteCommand(temp);
}
void LcdWriteData(char Wdata)
{RS_L;RW_H;EN_H;
while(PINA&Busy);
EN_L;RS_H;RW_L;
DDRA=0XFF;
PORTA=Wdata;
EN_H;
delay();delay();delay();
EN_L;
DDRA=0X00;
PORTA=0X00;
}
void DispOneChar(char x,char y,char Wdata)
{LocateXY(x,y);
LcdWriteData(Wdata);}
void ePutstr(uchar x,uchar y,char *ptr)
{char i,l=0;
while(ptr[l]>31)
l++;
for(i=0;i<l;i++)
{DispOneChar(x,y,ptr[i]);
x++;
if(x==16){x=0;y^=1;}}}
void LcdRest(void)
{
LcdWriteCommand(0x38);/*8位,2行,5*7字体*/
LcdWriteCommand(0x08);/*显示关,光标关,不闪烁*/
LcdWriteCommand(0x01);/*清除屏幕,置AC 为0,光标回位*/
LcdWriteCommand(0x06);/*增量方式不移位*/
LcdWriteCommand(0x0c);/*显示开,光标关,不闪烁*/
}
void main()
{
DDRA=0X00;
PORTA=0X00;
DDRC=0XE0;
LcdRest();
ePutstr(0,0,e);