Mam problem z prawidłowym ustawieniem protokołu 1-Wire dla czujnika DS18B20. Ustawiałem i zmieniałem już wiele razy ale dalej otrzymuje -1028,06 st C . Pracuje na ATmega8 z wew. kwarcem 1Mhz.
/* Szkielet prostego programu dla avr-gcc */
#define F_CPU 1000000 // 1MHz zegar procesora -> częstotliwość kwarcu
#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include "lcd_lib.h"
//makra
#define WE 0
#define PORT1_WIRE PINB
#define SET_1WIRE DDRB &=~_BV(WE)
#define CLEAR_1WIRE DDRB |=_BV(WE)
char buffer[8];
//resetujemy magistrale
unsigned char RESET_PULSE(void)
{
unsigned char PRESENCE;
CLEAR_1WIRE;
_delay_us(500);
SET_1WIRE;
_delay_us(30);
if( bit_is_clear(PORT1_WIRE , WE ) )
{
PRESENCE = 1;
}
else
{
PRESENCE = 0;
}
_delay_us(470);
if( bit_is_set( PORT1_WIRE , WE ) )
{
PRESENCE = 1;
}
else
{
PRESENCE = 0;
}
return PRESENCE;
}
void send(char value)
{
CLEAR_1WIRE;
_delay_us(5);
if( value == 1)
{
SET_1WIRE;
}
_delay_us(65);
SET_1WIRE;
}
unsigned char read(void)
{
unsigned char PRESENCE;
CLEAR_1WIRE;
_delay_us(1);
SET_1WIRE;
_delay_us(10);
if( bit_is_set(PORT1_WIRE , WE ) )
{
PRESENCE = 1;
}
else
{
PRESENCE = 0;
}
_delay_us(55);
return PRESENCE;
}
void send_byte( char wartosc )
{
unsigned char i;
unsigned char pom;
for( i=0; i < 8; i++ )
{
pom = wartosc>>i;
pom &= 0x01;
send(pom);
}
//_delay_us(100);
}
void send_temp( char wartosc )
{
unsigned char i;
unsigned char pom;
for( i=0; i < 8; i++ )
{
pom = wartosc>>i;
pom &= 0x01;
if( pom == 1)
LCDsendChar('1');
else
LCDsendChar('0');
}
}
unsigned read_byte(void)
{
unsigned char wartosc;
unsigned char i;
for( i=0; i<8;i++)
{
if( read() )
wartosc |= 0x01<<i;
}
return wartosc;
}
int main(void)
{
char temp1 = 0, temp2 = 0;
unsigned char sprawdz;
LCDinit();
LCDclr();
LCDsendChar('a');
LCDsendChar('b');
_delay_ms(200);
while(1)
{
sprawdz = RESET_PULSE();
if( sprawdz == 1 )
{
send_byte(0xCC);
send_byte(0x44);
_delay_ms(750);
sprawdz = RESET_PULSE();
if( sprawdz == 1 )
{
LCDGotoXY(10,1);
LCDsendChar('o');
LCDsendChar('k');
}
send_byte(0xCC);
send_byte(0xBE);
temp1 = read_byte();
temp2 = read_byte();
sprawdz = RESET_PULSE();
LCDGotoXY(0,0);
send_temp(temp2);
send_temp(temp1);
float temp = 0;
temp = (float)temp1+(temp2*256);
temp = temp/16;
dtostrf(temp,1,4,buffer);
LCDGotoXY(0,1);
LCDsendChar(buffer[0]);
LCDsendChar(buffer[1]);
LCDsendChar(buffer[2]);
LCDsendChar(buffer[3]);
LCDsendChar(buffer[4]);
LCDsendChar(buffer[5]);
LCDsendChar(buffer[6]);
LCDsendChar(buffer[7]);
_delay_ms(300);
}
else
{
LCDGotoXY(0,0);
LCDsendChar('l');
LCDsendChar('o');
LCDsendChar('l');
}
}
}