📄 lcd_61.c
字号:
//LCD1602 Driver Create by nie xiong 2006.11.18
#include "spce061a.h"
#define CS1 0x0004 // IOA2 Set 1
#define CS0 0xfffb //Set 0
#define RW1 0x0002 // IOA1
#define RW0 0xfffd //Set 0
#define DC1 0x0001 // IOA0
#define DC0 0xfffe //Set 0
#define VSS0 0xffbf //DB6 VSS(GND)
#define VCC1 0x0080 //DB7 VCC(5V)
#define LCDDIR *P_IOB_Dir // IOB0~IOB7 Data
#define LCDBUS *P_IOB_Data // IOB0~IOB7 Data
#define LCDCTRL *P_IOA_Buffer // IOA0~IOA7 Ctrl
unsigned int sys10mscounter;
//unsigned int syslimitcounter;
int path1[8]={0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f};/*自定义符号:横1*/
int path2[8]={0x1f,0x00,0x1f,0x00,0x1f,0x00,0x1f,0x00};/*自定义符号:横2*/
int pats1[8]={0x15,0x15,0x15,0x15,0x15,0x15,0x15,0x15};/*自定义符号:竖1*/
int pats2[8]={0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a};/*自定义符号:竖2*/
void soft_nop(){}
void soft_10ms()/***********12MHZ 提供10MS 软件延时************/
{ int i;
for(i=0;i<711;i++) *P_Watchdog_Clear = 0x01;
}
void soft_20ms()/***********12MHZ 提供20MS 软件延时************/
{ soft_10ms();
soft_10ms();
}
void hard_10ms(unsigned int delaytime) /*基于10MS 的硬件延时*/
{ sys10mscounter=delaytime;
while(sys10mscounter--) soft_10ms();
*P_Watchdog_Clear = 0x01;
}
unsigned int lcdcounter;
unsigned int lcdusing1,lcdusing2;
unsigned int lcd_checkbusy()/*检查LCD 忙*/
{ int lcdstate;
LCDCTRL &= DC0; //DC=1为数据,=0为命令.
LCDCTRL |= RW1; //RW=1为读,=0为写.
LCDCTRL |= CS1; //CS=1选通.
LCDDIR = 0x0000;
soft_nop();
lcdstate=LCDBUS & 0x00ff;
LCDDIR = 0xffff;
LCDCTRL &= CS0;
return((unsigned int )(lcdstate&0x80));
}
void lcd_wrcmd(unsigned int lcdcmd) /*写LCD 命令*/
{ lcdusing1=1;
while(lcd_checkbusy())*P_Watchdog_Clear = 0x01;
LCDBUS &=0xff00;
LCDBUS |= lcdcmd;
LCDCTRL &= DC0;
LCDCTRL &= RW0;
LCDCTRL |= CS1;
soft_nop();
LCDCTRL &= CS0;
LCDBUS |=0x00ff;
lcdusing1=0;
}
void lcd_moveto(int position) /*移动光标到指定位.0-79*/
{ int cmd=0x80;
lcdcounter=position;
if (position > 47)
position += 0x18;
else
{ if (position > 31)position -= 0x14; ///
else
{ if (position > 15) position += 0x2c; ////
}
}
cmd=cmd|position;
lcd_wrcmd(cmd);
*P_Watchdog_Clear = 0x01;
}
void lcd_wrdata(int lcddata) /*在当前显示位置显示数据*/
{ lcdusing2=1;
while(lcd_checkbusy()) *P_Watchdog_Clear = 0x01;
if(lcdcounter==16){
lcd_moveto(16);
while(lcd_checkbusy()) *P_Watchdog_Clear = 0x01;
}
if(lcdcounter==32){
lcd_moveto(32);
while(lcd_checkbusy()) *P_Watchdog_Clear = 0x01;
}
if(lcdcounter==60){
lcd_moveto(60);
while(lcd_checkbusy())*P_Watchdog_Clear = 0x01;
}
if(lcdcounter==80){
lcd_moveto(0);
while(lcd_checkbusy())*P_Watchdog_Clear = 0x01;
lcdcounter=0;
} /*为通用而如此*/
lcdcounter++;
LCDBUS &=0xff00;
LCDBUS |= lcddata;
LCDCTRL |= DC1; //DC=1为数据,=0为命令.
LCDCTRL &= RW0; //RW=1为读,=0为写.
LCDCTRL |= CS1; //CS=1选通.
soft_nop();
LCDCTRL &= CS0;
LCDBUS |=0x00ff;
lcdusing2=0;
}
void lcd_wrdata1(int index,int lcddata) /*在index显示位置显示字符lcddata*/
{ unsigned int position,cmd =0x80;
lcdusing2=1;
while(lcd_checkbusy()) *P_Watchdog_Clear = 0x01;
//lcd_moveto(index);
cmd =0x80;
position = index;
cmd=cmd|position;
lcd_wrcmd(cmd);
soft_20ms();
while(lcd_checkbusy()) *P_Watchdog_Clear = 0x01;
//lcdcounter++;
LCDBUS &=0xff00;
LCDBUS |= lcddata;
LCDCTRL |= DC1; //DC=1为数据,=0为命令.
LCDCTRL &= RW0; //RW=1为读,=0为写.
LCDCTRL |= CS1; //CS=1选通.
soft_nop();
LCDCTRL &= CS0;
LCDBUS |=0x00ff;
lcdusing2=0;
}
void lcd_string(unsigned int *strpoint) /*在当前显示位置显示LCD 字符串*/
{ int i=0;
while(strpoint[i]!=0){
lcd_wrdata(strpoint[i]);
i++;
*P_Watchdog_Clear = 0x01;
}
}
void lcd_init()/*初始化*/
{ lcd_wrcmd(0x38); /*设置8 位格式,2 行,5*7*/
lcd_wrcmd(0x0c); /*整体显示,关光标,不闪烁*/
lcd_wrcmd(0x06); /*设定输入方式,增量不移位*/
lcd_wrcmd(0x01); /*清除显示*/
lcdcounter=0;
}
void lcd_cls()/*清除显示*/
{ lcd_wrcmd(0x01);
lcdcounter=0;
}
void PortInit(void)
{
*P_IOA_Dir = 0xffbf;
*P_IOA_Attrib = 0xffff;
*P_IOA_Data = 0xffff;
*P_IOA_Buffer = 0xffbf;
*P_IOB_Dir = 0xffff;
*P_IOB_Attrib = 0xffff;
*P_IOB_Data = 0xffff;
//LCDCTRL &=VSS0;
}
void lcd_test(unsigned int data1)
{ unsigned int j;
lcd_init();
for(j=0;j<32;j++){lcd_wrdata(data1+j);}
hard_10ms(100);
}
main()
{
unsigned int j;
//IE=0;P0=0xff;P1=0xff;P2=0xff;P3=0xff; /*初始化T*/
PortInit();
lcd_init();soft_20ms();
lcd_test(0x30);
while(1)
{
lcd_init();
//lcd_string("1234567890123456 890123456789abc");
lcd_string("Niexoing,Who areyou. 2006.11.18.");
hard_10ms(500);
lcd_test(0x40);
hard_10ms(500);
*P_Watchdog_Clear = 0x01;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -