打印
[AVR单片机]

基于meg128温控程序 测不出?求解

[复制链接]
1183|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
笑飞侠LFB|  楼主 | 2013-7-10 14:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <avr/io.h>
#include<avr/interrupt.h>
#include <util/delay.h>
#include "LCD.h"
#define uchar unsigned char
#define uint unsigned int

#define DQ_IN DDRD&= ~(1 << PD3)
#define DQ_OUT DDRD|=(1 << PD3)
#define DQ_SET PORTD|=(1 << PD3)
#define DQ_CLR PORTD&=~(1 << PD3)
#define DQ_R PIND&(1 << PD3)



uchar table[3]={0,0,0};

uchar ds18b20_reset(void)
{
uchar i;
DQ_OUT;
DQ_CLR;
  _delay_us(500);
DQ_SET;  //release bus
_delay_us(100);
DQ_IN;
i = DQ_R;
  _delay_us(500);
DQ_SET;
return i;
}
/***************************************/
void ds18b20_write_byte(uchar value)
{
uchar i;
for(i=0; i<8; i++)
{
DQ_OUT;
DQ_CLR;
  _delay_us(10);
if(value&0x01)
{
  DQ_SET;
}
  else
           DQ_CLR;
  _delay_us(100);

value = value >> 1;
  DQ_SET;//总线释放
  _delay_us(2);
}
}

/******************************/
uchar ds18b20_read_byte(void)
{
uchar i;
uchar value;
for(i=0; i<8; i++)
{
  value = value>>1;
  DQ_OUT;
  DQ_CLR;
  _delay_us(10);
  DQ_SET;
  DQ_IN;//采样开始
  if(DQ_R)
  {
   value |= 0x80;
  }
  _delay_us(50);
}
return value;
}
/**************************************/
void data_pro(uint temp)
{
uint a;
table[2] = temp/100;
a = temp%100;
table[1] = a/10;
table[0] = a%10;
}


/****************************************/
void main(void)
{
            uchar k;
                uchar i,j;
                uint temp;

               
                system_init();                            //系统初始化,设
                _delay_ms(100);                            //延时100ms
                LCD_init();                               //液晶参数初始化
                LCD_clear();



while(1)
{
  ds18b20_reset();
  ds18b20_write_byte(0xcc);
  ds18b20_write_byte(0x44);
  _delay_us(2000);
  
  ds18b20_reset();
  ds18b20_write_byte(0xcc);
  ds18b20_write_byte(0xbe);
  i = ds18b20_read_byte();//lsb
  j = ds18b20_read_byte();//msb
  temp = j*256 + i;  //26.8
  temp = temp*0.625;//268
  data_pro(temp);
  for(k=0; k<20; k++)
  {
          LCD_write_com(0xc6);
    LCD_write_data(table[2]+0x30);
          LCD_write_data(table[1]+0x30);
        LCD_write_data(table[0]+0x30);

   }
}
}

相关帖子

沙发
zezy371| | 2013-7-15 10:57 | 只看该作者
貌似AVR单片机在转换输出和输入是要要两个时钟周期啊

使用特权

评论回复
板凳
zezy371| | 2013-7-15 10:59 | 只看该作者
相 DQ_IN;//采样开始
要写两次后再读入数据

使用特权

评论回复
地板
zezy371| | 2013-7-15 11:00 | 只看该作者
应当给AVR一个转换的时间

使用特权

评论回复
5
zezy371| | 2013-7-15 11:03 | 只看该作者
另外就是时序问题ds18b20对时序要求非常严格的,看一下你的延时精确吗?

使用特权

评论回复
6
aifang| | 2013-7-24 21:56 | 只看该作者
DS18B20的时序很重要的   
要是真正懂了程序应该不会出错

使用特权

评论回复
7
oldnewma| | 2013-7-30 09:39 | 只看该作者
同求呀。。

使用特权

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

本版积分规则

3

主题

7

帖子

0

粉丝