打印

用单片机模拟的DS18B20,求批判!

[复制链接]
1359|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
用PIC16F721 模拟的DS18B20,貌似和外部通信时,外部得到的温度值不定。求批判!
#include <htc.h>
#include "IO.h"
#include "Temperature.h"
bit Rest_Flag = 0;
bit Read_Data = 0;
extern unsigned char BCountL;
/*************************************************
*            
*   16M晶振延时3.25US      
*            
**************************************************/
void delay_us(unsigned int time)
{
  do
   {
     time--;
   }while (time > 1);
}
/*************************************************
*            
*   DQ口涵数方向      
*            
**************************************************/
void DQ_Direction(unsigned char value)
{
if(value)   
  DQ_TRIS = DQ_Value;  // 输入
else
{
  DQ_TRIS &= ~DQ_Value; // 输出 0
  DQ &= ~DQ_Value;
}
}
/*************************************************
*            
*   主机复位时序的标志位      
*            
**************************************************/
void Rest_Master_Flag()
{
if( BCountL >= 8 )   // 16M晶振,64US溢出次,512US
{
  BCountL = 0;
  if( DQ == !DQ_Value)  // DQ_Value =1
   Rest_Flag = 1;
  else
  {
   Rest_Flag = 0;
  }
}
}
/*************************************************
*            
*   主机读时序的标志位      
*            
**************************************************/
void Read_Master_Flag()
{
delay_us(2);    // 约延时6个us
if( DQ == !DQ_Value)
{
  Read_Data = 1;   // 读到的是数据
}
else
{
  Read_Data = 0;
}
}
/*************************************************
*            
*  从机读到主机传来的数据     
*            
**************************************************/
unsigned char Read_Byte()
{
unsigned char temp;
unsigned char i;

for(i = 8;i > 0;i--)
{
  temp = temp >> 1;  
  DQ_Direction(0);  // 输出低电平
  delay_us(9);   // 约延时30US
  
  DQ_Direction(1);  // 输入
  delay_us(9);   // 延时30US
  if(DQ)
  {
   temp |= 0x80;
  }
   delay_us(9);   //延时30us
}
return temp;
}
/*************************************************
*            
*   从机发送复位函数      
*            
**************************************************/
void Slave_Rest()
{

DQ_Direction(0); // 输出低
delay_us(61);  // 延时200us
DQ = DQ_Value;  // 输出高
   
delay_us(6); // 等待20us
}
/*************************************************
*            
*   从机发送写函数      
*            
**************************************************/
void Slave_Write( unsigned char value)
{
unsigned char i;
unsigned char temp;
DQ_TRIS &= ~DQ_Value;   // 输出
for(i = 8;i > 0;i --)
{
  DQ &= ~DQ_Value;  // 低电平
  temp = value & 0x01;  // 取低位
  delay_us(12);   // 延时39US;
     if(temp)
  {
   DQ |= DQ_Value;  // 输出高
  }
  else DQ &= ~DQ_Value; // 输出低
  delay_us(12);   // 延时39us
  DQ |= DQ_Value;   // 输出高
  delay_us(2);   // 延迟6.25US
  value = value >> 1;
}
}
/*************************************************
*            
*   从机发送温度数据函数      
*            
**************************************************/
void Send_Temperature_Data()
{
unsigned char BTemp;
unsigned char BTemp_Data;

DQ_Direction(1);  // 输入
Rest_Master_Flag();
Read_Master_Flag();

if( ( Rest_Flag == 1) && ( Read_Data == 0) )
{
  Slave_Rest();
}
if( Read_Data == 1)
{
  BTemp = Read_Byte();
  if( BTemp == 0X44)
  {
   BTemp_Data = Number();
  }
  if( BTemp == 0XBE)
  {
   Slave_Write( BTemp_Data );
  }
}
}

unsigned short Number()
{
unsigned short Wtemp;
Wtemp = (unsigned short)(60 / 0.0625);  
return Wtemp;
}

相关帖子

沙发
f136025450|  楼主 | 2012-9-7 15:39 | 只看该作者

使用特权

评论回复
板凳
znmcu| | 2012-9-7 15:42 | 只看该作者
你想让我黑进你的电脑去看你的图啊

使用特权

评论回复
地板
f136025450|  楼主 | 2012-9-7 15:44 | 只看该作者
貌似传不了图::(,我用的是外部中断,下降沿触发

使用特权

评论回复
5
f136025450|  楼主 | 2012-9-7 15:46 | 只看该作者
3# znmcu
望您指导!谢谢!

使用特权

评论回复
6
f136025450|  楼主 | 2012-9-8 13:55 | 只看该作者
时序图

未命名1.jpg (769.55 KB )

未命名1.jpg

未命名2.jpg (1008.76 KB )

未命名2.jpg

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

34

帖子

1

粉丝