📄 sht15.c
字号:
/*
sht15.c
*/
#include "delay.h"
#include "sht15.h"
#define ACK 1
#define NO_ACK 0
#define STATUS_REG_W (0x06)
#define STATUS_REG_R (0x07)
#define MEASURE_TEMP (0x03)
#define MEASURE_HUMI (0x05)
#define RESET (0x1e)
#define SHT_PORT (PORTC)
#define SHT_DDR (DDRC)
#define SHT_PIN (PINC)
#define DATA_PIN (0x20)
#define SCK_PIN (0x10)
/*
*/
void delay(void)
{
delay_nus(10);
}
void sht_init(void)
{
SHT_DDR |= DATA_PIN + SCK_PIN;
SHT_PORT |= DATA_PIN + SCK_PIN;
s_softreset();
}
unsigned char s_write_byte(unsigned char value)
{
unsigned char i,error=0;
SHT_DDR |= DATA_PIN;
for(i=0x80;i>0;i/=2)
{
delay();
if(i & value)
{
SHT_PORT |= DATA_PIN;
}
else
{
SHT_PORT &= ~DATA_PIN;
}
delay();
SHT_PORT |= SCK_PIN;
delay();
delay();
delay();
SHT_PORT &= ~SCK_PIN;
delay();
}
SHT_PORT |= DATA_PIN;
SHT_DDR &= ~DATA_PIN;
delay();
SHT_PORT |= SCK_PIN;
delay();
delay();
delay();
error=SHT_PIN;
error &= DATA_PIN;
SHT_PORT &= ~SCK_PIN;
delay();
return error;
}
unsigned char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
SHT_PORT |= DATA_PIN;
SHT_DDR &= ~DATA_PIN;
for(i=0x80;i>0;i/=2)
{
delay();
SHT_PORT |= SCK_PIN;
delay();
if(SHT_PIN & DATA_PIN)
{
val=(val | i);
}
SHT_PORT &= ~SCK_PIN;
}
delay();
if(ack==1)
{
SHT_DDR |= DATA_PIN;
SHT_PORT &= ~DATA_PIN;
delay();
SHT_PORT |= SCK_PIN;
delay();
SHT_PORT &= ~SCK_PIN;
}
else
{
delay();
SHT_PORT |= SCK_PIN;
delay();
SHT_PORT &= ~SCK_PIN;
}
return val;
}
/*
start信号
DATA_PIN ----______-------
SCK_PIN ___---___---______
*/
void s_transstart(void)
{
SHT_DDR |= DATA_PIN;
SHT_PORT |= DATA_PIN;
SHT_PORT &= ~SCK_PIN;
delay();
SHT_PORT |= SCK_PIN;
delay();
SHT_PORT &= ~DATA_PIN;
delay();
SHT_PORT &= ~SCK_PIN;
delay();
SHT_PORT |= SCK_PIN;
delay();
SHT_PORT |= DATA_PIN;
delay();
SHT_PORT &= ~SCK_PIN;
delay();
}
void s_connectionreset(void)
{
unsigned char i;
SHT_DDR |= DATA_PIN|SCK_PIN;
SHT_PORT |= DATA_PIN;
SHT_PORT &= ~SCK_PIN;
for(i=0;i<9;i++)
{
delay();
SHT_PORT |= SCK_PIN;
delay();
SHT_PORT &= ~SCK_PIN;
}
s_transstart();
}
unsigned char s_softreset(void)
{
unsigned char error=0;
s_connectionreset();
error+=s_write_byte(RESET);
delay_nms(15);
return error;
}
unsigned char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
{
unsigned char error=0;
s_transstart();
error=s_write_byte(STATUS_REG_R);
*p_value=s_read_byte(ACK);
*p_checksum=s_read_byte(NO_ACK);
return error;
}
unsigned char s_write_statusreg(unsigned char *p_value)
{
unsigned char error=0;
s_transstart();
error+=s_write_byte(STATUS_REG_W);
error+=s_write_byte(*p_value);
return error;
}
unsigned char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned char error=0;
unsigned int measure_i;
s_transstart();
switch(mode)
{
case 0 :
{
error+=s_write_byte(MEASURE_TEMP);
break;
}
case 1 :
{
error+=s_write_byte(MEASURE_HUMI);
break;
}
default : break;
}
SHT_PORT |= DATA_PIN;
SHT_DDR &= ~DATA_PIN;
for(measure_i=0;measure_i<65525;measure_i++)
{
if((SHT_PIN & DATA_PIN) == 0) break;
}
if((SHT_PIN & DATA_PIN))
{
error+=1;
}
*(p_value) =s_read_byte(ACK);
*(p_value+1) =s_read_byte(ACK);
*p_checksum =s_read_byte(NO_ACK);
return error;
}
void calc_sht(unsigned char *p_sht_data,unsigned char *p_calc_data)
{
unsigned int temp,humi;
unsigned char calc_i;
temp=(((unsigned int)*p_sht_data)<<8)+(*(p_sht_data+1));
humi=(((unsigned int)*(p_sht_data+2))<<8) + (*(p_sht_data+3));
humi *= 3;
for(calc_i=0;calc_i<5;calc_i++)
{
*(p_calc_data+4-calc_i)=((temp%10)+48);
temp=temp/10;
}
for(calc_i=0;calc_i<5;calc_i++)
{
*(p_calc_data+9-calc_i)=((humi%10)+48);
humi=humi/10;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -