📄 lcd._c
字号:
#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
#define LCM_RS_1 PORTB|=BIT(PB0)
#define LCM_RS_0 PORTB&=~BIT(PB0)
#define LCM_RW_1 PORTB|=BIT(PB2)
#define LCM_RW_0 PORTB&=~BIT(PB2)
#define LCM_EN_1 PORTB|=BIT(PB3)
#define LCM_EN_0 PORTB&=~BIT(PB3)
#define DataPort PORTC
#define Busy 0x80
//***************************************
void ePutstr(uchar x,uchar y,uchar const *ptr)
{
uchar i,l=0;
while(ptr[l]>31){l++;}// ptr[l]大于31时,为ASCII码,进入while语句循环,
//l累加,计算出字符串长度。
for(i=0;i<l;i++){
DisplayOneChar(x++,y,ptr[i]);// 显示单个字符,同时x轴座标递增。
if(x==16){//若x等于16,进入if语句。
x=0;y^=1;// x赋0,y与1按位异或(取反)。
}
}
}
//*************************************
void LocateXY(char posx,char posy)
{
uchar temp;
temp&=0x7f;// temp 的变化范围0~15。
temp=posx&0x0f;//屏蔽高4位。
posy&=0x03;// posy的变化范围0~1。
switch(posy)
{
case 1:temp|=0x40;break;//若posy 为1(显示第2行),地址码+0x40。
case 2:temp|=0x10;break;//若posy 为2(显示第3行),地址码+0x10。
case 3:temp|=0x50;break;//若posy 为3(显示第3行),地址码+0x50。
default:break;
}
temp|=0x80;//指令码为地址码+0x80。
LcdWriteCommand(temp,0);// 将指令temp 写入LCM,忽略忙信号检测。
}
//**************************************
void DisplayOneChar(uchar x,uchar y,uchar Wdata)
{
LocateXY(x,y);//调用LocateXY 函数定位显示地址。
LcdWriteData(Wdata);//将数据Wdata写入LCM。
}
//****************************************
void InitLcd(void)
{
LcdWriteCommand(0x38,0); //8位数据传送,2行显示,5*7字形,不检测忙信号
Delay_nms(5);
LcdWriteCommand(0x38,0); //8位数据传送,2行显示,5*7字形,不检测忙信号
Delay_nms(5);
LcdWriteCommand(0x38,0); //8位数据传送,2行显示,5*7字形,不检测忙信号
Delay_nms(5);
LcdWriteCommand(0x38,1); //8位数据传送,2行显示,5*7字形,检测忙信号
LcdWriteCommand(0x08,1); //关闭显示,检测忙信号
LcdWriteCommand(0x01,1); //清屏,检测忙信号
LcdWriteCommand(0x06,1); //显示光标右移设置,检测忙信号
LcdWriteCommand(0x0c,1); //显示屏打开,光标不显示、不闪烁,检测忙信号
}
//****************************************
void LcdWriteCommand(uchar CMD,uchar Attribc)
{
if(Attribc)WaitForEnable();
Delay_nms(5);
LCM_RS_0;LCM_RW_0;_NOP();
DataPort=CMD;_NOP();
LCM_EN_1;_NOP();_NOP();LCM_EN_0;
}
//***************************************
void LcdWriteData(uchar dataW)
{
WaitForEnable();
Delay_nms(5);
LCM_RS_1;LCM_RW_0;_NOP();
DataPort=dataW;_NOP();
LCM_EN_1;_NOP();_NOP();LCM_EN_0;
}
//****************************************
void WaitForEnable(void)
{
uchar val;
DataPort=0xff;
LCM_RS_0;LCM_RW_1;_NOP();
LCM_EN_1;_NOP();_NOP();
DDRC=0x00;
val=PINA;
while(val&Busy){val=PINC;WDR();}
LCM_EN_0;
DDRC=0xff;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -