|
这个是基于CC2530的sht11温湿度代码希望能帮助需要帮助的朋友
#define SDA_W() (P1DIR |=BV(3) )
#define SDA_R() (P1DIR &=~BV(3) )
#define st(x) do { x } while (__LINE__ == -1)
#define HAL_IO_SET(port, pin, val) HAL_IO_SET_PREP(port, pin, val)
#define HAL_IO_SET_PREP(port, pin, val) st( P##port##_##pin## = val; )
#define HAL_IO_GET(port, pin) HAL_IO_GET_PREP( port,pin)
#define HAL_IO_GET_PREP(port, pin) ( P##port##_##pin)
#define SHT11_SCK_0() HAL_IO_SET(1,4,0)
#define SHT11_SCK_1() HAL_IO_SET(1,4,1)
#define SHT11_DTA_0() HAL_IO_SET(1,3,0)
#define SHT11_DTA_1() HAL_IO_SET(1,3,1)
#define SHT11_DTA() HAL_IO_GET(1,3)
#define u8_t char
/* adr command r/w */
#define STATUS_REG_W 0x06 /* 000 0011 0 */
#define STATUS_REG_R 0x07 /* 000 0011 1 */
#define TEMP 0x03 /* 000 0001 1 */
#define HUMI 0x05 /* 000 0010 1 */
#define RESET 0x1e /* 000 1111 0 */
#define delay_4() {asm("nop");asm("nop");asm("nop");asm("nop");}
//void delay_400ns(void)
//{
//asm("nop");asm("nop");
//asm("nop");asm("nop");
//asm("nop");asm("nop");
//asm("nop");asm("nop");
//asm("nop");asm("nop");
//}
void delay() //brief delay n us
{
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
asm("nop");asm("nop");
}
void
sht11_init(void)
{
P0IEN&=~(BV(3)|BV(5));
P0SEL&=0X00;
P0DIR|=(BV(3)|BV(5));
}
static void
sstart(void)
{
SDA_W();
SHT11_DTA_1(); SHT11_SCK_0();//initial state
delay_4();
SHT11_SCK_1();
delay_4();
SHT11_DTA_0();
delay_4();
SHT11_SCK_0();
delay_4();
SHT11_SCK_1();
delay_4();
SHT11_DTA_1();
delay_4();
SHT11_SCK_0();
delay_4();
}
static void
sreset(void)
{
int i;
SDA_W();
SHT11_DTA_1();
SHT11_SCK_0();
for(i = 0; i < 9 ; i++) {
delay_4();
SHT11_SCK_1();
delay_4();
SHT11_SCK_0();
}
sstart(); /* Start transmission, why??? */
}
static int
swrite(unsigned value) //如果收到0表示正确
{
int i,error=0;
for(i=0x80;i>0;i/=2)
{
if(i&value)
SHT11_DTA_1();
else
SHT11_DTA_0();
//delay_400ns();
SHT11_SCK_0();
delay_4();
SHT11_SCK_1();
delay_4();
SHT11_SCK_0();
}
SHT11_DTA_1();
SDA_R();
delay_4();
SHT11_SCK_1();
delay_4();
//SDA_R();//reslease
if(SHT11_DTA()) //检查ACK
error=1;
delay_4();
SHT11_SCK_0();
return error;//error=1出错
}
static unsigned
sread(int ack)
{
int i;
unsigned char val=0;
SHT11_DTA_1();
SDA_R() ;
for(i=0x80;i>0;i/=2)
{
SHT11_SCK_1();
delay_4();
if(SHT11_DTA())
val=(val | i);
SHT11_SCK_0();
delay_4();
}
SDA_W();
if(ack)
SHT11_DTA_0();
SHT11_SCK_1();
delay_4();
SHT11_SCK_0();
SHT11_DTA_1();
return val;
}
static unsigned int convert_sht(unsigned cmd)
{
int i,ack=1;
unsigned valueM,valueL,checksum;
unsigned int com;
//float result;
//sht11_init();
do{
sreset();
switch(cmd)
{
case TEMP: ack=swrite(0x03);break;
case HUMI: ack=swrite(0x05);break;
default: break;
}
}while(ack==1);
SDA_R();
for(i=0;i<65535;i++)
{
if(SHT11_DTA()==0)
break;
}
valueM=sread(1);
valueL =sread(1);
checksum=sread(0);
/*#ifdef CRC_CHECK
{
unsigned crc;
crc = crc8_add(0x0, cmd);
crc = crc8_add(crc, valueM);
crc = crc8_add(crc, valueL);
if(crc != rev8bits(checksum)) {
goto fail;}
}
#endif
*/
com=(valueM<< 8) | valueL;
//com=(float)valueM*256+(float)valueL;
//switch(cmd)
//{
//case TEMP:result=com*0.01-40;break;
// case HUMI:result=convert_humi(com);break;
// }
return com;
fail:
sreset();
return -1;
}
unsigned int
sht11_temp(void)
{
return (-39.60 + 0.01 * convert_sht(TEMP));
}
unsigned int
sht11_humi(void)
{
unsigned rh;
rh=convert_sht(HUMI);
return (unsigned)(-4 + 0.0405*rh - 2.8e-6*(rh*rh));
}
unsigned int
sht11_humi_ture(void)
{
unsigned tc,rh;
tc=(-39.60 + 0.01 * convert_sht(TEMP));
rh=convert_sht(HUMI);
return (unsigned)((tc-25)*(0.01+0.00008*rh)+(-4 + 0.0405*rh - 2.8e-6*(rh*rh)));
}
|
|