近期在做一个温湿度传感器采集,用1602液晶显示,在网上找了一个程序,一直读不到数.
[复制链接]
/*2015-11-15*/
#ifndef __TOU_H__
#define __TOU_H__
#include
#include
//#include //Keil library
sbit d=P1^5;
#define uint unsigned int
#define uchar unsigned char
enum {TEMP,HUMI};
sbit DATA = P1^1;
sbit SCK = P1^0;
sbit lcdrs=P0^0;
sbit lcdrw=P0^1;
sbit lcden=P0^2;
#define lcd_date P0
#define delay5us(){_nop_();_nop_();_nop_();_nop_();_nop_();}
/******** SHT10???? ********/
void s_connectionreset(void);
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
void calc_sth10(float *p_humidity ,float *p_temperature);
//float calc_dewpoint(float h,float t);
#endif
/***********************************************************************************************************************************************************/
//SHT10??(SHT10.c):
//#include
#define noACK 0 //??????,??????????
#define ACK 1 //??????;
//?? ?? ?/?
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0
//?????
char s_write_byte(unsigned char value)
{
unsigned char i,error=0;
for (i=0x80;i>0;i>>=1) //???1,????
{
if (i&value) DATA=1; //????????,???????
else DATA=0;
SCK=1;
_nop_();_nop_();_nop_(); //??3us
SCK=0;
}
DATA=1; //?????
SCK=1;
error=DATA; //??????,??????
_nop_();_nop_();_nop_();
SCK=0;
DATA=1;
return error; //error=1 ????
}
//?????
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
{
unsigned char i,val=0;
DATA=1; //?????
for(i=0x80;i>0;i>>=1) //???1,????
{
SCK=1;
if(DATA) val=(val|i); //????????
SCK=0;
}
DATA=!ack; //?????,????????;
SCK=1;
_nop_();_nop_();_nop_(); //??3us
SCK=0;
_nop_();_nop_();_nop_();
DATA=1; //?????
return val;
}
//????
void s_transstart(void)
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
DATA=1; SCK=0; //??
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();_nop_();_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}
//????
void s_connectionreset(void)
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
DATA=1; SCK=0; //??
for(i=0;i<9;i++) //DATA???,SCK????9?,??????,?????
{
SCK=1;
SCK=0;
}
s_transstart(); //????
}
//?????
char s_softreset(void)
// resets the sensor by a softreset
{
unsigned char error=0;
s_connectionreset(); //??????
error+=s_write_byte(RESET); //??????
return error; //error=1 ????
}
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
*p_value=s_read_byte(ACK); //read status register (8-bit)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}
//??????
char s_write_statusreg(unsigned char *p_value)
// writes the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error+=s_write_byte(STATUS_REG_W);//send command to sensor
error+=s_write_byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}
//?????
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
// ??????????,???mode??????;
{
// enum {TEMP,HUMI}; //?????????
unsigned error=0;
unsigned int i;
s_transstart(); //????
switch(mode) //??????
{
case TEMP : error+=s_write_byte(MEASURE_TEMP); break; //????
case HUMI : error+=s_write_byte(MEASURE_HUMI); break; //????
default : break;
}
for (i=0;i<65535;i++) if(DATA==0) break; //??????
if(DATA) error+=1; // ????????????,??????
*(p_value) =s_read_byte(ACK); //??????,??? (MSB)
*(p_value+1)=s_read_byte(ACK); //??????,??? (LSB)
*p_checksum =s_read_byte(noACK); //read CRC???
return error; // error=1 ????
}
//?????????????
void calc_sth10(float *p_humidity ,float *p_temperature)
{
const float C1=-4.0; // 12????? ????
const float C2=+0.0405; // 12????? ????
const float C3=-0.0000028; // 12????? ????
const float T1=+0.01; // 14????? 5V?? ????
const float T2=+0.00008; // 14????? 5V?? ????
float rh=*p_humidity; // rh: 12? ??
float t=*p_temperature; // t: 14? ??
float rh_lin; // rh_lin: ?? linear?
float rh_true; // rh_true: ?? ture?
float t_C; // t_C : ?? ?
t_C=t*0.01 - 40; //????
rh_lin=C3*rh*rh + C2*rh + C1; //?????????
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //?????????????
if(rh_true>100)rh_true=100; //??????
if(rh_true<0.1)rh_true=0.1; //??????
*p_temperature=t_C; //??????
*p_humidity=rh_true; //??????
}
//????????????
/*float calc_dewpoint(float h,float t)
{
float logEx,dew_point;
logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
} */
/***********************************************************************************************************************************************************/
//DS1602??(1602.c):
//#include
//??????**************************************************************
void delayms(uchar x)
{
uchar t;
while(x--)
for(t=0;t<120;t++);
}
//??????
void lcd_busy_wait()
{
uchar Hi,Lo;
do
{
lcd_date |=0xf0;//4?????????????
lcdrs=0;
lcdrw=1;//??????? ?
lcden=1;
delay5us();
Hi=lcd_date;
delay5us();
lcden=0;
delay5us();
lcden=1;
delay5us();
Lo=lcd_date;
delay5us();
lcden=0;
delay5us();
}
while(Hi&0x80);
}
//???
void lcd_1602_write_com(uchar com)
{
lcd_busy_wait();//?????
lcd_date=com&0xf0|0x04;//?????,?????rs,rw,en?????
delay5us();
lcden=0;
delay5us();
lcd_date=com<<4|0x04;//?????
delay5us();
lcden=0;
delay5us();
}
//???
void lcd_1602_write_dat(uchar dat)
{
lcd_busy_wait();//?????
lcd_date=dat&0xf0|0x05;//?????
delay5us();
lcden=0;
delay5us();
lcd_date=dat<<4|0x05;//?????
delay5us();
lcden=0;
delay5us();
}
//?????
void Lcd_1602_init()
{
lcd_date=0xff;//?????????????
lcd_date=0x24;//4???,?lcden,lcdrw,lcdrs=100(4),?????????
lcden=0;//?????????
lcd_1602_write_com(0x28); //4?,2?,5x7??
delayms(15);
lcd_1602_write_com(0x0c); //???
delayms(15);
lcd_1602_write_com(0x06); //?????,????
delayms(15);
lcd_1602_write_com(0x01);//??
delayms(15);
lcd_1602_write_com(0x02);//????????
delayms(15);
}
void lcd_string(uchar r,uchar c,char *str)
{
uchar i=0;
code uchar ddram[]={0x80,0xc0};//???????????
lcd_1602_write_com(ddram[r]|c);//????????
//?????
for(i=0;i<16&&str!= '\0';i++)
{
lcd_1602_write_dat(str);
}
}
/*
void GotoXY(unsigned char x, unsigned char y)
{
if(y==0)
lcd_1602_write_com(0,0x80|x);
if(y==1)
lcd_1602_write_com(0,0x80|(x-0x40));
}
//??????????
void Print(unsigned char *str)
{
while(*str!='\0')
{
lcd_1602_write_dat(1,*str);
str++;
}
}
*/
/***********************************************************************************************************************************************************/
//???(main.c):
//#include
typedef union //???????
{
unsigned int i;
float f;
} value;
//????
void delay(int z) //z????
{
int x,y;
for(x=z;x>0;x--)
for(y=125;y>0;y--);
}
void main()
{
unsigned int temp,humi;
value humi_val,temp_val; //???????,??????,??????
// float dew_point; //???????
unsigned char error; //??????????
unsigned char checksum; //CRC
uchar wendu[6]; //??????
uchar shidu[6]; //??????
Lcd_1602_init(); //?????
//GotoXY(0,0); //????????
//Print("TEMP: %C"); //5???
//GotoXY(0,1); //????????
//Print("HUMI: %RH"); //5???
lcd_string(0,0,"TEMP: %C");
delayms(50);
lcd_string(1,0,"HUMI: %RH");
s_connectionreset(); //??????
while(1)
{
error=0; //???error=0,?????
error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP); //????
error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI); //????
if(error!=0){ s_connectionreset();
//d=0;
}
else
{
d=0;
humi_val.f=(float)humi_val.i; //??????
temp_val.f=(float)temp_val.i; //??????
calc_sth10(&humi_val.f,&temp_val.f); //?????????
// dew_point=calc_dewpoint(humi_val.f,temp_val.f); //??e dew_point
temp=temp_val.f*10;
humi=humi_val.f*10;
//GotoXY(5,0); //????????
wendu[0]=temp/1000+'0'; //????
wendu[1]=temp%1000/100+'0'; //????
wendu[2]=temp%100/10+'0'; //????
wendu[3]=0x2E; //???
wendu[4]=temp%10+'0'; //?????????
//Print(wendu); //????
lcd_string(0,5,wendu);
// GotoXY(5,1);
shidu[0]=humi/1000+'0'; //????
shidu[1]=humi%1000/100+'0'; //????
shidu[2]=humi%100/10+'0'; //????
shidu[3]=0x2E; //???
shidu[4]=humi%10+'0'; //?????????
//Print(shidu); //????
lcd_string(1,5,shidu);
}
delay(800); //????????,????????
}
}
/*2015-11-15*/
#ifndef __TOU_H__
#define __TOU_H__
#include
#include
//#include //Keil library
sbit d=P1^5;
#define uint unsigned int
#define uchar unsigned char
enum {TEMP,HUMI};
sbit DATA = P1^1;
sbit SCK = P1^0;
sbit lcdrs=P0^0;
sbit lcdrw=P0^1;
sbit lcden=P0^2;
#define lcd_date P0
#define delay5us(){_nop_();_nop_();_nop_();_nop_();_nop_();}
/******** SHT10???? ********/
void s_connectionreset(void);
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
void calc_sth10(float *p_humidity ,float *p_temperature);
//float calc_dewpoint(float h,float t);
#endif
/***********************************************************************************************************************************************************/
//SHT10??(SHT10.c):
//#include
#define noACK 0 //??????,??????????
#define ACK 1 //??????;
//?? ?? ?/?
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0
//?????
char s_write_byte(unsigned char value)
{
unsigned char i,error=0;
for (i=0x80;i>0;i>>=1) //???1,????
{
if (i&value) DATA=1; //????????,???????
else DATA=0;
SCK=1;
_nop_();_nop_();_nop_(); //??3us
SCK=0;
}
DATA=1; //?????
SCK=1;
error=DATA; //??????,??????
_nop_();_nop_();_nop_();
SCK=0;
DATA=1;
return error; //error=1 ????
}
//?????
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
{
unsigned char i,val=0;
DATA=1; //?????
for(i=0x80;i>0;i>>=1) //???1,????
{
SCK=1;
if(DATA) val=(val|i); //????????
SCK=0;
}
DATA=!ack; //?????,????????;
SCK=1;
_nop_();_nop_();_nop_(); //??3us
SCK=0;
_nop_();_nop_();_nop_();
DATA=1; //?????
return val;
}
//????
void s_transstart(void)
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
DATA=1; SCK=0; //??
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();_nop_();_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}
//????
void s_connectionreset(void)
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
DATA=1; SCK=0; //??
for(i=0;i<9;i++) //DATA???,SCK????9?,??????,?????
{
SCK=1;
SCK=0;
}
s_transstart(); //????
}
//?????
char s_softreset(void)
// resets the sensor by a softreset
{
unsigned char error=0;
s_connectionreset(); //??????
error+=s_write_byte(RESET); //??????
return error; //error=1 ????
}
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
*p_value=s_read_byte(ACK); //read status register (8-bit)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}
//??????
char s_write_statusreg(unsigned char *p_value)
// writes the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error+=s_write_byte(STATUS_REG_W);//send command to sensor
error+=s_write_byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}
//?????
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
// ??????????,???mode??????;
{
// enum {TEMP,HUMI}; //?????????
unsigned error=0;
unsigned int i;
s_transstart(); //????
switch(mode) //??????
{
case TEMP : error+=s_write_byte(MEASURE_TEMP); break; //????
case HUMI : error+=s_write_byte(MEASURE_HUMI); break; //????
default : break;
}
for (i=0;i<65535;i++) if(DATA==0) break; //??????
if(DATA) error+=1; // ????????????,??????
*(p_value) =s_read_byte(ACK); //??????,??? (MSB)
*(p_value+1)=s_read_byte(ACK); //??????,??? (LSB)
*p_checksum =s_read_byte(noACK); //read CRC???
return error; // error=1 ????
}
//?????????????
void calc_sth10(float *p_humidity ,float *p_temperature)
{
const float C1=-4.0; // 12????? ????
const float C2=+0.0405; // 12????? ????
const float C3=-0.0000028; // 12????? ????
const float T1=+0.01; // 14????? 5V?? ????
const float T2=+0.00008; // 14????? 5V?? ????
float rh=*p_humidity; // rh: 12? ??
float t=*p_temperature; // t: 14? ??
float rh_lin; // rh_lin: ?? linear?
float rh_true; // rh_true: ?? ture?
float t_C; // t_C : ?? ?
t_C=t*0.01 - 40; //????
rh_lin=C3*rh*rh + C2*rh + C1; //?????????
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //?????????????
if(rh_true>100)rh_true=100; //??????
if(rh_true<0.1)rh_true=0.1; //??????
*p_temperature=t_C; //??????
*p_humidity=rh_true; //??????
}
//????????????
/*float calc_dewpoint(float h,float t)
{
float logEx,dew_point;
logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
} */
/***********************************************************************************************************************************************************/
//DS1602??(1602.c):
//#include
//??????**************************************************************
void delayms(uchar x)
{
uchar t;
while(x--)
for(t=0;t<120;t++);
}
//??????
void lcd_busy_wait()
{
uchar Hi,Lo;
do
{
lcd_date |=0xf0;//4?????????????
lcdrs=0;
lcdrw=1;//??????? ?
lcden=1;
delay5us();
Hi=lcd_date;
delay5us();
lcden=0;
delay5us();
lcden=1;
delay5us();
Lo=lcd_date;
delay5us();
lcden=0;
delay5us();
}
while(Hi&0x80);
}
//???
void lcd_1602_write_com(uchar com)
{
lcd_busy_wait();//?????
lcd_date=com&0xf0|0x04;//?????,?????rs,rw,en?????
delay5us();
lcden=0;
delay5us();
lcd_date=com<<4|0x04;//?????
delay5us();
lcden=0;
delay5us();
}
//???
void lcd_1602_write_dat(uchar dat)
{
lcd_busy_wait();//?????
lcd_date=dat&0xf0|0x05;//?????
delay5us();
lcden=0;
delay5us();
lcd_date=dat<<4|0x05;//?????
delay5us();
lcden=0;
delay5us();
}
//?????
void Lcd_1602_init()
{
lcd_date=0xff;//?????????????
lcd_date=0x24;//4???,?lcden,lcdrw,lcdrs=100(4),?????????
lcden=0;//?????????
lcd_1602_write_com(0x28); //4?,2?,5x7??
delayms(15);
lcd_1602_write_com(0x0c); //???
delayms(15);
lcd_1602_write_com(0x06); //?????,????
delayms(15);
lcd_1602_write_com(0x01);//??
delayms(15);
lcd_1602_write_com(0x02);//????????
delayms(15);
}
void lcd_string(uchar r,uchar c,char *str)
{
uchar i=0;
code uchar ddram[]={0x80,0xc0};//???????????
lcd_1602_write_com(ddram[r]|c);//????????
//?????
for(i=0;i<16&&str!= '\0';i++)
{
lcd_1602_write_dat(str);
}
}
/*
void GotoXY(unsigned char x, unsigned char y)
{
if(y==0)
lcd_1602_write_com(0,0x80|x);
if(y==1)
lcd_1602_write_com(0,0x80|(x-0x40));
}
//??????????
void Print(unsigned char *str)
{
while(*str!='\0')
{
lcd_1602_write_dat(1,*str);
str++;
}
}
*/
/***********************************************************************************************************************************************************/
//???(main.c):
//#include
typedef union //???????
{
unsigned int i;
float f;
} value;
//????
void delay(int z) //z????
{
int x,y;
for(x=z;x>0;x--)
for(y=125;y>0;y--);
}
void main()
{
unsigned int temp,humi;
value humi_val,temp_val; //???????,??????,??????
// float dew_point; //???????
unsigned char error; //??????????
unsigned char checksum; //CRC
uchar wendu[6]; //??????
uchar shidu[6]; //??????
Lcd_1602_init(); //?????
//GotoXY(0,0); //????????
//Print("TEMP: %C"); //5???
//GotoXY(0,1); //????????
//Print("HUMI: %RH"); //5???
lcd_string(0,0,"TEMP: %C");
delayms(50);
lcd_string(1,0,"HUMI: %RH");
s_connectionreset(); //??????
while(1)
{
error=0; //???error=0,?????
error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP); //????
error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI); //????
if(error!=0){ s_connectionreset();
//d=0;
}
else
{
d=0;
humi_val.f=(float)humi_val.i; //??????
temp_val.f=(float)temp_val.i; //??????
calc_sth10(&humi_val.f,&temp_val.f); //?????????
// dew_point=calc_dewpoint(humi_val.f,temp_val.f); //??e dew_point
temp=temp_val.f*10;
humi=humi_val.f*10;
//GotoXY(5,0); //????????
wendu[0]=temp/1000+'0'; //????
wendu[1]=temp%1000/100+'0'; //????
wendu[2]=temp%100/10+'0'; //????
wendu[3]=0x2E; //???
wendu[4]=temp%10+'0'; //?????????
//Print(wendu); //????
lcd_string(0,5,wendu);
// GotoXY(5,1);
shidu[0]=humi/1000+'0'; //????
shidu[1]=humi%1000/100+'0'; //????
shidu[2]=humi%100/10+'0'; //????
shidu[3]=0x2E; //???
shidu[4]=humi%10+'0'; //?????????
//Print(shidu); //????
lcd_string(1,5,shidu);
}
delay(800); //????????,????????
}
}
我用点亮1个灯检测了一下程序,发现只能执行到 if(error!=0){ s_connectionreset(); 这一句,就一直回到复位去了,不知道是电路的错,还是程序出错了