Strona 1 z 1

Biblioteka DS18B20 odbiera dziwne wartości

: wtorek 11 paź 2016, 16:02
autor: StaryAnoda
Hej

Starałem się napisać bibliotekę do odbioru jednego czujnik typu DS18B20. Niestety odbiera dziwne wartości czy może ktoś podpowiedzieć co robię źle ?

Kod: Zaznacz cały

/*
 * main.c
 *
 *  Created on: Oct 11, 2016
 *      Author: StaryAnoda
 */


#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <stdlib.h>

#include "LCD/lcd44780.h"


//---Ustawienia MAGISTRALI 1 WIRE-------------------
#define PIN_1WIRE (1<<PD0)
#define DDR_1WIRE DDRD
#define PORT_1WIRE PORTD


#define OUT_1WIRE_LOW (PORT_1WIRE &= ~PIN_1WIRE)
#define OUT_1WIRE_HIGH (PORT_1WIRE |= PIN_1WIRE)
#define DIR_1WIRE_IN (DDR_1WIRE &= ~PIN_1WIRE)
#define DIR_1WIRE_OUT (DDR_1WIRE |= PIN_1WIRE)
//--------------------------------------------------

#define LED_PIN (1<<PA7)
#define LED_OFF PORTA &= ~ LED_PIN
#define LED_ON PORTA |= LED_PIN
#define LED_TOG PORTA ^= LED_PIN

char cStringBuffer[8];
uint8_t uc1Wire_ResetPulse(void);
void v1Wire_SendBit(uint8_t cBit);
void v1Wire_SendByte (uint8_t ucByteValue);
uint8_t uc1Wire_ReadBit(void);
uint8_t uv1Wire_ReadByte(void);

uint8_t ucReset;
uint8_t cTemperatureH = 0, cTemperatureL = 0;
float fTemperature = 0;

char Bufor[15];
int main (void)
{

   DDRA |= LED_PIN;
   LED_ON;

   lcd_init();
   lcd_str_P(PSTR("TERMOMETR:"));

   
   lcd_cls();
   while(1)
   {
      ucReset = uc1Wire_ResetPulse();

      if(ucReset == 1)
      {
         v1Wire_SendByte(0xCC);
         v1Wire_SendByte(0x44);
         _delay_ms(750);
         ucReset = uc1Wire_ResetPulse();
         v1Wire_SendByte(0xCC);
         v1Wire_SendByte(0xBE);
         cTemperatureL = uv1Wire_ReadByte();
         cTemperatureH = uv1Wire_ReadByte();
         ucReset = uc1Wire_ResetPulse();

         fTemperature = (float)(cTemperatureL + (cTemperatureH<<8))/16;
         dtostrf(fTemperature,1,1,cStringBuffer);
         lcd_locate(0,0);
         lcd_str(cStringBuffer);

         _delay_ms(200);
      }
   }
}

uint8_t uc1Wire_ResetPulse(void)
{
   uint8_t ucPresenceImpulse;

   OUT_1WIRE_LOW;
   DIR_1WIRE_OUT;
   _delay_us(500);
   DIR_1WIRE_IN;
   _delay_us(45);
   if(!(PIND & PIN_1WIRE))
   {
      ucPresenceImpulse = 1;
   }
   else
   {
      ucPresenceImpulse = 0;
   }
   _delay_us(470);
   return ucPresenceImpulse;
}

void v1Wire_SendBit(uint8_t cBit)
{
   DIR_1WIRE_OUT;
   _delay_us(5);

   if(cBit == 1)
   {
      DIR_1WIRE_IN;
   }

   _delay_us(80);

   DIR_1WIRE_IN;
}

uint8_t uc1Wire_ReadBit(void)
{
   uint8_t ucBit;

   DIR_1WIRE_OUT;
   _delay_us(2);
   DIR_1WIRE_IN;
   _delay_us(15);

   if(PIND & PIN_1WIRE)
   {
      ucBit = 1;
   }
   else
   {
      ucBit = 0;
   }

   return(ucBit);
}

void v1Wire_SendByte (uint8_t ucByteValue)
{
   uint8_t ucCounter;
   uint8_t ucValueToSend;

   for(ucCounter = 0; ucCounter < 8; ucCounter ++)
   {
      ucValueToSend = ucByteValue >> ucCounter;
      ucValueToSend &= 0x01;
      v1Wire_SendBit(ucValueToSend);
   }
   _delay_us(100);
}

uint8_t uv1Wire_ReadByte(void)
{
   uint8_t ucCounter;
   uint8_t ucReadByte = 0;

   for(ucCounter = 0; ucCounter < 8; ucCounter ++)
   {
      if(uc1Wire_ReadBit())
      {
         ucReadByte |= 0x01 << ucCounter;
         _delay_us(15);
      }
   }
   return (ucReadByte);
}


Re: Biblioteka DS18B20 odbiera dziwne wartości

: sobota 05 lis 2016, 22:32
autor: Antystatyczny
Moim zdaniem możesz mieć problemy z poprawnym wysyłaniem bitów z powodu zbyt długiej inicjalizacji slotów czasowych. W dokumentacji piszą, że inicjalizacja slotu trwa 1us, a u Ciebie jest 5us w przypadku wysyłki bitu oraz 2us w przypadku odbioru bitów.

Re: Biblioteka DS18B20 odbiera dziwne wartości

: niedziela 06 lis 2016, 07:30
autor: StaryAnoda
Cześć !!!

Problem został rozwiązany, niestety nie jestem w stanie sobie przypomnieć co było źle.