资源说明:msp430f5438 sht10程序
/*==================================================*/
/* SHT10温湿度传感器 */
/* P5.0(DATA) P5.1(SCLK) */
/*==================================================*/
#include"public.h"
#include
#define noACK 0
#define ACK 1
#define SHT10_SCLK_1 P5OUT|=BIT1
#define SHT10_SCLK_0 P5OUT&=~BIT1
#define SHT10_DATA_1 P5OUT|=BIT0
#define SHT10_DATA_0 P5OUT&=~BIT0
#define HUMI 0
#define TEMP 1
uchar array[4];
int Temperature=0,Humidity=0;
/*======================初始化SHT10===========================*/
void Init_SHT10(void)
{
P5SEL&=~(BIT1+BIT0);
P5DIR|=BIT1;
P5DIR&=~BIT0;
P5REN|=BIT0;
}
/*======================启动SHT10===========================*/
void Start_SHT10()
{
P5DIR|=BIT0;
SHT10_DATA_1;SHT10_SCLK_0;
_NOP();
SHT10_SCLK_1;
_NOP();
SHT10_DATA_0;
_NOP();
SHT10_SCLK_0;
_NOP();_NOP();_NOP();
SHT10_SCLK_1;
_NOP();
SHT10_DATA_1;
_NOP();
SHT10_SCLK_0;
P5DIR&=~BIT0;
P5REN|=BIT0;
}
/*======================SHT10写字节===========================*/
char SHT10_WriteByte(unsigned char value)
{
unsigned char i,error=0;
P5DIR|=BIT0;
for(i=0x80;i>0;i/=2) //循环8次
{
if(i&value)
SHT10_DATA_1;
else
SHT10_DATA_0;
SHT10_SCLK_1;
_NOP();_NOP();_NOP();
SHT10_SCLK_0;
}
SHT10_DATA_1; //释放数据线
P5DIR&=~BIT0; P5REN|=BIT0; //将数据线变为输入状态
SHT10_SCLK_1;
error=P5IN&BIT0; //检查确认数据线是否被下拉
P5DIR|=BIT0;
SHT10_SCLK_0;
return error;
/*if(error)
return 1; //error=1 in case of no acknowledge
else
return 0;*/
}
/*======================SHT10读字节===========================*/
char SHT10_ReadByte(unsigned char ack)
{
unsigned char i,val=0;
P5DIR|=BIT0;
SHT10_DATA_1; //释放数据线
P5DIR&=~BIT0; P5REN|=BIT0;
for(i=0x80;i>0;i/=2)
{
SHT10_SCLK_1;
if(P5IN&BIT0)
val=(val|i); //读字节
SHT10_SCLK_0;
}
P5DIR|=BIT0;
if(ack) //在"ACK==1"时下拉数据线
SHT10_DATA_0;
else
SHT10_DATA_1;
SHT10_SCLK_1;
_NOP();_NOP();_NOP();
SHT10_SCLK_0;
SHT10_DATA_1;
//P5DIR&=~BIT0;
return val;
}
void S_Connectionreset()
{
unsigned char ClkCnt;
P5DIR|=BIT0;
SHT10_DATA_1;SHT10_SCLK_1; //Initial state
for(ClkCnt=0;ClkCnt<9;ClkCnt++) //9 SCK cycles
{
SHT10_SCLK_1;
SHT10_SCLK_0;
}
Start_SHT10(); //transmission start
}
/*======================SHT10测量函数===========================*/
char SHT10_Measure(unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
unsigned int i;
Start_SHT10();
P5DIR &= ~BIT0; P5REN|=BIT0;
switch(mode)
{ //判断是温度还是湿度
case 1 : error+=SHT10_WriteByte(0X03); break;
case 0 : error+=SHT10_WriteByte(0X05); break;
default : break;
}
for (i=0;i<65535;i++) if((P5IN&BIT0)==0) break; //等待计算完毕
if(P5IN&BIT0) error+=1; //超时
if(mode)
{
array[3]=SHT10_ReadByte(1); //读高字节数据
array[2]=SHT10_ReadByte(1); //读低字节数据
}
else
{
array[1]=SHT10_ReadByte(1); //读高字节数据
array[0]=SHT10_ReadByte(1); //读低字节数据
}
*p_checksum =SHT10_ReadByte(0); //读校验
return error;
}
void Dispose_SHT10(void)
{
unsigned char error,checksum=0;
int SOh=0,SOt=0;
float h=0,t=0;
error=0;
error+=SHT10_Measure(&checksum,0);
error+=SHT10_Measure(&checksum,1);;
// if(error!=0) S_Connectionreset();
// else
// {
SOh=(0x0f&array[1])*256+array[0];
SOt=(0x3f&array[3])*256+array[2];
h=0.0367*SOh-2.0468-1.5955*pow(10,-6)*pow(SOh,2);
t=0.01*SOt-40.1;
h=(t-25)*(0.01+0.00008*(float)SOh)+h;
Humidity=(int)(h*100);
Temperature=(int)(t*100);
Display_Temperature();
// }
delay_ms(800);
}
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。