Wiem ze bylo juz mase postow na forum o ds18b20, przeczytalem chyba wszystkie i nawet zarzekalem sie ze nie napisze kolejnego, ale po kilku dniach walki rece mi opadaja. Chce zczytac temp. atmega8 z tego ukladu. Zczytuje ladnie ale tylko do 23,9 stopni jezeli bardziej ogrzeje czujnik wyswietlacz wskazuje mi temperature -7, -6 cos kolo tego. Oto moj kod, prosze pomozcie:cry:
#include <avr/io.h>
#include <stdlib.h>
#include <stdio.h>
#include <util/delay.h>
#include "D:\AVR\Project Proteus\header\lcd.h"
#include "D:\AVR\Project Proteus\header\twi.h"
#include "D:\AVR\Project Proteus\header\rs232.h"
unsigned long long int tmp2, p ;
//int cos;
unsigned int pomiar1, tmp, tmp1, o, cisn, srednia, srednia1,srednia2;
char bufor1[5],bufor2[5],bufor3[50], bufor4[5], cos, lcd_buf[50];
unsigned char temperature;
unsigned char temperatura=1;
short int x;
#define F_CPU 1000000UL
#define CYCLES_PER_US ((F_CPU+500000)/1000000) // cpu cykli na mikrosekunde
void LCD_xy(uint8_t x, uint8_t y)
{
switch(y)
{
case 1: y=0x40; break;
case 2: y=0x14; break;
}
write_command(0x80+y+x);
}
unsigned char ow_reset__(void) // reset lini one wire
{
unsigned char presence=1;
DDRC |= _BV(0); //DQ = 0; //pull DQ line
_delay_us(480);
DDRC &= ~_BV(0); //DQ = 1; // allow line to //powrót lini 1w do trybu wejścia
_delay_us(70);// wait for presence pulse(pochodzący od DS-a) //czekamy na ustab. lini
if(bit_is_clear(PINC,0)) presence=0; //odczytujemy co wystawił na linię DS
_delay_us(410);// wait for end
if(bit_is_clear(PINC,0)) presence=1; //odczytujemy co wystawił na linię DS
return(presence); // presence signal
} // 0=presence, 1 = no part
unsigned char test(void) // test na oecnosc DALLASA
{
if (ow_reset__()==1)
{
write_text("BRAK CZUJNIKA");
_delay_ms(200);
_delay_ms(200);
while(1)
{
asm ("NOP");
}
}
_delay_ms(200);
_delay_ms(200);
}
unsigned char read_bit(void)
{
unsigned char presence=0;
DDRC |= _BV(0); //DQ = 0; // pull DQ low to start timeslot
_delay_us(6);
DDRC &= ~_BV(0); //DQ = 1; // then return high
_delay_us(9); // delay 15us from start of timeslot
if(bit_is_clear(PINC,0)) presence=0;
if(bit_is_set(PINC,0)) presence=1;
_delay_us(55);
return(presence); // return value of DQ line
}
///---------------------------------------------------------
void write_bit(char bitval) //WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.
{
DDRC |= _BV(0); //DQ = 0; // pull DQ low to start timeslot
_delay_us(6);
if(bitval==1) DDRC &= ~_BV(0); //DQ =1; // return DQ high if write 1
_delay_us(64); // hold value for remainder of timeslot - delay 104us
DDRC &= ~_BV(0);//DQ = 1;
}
///---------------------------------------------------------
unsigned char read_byte(void) // READ_BYTE - reads a byte from the one-wire bus
{
unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{
if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
_delay_us(80); // ??shifts it left wait for rest of timeslot 120us
}
return(value);
}
///---------------------------------------------------------
void write_byte(char val) // WRITE_BYTE - writes a byte to the one-wire bus.
{
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_us(80);
}
void Read_Temperature(void)
{
char tmp[10];
char temp1, temp2;
unsigned short int t;
if (ow_reset__()==1)
test();
write_byte(0xCC); //Skip ROM
write_byte(0x44); // Start Conversion
_delay_ms(950);
ow_reset__();
write_byte(0xCC); // Skip ROM
write_byte(0xBE); // Read Scratch Pad
temp1=read_byte();
temp2=read_byte();
ow_reset__();
dtostrf((float)((temp2<<8)|temp1)*10/16/10,5,1,tmp);
LCD_xy(1,0);
write_text(tmp);
}
int main(void)
{
DDRB=0xFF;
PORTB =0xFF;
write_command(0x01);
DDRC=0x00;
PORTC=0x00;
write_byte(0xCC);
lcd_init();
USART_Init();
while(1)
{
Read_Temperature();
waitms(40);
write_command(0x01);
}
return (0);
}