logo elektroda
logo elektroda
X
logo elektroda
REKLAMA
REKLAMA
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

ATxmega128a1 i sensor DS18B20 nie współpracują ze mną :-)

lukasz182cz 12 Sty 2011 15:10 1085 0
REKLAMA
  • #1 8995278
    lukasz182cz
    Poziom 1  
    Witam! Mam problem z poprawnym zaprogramowaniem kontrolera.

    Teoretycznie powinno wszystko działać, program kompiluje się bez problemu jednak nie wykonuje moich założeń czyli odczytu temperatury, jej konwersji oraz wyświetleniu(przez Terminal). Jeśli ktokolwiek spotkał się z podobnym problemem bądź wie jak ów rozwiązać to byłbym bardzo wdzięczny. Poniżej przedstawiam kod mojego programu.

    [b]plik ds18b20.h[/b]
    
    [color=green]#ifndef DS18B20_H
    #define DS18B20_H
    
    /* DS18B20 przyłączony do portu  PD5 AVRa  */
    
    #define SET_ONEWIRE_PORT     PORTD.OUT  =   0b000100000
    #define CLR_ONEWIRE_PORT     PORTD.OUT  =   0b000000000
    #define IS_SET_ONEWIRE_PIN   PORTD.OUT  ==  0b000100000
    #define SET_OUT_ONEWIRE_DDR  PORTD.DIR  =   0b000100000
    #define SET_IN_ONEWIRE_DDR   PORTD.DIR  =   0b000000000
    
    unsigned char ds18b20_ConvertT(void);
    int ds18b20_Read(unsigned char []);
    void OneWireStrong(char);
    unsigned char OneWireReset(void);
    void OneWireWriteByte(unsigned char);
    unsigned char OneWireReadByte(void);
    
    #endif[/color]
    
    [b]
    plik ds18b20.c[/b]
    
    #define F_CPU 2000000UL //2MHz
    #include <avr/io.h>
    #include <util/delay.h>
    #include "ds18b20.h"
    
    
    
    #define BSCALE_VALUE  0
    #define BSEL_VALUE   12
    
    #define TEST_CHARS  100
    
    #define LEDPORT PORTE
    
    #define USART USARTC0
    #define USART_PORT PORTC
    */
    /**********************************************************/
    
    unsigned char ds18b20_ConvertT(void)
    {
      if (!OneWireReset()) return 0;
    
      OneWireWriteByte(0xcc); // SKIP ROM
      OneWireWriteByte(0x44); // CONVERT T
    
      return -1;
    }
    
    /***********************************************************/
    
    int ds18b20_Read(unsigned char scratchpad[])
    {
      unsigned char i;    
    
      if (!OneWireReset()) return 0;
    
      OneWireWriteByte(0xcc); // SKIP ROM
      OneWireWriteByte(0xbe); // READ SCRATCHPAD
    
      for(i=0; i<10; i++) scratchpad[i] = OneWireReadByte();
     
      return 1;
    }
    
    /**********************************************************/
    
    void OneWireStrong(char s)
    {
      if (s)
      {
         SET_ONEWIRE_PORT; 
         SET_OUT_ONEWIRE_DDR; 
      }
      else
      {
         SET_IN_ONEWIRE_DDR; 
      }
    }
    
    /**********************************************************/
    
    unsigned char OneWireReset()
    {
      CLR_ONEWIRE_PORT; 
    
      if (!(IS_SET_ONEWIRE_PIN)) return 0;  
    
      SET_OUT_ONEWIRE_DDR; 
      _delay_us(500);
      SET_IN_ONEWIRE_DDR; 
      _delay_us(70);
    
      if(!(IS_SET_ONEWIRE_PIN))
      {
        _delay_us(500);
        return(1);
      }
    
      _delay_us(500);
    
    return(0);
    }
    
    /**********************************************************/
    
    void OneWireWriteByte(unsigned char byte)
    {
       unsigned char i;
    
       CLR_ONEWIRE_PORT; 
    
       for (i=0; i<8; i++)
       {
         SET_OUT_ONEWIRE_DDR; 
    
         if (byte & 0x01)
         {
           _delay_us(7);
           SET_IN_ONEWIRE_DDR; 
           _delay_us(70);
         }
         else
         {
            _delay_us(70);
            SET_IN_ONEWIRE_DDR; 
            _delay_us(7);
         }
    
         byte >>= 1;
       }
    }
    
    /***********************************************************/
    
    unsigned char OneWireReadByte(void)
    {
      unsigned char i, byte = 0;
    
      SET_IN_ONEWIRE_DDR; 
      
      for (i=0; i<8; i++)
      {
         SET_OUT_ONEWIRE_DDR; 
         _delay_us(7);
         SET_IN_ONEWIRE_DDR; 
         _delay_us(7);
         byte >>= 1;
         
         if(IS_SET_ONEWIRE_PIN) byte |= 0x80;
    
         _delay_us(70);
      }
    
      return byte;
    }
    
    
    [b]plik task.c[/b]
    
    include <avr/io.h>
    #define F_CPU 2000000UL
    #include <util/delay.h>
    #include <stdbool.h>
    #include <stdio.h>
    #include "ds18b20.h"
    
    
    #define BSCALE_VALUE  0
    #define BSEL_VALUE   12
    
    #define TEST_CHARS  100
    
    #define LEDPORT PORTE
    
    #define USART USARTC0
    #define USART_PORT PORTC
    
    /*  Example USART application.
     *
     *  This example configures USARTD0 with the parameters:
     *      - 8 bit character size
     *      - No parity
     *      - 1 stop bit
     *      - 9600 Baud
     *
     *  This function will send the values 0-100 and test if the received data is
     *  equal to the sent data. The code can be tested by connecting PD3 to PD2. If
     *  the variable 'TransferError' is false and LEDs are lit up at the end of
     *  the function, the bytes have been successfully sent and received by USARTD0.
     */
    
    void USART_send(unsigned char data, FILE* strema)
    {
    	while((USART.STATUS & USART_DREIF_bm) == 0) {}
    	USART.DATA = data;
    }
    
    int main(void)
    {
    	static FILE mystdout = FDEV_SETUP_STREAM(USART_send, NULL, _FDEV_SETUP_WRITE);
    	stdout = &mystdout;  	
    
    	LEDPORT.DIR = 0xFF;
    	LEDPORT.OUT = 0xFF;
    
    	USART_PORT.DIRSET   = PIN3_bm;   // Pin 3 (TXD0) as output.
    	USART_PORT.DIRCLR   = PIN2_bm;   // Pin 2 (RXD0) as input.
    
    	// USARTD0, 8 Data bits, No Parity, 1 Stop bit
    	USART.CTRLC = (uint8_t) USART_CHSIZE_8BIT_gc | USART_PMODE_DISABLED_gc | false;
    
       /* Target: Internal RC 2MHz (default) 
        * Example (9600bps)  :   - I/O clock = 2MHz
     	*                        - 9600bps can be acheived by 9600bps / 2^0
     	*                        - UBRR = 2MHz/(16*9600)-1 = 12.02
     	*                        - ScaleFactor = 0
    	*/
    	
    	USART.BAUDCTRLA = BSEL_VALUE;
    
    	// Enable both RX and TX
    	USART.CTRLB |= USART_RXEN_bm;
    	USART.CTRLB |= USART_TXEN_bm;
    
    	
    double temp;
    unsigned char ds18b20_pad[9];
    
    /*petla odpowaidajaca za zczytanie i wyswietlanie temperatury*/
    	while(1)
        {
    		if(ds18b20_ConvertT())
    		{
    			_delay_ms(750);
    			ds18b20_Read(ds18b20_pad);
    			temp = ((ds18b20_pad[1] << 8) + ds18b20_pad[0]) / 16.0 ;
    			printf("temp:%5.2fC\n",temp);
    
    			/*przykladowy tekst*/
    //			printf("tekst\n");
    			/*mrygajace diodki*/
    			LEDPORT.OUT = ds18b20_pad[0];
    		}
        }
    }
    
    Myślę że główny problem istnieje w funkcji 'ds18b20_ConvertT()' gdyż po wyłączeniu pętli(dokładniej wyłączenie if(ds18b20_ConvertT())):
    
    *petla odpowaidajaca za zczytanie i wyswietlanie temperatury*/
    	while(1)
        {
    		if(ds18b20_ConvertT())
    		{
    			_delay_ms(750);
    			ds18b20_Read(ds18b20_pad);
    			temp = ((ds18b20_pad[1] << 8) + ds18b20_pad[0]) / 16.0 ;
    			printf("temp:%5.2fC\n",temp);
    
    			/*przykladowy tekst*/
    //			printf("tekst\n");
    			/*mrygajace diodki*/
    			LEDPORT.OUT = ds18b20_pad[0];
    		}
        }
    }


    Program wysyła nam tekst sprawdzający ale w dalszym ciągy nie przekazuje nam temperatury. Zamiast wyniku temperatury otrzymujemu znak '?'

    Jeśli ktokolwiek ma jakiś pomysł prosze o odzew bo siedze nad tym juz dobry miesiąc, a efektów nie widać. Pozdrawiam


    inventco.eu - temat poprawiłem. Znaczniki CODE dodałem.
  • REKLAMA
REKLAMA