先上图
1.jpg (2.18 MB, 下载次数: 0)
下载附件
1602显示的结果
2015-2-11 20:18 上传
焊接了sht10,DATA也接了10k的上拉电阻,但是程序下载进去以后1602上面只显示了RH2个字母,引脚连接也都检查了,都正常,但是不知道为什么出了这样的问题
程序在这里,大家帮忙看下
#include <REG52.h> //Microcontroller specific library, e.g. port definitions
#include <intrins.h> //Keil library (is used for _nop()_ operation)
#include <math.h> //Keil library
#include <stdio.h> //Keil library
typedef union
{ unsigned int i;
float f;
} value;
int tem,humi,x,y,z,m;
enum {TEMP,HUMI};
sbit DATA = P1^0;
sbit SCK = P1^1;
#define noACK 0
#define ACK 1
//adr command r/w
#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
#define uchar unsigned char
uchar jgh[10]={"000%c"};
void show_time(); //液晶显示程序
/**************************
1602液晶显示部分子程序
*****************************/
//Port Definitions**********************************************************
sbit LcdRs = P3^4;
sbit LcdRw = P3^5;
sbit LcdEn = P3^6;
sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.数据端口
//内部等待函数**************************************************************************
unsigned char LCD_Wait(void)
{
LcdRs=0;
LcdRw=1; _nop_();
LcdEn=1; _nop_();
LcdEn=0;
return DBPort;
}
//向LCD写入命令或数据************************************************************
#define LCD_COMMAND 0 // Command
#define LCD_DATA 1 // Data
#define LCD_CLEAR_SCREEN 0x01 // 清屏
#define LCD_HOMING 0x02 // 光标返回原点
void LCD_Write(bit style, unsigned char input)
{
LcdEn=0;
LcdRs=style;
LcdRw=0; _nop_();
DBPort=input; _nop_();//注意顺序
LcdEn=1; _nop_();//注意顺序
LcdEn=0; _nop_();
LCD_Wait();
}
//设置显示模式************************************************************
#define LCD_SHOW 0x04 //显示开
#define LCD_HIDE 0x00 //显示关
#define LCD_CURSOR 0x02 //显示光标
#define LCD_NO_CURSOR 0x00 //无光标
#define LCD_FLASH 0x01 //光标闪动
#define LCD_NO_FLASH 0x00 //光标不闪动
void LCD_SetDisplay(unsigned char DisplayMode)
{
LCD_Write(LCD_COMMAND, 0x08|DisplayMode);
}
//设置输入模式************************************************************
#define LCD_AC_UP 0x02
#define LCD_AC_DOWN 0x00 // default
#define LCD_MOVE 0x01 // 画面可平移
#define LCD_NO_MOVE 0x00 //default
void LCD_SetInput(unsigned char InputMode)
{
LCD_Write(LCD_COMMAND, 0x04|InputMode);
}
//初始化LCD************************************************************
void LCD_Initial()
{
LcdEn=0;
LCD_Write(LCD_COMMAND,0x38); //8位数据端口,2行显示,5*7点阵
LCD_Write(LCD_COMMAND,0x38);
LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); //开启显示, 无光标
LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); //AC递增, 画面不动
}
//液晶字符输入的位置************************
void GotoXY(unsigned char x, unsigned char y)
{
if(y==0)
LCD_Write(LCD_COMMAND,0x80|x);
if(y==1)
LCD_Write(LCD_COMMAND,0x80|(x-0x40));
}
//将字符输出到液晶显示
void Print(unsigned char *str)
{
while(*str!=' ')
{
LCD_Write(LCD_DATA,*str);
str++;
}
}
//----------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{ if (i & value) DATA=1; //masking value with i , write to SENSI-BUS
else DATA=0;
SCK=1; //clk for SENSI-BUS
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=0;
}
DATA=1; //release DATA-line
SCK=1; //clk #9 for ack
error=DATA; //check ack (DATA will be pulled down by SHT11)
SCK=0;
return error; //error=1 in case of no acknowledge
}
//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
unsigned char i,val=0;
DATA=1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{ SCK=1; //clk for SENSI-BUS
if (DATA) val=(val | i); //read bit
SCK=0;
}
DATA=!ack; //in case of "ack==1" pull down DATA-Line
SCK=1; //clk #9 for ack
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=0;
DATA=1; //release DATA-line
return val;
}
//----------------------------------------------------------------------------------
void s_transstart(void)
{
DATA=1; SCK=0; //Initial state
_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
{
unsigned char i;
DATA=1; SCK=0; //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{ SCK=1;
SCK=0;
}
s_transstart(); //transmission start
}
/**********************************
复位sht10
*********************************
char s_softreset(void)
{
unsigned char error=0;
s_connectionreset(); //reset communication
error+=s_write_byte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
} */
/**********************************
读取sht10(8-bit)
*********************************
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
{
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
}
*/
/**********************************
写sht10(8-bit)
*********************************
char s_write_statusreg(unsigned char *p_value)
{
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)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
unsigned int i;
s_transstart(); //transmission start
switch(mode){ //send command to sensor
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; //wait until sensor has finished the measurement
if(DATA) error+=1; // or timeout (~2 sec.) is reached
*(p_value) =s_read_byte(ACK); //read the first byte (MSB)
*(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
*p_checksum =s_read_byte(noACK); //read checksum
return error;
}
/**********************************
设置sht10波特率
11.059 MHz 9600 bps
*********************************
void init_uart()
{
SCON = 0x52;
TMOD = 0x20;
TCON = 0x69;
TH1 = 0xfd;
} */
/**********************************
数据计算处理
input : humi [Ticks] (12 bit)
temp [Ticks] (14 bit)
output: humi [%RH]
**********************************/
void calc_sth11(float *p_humidity ,float *p_temperature)
{ const float C1=-4.0; // for 12 Bit
const float C2=+0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=+0.01; // for 14 Bit @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [癈]
t_C=t*0.01 - 40; //calc. temperature from ticks to [癈]
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature [癈]
*p_humidity=rh_true; //return humidity[%RH]
}
//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [癈]
// output: dew point [癈]
{ 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;
}
/**********************************
主函数
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [癈]
// 4. calculate dew point [癈]
// 5. print temperature, humidity, dew point
**********************************/
void main()
{
value humi_val,temp_val;
float dew_point;
unsigned char error,checksum;
//init_uart();
LCD_Initial();
GotoXY(0,0);
Print("TEMP: . %C");
GotoXY(0,1);
Print("HUMI: RH");
s_connectionreset();
while(1)
{
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!=0) s_connectionreset(); //in case of an error: connection reset
else
{ humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
tem=temp_val.f*10;
humi=humi_val.f*10;
GotoXY(5,0);//液晶字符显示位置
x=tem/1000;
x+=0x30;
jgh[0]=x;
y=tem%1000/100;
y+=0x30;
jgh[1]=y;
z=tem%1000%100/10;
z+=0x30;
jgh[2]=z;
m=tem%10;
m+=0x30;
jgh[3]=0x2E;
jgh[4]=m;
Print(jgh);
GotoXY(5,1);//液晶字符显示位置
x=humi/1000;
x+=0x30;
jgh[0]=x;
y=humi%1000/100;
y+=0x30;
jgh[1]=y;
z=humi%1000%100/10;
z+=0x30;
jgh[2]=z;
m=humi%10;
m+=0x30;
jgh[3]=0x2E;
jgh[4]=m;
Print(jgh);
}
}
}
#include "reg51.h"
#include "common.h"
#include <intrins.h> //Keil library (is used for _nop()_ operation)
#include <math.h> //Keil library
#include <stdio.h> //Keil library
#include "sht10.h"
#include "uart.h"
/*
PORT DEFINE
*/
sbit SHT10_SCK = P2^0;
sbit SHT10_DAT = P2^1;
typedef union
{
unsigned int i;
float f;
}value;
enum {TEMP,HUMI};
#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
static xdata value humi_val;
static xdata value temp_val;
void s_transstart(void); //启动传输函数
void s_connectionreset(void); //连接复位函数
char s_write_byte(unsigned char value);//SHT10写函数
char s_read_byte(unsigned char ack); //SHT10读函数
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);//测量温湿度函数
void calc_sht10(float *p_humidity ,float *p_temperature);//温湿度补偿
float calc_dewpoint(float h,float t); //计算露点
char s_write_byte(unsigned char value)
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{ if (i & value) SHT10_DAT=1; //masking value with i , write to SENSI-BUS
else SHT10_DAT=0;
SHT10_SCK=1; //clk for SENSI-BUS
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SHT10_SCK=0;
}
SHT10_DAT=1; //release SHT10_DAT-line
SHT10_SCK=1; //clk #9 for ack
error=SHT10_DAT; //check ack (SHT10_DAT will be pulled down by SHT11)
SHT10_SCK=0;
return error; //error=1 in case of no acknowledge
}
char s_read_byte(unsigned char ack)
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
unsigned char i,val=0;
SHT10_DAT=1; //release SHT10_DAT-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{ SHT10_SCK=1; //clk for SENSI-BUS
if (SHT10_DAT) val=(val | i); //read bit
SHT10_SCK=0;
}
SHT10_DAT=!ack; //in case of "ack==1" pull down SHT10_DAT-Line
SHT10_SCK=1; //clk #9 for ack
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SHT10_SCK=0;
SHT10_DAT=1; //release SHT10_DAT-line
return val;
}
void s_transstart(void)
// generates a transmission start
// _____ ________
// SHT10_DAT: |_______|
// ___ ___
// SHT10_SCK : ___| |___| |______
{
SHT10_DAT=1; SHT10_SCK=0; //Initial state
_nop_();
SHT10_SCK=1;
_nop_();
SHT10_DAT=0;
_nop_();
SHT10_SCK=0;
_nop_();_nop_();_nop_();
SHT10_SCK=1;
_nop_();
SHT10_DAT=1;
_nop_();
SHT10_SCK=0;
}
void s_connectionreset( void )
// communication reset: SHT10_DAT-line=1 and at least 9 SHT10_SCK cycles followed by transstart
// _____________________________________________________ ________
// SHT10_DAT: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SHT10_SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
SHT10_DAT=1; SHT10_SCK=0; //Initial state
for(i=0;i<9;i++) //9 SHT10_SCK cycles
{ SHT10_SCK=1;
SHT10_SCK=0;
}
s_transstart(); //transmission start
}
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
unsigned int i;
s_transstart(); //transmission start
switch(mode){ //send command to sensor
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(SHT10_DAT==0) break; //wait until sensor has finished the measurement
if(SHT10_DAT) error+=1; // or timeout (~2 sec.) is reached
* (p_value)=s_read_byte(ACK); //read the first byte (MSB)
*(p_value+1) =s_read_byte(ACK); //read the second byte (LSB)
*p_checksum =s_read_byte(noACK); //read checksum
return error;
}
void calc_sth11(float *p_humidity ,float *p_temperature)
// calculates temperature [℃] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [℃]
{ const float C1=-4.0; // for 12 Bit
const float C2=+0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=+0.01; // for 14 Bit @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [℃]
t_C=t*0.01 - 40; //calc. temperature from ticks to [℃]
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature [℃]
*p_humidity=rh_true; //return humidity[%RH]
}
float calc_dewpoint(float h,float t)
// calculates dew point
// input: humidity [%RH], temperature [℃]
// output: dew point [℃]
{ 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;
}
void sht10_run( void )
{
float dew_point;
unsigned char error,checksum;
//char buffer[30];
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!=0) s_connectionreset(); //in case of an error: connection reset
else
{
humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
// sprintf(buffer, "T%5.1f H:%5.1f%% D%5.1f℃ ",temp_val.f,humi_val.f,dew_point);
// uart_sendstring(buffer);
}
}
uint8_t sht10_getdata( sht10_data_t* pdat)
{
pdat->temp = temp_val.f;
pdat->humi = humi_val.f;
}
//uint8_t sht10_getdata( sht10_data_t* pdat)
//{
// if ( temp_val.f > 0 )
// {
// pdat->temp_sign = 0;
// }
// else
// {
// pdat->temp_sign = 1;
// }
// pdat->temp_integer = (uint8_t)(temp_val.f);
// pdat->temp_decimal = (uint8_t)(temp_val.f*10)%10;
// pdat->humi_integer = (uint8_t)(humi_val.f);
// pdat->humi_decimal = (uint8_t)(humi_val.f*10)%10;
// return 1;
//}
非常感谢你的分享 我去看看
一周热门 更多>