?? 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 軟件延時(shí)************/
{ int i;
for(i=0;i<711;i++) *P_Watchdog_Clear = 0x01;
}
void soft_20ms()/***********12MHZ 提供20MS 軟件延時(shí)************/
{ soft_10ms();
soft_10ms();
}
void hard_10ms(unsigned int delaytime) /*基于10MS 的硬件延時(shí)*/
{ 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為數(shù)據(jù),=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) /*移動光標(biāo)到指定位.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) /*在當(dāng)前顯示位置顯示數(shù)據(jù)*/
{ 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為數(shù)據(jù),=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為數(shù)據(jù),=0為命令.
LCDCTRL &= RW0; //RW=1為讀,=0為寫.
LCDCTRL |= CS1; //CS=1選通.
soft_nop();
LCDCTRL &= CS0;
LCDBUS |=0x00ff;
lcdusing2=0;
}
void lcd_string(unsigned int *strpoint) /*在當(dāng)前顯示位置顯示LCD 字符串*/
{ int i=0;
while(strpoint[i]!=0){
lcd_wrdata(strpoint[i]);
i++;
*P_Watchdog_Clear = 0x01;
}
}
void lcd_init()/*初始化*/
{ lcd_wrcmd(0x38); /*設(shè)置8 位格式,2 行,5*7*/
lcd_wrcmd(0x0c); /*整體顯示,關(guān)光標(biāo),不閃爍*/
lcd_wrcmd(0x06); /*設(shè)定輸入方式,增量不移位*/
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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -