📄 lcd.c
字号:
#include<pic.h> //Vo pin---Voltage about 0.6V is engough!!!
#include<string.h>
#define LEFT 0
#define RIGHT 1
const unsigned int delay=100; //serve for function"Delay1ms"
const unsigned char string[11]={0x57,0x45,0x4c,0x43,0x4f,0x4d,0x45,0x20,0x59,0x4f,0x55};
/*delay 1ms*/
void Delay1ms()
{
unsigned int d=delay;
while(--d){;}
}
/*delay N ms*/
void DelayNms(unsigned int n)
{
unsigned int i;
for(i=n;i--;)
Delay1ms();
}
/*Produce a SignalE Toggle */
void Toggle()
{
RD7=1; //E=1
RD7=0; //E=0
// DelayNms(3);
}
/*Read the busy Flag*/
unsigned char ReadBF()
{
unsigned char temp;
TRISC=0XFF;
while(1)
{
RD5=0; //RS=0
RD6=1; //R/W=1
RD7=1; //E=1
temp=PORTC; //READ THE BF AND ADD
RD7=0; //E=0;
if(RC7==0) break;
}
return temp;
}
/*Write command code*/
void WRCommand(unsigned char command)
{
ReadBF();
TRISC=0;
RD6=0; //R/W=0
PORTC=command;
Toggle();
}
/*Write Display Data*/
void WRData(unsigned char data)
{
ReadBF();
TRISC=0;
RD5=1; //RS=1
RD6=0; //R/w=0
PORTC=data;
Toggle();
}
/*Read display data */
unsigned char RDData()
{
unsigned char data;
ReadBF();
RD5=1; //RS=1
RD6=1; //R/W=1
RD7=1; //E=1
data=PORTC;
RD7=0;
return data;
}
/*initial LCD*/
void Init_LCD()
{
TRISC=0X00;
PORTC=0X30;
RD5=0; //RS=0
RD6=0; //R/W=0
Toggle();
DelayNms(5);
PORTC=0X30;
RD5=0; //RS=0
RD6=0; //R/W=0
Toggle();
Delay1ms();
PORTC=0X30;
RD5=0;
RD6=0;
Toggle();
DelayNms(2);
WRCommand(0x38); //8-bit bus,2 line display,5*8 pixel
WRCommand(0x08); //Display Off
WRCommand(0x01); //Clean Screen
DelayNms(20);
WRCommand(0x06); //pattern not move,cursor move right
WRCommand(0x0F); //display,cursor,blast
}
/*Display Character String*/
void DisplayChar(const unsigned char *str,unsigned char x,unsigned char y)
{ //str--name of the array;len--length of array;x--x axis;y--y axis
//if the output char contain CGRAM,must run SetCGRAM first
unsigned char i,pointer=0x80+x+y*64,temp;
unsigned int len=strlen(str);
WRCommand(pointer);
for(i=0;i<len;i++)
{
if(str[i]<='Z' && str[i]>='A')
temp=0x41+(str[i]-'A');
if(str[i]<='z' && str[i]>='a')
temp=0x61+(str[i]-'a');
if(str[i]<='9' && str[i]>='0')
temp=0x30+(str[i]-'0');
if(str[i]=='!') temp=0x21;
if(str[i]==',') temp=0x2c;
if(str[i]==':') temp=0x3a;
if(str[i]=='.') temp=0x2e;
if(str[i]==' ') temp=0x20;
if(str[i]== 39) temp=0x27;
if(str[i]=='-') temp=0x2d;
if(str[i]=='<') temp=0x3c;
WRData(temp);
}
}
/*Build custom character model union*/
void SetCGRAM(const unsigned char *str,unsigned char len)
{
unsigned char i;
WRCommand(0x40); //the beginning address of CGRAM
for(i=0;i<len;i++)
WRData(str[i]);
}
/*Move cursor*/
void CursorScroll(unsigned char direction,unsigned char x,unsigned char y,unsigned char len)
{
unsigned char i,pointer=0x80+x+y*64;
WRCommand(pointer);
for(i=0;i<len;i++)
{
if(direction==LEFT) WRCommand(0x10);
else WRCommand(0x14);
DelayNms(200);
}
}
/*Screen scroll*/
void ScreenScroll(unsigned char direction) //circularly move
{
while(1)
{
if(direction==LEFT) WRCommand(0x18); //Left scroll
else WRCommand(0x1C); //Right scroll
DelayNms(600);
}
}
/*Return to initial position*/
void ReturnHome()
{
WRCommand(0x02);
}
/*clean screen*/
void CleanScreen()
{
WRCommand(0x01);
}
/*hide cursor*/
void HideCursor()
{
WRCommand(0x0c);
}
/*display cursor*/
void CursorOn()
{
WRCommand(0x0f);
}
/*initial I/O PORTs*/
void Init_IO()
{
TRISD=0X00;
TRISC=0X00;
}
/*Test*/
void Test()
{
CleanScreen();
DisplayChar("Y're welcome!!",1,0);
DisplayChar("What a fuck !!",18,1);
HideCursor();
for(;;)ScreenScroll(LEFT);
}
void DisplayNum(int val,unsigned char x,unsigned char y) //4-bit decimal number to characters
{
unsigned char digit[5];
digit[4]=val%10+48;
digit[3]=(val%100-digit[4])/10+48;
digit[2]=(val%1000-digit[3]-digit[4])/100+48;
digit[1]=(val%10000-digit[3]-digit[2]-digit[4])/1000+48;
if(val<0)digit[0]='-';
else digit[0]=' ';
DisplayChar(digit,x,y);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -