|
寫過(guò)一個(gè)LED數(shù)碼管顯示的電壓表:http://www.raoushi.com/bbs/dpj-43375-1.html
今天寫了1602 LCD 顯示的。
//E:\DPJ_C\ICL7135\ICL7135_LCD.C 2012.03.07
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_DB P0
sbit LCD_RS=P2^0;
sbit LCD_RW=P2^1;
sbit LCD_E=P2^2;
uchar dat0[ ]={" ICL7135 TEST "};
uchar dat1[ ]={"0123456789."};
uchar dat2[ ]={"Voltage = V"};
uint V;
void LCD_init();//初始化函數(shù)
void LCD_write_command(uchar com);//寫指令函數(shù)
void LCD_write_data(uchar dat);//寫數(shù)據(jù)函數(shù)
void LCD_disp_char(uchar x,uchar y,uchar dat);//顯示一個(gè)字符函數(shù)
void LCD_display();//顯示函數(shù)
void LCD_write_data1(uint volts);//寫電壓數(shù)據(jù)函數(shù)
void delay_n40us(uint n);//延時(shí)函數(shù)
void delay(uint ms)
{
uchar i;
while(ms--)
for(i=0;i<123;i++);
}
void main()
{
TMOD=0X0d;//00001101
TH0=0;
TL0=0;
ET0=1;
EX0=1;
IT0=1;
EA=1;
TR0=1;
LCD_init();
while(1)
{
LCD_display();
delay(500);
}
}
void ADC_7135() interrupt 0
{
V=TH0;
V<<=8;
V+=TL0;
V-=10001;
TL0=0;
TH0=0;
}
//********初始化函數(shù)************
void LCD_init()
{
LCD_write_command(0x38);//設(shè)置 8位格式,2行,5x7
LCD_write_command(0x0c);//整體顯示,關(guān)光標(biāo),不閃爍
LCD_write_command(0x06);//設(shè)定輸入方式,增量不移位
LCD_write_command(0x01);//清除屏幕顯示
delay_n40us(100);// 2ms
}
//********寫指令函數(shù)************
void LCD_write_command(uchar com)
{
LCD_DB=com;
LCD_RS=0;//指令
LCD_RW=0;//寫入
LCD_E=1;//允許
LCD_E=0;
delay_n40us(1);
}
//********寫數(shù)據(jù)函數(shù)*************
void LCD_write_data(uchar dat)
{
LCD_DB=dat;
LCD_RS=1;//數(shù)據(jù)
LCD_RW=0;//寫入
LCD_E=1;//允許
LCD_E=0;
delay_n40us(2);
}
//********寫電壓數(shù)據(jù)函數(shù)*************
void LCD_write_data1(uint volts)
{
LCD_DB=volts;
LCD_RS=1;//數(shù)據(jù)
LCD_RW=0;//寫入
LCD_E=1;//允許
LCD_E=0;
delay_n40us(2);
}
//*******顯示一個(gè)字符函數(shù)*********
void LCD_disp_char(uchar x,uchar y,uchar dat)
{
uchar address;
if(y==1)
address=0x80|x;
else
address=0xc0|x;
LCD_write_command(address);
LCD_write_data(dat);
}
//*******顯示函數(shù)*********
void LCD_display()
{
uchar i,a[5];
for(i=0;i<16;i++)
{
LCD_disp_char(i,1,dat0);
LCD_disp_char(i,2,dat2);
}
LCD_write_command(0xc9);//第9個(gè)字符開(kāi)始顯示
a[0]=V/10000;
a[1]=V%10000/1000;
a[2]=V%1000/100;
a[3]=V%100/10;
a[4]=V%10;
LCD_write_data1(dat1[a[0]]);
LCD_write_data1(dat1[10]);
LCD_write_data1(dat1[a[1]]);
LCD_write_data1(dat1[a[2]]);
LCD_write_data1(dat1[a[3]]);
LCD_write_data1(dat1[a[4]]);
}
void delay_n40us(uint n)
{
uint i;
uchar j;
for(i=n;i>0;i--)
for(j=0;j<2;j++);
}
|
|