1、 /* 用法: LCD_init(); LCD_write_string(列,行,"字符串"); LCD_write_char(列,行,'字符'); --------------------------------------------------------------- 下面是STC与LCD连接信息 LCD data P0.4-P0.7 RS P2.4 EN P2.6 RW P2.5(RW可不用端口,直接接地) 要使用本驱动,改变下面配置信息即可 ---------------------------------------------------
2、/
#include
3、 #define LCD_DATA_PORT P0 #define LCD_RS P24 // out #define LCD_EN P26 // out #define LCD_RW P25 // out 可以直接接地 uchar dis1[]={"CCFROBOT IR:0A21"}; uchar dis2[]={"Range: cm"}; uchar ch=0; uint ADC_HS; uchar bai,shi,ge; uchar buf[6]; uint A2D; /*--
4、 函数说明 --------------------------------------------------------------------------------------------------*/ void LCD_init(void); void LCD_en_write(void); void LCD_write_command(uchar command) ; void
5、LCD_write_data(uchar data1); // (uchar data); 不能定义为data,data在kiel51中是关键词。 void LCD_set_xy (uchar x, uchar y); void LCD_write_string(uchar X,uchar Y,uchar *s); void LCD_write_char(uchar X,uchar Y,uchar data1); //----------------------------------------------------------------------------------
6、
void delay_1us(void) //1us延时函数
{
_nop_(); //asm("nop");
}
void delay_nus(uint n) //N us延时函数
{
uint i=0;
for (i=0;i 7、b>0;b--)
for(a=12;a>0;a--);
}
void delay_nms(uint n) //N ms延时函数
{
uint i=0;
for (i=0;i 8、nd(0x38); //0x38为8位显示 0x28为4位显示
LCD_write_command(0x0c); //显示开
LCD_write_command(0x01); //清屏
delay_nms(2);
}
void LCD_en_write(void) //液晶使能
{
LCD_EN=1;
delay_nus(1);
LCD_EN=0;
}
void LCD_write_command(uchar command) //写指令
{
/*
//连线为高4位的写法
delay_nus(16);
L 9、CD_RS=0; //RS=0
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写高四位
LCD_en_write();
command=command<<4; //低四位移到高四位
LCD_DATA_PORT&=0x0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写低四位
LCD_en_write(); */
//数据线8线写法
delay_nus(16 10、);
LCD_RS=0; //RS=0
LCD_DATA_PORT=command;
LCD_en_write();
/*
//连线为低四位的写法
delay_nus(16);
LCD_RS=0; //RS=0
LCD_DATA_PORT&=0xf0; //清高四位
LCD_DATA_PORT|=(command>>4)&0x0f; //写高四位
LCD_en_write();
LCD_DATA_PORT&=0xf0; //清高四位
LCD_DATA 11、PORT|=command&0x0f; //写低四位
LCD_en_write();
*/
}
void LCD_write_data(uchar data1) //写数据
{
/*
//连线为高4位的写法
delay_nus(16);
LCD_RS=1; //RS=1
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=data1&0xf0; //写高四位
LCD_en_write();
data1=data1<<4; //低四位移 12、到高四位
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=data1&0xf0; //写低四位
LCD_en_write(); */
//数据线8线写法
delay_nus(16);
LCD_RS=1; //RS=1
LCD_DATA_PORT=data1;
LCD_en_write();
/*
//连线为低四位的写法
delay_nus(16);
LCD_RS=1; //RS=1
LCD_DATA_P 13、ORT&=0Xf0; //清高四位
LCD_DATA_PORT|=(data>>4)&0x0f; //写高四位
LCD_en_write();
LCD_DATA_PORT&=0Xf0; //清高四位
LCD_DATA_PORT|=data&0x0f; //写低四位
LCD_en_write();
*/
}
void LCD_set_xy( uchar x, uchar y ) //写地址函数
{
uchar address;
if (y == 0) address = 0x80 + 14、 x;
else address = 0xc0 + x;
LCD_write_command( address);
}
void LCD_write_string(uchar X,uchar Y,uchar *s) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
while (*s!='\0') // 写显示字符
{
LCD_write_data( *s );
s ++;
}
}
void LCD_write_ch 15、ar(uchar X,uchar Y,uchar data1) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
LCD_write_data( data1);
}
void AD_Init()
{
P1ASF = 0x01;
ADC_RES = 0;
ADC_CONTR = ADC_POWER|ADC_SPEEDLL|ADC_START|ch;
delay_nms(2);
}
void GetADCResult()
{
ADC_CONTR = ADC_POWER|ADC_SPEEDLL|0| 16、ADC_START;
_nop_();
_nop_();
_nop_();
_nop_();
while(!(ADC_CONTR&ADC_FLAG));
ADC_CONTR&=~ADC_FLAG;
A2D=ADC_RES*256+ADC_RESL;
ADC_HS = (7605/(A2D+12))-4;
// return ADC_HS;
}
void Count()
{
bai=(ADC_HS/100+48);
shi=(ADC_HS%100/10+48);
ge=(ADC_HS%100%10+48);
buf[0] = b 17、ai;
buf[1] = shi;
buf[2] = ge;
buf[3] = 0x0d;
buf[4] = 0x0a;
buf[5] = 0x00;
}
void main()
{
AUXR1=0x04;
LCD_init();
AD_Init();
LCD_write_string(0,0,dis1);
LCD_write_string(0,1,dis2);
while(1)
{
GetADCResult();
Count();
LCD_write_char(7,1,bai);
LCD_write_char(8,1,shi);
LCD_write_char(9,1,ge);
}
}






