资源描述
/* 用法:
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可不用端口,直接接地)
要使用本驱动,改变下面配置信息即可
-----------------------------------------------------------------*/
#include <STC12C5A.h >
#include <intrins.h>
#define ADC_POWER 0x80
#define ADC_FLAG 0x10
#define ADC_START 0x08
#define ADC_SPEEDLL 0x00
#define ADC_SPEEDL 0x20
#define ADC_SPEEDH 0x40
#define ADC_SPEEDHH 0x60
#define uchar unsigned char
#define uint unsigned int
#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;
/*--------------------------------------------------------------------------------------------------
函数说明
--------------------------------------------------------------------------------------------------*/
void LCD_init(void);
void LCD_en_write(void);
void LCD_write_command(uchar command) ;
void 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);
//-----------------------------------------------------------------------------------------
void delay_1us(void) //1us延时函数
{
_nop_(); //asm("nop");
}
void delay_nus(uint n) //N us延时函数
{
uint i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //误差 0us 可以用单片机小精灵计算 已包含调用时间。
{
uchar a,b;
for(b=222;b>0;b--)
for(a=12;a>0;a--);
}
void delay_nms(uint n) //N ms延时函数
{
uint i=0;
for (i=0;i<n;i++)
delay_1ms();
}
void LCD_init(void) //液晶初始化
{
LCD_RW=0; //RW=0
LCD_write_command(0x38);
LCD_en_write();
delay_nus(40);
LCD_write_command(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);
LCD_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);
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_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; //低四位移到高四位
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_PORT&=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 + 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_char(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|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] = bai;
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);
}
}
展开阅读全文