/*
**------------------------------------------------------------------------------------------------------
** System Function: Sht10 Driver------温湿度传感器SHT10驱动
** SCK: P4.5
** SDA: P4.6
**
********************************************************************************************************/
#include <msp430x14x.h>
#include <string.h>
/*宏定义,延时函数,参数为1时相应延时分别为1us和1ms*/
/*常量定义*/
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
#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 bitselect 0x01 //选择温度与湿度的低位读
#define noACK 0
#define ACK 1
#define HUMIDITY 2
#define TEMPERATURE 1
#define SCK BIT0
#define SDA BIT1
#define SCK_H P2OUT|=SCK
#define SCK_L P2OUT&=~SCK
#define SDA_H P2OUT|=SDA
#define SDA_L P2OUT&=~SDA
#define MAX485ENABLE P2DIR|=BIT4
#define MAX485SEND P2OUT|=BIT4
#define MAX485REV P2OUT&=~BINT4
typedef union
{
unsigned int i;
float f;
}value;
void delay_us( ulong n)
{
ulong i;
for (i=0;i<n;i++)
{
asm("nop");
asm("nop");
}
}
void delay_ms( ulong n)
{
ulong i;
for (i=0;i<n;i++)
{
delay_us(1000);
}
}
uint table_temp[3];
uint table_humi[3];
uint temten;
uint humi_true;
int RecBuf[20] = {0};
int Rindex = 0;
/**********************************************************************************************************
**Function Name: S_Transstart
**Description: 发送开始时序
**
** generates a transmission start
** _____ ________
** DATA: |_______|
** ___ ___
** SCK : ___| |___| |______
**Input Parameters: 无
**Output Parameters: 无
**********************************************************************************************************/
void S_Transstart()
{
P2DIR|=SDA;
SDA_H;SCK_L;
delay_us(1);
SCK_H;
delay_us(1);
SDA_L;
delay_us(1);
SCK_L;
delay_us(1);delay_us(1);delay_us(1);
SCK_H;
delay_us(1);
SDA_H;
delay_us(1);
SCK_L;
P2DIR&=~SDA;
}
/**********************************************************************************************************
**Function Name: S_WriteByte
**Description: 写时序
**Input Parameters: 无
**Output Parameters: 无
**********************************************************************************************************/
char S_WriteByte(unsigned char value)
{
unsigned char i,error=0;
P2DIR|=SDA;
for(i=0x80;i>0;i/=2) //shift bit for masking
{
if(i&value)
SDA_H; //masking value with i , write to SENSI-BUS
else
SDA_L;
SCK_H; //clk for SENSI-BUS
delay_us(1);delay_us(1);delay_us(1); //pulswith approx. 5 us
SCK_L;
}
SDA_H; //release DATA-line
P2DIR&=~SDA; //Change SDA to be input
SCK_H; //clk #9 for ack
error=P2IN; //check ack (DATA will be pulled down by SHT11)
error&=SDA;
P2DIR|=SDA;
SCK_L;
if(error)
return 1; //error=1 in case of no acknowledge
return 0;
}
/**********************************************************************************************************
**Function Name: S_ReadByte
**Description: 读时序
**Input Parameters: ack--->reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
**Output Parameters: 无
**********************************************************************************************************/
char S_ReadByte(unsigned char ack)
{
unsigned char i,val=0;
P2DIR|=SDA;
SDA_H; //release DATA-line
P2DIR&=~SDA;
for(i=0x80;i>0;i/=2) //shift bit for masking
{
SCK_H; //clk for SENSI-BUS
if(P2IN&SDA)
val=(val|i); //read bit
SCK_L;
}
P2DIR|=SDA;
if(ack) //in case of "ack==1" pull down DATA-Line
SDA_L;
else
SDA_H;
SCK_H; //clk #9 for ack
delay_us(1);delay_us(1);delay_us(1); //pulswith approx. 5 us
SCK_L;
SDA_H; //release DATA-line
P2DIR&=~SDA;
return val;
}
/**********************************************************************************************************
**Function Name: S_Connectionreset
**Description: 通讯复位时序
** communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
** _____________________________________________________ ________
** DATA: |_______|
** _ _ _ _ _ _ _ _ _ ___ ___
** SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
**Input Parameters: 无
**Output Parameters: 无
**********************************************************************************************************/
void S_Connectionreset()
{
unsigned char ClkCnt;
P2DIR|=SDA;
SDA_H;SCK_L; //Initial state
for(ClkCnt=0;ClkCnt<9;ClkCnt++) //9 SCK cycles
{
SCK_H;
SCK_L;
}
S_Transstart(); //transmission start
}
/*********************************************************************
软件复位时序resets the sensor by a softreset
********************************************************************/
char S_Softreset()
{
unsigned char error=0;
S_Connectionreset(); //reset communication
error+=S_WriteByte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}
/**********************************************************************************************************
**Function Name: S_WriteStatusReg
**Description: 写状态寄存器
**Input Parameters: *p_value
**Output Parameters: 无
**********************************************************************************************************/
char S_WriteStatusReg(unsigned char *p_value)
{
unsigned char error=0;
S_Transstart(); //transmission start
error+=S_WriteByte(STATUS_REG_W); //send command to sensor
error+=S_WriteByte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}
/**********************************************************************************************************
**Function Name: S_Mearsure
**Description: 开始测量 makes a measurement (humidity/temperature) with checksum
**Input Parameters: *p_value ,*p_checknum ,mode
**Output Parameters: 无
**********************************************************************************************************/
unsigned char S_Measure(unsigned char *p_