|
#include <reg51.h>
#include "intrins.h"
#define uchar unsigned char
#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
enum {TEMP,HUMI};
sbit DATA = P2^5;
sbit SCK = P2^4;
sbit RS = P2^0;
sbit RW = P2^1;
sbit E = P2^2;
sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.????
/******** DS1602???? ********/
void LCD_Initial();
void GotoXY(unsigned char x, unsigned char y);
void Print(unsigned char *str);
void LCD_Write(bit style, unsigned char input);
/******** 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);
/****************************************************************/
//?????
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<tou.h>
//??????**************************************************************
unsigned char LCD_Wait(void)
{
RS=0;
RW=1; _nop_();
E=1; _nop_();
E=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)
{
E=0;
RS=style;
RW=0; _nop_();
DBPort=input; _nop_();//????
E=1; _nop_();//????
E=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()
{
E=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!='\0')
{
LCD_Write(LCD_DATA,*str);
str++;
}
}
/***********************************************************************************************************************************************************/
//???(main.c):
//#include<tou.h>
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_Initial(); //?????
GotoXY(0,0); //????????
Print("TEMP: %C"); //5???
GotoXY(0,1); //????????
Print("HUMI: %RH"); //5???
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(); ////??????,????
else
{
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); //????
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); //????
}
delay(800); //????????,????????
}
***********************************************
INTRINS.H
Intrinsic functions for C51.
Copyright (c) 1988-2010 Keil Elektronik GmbH and ARM Germany GmbH
All rights reserved.
--------------------------------------------------------------------------*/
#ifndef __INTRINS_H__
#define __INTRINS_H__
#pragma SAVE
#if defined (__CX2__)
#pragma FUNCTIONS(STATIC)
/* intrinsic functions are reentrant, but need static attribute */
#endif
extern void _nop_ (void);
extern bit _testbit_ (bit);
extern unsigned char _cror_ (unsigned char, unsigned char);
extern unsigned int _iror_ (unsigned int, unsigned char);
extern unsigned long _lror_ (unsigned long, unsigned char);
extern unsigned char _crol_ (unsigned char, unsigned char);
extern unsigned int _irol_ (unsigned int, unsigned char);
extern unsigned long _lrol_ (unsigned long, unsigned char);
extern unsigned char _chkfloat_(float);
#if defined (__CX2__)
extern int abs (int);
extern void _illop_ (void);
#endif
#if !defined (__CX2__)
extern void _push_ (unsigned char _sfr);
extern void _pop_ (unsigned char _sfr);
#endif
#pragma RESTORE
#endif
***************************************************************************
compiling keshe8.c...
F:\Keil C51 & MDK 安裝視頻教程\C51\Inc\intrins.h(2): error C129: missing ';' before '.'
keshe8.c - 1 Error(s), 0 Warning(s).
|
|