求問,我的程序哪里出了問題,哭哭
/**************頭文件********/
#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
/**************鎖存端聲明****/
//SHT10接口
sbit DATA=P2^5;
sbit SCK=P2^4;
//LCD1206接口
#define LCD_DB P0
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_En = P2^2;
sfr DBPort= 0x80;
/**************lcd1602設置******/
void LCD_init(void); //初始化函數
void LCD_write_command(uchar command); //寫指令
void LCD_write_data(uchar dat);
void LCD_disp_char(uchar x, uchar y, uchar dat); //顯示字符
void LCD_disp_str(uchar x,uchar y,uchar *str); //顯示字符串
void delay_n10us(uint n);
/*************初始化lcd********/
void LCD_init(void)
{
delay_n10us(10);
LCD_write_command(0x38);
delay_n10us(10);
LCD_write_command(0x0c);
delay_n10us(10);
LCD_write_command(0x06);
delay_n10us(10);
LCD_write_command(0x01);
delay_n10us(100);
}
//lcd寫指令
void LCD_write_command(uchar dat)
{
delay_n10us(10);
LCD_RS=0; //指令
LCD_RW=0; //寫入
LCD_En=1; //允許
LCD_DB=dat;
delay_n10us(10);
LCD_En=0;
delay_n10us(10);
}
//lcd寫數據函數
void LCD_write_data(uchar dat)
{
delay_n10us(10);
LCD_RS=1; //數據
LCD_RW=0; //寫入
LCD_En=1; //允許
LCD_DB=dat;
delay_n10us(10);
LCD_En=0;
delay_n10us(10);
}
//lcd顯示字符函數
void LCD_disp_char(uchar x,uchar y,uchar dat)
{
uchar address;
if(y==1)
address=0x80+x;
else
address=0xc0+x;
LCD_write_command(address);
LCD_write_data(dat);
}
//lcd顯示字符串函數
void LCD_disp_str(uchar x,uchar y,uchar *str)
{
uchar address;
if(y==1)
address=0x80+x;
else
address=0xc0+x;
LCD_write_command(address);
while(*str!='\0') //str 為所要顯示字符串對應的指針參數
{
LCD_write_data(*str);
str++;
}
}
//延時n個10us
void delay_n10us(uint n)
{
uint x;
for(x=n;x>0;x--)
{
_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}
}
/**************sht10設置******/
typedef union
{
unsigned int i;
float f;
}
value;
enum{TEMP,HUMI}; //TEMP=0,HUMI=1
#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
//定義函數
void s_transstart(void); //啟動傳輸函數
void s_connectionreset(void); //連接復位函數
char s_write_byte(unsigned char value);
char s_read_byte(unsigned char ack);
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode); // 測溫濕度
void calc_SHT10(float *p_humidity,float *p_temperature); //溫濕度補償
//啟動傳輸函數
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_connectionrese(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();
}
//sht10寫函數
char s_write_byte(unsigned char value)
{
unsigned char i,error=0;
for(i=0x80;i>0;i/=2)
{
if(i&value)DATA=1;
else DATA=0;
SCK=1;
_nop_();_nop_();_nop_();
SCK=0;
}
DATA=1;
SCK=1;
error=DATA;
_nop_();_nop_();_nop_();
SCK=0;
DATA=1;
return error;
}
//sht10讀程序
char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
DATA=1;
for(i=0x80;i>0;i/=2)
{SCK=1;
if(DATA)val=(val|i);
_nop_();_nop_();_nop_();
SCK=0;
}
if(ack==1)DATA=0;
else DATA=1;
_nop_();_nop_();_nop_();
SCK=1;
_nop_();_nop_();_nop_();
SCK=0;
_nop_();_nop_();_nop_();
DATA=1;
return val;
}
//測量溫濕度函數
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
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);
*(p_value+1)=s_read_byte(ACK);
*p_checksum=s_read_byte(noACK);
return error;
}
//溫度補償函數
void calc_sht10(float*p_humidity,float*p_temperature)
{
const float C1=-4.0;
const float C2=+0.0405;
const float C3=-0.0000028;
const float T1=+0.01;
const float T2=+0.00008;
float rh=*p_humidity;
float t=*p_temperature;
float rh_lin;
float rh_true;
float 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;
}
/********************主函數*****************/
void main(void)
{
value humi_val,temp_val;
unsigned char error,checksum;
unsigned int wendu,shidu;
LCD_init();
s_connectionreset();
LCD_disp_str(0,1,"TE");
LCD_disp_str(0,2,"RH");
//初始化溫度顯示區
LCD_disp_str(2,1,"TTT.TC");
//初始化濕度縣市區
LCD_disp_str(2,2,"RRR.R%");
delay_n10us(20000); //延時0.2s
while(1)
{
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);
if (error!=0)s_connectionreset();
else
{
humi_val.f=(float)humi_val.i;
temp_val.f=(float)temp_val.i;
calc_sht10(&humi_val.f,&temp_val.f);
wendu=10*temp_val.f;
LCD_disp_char(2,1,wendu/1000+'0'); //顯示溫度百位
LCD_disp_char(3,1,(wendu/1000)/100+'0'); //顯示溫度十位
LCD_disp_char(4,1,(wendu/100)/10+'0'); //顯示溫度個位
LCD_disp_char(6,1,(wendu/10)+'0'); //顯示溫度小數點后一位
shidu=10*humi_val.f;
LCD_disp_char(2,2,shidu/1000+'0');
LCD_disp_char(3,2,(shidu/1000)/100+'0');
LCD_disp_char(4,2,(shidu/100)/10+'0');
LCD_disp_char(6,2,(shidu/10)+'0');
}
delay_n10us(80000);
}
}
|