#include <reg51.h>
#include <intrins.h>
#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 Lcd_Data P0 //定义LCD数据端口
sbit RS = P2^ 5; //定义和LCM的连接端口
sbit RW = P2 ^ 6;
sbit E = P2 ^ 7;
sbit DATA = P3^7;
sbit SCK = P3^5;
sbit io_dat = P1^1 ; //定义DS1302数据端口
sbit rst = P1^2 ;
sbit sclk = P1^0 ;
sbit RE = P3^4 ; //继电器端口
sbit BELL = P2^4 ; //蜂鸣器端口
sbit k1 = P2^0 ; //按键端口
sbit k2 = P2^1 ;
sbit k3 = P2^2 ;
sbit k4 = P2^3 ;
enum {TEMP,HUMI};
unsigned char code str[] = " Sensors error !" ;
unsigned char code menum[] = "T: RH: %" ;
unsigned char code set_arm[] = " SET Alarm Range" ;
unsigned char code set_ftime[] = "SET Funtain Time" ;
unsigned char code temper_arm[] = "Temperature High" ;
unsigned char code humidy_arm[] = " Humidity lower " ;
unsigned char code fountain[] = " Fountain Time " ;
unsigned char p_value[2], p_checksum ;
unsigned int t_value ,rh_value;
unsigned long record_tp ,record_rhp ;
unsigned char I= 0x00 ,J=0x00;
unsigned int tem_true ;
unsigned long rh_true;
unsigned char arm_t = 28,arm_rh = 80 ;
unsigned char set_hour=0x14,set_min=0x57 ,keeptime=0x10,keeptime1;
unsigned char set_ty=0;
unsigned char time_data[3];
unsigned char bdata time ;
sbit timehf =time^7 ;
bit alarm_flag = 0 ;
void Delay(unsigned int ms)
{
unsigned char i ;
while(ms--)
for(i=0;i<125;i++);
}
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)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
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
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
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
}
//----------------------------------------------------------------------------------
char s_softreset(void)
//----------------------------------------------------------------------------------
// resets the sensor by a softreset
{
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
}
char s_measure(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;
}
void Write_Comm(unsigned char lcdcomm) //写指令函数
{
Lcd_Data = lcdcomm;
RS = 0;
RW = 0;
E = 0;
E = 1;
Delay(5) ;
}
/******************************************************************************/
void Write_Data(unsigned char lcddata)//写数据函数
{
Lcd_Data = lcddata;
RS = 1;
RW = 0;
E = 0;
E = 1;
Delay(5) ;
}
/******************************************************************************/
void Init_LCD(void)//初始化LCD
{
Delay(40); //稍微延时,等待LCM进入工作状态
Write_Comm(0x01);//清显示
// Write_Comm(0x02);//光标归位
Write_Comm(0x38);//8位2行5*8
Write_Comm(0x06);//文字不动,光标右移
Write_Comm(0x0c);//显示开/关,光标开闪烁开
// Write_Comm(0x18);//左移
}
void ds1302_write(unsigned char wcom , unsigned char wdat)
{
unsigned char dat , wbyte8 = 8 , i = 2;
sclk = 0;
_nop_();
rst = 1;
_nop_();
dat = wcom ;
while(i--)
{
while(wbyte8--)
{
sclk = 0;
_nop_();
io_dat = (bit)(dat & 0x01);
dat>>=1;
_nop_();
sclk = 1;
_nop_();
_nop_();
}
dat = wdat ;
}
rst = 0;
}
void ds1302_read(unsigned char add )
{
unsigned char rbyte8 = 8;
sclk = 0;
_nop_();
rst = 1;
_nop_();
while(rbyte8--)
{
sclk = 0;
_nop_();
io_dat = (bit)(add & 0x01);
add>>=1;
_nop_();
sclk = 1;
_nop_();
}
io_dat = 1 ;
_nop_();
rbyte8 = 8;
while(rbyte8--)
{
sclk = 1;
_nop_();
timehf = io_dat ;
time >>=1 ;
_nop_();
sclk = 0;
_nop_();
}
rst = 0;
}
void ds1302initial(void)
//1302初始化,请在这里修改当前时间
{
ds1302_write( 0x8e , 0x00 ); //1302控制,数据允许写入
ds1302_write( 0x90 , 0xa4 ); //对VCC1充电 ;
评论0