完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include #include #define DD P2 ***it CS=P1^0; //定义端口 ***it CLK=P1^1; ***it Do=P1^2; ***it Di=P1^3; ***it KEY=P1^7; ***it buzzer=P0^0; ***it Rs=P1^4;//LCD定义端口 ***it Rw=P1^5; ***it E=P1^6; ***it busy_p=ACC^7; unsigned char CH=0x02,key_count=1; unsigned char HC[6]={0}; unsigned char BZ=0; void delay_1ms(unsigned char i) //延时函数1ms { unsigned char j; while(i--) for(j=0;j<125; j++); } void delay_10ns(unsigned char i)//延时函数10ns { unsigned char j; while(i--) for(j=0;j<10; j++); } void xiezhiling(unsigned char com,bit p)//写命令函数 { if(p) delay_10ns(5); E=0; Rs=0; Rw=0; DD=com; delay_10ns(50); E=1; delay_1ms(2); E=0; delay_10ns(4); } void xieshuju(unsigned char DATA)//写数据函数 { delay_10ns(50); E=0; Rs=1; Rw=0; DD=DATA; delay_10ns(50); E=1; delay_10ns(50); E=0; delay_10ns(4); } void init(void)//初始化函数 { delay_1ms(15); xiezhiling(0x38,0); delay_1ms(5); xiezhiling(0x38,0); delay_1ms(5); xiezhiling(0x38,0); delay_1ms(5); xiezhiling(0x38,1); //功能设定(8位,2行,5*7点矩阵) xiezhiling(0x08,1); xiezhiling(0x01,1); //清除屏幕 xiezhiling(0x06,1); //加1 xiezhiling(0x0c,1);//显示器ON,光标OFF,闪烁OFF } unsigned char ADconv(void)//AD转换子程序 { unsigned char i; unsigned int data_f=0,data_c=0; Di=1; CS=1; _nop_(); CS=0; Di=1; CLK=1; _nop_(); _nop_(); CLK=0; _nop_(); _nop_(); CLK=1; Di=(bit)(0x02&CH); _nop_(); CLK=0; _nop_(); _nop_(); CLK=1; Di=(bit)(0x01&CH); _nop_(); _nop_(); CLK=0; Di=1; CLK=1; _nop_(); _nop_(); CLK=0; _nop_(); CLK=1; for(i=8;i>0;i--) { data_f|=Do; data_f<<=1; CLK=1; _nop_(); _nop_(); CLK=0; _nop_(); } for(i=8;i>0;i--) { data_c<<=1; data_c|=Do; _nop_(); CLK=1; _nop_(); _nop_(); CLK=0; _nop_(); } CLK=0; _nop_(); _nop_(); CLK=1; _nop_(); _nop_(); CLK=0; _nop_(); _nop_(); CLK=1; _nop_(); CS=1; _nop_(); _nop_(); return data_f; } void xs_int(unsigned int shuju)//将一个数据各个位分开并存放在H C[]数组中 { if(shuju < 10) BZ = 1; else if(shuju < 100) BZ = 2; else if(shuju < 1000) BZ = 3; else if(shuju < 10000) BZ = 4; else if(shuju < 65535) BZ = 5; switch(BZ) { case 5:HC[5] = shuju/10000; //百位 case 4:HC[3] = shuju%10000/1000;//十位 case 3:HC[2] = shuju%1000/100;//个位 case 2:HC[1] = shuju%100/10;//小数点后一位 case 1:HC[0] = shuju%10; break; default:break; } } void main(void)//主函数 { unsigned int data_temp=0; init(); TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; ET0=1; TR0=1; while(1) { data_temp=ADconv(); if(data_temp>128)//报警数值 { EA=1; } else { EA=0; buzzer=0; } P3=~data_temp; xs_int(196*data_temp); if(KEY==0) { delay_1ms(10); if(KEY==0) while(!KEY); key_count++; if(key_count==3) key_count=1; xiezhiling(0x01,0); } if(key_count==1) { xiezhiling(0x80,1);//LCD第一行 xieshuju('P'); xieshuju('R'); xieshuju('E'); xieshuju('S'); xieshuju('S'); xieshuju('U'); xieshuju('R'); xieshuju('E'); xieshuju('-'); xieshuju(HC[5]+0x30); xieshuju(HC[3]+0x30); xieshuju(HC[2]+0x30); xieshuju('.'); xieshuju(HC[1]+0x30); xieshuju('P'); xieshuju('a'); xiezhiling(0x80+0x40,1);//LCD第二行 xieshuju('A'); xieshuju('l'); xieshuju('a'); xieshuju('r'); xieshuju('m'); xieshuju(' '); xieshuju('S'); xieshuju('t'); xieshuju('a'); xieshuju('t'); xieshuju('u'); xieshuju('s'); xieshuju('-'); xieshuju('-'); xieshuju('O'); xieshuju('N'); } if(key_count==2) { xiezhiling(0x80,1);//LCD第一行 xieshuju('P'); xieshuju('r'); xieshuju('e'); xieshuju('s'); xieshuju('s'); xieshuju('u'); xieshuju('r'); xieshuju('e'); xieshuju(' '); xieshuju('i'); xieshuju('s'); xieshuju(' '); xieshuju('O'); xieshuju('K'); xieshuju(' '); xieshuju(' '); xiezhiling(0x80+0x40,1);//LCD第二行 xieshuju('m'); xieshuju('a'); xieshuju('d'); xieshuju('e'); xieshuju(' '); xieshuju('B'); xieshuju('Y'); xieshuju(' '); xieshuju('y'); xieshuju('u'); xieshuju('a'); xieshuju('n'); xieshuju('s'); xieshuju('h'); xieshuju('u'); xieshuju('o'); xieshuju(' '); } } } void timer0() interrupt 1 // 中断方式1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; buzzer=!buzzer; }
|
|
相关推荐
4个回答
|
|
你可以参考一下 #include #define LCD1602_DB P0 //data bus 数据总线 ***it LCD1602_RS = P2^6; ***it LCD1602_RW = P2^5; ***it LCD1602_EN = P2^7; void InitLcd1602(); void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str); void main() { unsigned char str[] = "Welcome To Lcd"; InitLcd1602(); LcdShowStr(0,0,str); while(1); } void Read_Busy() //忙检测函数,判断bit7是0,允许执行;1禁止 { unsigned char sta; // LCD1602_DB = 0xff; LCD1602_RS = 0; LCD1602_RW = 1; do { LCD1602_EN = 1; sta = LCD1602_DB; LCD1602_EN = 0; //使能,用完就拉低,释放总线 }while(sta & 0x80); } void Lcd1602_Write_Cmd(unsigned char cmd) //写命令 { Read_Busy(); LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_DB = cmd; LCD1602_EN = 1; LCD1602_EN = 0; } void Lcd1602_Write_Data(unsigned char dat) //写数据 { Read_Busy(); LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_DB = dat; LCD1602_EN = 1; LCD1602_EN = 0; } void LcdSetCursor(unsigned char x,unsigned char y) //坐标显示 { unsigned char addr; if(y == 0) addr = 0x00 + x; else addr = 0x40 + x; Lcd1602_Write_Cmd(addr|0x80); } void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str) //显示字符串 { LcdSetCursor(x,y); //当前字符的坐标 while(*str != ' |