OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 1375|回复: 1

STM32 SHT11 51转32 程序 出来的不对 串口1 求大神

[复制链接]

2

主题

6

帖子

0

精华

初级会员

Rank: 2

积分
60
金钱
60
注册时间
2016-6-12
在线时间
12 小时
发表于 2016-6-12 16:37:00 | 显示全部楼层 |阅读模式
1金钱
#include "sht10.h"
#include "delay.h"
#include "sys.h"
#define DATA_OUT PCout(6)
#define DATA_IN PCin(6)
#define SCLK_OUT PCout(7)
#define SCLK_IN PCin(7)
void INIT_SHT10GPIO(void)
{
GPIO_InitTypeDef  GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;   
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   //IO¿ÚËÙ¶ÈΪ50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure);     
}
void DATAModeInput(void)
{
  GPIOC->CRL &=0xf0ffffff;
GPIOC->CRL |=0x04000000;
GPIOC->ODR&=~(1<<7);
}
void DATAModeOutput(void)
{
  GPIOC->CRL &=0xf0ffffff;
GPIOC->CRL |=0x03000000;
}
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
   INIT_SHT10GPIO();
   DATA_OUT=1;SCLK_OUT=0;
   delay_us(1);
   SCLK_OUT=1;
   delay_us(1);
  DATA_OUT=0;
  delay_us(1);
  SCLK_OUT=0;
  delay_us(50);
  SCLK_OUT=1;
  delay_us(1);
   DATA_OUT=1;
   delay_us(1);
   SCLK_OUT=0;
}
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
  INIT_SHT10GPIO();
  DATA_OUT=1;
  SCLK_OUT=0;
  delay_us(1);
  for(i=0;i<9;i++)           //9 SCK cycles
  {
   SCLK_OUT=1;
   delay_us(1);
   SCLK_OUT=0;
   delay_us(1);
  }
  s_transstart();            //transmission start
}
//-------------------------------
//----------------------------------------------------------------------------------
// modul-var
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
  unsigned char i, error=0;  
  INIT_SHT10GPIO();
  for (i=0x80;i>0;i/=2)                         //shift bit for masking
  {  
    if (i & value)
  DATA_OUT=1;   
    else
  DATA_OUT=0;  
  delay_us(1);
  SCLK_OUT=1;                  
    delay_us(10);     
    SCLK_OUT=0;
  delay_us(1);
  }

  DATA_OUT=1;
delay_us(1);
  SCLK_OUT=1;  
delay_us(1);

DATAModeInput();
  error=DATA_IN;
DATAModeOutput();

SCLK_OUT=0;

  return error;                                //error=1 in case of no acknowledge //??:0??,1??
}
//----------------------------------------------------------------------------------
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;  
DATA_OUT=1;
DATAModeInput();
  for (i=0x80;i>0;i/=2)            
  {
    SCLK_OUT=1;                                      
    delay_us(1);
    if(DATA_IN)val=(val|i);   
    SCLK_OUT=0;
  delay_us(1);  
  }

DATAModeOutput();
if(ack)DATA_OUT=0;
else DATA_OUT=1;                 
  delay_us(1);                                                
  SCLK_OUT=1;                                
  delay_us(10);                                                
  SCLK_OUT=0;                     
  delay_us(1);         
  DATA_OUT=1;      
  return val;
}
//----------------------------------------------------------------------------------
char s_softreset(void)
//----------------------------------------------------------------------------------
// resets the sensor by a softreset
{
   unsigned char error=0;
   s_connectionreset(); //reset communication
   error+=s_write_byte(Reset); //send RESET-command to sensor
   return error; //error=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
   unsigned char error=0;
   s_transstart(); //transmission start
   error=s_write_byte(STATUS_REG_R); //send command to sensor
   *p_value=s_read_byte(ACK); //read status register (8-bit)
   *p_checksum=s_read_byte(noACK); //read checksum (8-bit)
   return error; //error=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------
char s_write_statusreg(unsigned char *p_value)
//----------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
{
   unsigned char error=0;
   s_transstart(); //transmission start
   error+=s_write_byte(STATUS_REG_W);//send command to sensor
   error+=s_write_byte(*p_value); //send value of status register
   return error; //error>=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------
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;
   }
  DATAModeInput();
  for(i=0;i<50000;i++)
  {
     delay_us(10);
     if(DATA_IN==0)break;
   }
   if(DATA_IN)error+=1;  
  DATAModeOutput();
   *(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
   return error;
}
//---------------------------------------
//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
{
   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 [&not;C
   t_C=t*0.01-40; //calc. Temperature from ticks to [&not;C
   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 [&not;C
   *p_humidity=rh_true; //return humidity[%RH]
}
//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
{
   float logEx,dew_point ;
   logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2) ;
   dew_point=(logEx-0.66077)*237.3/(0.66077+7.5-logEx);
   return dew_point;
}
//-------------------------------------------------------------
这是代码 延时用的原子哥的

#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "usart.h"
#include "sht10.h"
typedef union
{
unsigned int i;           //????????
  float f;

} value;
int main()
{
value humi_val,temp_val;
  float dew_point;
  unsigned char error,checksum;
uart_init(9600);
LED_Init();
INIT_SHT10GPIO();
delay_init();
printf("start!!!\n");
delay_ms(20);
s_connectionreset();
  while(1)  
  {
   error=0;
     error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
   SendString(USART1,"EEROR1:");SendData(USART1,error+0x30);
     error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
   SendString(USART1,"EEROR2:");SendData(USART1,error+0x30);
    SendString(USART1,"hello\n") ;
     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
        dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
   
      printf("Temp:%5.1f&iexcl;&aelig; Humi:%5.1f%% Dew point:%5.1f&iexcl;&aelig; \n",temp_val.f,humi_val.f,dew_point);
   
      }
   
   
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
      delay_ms(800); //(be sure that the compiler doesn’t eliminate this line!)
  }

  return (1);

}


4~AG}6Q9LZ[(MYW(@`}$$M6.png
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2016-6-15 22:25:31 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-17 11:17

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表