久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3289|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

誰有STM8S103F3 SHT10的C語言程序,編譯環(huán)境是IAR,且是通過寄存器操作來驅(qū)動SHT10

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:170270 發(fā)表于 2017-11-18 17:03 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
100黑幣
如題  誰有STM8S103F3P6 讀寫 SHT10的C語言程序,編譯環(huán)境是IAR,且是通過寄存器操作來驅(qū)動SHT10

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:170270 發(fā)表于 2017-11-21 17:18 | 只看該作者
#include "IOSTM8S103F3.h"
//#include <intrins.h> //Keil library (is used for _nop()_ operation)
#include <math.h> //Keil library
#include <stdio.h> //Keil library



typedef union
{ unsigned int i;
float f;
} value;
//----------------------------------------------------------------------------------
// modul-var
//----------------------------------------------------------------------------------
enum {TEMP,HUMI};

#define DS_CLK_H()        PB_ODR_ODR4=1
#define DS_CLK_L()        PB_ODR_ODR4=0
#define DS_DATA_H()       PB_ODR|= 0x20
#define DS_DATA_L()       PB_ODR&=~0x20
#define DS_DATA_STATE     PB_IDR_IDR5
#define SHT_DATA_IN       {PB_DDR&=~ 0x20;PB_CR1&=~ 0x20;PB_CR2|=0x00;}
#define SHT_DATA_OUT      {PB_DDR|=0x20;PB_CR1|=0x20;PB_CR2|=0x00;}//PB_CR1|=0x20

#define noACK 0
#define ACK 1
//adr command r/w
#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

/*******************************************
函數(shù)名稱:SHT_Init()
功    能:溫濕度傳感器IO初始化
參    數(shù):無
返回值  :無
********************************************/
void SHT_Init(void)
{
   // P3SEL &=~(BIT6 + BIT7);
   // P3OUT |= (BIT6 + BIT7);//SDA SCK輸出
   // SHT_DATA_HIGH;
   // SHT_SCK_HIGH;
   // SHTConnectionRst();//connection reset sequence
   PB_DDR|=0x10;
   PB_CR1|=0x10;
   PB_CR2|=0x00;
   SHT_DATA_OUT;
   DS_DATA_H();
   DS_CLK_H();
   //s_connectionreset();
}

void Delay(long nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}




//----------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{ if (i & value) DS_DATA_H(); //masking value with i , write to SENSI-BUS
else DS_DATA_L();
DS_CLK_H(); //clk for SENSI-BUS
Delay(2);Delay(2);Delay(2); //pulswith approx. 5 us
DS_CLK_L();
}
DS_DATA_H(); //release DATA-line
SHT_DATA_IN;
DS_CLK_H(); //clk #9 for ack
error=DS_DATA_STATE; //check ack (DATA will be pulled down by SHT11)
DS_CLK_L();
Delay(2);
SHT_DATA_OUT;
  return error; //error=1 in case of no acknowledge
}

//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
unsigned char i,val=0;
SHT_DATA_OUT;
DS_DATA_H(); //release DATA-line
SHT_DATA_IN;
for (i=0x80;i>0;i/=2) //shift bit for masking
{ DS_CLK_H(); //clk for SENSI-BUS
if (DS_DATA_STATE) val=(val | i); //read bit
DS_CLK_L();
}
SHT_DATA_OUT;
//DS_DATA_STATE=!ack; //in case of "ack==1" pull down DATA-Line
if(ack==1)
                                {
             
                                DS_DATA_L();
                                }
                                        else
                                        {
                                        DS_DATA_H();
                                 }  
//if(ack==1)  DS_DATA_L();
Delay(2);
DS_CLK_H(); //clk #9 for ack
Delay(2);Delay(2);Delay(2); //pulswith approx. 5 us
DS_CLK_L();
Delay(2);Delay(2);Delay(2);
//DS_DATA_H(); //release DATA-line
return val;
}

//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
DS_DATA_H();
DS_CLK_L(); //Initial state
Delay(2);
DS_CLK_H();
Delay(2);
DS_DATA_L();
Delay(2);
DS_CLK_L();
Delay(2);Delay(2);Delay(2);
DS_CLK_H();
Delay(2);
DS_DATA_H();
Delay(2);
DS_CLK_L();
}

//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
DS_DATA_H(); DS_CLK_L(); //Initial state
for(i=0;i<9;i++) //9 SCK cycles
{ DS_CLK_H();
DS_CLK_L();
}
s_transstart(); //transmission start
}

//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
unsigned int i;
s_transstart(); //transmission start
switch(mode){ //send command to sensor
case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
default : break;
}
SHT_DATA_IN;
for (i=0;i<65535;i++) if(DS_DATA_STATE==0) break; //wait until sensor has finished the measurement
if(DS_DATA_STATE) error+=1; // or timeout (~2 sec.) is reached
*(p_value) =s_read_byte(ACK); //read the first byte (MSB)
*(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
*p_checksum =s_read_byte(noACK); //read checksum
SHT_DATA_OUT;
return error;
}

//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [C] and humidity [%RH]
// input : humi [Ticks] (12 bit)
// temp [Ticks] (14 bit)
// output: humi [%RH]
// temp [C]
{ const float C1=-4.0; // for 12 Bit
const float C2= 0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=0.01; // for 14 Bit @ 5V
const float T2=0.00008; // for 14 Bit @ 5V
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature [C]

//t_C=t*0.01 – 40; //calc. Temperature from ticks to [C]
t_C=t*0.01;
t_C=t_C-40;
rh_lin=C3*rh*rh + C2*rh + C1; //calc. Humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. Temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature [C]
*p_humidity=rh_true; //return humidity[%RH]
}


//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [C]
// 4. calculate dew point [C]
// 5. print temperature, humidity, dew point
{
  value humi_val,temp_val;
unsigned char error,checksum;
unsigned int i;
SHT_Init();
s_connectionreset();


        while(1)
        {
          //s_transstart();
          //s_write_byte(0x03);
           error=0;
           error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
           error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
           if(error!=0) s_connectionreset(); //in case of an error: connection reset
           else
           { humi_val.f=(float)humi_val.i; //converts integer to float
             temp_val.f=(float)temp_val.i; //converts integer to float
             calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
             Delay(2);
           }
           printf("temp:%3.1f humi:%3.1f",temp_val.f,humi_val.f);
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
for (i=0;i<40000;i++); //(be sure that the compiler doesn’t eliminate this line!)
//-----------------------------------------------------------------------------------
        }
         



                               


}
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 在线精品一区 | 日韩视频在线一区二区 | 午夜精品一区 | 中文字幕1区2区 | 日韩中文字幕免费在线 | 91福利电影在线观看 | 欧美久久久久久久 | 欧美日韩高清在线一区 | 成人免费看片又大又黄 | 久久亚洲国产 | 亚洲视频观看 | 国产性网 | 日韩成人在线网站 | 操皮视频| 超碰日本 | 亚洲一级二级三级 | 亚洲综合无码一区二区 | 草草视频在线播放 | 国产一区| 人人草天天草 | 国产午夜亚洲精品不卡 | a级片在线观看 | 欧美成人a∨高清免费观看 老司机午夜性大片 | 欧美一区二区三区免费在线观看 | 一本色道精品久久一区二区三区 | 国产自产c区 | 国产高清视频一区 | 亚洲小视频在线播放 | 亚洲视频免费 | 日韩在线视频免费观看 | 久久国产精品无码网站 | 毛片.com | 久久乐国产精品 | 中国免费黄色片 | 日韩在线视频一区 | 在线一区二区观看 | 欧美日本一区 | 精品国产乱码久久久久久蜜退臀 | 巨大黑人极品videos精品 | 亚洲色视频 | 日日夜夜狠狠操 |