标题:
这个单片机程序错误如何修改?128行K1num显示错误
[打印本页]
作者:
ovov
时间:
2020-4-25 22:29
标题:
这个单片机程序错误如何修改?128行K1num显示错误
#include<reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit CS=P1^0;
sbit CLK=P1^2;
sbit DO=P1^1;
sbit Wat=P1^3;
sbit k1=P3^5;
sbit k2=P3^6;
sbit k3=P3^7;
sbit lcd_rw=P2^2;
sbit lcd_rs=P2^0;
sbit lcd_e=P2^1;
uchar adat; //读出1302的数据
uint k1n;
uchar shiduH=70,shiduL=20; //湿度的上下限
void delay(uint time)
{
uint i,j;
for(i=0;i<time;i++)
for(j=0;j<100;j++);
}
uchar adc0832()
{
uchar i,temp,temp1,num;
CLK=0; DO=1;CS=0; CLK=1;
CLK=0; DO=1; CLK=1;
CLK=0; DO=0; CLK=1;
CLK=0; DO=1;
for(i=0;i<8;i++)
{
CLK=1;
CLK=0;
temp<<=1;
if(DO)temp++;
}
for(i=0;i<8;i++)
{
temp1>>=1;
if(DO)temp1+=0x80;
CLK=1;
CLK=0;
}
CLK=1;DO=1;
CS=1;
if(temp==temp1) num=100-temp*99/255; // adat=100-adat*99/255;
return num;
}
void Read_Busy() //忙检测函数,判断bit7是0,允许执行;1禁止
{
unsigned char sta; //
P0 = 0xff;
lcd_rs= 0;
lcd_rw = 1;
do
{
lcd_e= 1;
sta = P0;
lcd_e = 0; //使能,用完就拉低,释放总线
}
while(sta & 0x80);
}
void Lcd1602_Write_Cmd(uchar cmd) //写命令
{
Read_Busy();
lcd_rs = 0;
lcd_rw = 0;
P0= cmd;
lcd_e = 1;
lcd_e = 0;
}
void Lcd1602_Write_Data(uchar dat) //写数据
{
Read_Busy();
lcd_rs = 1;
lcd_rw = 0;
P0 = dat;
lcd_e = 1;
lcd_e = 0;
}
void init_lcd()
{
uchar code D[]="RH: ";
uchar code T[]="RL: "; //显示湿度下限
uchar code S[]="ShiDu: ";
uchar i;
Lcd1602_Write_Cmd(0x38); //打开,5*8,8位数据
Lcd1602_Write_Cmd(0x0c);
Lcd1602_Write_Cmd(0x06);
Lcd1602_Write_Cmd(0x01); //清屏
Lcd1602_Write_Cmd(0x80);
for(i=0;i<5;i++)
{
Lcd1602_Write_Data(D[ i]);
delay(5);
}
Lcd1602_Write_Cmd(0x87);
for(i=0;i<8;i++)
{
Lcd1602_Write_Data(S);
delay(5);
}
Lcd1602_Write_Cmd(0x80+0x40);
for(i=0;i<5;i++)
{
Lcd1602_Write_Data(T);
delay(5);
}
}
void key0()
{
if(k1==0)
{
delay(5);
if(k1==0)
{
while(!k1);
TR0=0;
Wat=1;
Lcd1602_Write_Cmd(0x0f);
k1n++;
if(k1n==4)
{
TR0=1;
k1num=0;
Lcd1602_Write_Cmd(0x0c);
Lcd1602_Write_Cmd(0x80+0x4a) ;
Lcd1602_Write_Data(0x10);
Lcd1602_Write_Data(0x10);
Lcd1602_Write_Data(0x10);
}
}
}
}
void key1()
{
if(k1n==1)
{
Lcd1602_Write_Cmd(0x84);
if(k2==0)
{
delay(5);
if(k2==0)
{
while(!k2);
shiduH++;
if(shiduH>99)shiduH=99;
Lcd1602_Write_Cmd(0x83);
Lcd1602_Write_Data(0x30+shiduH/10);
Lcd1602_Write_Data(0x30+shiduH%10);
}
}
if(k3==0)
{
delay(5);
if(k3==0)
{
while(!k3);
shiduH--;
if(shiduH<=shiduL)shiduH=shiduL+1;
Lcd1602_Write_Cmd(0x83);
Lcd1602_Write_Data(0x30+shiduH/10);
Lcd1602_Write_Data(0x30+shiduH%10);
}
}
Lcd1602_Write_Cmd(0x84);
}
if(k1n==2)
{
Lcd1602_Write_Cmd(0x80+0x44);
if(k2==0)
{
delay(5);
if(k2==0)
{
while(!k2);
shiduL++;
if(shiduL>shiduH)shiduL=shiduH-1;
Lcd1602_Write_Cmd(0x80+0x43);
Lcd1602_Write_Data(0x30+shiduL/10);
Lcd1602_Write_Data(0x30+shiduL%10);
}
}
if(k3==0)
{
delay(5);
if(k3==0)
{
while(!k3);
shiduL--;
if(shiduL<=1)shiduL=1;
Lcd1602_Write_Cmd(0x80+0x43);
Lcd1602_Write_Data(0x30+shiduL/10);
Lcd1602_Write_Data(0x30+shiduL%10);
}
}
Lcd1602_Write_Cmd(0x80+0x44);
}
if(k1n==3)
{
Lcd1602_Write_Cmd(0x80+0x4a) ;
Lcd1602_Write_Data(0x3e);
Lcd1602_Write_Data(0x3e);
Lcd1602_Write_Data(0x3e);
Lcd1602_Write_Cmd(0x0c) ;
if(k2==0)
{
delay(5);
if(k2==0)
{
while(!k2);
Wat=0;
}
}
if(k3==0)
{
delay(5);
if(k3==0)
{
while(!k3);
Wat=1;
}
}
}
}
void water()
{
if(adat<shiduL) TR0=1;
if(adat>shiduH)
{
TR0=0;
Wat=1;
}
}
void disp0()
{
adat=adc0832();
Lcd1602_Write_Cmd(0x83);
Lcd1602_Write_Data(0x30+shiduH/10);
Lcd1602_Write_Data(0x30+shiduH%10);
Lcd1602_Write_Cmd(0x8D);
Lcd1602_Write_Data(0x30+adat/10);
Lcd1602_Write_Data(0x30+adat%10);
Lcd1602_Write_Cmd(0x80+0x43);
Lcd1602_Write_Data(0x30+shiduL/10);
Lcd1602_Write_Data(0x30+shiduL%10);
}
void main()
{
TMOD=0x01;
TH0=(65536-50000)/256; //初值50ms
TL0=(65536-50000)%256;
EA=1; //开总中断
ET0=1; //开定时器0中断
// TR0=1; //启动定时器0
k1n=0;
init_lcd();
while(1)
{
key0();
if(k1n==0)
{
adat=adc0832();
water();
disp0();
}
if(k1n!=0)
{
key1();
}
}
}
void t0()interrupt 1
{
uchar count;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count>0&&count<=60)
{
Wat=0;
}
if(count>60&&count<=120)
{
if(count==120)count=0;
Wat=1;
}
}
复制代码
作者:
wulin
时间:
2020-4-26 06:05
k1num这个变量没有定义,根据上下文判断应该是楼主笔误把k1n写成k1num。
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1