📄 sht11.c
字号:
#include<msp430x16x.h>
#define uint unsigned int
#define uchar unsigned char
uchar data[3];
float temp;
void shd_high() //数据线置高
{
P6OUT |=0x02;
}
void shd_low() //数据线置低
{
P6OUT &=~0x02;
}
void shc_high() //时钟线置高
{
P6OUT |=0x10;
}
void shc_low() //时钟线置低
{
P6OUT &=~0x10;
}
uchar send(uchar data) //发送8位的数据
{
uchar i,j=0x80,x,ack=1;
P6DIR=0x12;
for(i=8;i>0;i--)
{
if(data&j)
{
shd_high();
shc_high();
for(x=5;x>0;x--);
j=j>>1;
shc_low();
//for(x=5;x>0;x--);
}
else
{
shd_low();
//for(x=5;x>0;x--);
shc_high();
for(x=5;x>0;x--);
j=j>>1;
shc_low();
//for(x=5;x>0;x--);
}
}
shd_high();
for(x=5;x>0;x--);
P6DIR &=~0x02;
shc_high();
for(x=5;x>0;x--);
if(P6IN&0x02)
ack=0;
else
ack=1;
shc_low();
for(x=5;x>0;x--);
P6DIR |=0x02;
shd_high();
return ack;
}
uchar reci(uchar ack) //接受一个八位的数据
{
uchar i,j=0x80,x,data;
P6DIR &=~0x02;
for(i=8;i>0;i--)
{
shc_high();
for(x=5;x>0;x--);
if(P6IN&0x02)
{
data=data|j;
j=j>>1;
}
else
{
data=data&(~j);
j=j>>1;
}
shc_low();
}
P6DIR |=0x02;
if(ack)
shd_high();
else
shd_low();
shc_high();
for(x=5;x>0;x--);
shc_low();
return data;
}
void start() //发送SHT11 的 触发信号
{
uchar x;
shc_low();
shd_high();
shc_high();
for(x=5;x>0;x--);
shd_low();
for(x=5;x>0;x--);
shc_low();
for(x=5;x>0;x--);
shc_high();
for(x=5;x>0;x--);
shd_high();
for(x=5;x>0;x--);
shc_low();
}
void temper() //温度的测量
{
uchar ack;
ack=send(0x03);
P6DIR &=~0x02;
while(ack==0);
while(P6IN&0x02);
P6DIR |=0x02;
data[0]=reci(1);
data[1]=reci(1);
data[2]=reci(0);
}
main()
{
WDTCTL=WDTPW+WDTHOLD;
P6DIR=0x12;
start();
temper();
temp=data[0]*256+data[1];
temp=0.01*temp-40;
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -