6.2程序代码
#include
#define uchar unsigned char #define uint unsigned int
uchar code zifux[]=%uchar code zifu0[]=\ Constant %uchar code zifu1[]=\ \//按键
sbit key1=P3^0; sbit key2=P3^1;
unsigned long da1=100;// unsigned long dax1=200;// voidxianshi() { //out
lcd1602_adr(0x45);
lcd1602_writenumber(0x30+dax1000/1000); lcd1602_writenumber(0x30+dax100/100); lcd1602_writenumber(0x30+dax10/10); lcd1602_writenumber(0x30+dax1); da1=dax1*975/2000; DAconver1(da1); //调节 void main() {
_delay_ms(1);
11
//初始化 lcd1602_init();
LCD1602_string(1,1,zifu0); LCD1602_string(1,1,zifu0); LCD1602_string(2,1,zifu1); //初始化 while(1) {
//显示函数 xianshi(); }
}
sbit clk1=P2^0; sbit cs1=P2^1; sbit din1=P2^2; sbit dout1=P2^3; //延时函数ms void _delay_ms(uint t) { uinti,j; for(i=0;i //延时函数us void _delay_us(uchar t) { while(t>0)t--; } //12个时钟驱动,十位数据在12位的前 12 //16位数据DAValue先左移6位,剩下的十位是有效数据 void DAconver1(uintDAValue) { uchari; DAValue<<= 6; cs1 = 0; clk1 = 0; for(i = 0; i< 12; i++) // { din1 = (bit)(DAValue& 0x8000); clk1 = 1; DAValue<<= 1; clk1 = 0; } cs1 = 1; clk1 = 0; } sbit RS=P3^2; //写信号 sbit RWW=P3^3; //读信号 sbit E=P3^4; //使能信号 #define e1 E=1 #define rd1 RWW=1 #define rs1 RS=1 #define e0 E=0 #define rd0 RWW=0 #define rs0 RS=0 #define PP P1 //写1602控制字 void lcd1602_writecrtl(uchardat) 内输入的是10位DA数据,后两个时钟周期 // 为填充字节。 // CS的上升沿和下降沿只有在clk为低的时候 // 才有效 13 { rd0;//读信号置0 rs0;//写信号置0 _delay_us(5); PP=dat; e1;//使能信号置1 _delay_us(5); e0;//使能信号置0 } //写1602数据 void lcd1602_writenumber(uchardat) { rd0;//读信号置0 rs1;//写信号置1 _delay_us(5); PP=dat; e1;//使能信号置1 _delay_us(5); e0;//使能信号置0 } //1602初始化 void lcd1602_init() { lcd1602_writecrtl(0x38); //显示模式 lcd1602_writecrtl(0x06); //显示光标移动位置 lcd1602_writecrtl(0x0c); //显示开及光标设置 lcd1602_writecrtl(0x01); //显示清屏 } //显示地址 14 void lcd1602_adr(uchardat) { lcd1602_writecrtl(0x80 | dat); } //行显示- void LCD1602_string(ucharhang,ucharlie,ucharconst *p) { uchar a; if(hang == 1) a = 0x00; if(hang == 2) a = 0x40; a = a + lie - 1; lcd1602_adr(a); while(1) { if(*p == '\\0') break; lcd1602_writenumber(*p); p++; } } 15