这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » STM32F051读取18B20 要么全1 要么全0 则么回事啊?

共1条 1/1 1 跳转至

STM32F051读取18B20 要么全1 要么全0 则么回事啊?

菜鸟
2014-07-14 10:43:00     打赏
#include "arm_comm.h"
#include "stm32f0xx.h"
#include "stm32f0xx_it.h"
#include "tim_init.h"

//#include "stm320Fxx_ksk.h"
//#include "drv_hd44780.h"
//include "accl_drv.h"
//#include <stdio.h>
//#include <string.h>

#define windstart                       50        //18B20     -55℃~~125℃
/*

#define DQ_PIN                             GPIO_Pin_9
#define DQ_GPIO_PORT                       GPIOC
#define DQ_GPIO_CLK                        RCC_AHBPeriph_GPIOC       */

#define DQ_PIN                             GPIO_Pin_15
#define DQ_GPIO_PORT                       GPIOB
#define DQ_GPIO_CLK                        RCC_AHBPeriph_GPIOB


int flag;
unsigned char temp_data[2];

void DQ_Init1(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;

  /* Enable the GPIO_LED Clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

  /* Configure the GPIO_LED pin */
  GPIO_InitStructure.GPIO_Pin=DQ_PIN;
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
  GPIO_Init(DQ_GPIO_PORT,&GPIO_InitStructure);
// DQ_GPIO_PORT->BSRR=DQ_PIN ;
}

void DQ_Init2(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;

  /* Enable the GPIO_LED Clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

  /* Configure the GPIO_LED pin */
  GPIO_InitStructure.GPIO_Pin=DQ_PIN;
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
  GPIO_Init(DQ_GPIO_PORT,&GPIO_InitStructure);
// DQ_GPIO_PORT->BSRR=DQ_PIN ;
}

void DQ_on()
{
  GPIO_SetBits(DQ_GPIO_PORT,DQ_PIN);
}



void DQ_off()
{
  GPIO_ResetBits(DQ_GPIO_PORT,DQ_PIN );
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
void delay(Int32U Delay)                //1us
{
  while(Delay--)
    for(volatile int i=4;i>0;i--){;}
}

unsigned char  Init_DS18B20()
{
unsigned char presence;
  DQ_on();                                       //I/O set as output
  delay(10);
 DQ_off();   //pull DQ line low
delay(330); // leave it low for 480 μ s
DQ_on();        // allow line to return high
 DQ_Init2();
delay(7);  // wait for presence
                                       //I/O set as input
presence=GPIO_ReadInputDataBit(DQ_GPIO_PORT,DQ_PIN);                // get presence signal

//delay(80); // wait for end of timeslot
//presence=GPIO_ReadInputDataBit(DQ_GPIO_PORT,DQ_PIN);
delay(165);
DQ_Init1();
DQ_on();
return(presence); // presence signal returned
}                            //     presence = 0,  no part = 1

void write_bit(char bitval)
{
 DQ_off();  //pull DQ line low
delay(1);
if(bitval==1)DQ_on(); // return DQ high if write 1

delay(42);// hold value for remainder of timeslot

DQ_on();         // allow line to return high
delay(1);  // wait for presence

}// Delay provides 16 μ s per loop, plus 24μ s     Therefore, delay(5) = 104 μ s

void write_byte(char val)
{
unsigned char i;
unsigned char temp;
for(i=0;i<8;i++)   // writes byte, one bit at a time
{
temp=val>>i;    // shifts val right  ‘ i’ spaces
temp&=0x01;   // copy that bit to temp
write_bit(temp);  // write bit in temp into
}
delay(1);
}

unsigned char read_bit(void)
{
unsigned char presence;                                         //I/O set as output
DQ_off();   //pull DQ line low
delay(1); //
DQ_on();         // allow line to return high
DQ_Init2();
delay(7);  // wait for presence
//presence=GPIO_ReadInputDataBit(DQ_GPIO_PORT,DQ_PIN);                                             //I/O set as input
//delay(15); // delay 60 μs from start of timeslot
presence=GPIO_ReadInputDataBit(DQ_GPIO_PORT,DQ_PIN);
delay(35);
DQ_Init1();
return(presence); // return value of DQ line
}

unsigned char read_byte(void)
{
unsigned char i;
unsigned char value=0;
DQ_on();  
for(i=0;i<8;i++)
{
if(read_bit())value|=0x01<<i;    
// reads byte in, one byte at a time and then
// shifts it left
delay(7); // wait for rest of timeslot
}
return(value);
}

void Read_Temperature(void)
{ // read
 // unsigned char  m;
 
  DQ_Init1();

  while( (Init_DS18B20()>=1));  // if DS18B20 is unnormal,wait  till  it is normal,than go to next step
 
    // flash=0;
 write_byte(0xCC); // snip order
 write_byte(0x44); // start conver
 
 Init_DS18B20();
 write_byte(0xCC); // snip order
 write_byte(0xBE); // write read command
 
 temp_data[0]=read_byte(); // temp_L8
 temp_data[1]=read_byte(); // temp_H8
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TIM_Configuration(void)       //TIM2配置    0.05s
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_DeInit( TIM2);                              //复位TIM2定时器
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);      
//TIM1 使用内部时钟
//TIM_InternalClockConfig(TIM1);
TIM_BaseInitStructure.TIM_Period = 50000; //TIM_Period(TIM1_ARR)=1000,计数器向上计数到1000后产生更新事件,计数值归零
TIM_BaseInitStructure.TIM_Prescaler = 47; //设置预分频器分频系数48,即APB2=48M, TIM1_CLK=48/48=1MHz
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_BaseInitStructure.TIM_RepetitionCounter = 0; //TIM_RepetitionCounter(TIM1_RCR)=0,每次向上溢出都产生更新事件
TIM_TimeBaseInit(TIM2, &TIM_BaseInitStructure);
//清中断,以免一启用中断后立即产生中断
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
//使能TIM1中断源
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
//TIM1总开关:开启
TIM_Cmd(TIM2, ENABLE);

}


void TIM2_IRQHandler(void)             //TIM2中断服务程序    20times     20*0.05=1s
{  
         int i=0;
  if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //再次确认是否是本中断来了
  {
          TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //TIM_ClearITPendingBit(清除TIMx 的中断待处理位),
//        TIM_ClearITPendingBit(TIM1, TIM_IT_COM);
          i++;
          if(i==20)
          {flag=0;               //flag
           i=0;
          }
   }
 
}

/*void SysTick_Config(void)             //void SysTick_Handler(void)     stm32f0xx_it.c
{
SysTick->VAL=0;
SysTick->LOAD=10;
SysTick->CTRL|=0x07;
}
*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
void main ()
{
 uint16_t temp1=0;
 uint16_t temp2=0;
  char flag=0;
 

 
//TIM_Configuration();            //clock
 // usart();                              //usart init

 
                                                          /*RCC_ClocksTypeDef RCC_Clocks;
                                                        RCC_GetClocksFreq(&RCC_Clocks);
                                                                                           */
 //tim_init(5*100/10);               //50%占空比PWM test

while(1)
{
 
 
 
//  while(!flag)
//{   
//GPIOB->BSRR = 0x8000;//delay(1);        //I/O  ON
//GPIOB->BRR = 0x8000;//delay(1);        //I/O   OFF
 
    // tim_init(10*100/10);               
 
    Read_Temperature();  // read temp\
     
     
 //    USART_SendData(USART1,temp_data[0] );   //uart  
 //    USART_SendData(USART1,temp_data[1] );
 
 //===============================================//
    if(temp_data[1]&&0x80)temp_data[1]=0;                                       //-55℃~~125℃  ,here  the  temp>0,temp_data[1] will be zero
   temp2=(temp_data[0]+temp_data[1]*256)/2;                                    //1/2℃LSB
    if(temp2>windstart)
       {
    //    flag=1;
        if(temp2!=temp1)             //pwm init and set
              {  
                 temp1=temp2;
                 if(temp1>35)tim_init(99);
                   
                  tim_init(temp1/2-30+85);
          //       tim_init((temp1-windstart)*100/125);                                       //cacculate and set PWM  此处设好 可有恒温效果
              }
        }
     if(temp2<(windstart-20))TIM_CtrlPWMOutputs(TIM1, ENABLE);                  //shutdown wind
//==================================================//
 // flag=1;
 //}        //while
    
delay(1000000);

 }      //for


 }       //main
    




关键词: STM32 18B20    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]