#define uchar unsigned char
#define uint unsigned int
#define RS_1 P1OUT|=BIT3;//输入数据
#define RS_0 P1OUT&=~BIT3;//输入指令
#define RW_1 P1OUT|=BIT4;//写入指令或者数据
#define RW_0 P1OUT&=~BIT4;//从lcd里面读取数据
#define EN_1 P1OUT|=BIT5;//读取信息
#define EN_0 P1OUT&=~BIT5;//下降沿时候执行指令
#define datt P2OUT;
uchar temp=0;
uchar table1[]={"hello"};
uchar table2[]={"hello"};
/*.................................................
* 延时程序
.................................................. */
void delay(uint i)
{
uint j;
while(--i!=0)
{
for(j=0;j<255;j++);
}
}
/*.................................................
* 忙信号检查
.................................................. */
uchar chkbusy()
{
uchar busy;
P2OUT=0xf0;
EN_0;//读取数据
RS_0;//输入指令
RW_1;//写入指令或者数据
EN_1;//读取信息
busy=P2OUT&0x80;
delay(1);
EN_0;//下降沿执行指令
return(busy);
}
/*.................................................
* 写指令
.................................................. */
void writecom(uchar x,uchar comm)
{
//if(x) while(chkbusy());
EN_0;
RS_0;
RW_0;
EN_1;
P2OUT=(comm&0xf0);
delay(5);
EN_0;
delay(5);
EN_1;
P2OUT=(comm&0x0f)<<4;
delay(5);
EN_0;
}
/*.................................................
* 写数据
.................................................. */
void writedata(uchar dat)
{
//while(chkbusy()); //检查忙信号
EN_0;
RS_1;
RW_0;
EN_1;
P2OUT=(dat&0xf0); //送高四位数据到P2口
delay(5);
EN_0;
delay(5);
EN_1;
P2OUT=(dat&0x0f)<<4; //送低四位数据到P2口
delay(5);
EN_0;
P2OUT=P2OUT|0x0f; //置低四位为1
}
/*.................................................
* lcd初始化程序
.................................................. */
void lcdinit()
{
delay(30);
writecom(0,0x02);//数据指针清零
delay(5);
writecom(1,0x28);//
writecom(1,0x0c);//显示光标开及设置
writecom(1,0x06);//显示光标移动设置
writecom(1,0x01);//显示清屏
delay(10);
}
/*.................................................
* 在指定位置显示一个字符,x为行,Y为列
.................................................. */
void displayone(uchar x,uchar y,uchar *n)
{
uchar add;
if (x == 1) add=0x80+y;
//writecom(1,add);
else add=0xc0+y;
writecom(1,add);
writedata(*n);
}
/*.................................................
* 显示字符串
.................................................. */
void displayleng(uchar x,uchar y,uchar *s)
{
y=y&0x0f;
while(*s)
{
displayone(x,y,s);
y++;
s++;
}
}
/*.................................................
* 主函数
.................................................. */
void main()
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P2SEL=0x00;
P2DIR = 0xff;
P1DIR = 0x38;
lcdinit();
//lcdcls();
P2OUT=0xff;
writecom(1,0x01);
//writedata(0x01);
while(1)
{
displayleng(1,0,table1);
delay(100);
displayleng(2,1,table2);
}
}
上一篇:用STC12C5A60S2自制逻辑分析仪
下一篇:串口通信基本接线方法
推荐阅读最新更新时间:2024-03-16 14:05