Una libreria in C per la gestione di dispositivi 1-wire con AVR

Una libreria per la gestione di dispositivi 1-wire con AVR. La libreria è scritta in C per WinAVR (AVR GCC).

Per la gestione dei dispositivi 1-wire ecco una libreria in C per l’inizializzazione, l’invio e la ricezione dei dati. La funzione di inizializzazione init_1W() non richiede parametri e predispone il dispositivo 1-wire alla trasmissione/ricezione dati. La trasmissione verso il dispositivo viene effettuata con la funzione send_1W(char) alla quale deve essere passato come parametro il carattere da inviare. La ricezione di un dato avviene con la funzione get_1W() che non richiede parametri e ritorna il carattere ricevuto. Le tre funzioni si appoggiano ad una funzione ausiliaria per la creazione del ritardo opportuno. I listati 1 e 2 riportano rispettivamente i file di definizione ed il codice delle funzioni 1-wire, mentre i listati 3 e 4 riportano la definizione e l’implementazione della funzione per la generazione del ritardo.

/*** 1wire.h
#ifndef _1wire_h_
#define _1wire_h_
typedef struct _bit_struct
{
unsigned char bit0: 1;
unsigned char bit1: 1;
unsigned char bit2: 1;
unsigned char bit3: 1;
unsigned char bit4: 1;
unsigned char bit5: 1;
unsigned char bit6: 1;
unsigned char bit7: 1;
}field_bit;

#define BIT(adr) (*((volatile field_bit*) (adr)))
#define _1wPORTx 0x32 //change 1wire port DDR register
#define _1wDDRx 0x31 //change 1wire port PORT register
#define _1wPINx 0x30 //change 1wire port PIN register
#define wPINx BIT(_1wPINx).bit3 //PIN read 1wire, change 1wire PIN
#define wDDRx BIT(_1wDDRx).bit3 //direction 1wire, change 1wire PIN
#define wPORTx BIT(_1wPORTx).bit3 //pull up on/off on 1wire, change 1wire PIN
unsigned char init_1w(void); //function initialize 1wire line
void send_1w(unsigned char); //function send data to 1wire chip
unsigned char get_1w(void); //function receive data from 1wire chip
#endif
Listato 1
/**** 1wire.c

#include <avr/io.h>
#include “1wire.h”
#include “delay.h” //you have to include
library delay.h

unsigned char init_1w(void)
{
wDDRx=1;
delay_us(500);
wDDRx=0;
delay_us(70);
if(wPINx==0)
{
delay_us(240);
return 1;
}
else
return 0;
}
void send_1w(unsigned char data)
{
unsigned char u;
unsigned char x;
for(u=0; u<8; u++)
{
x=0x01&data;
if (x==0)
{
wDDRx=1;
delay_us(70);
wDDRx=0;
delay_us(4);
}
else
{
wDDRx=1;
delay_us(5);
wDDRx=0;
delay_us(70);
}
data>>=1;
}
}
unsigned char get_1w(void)
{
unsigned char x=0x00;
unsigned char u;
for(u=0; u<8; u++)
{
x>>=1;
wDDRx=1;
delay_us(4);
wDDRx=0;
delay_us(4);
if(wPINx==1)
{
x=x+0x80;
}
delay_us(47);
}
return x;
}
Listato 2
/****delay.h
#ifndef _delay_h_
#define _delay_h_

#include <inttypes.h>
#include <avr/io.h>
#define F_OSC 6250000

/* delay function for microsec
4 cpu cycles per loop + 1 cycles(?) overhead
when a constant is passed. */
static inline void delayloop16(uint16_t count)
{
      asm volatile ( “cp %A0,__zero_reg__ \n\t” \
                     “cpc %B0,__zero_reg__ \n\t” \
                     “breq L_Exit_%= \n\t” \
                     “L_LOOP_%=: \n\t” \
                     “sbiw %0,1 \n\t” \
                     “brne L_LOOP_%= \n\t” \
                     “L_Exit_%=: \n\t” \
                     : “=w” (count)
                                 : “0” (count)
                  );
}
// delayloop16(x) eats 4 cycles per x
#define DELAY_US_CONV(us) ((uint16_t)(((((us)*1000L)/(1000000000/F_OSC))-1)/4))
#define delay_us(us) delayloop16(DELAY_US_CONV(us))

/* delay function for millisec
(6 cycles per x + 20(?) overhead) */
void delayloop32( uint32_t l); // not inline
#define DELAY_MS_CONV(ms) ( (uint32_t) (ms*(F_OSC/6000L)) )
#define delay_ms(ms) delayloop32(DELAY_MS_CONV(ms))

/* mth 9/04:
Remark uSeconds:
Main Oscillator Clock given by F_OSC (makefile) in Hz
one CPU-Cycle takes 1/F_OSC seconds => 1000000/F_OSC uSeconds
so: 1 uSecond takes F_OSC/1000000 CPU-Cyles. The following code
is inspired by the avr-libc delay_loop2 function.
This it not “that precise” since it takes at least 4 cycles
but should be o.k. with any parameter (even 0).
Call function with delayloop(DELAYUS(dt [in uSeconds])).
*/

#endif
Listato 3
/*****delay.c
#include <avr/io.h>
#include <inttypes.h>

#include “delay.h”

void delayloop32(uint32_t loops)
{
__asm__ volatile (“cp %A0,__zero_reg__
\n\t” \
   “cpc %B0,__zero_reg__ \n\t” \
   “cpc %C0,__zero_reg__ \n\t” \
   “cpc %D0,__zero_reg__ \n\t” \
   “breq L_Exit_%=       \n\t” \
   “L_LOOP_%=:           \n\t” \
   “subi %A0,1           \n\t” \
   “sbci %B0,0           \n\t” \
   “sbci %C0,0           \n\t” \
   “sbci %D0,0           \n\t” \
   “brne L_LOOP_%=           \n\t” \
   “L_Exit_%=:           \n\t” \
   : “=w” (loops) \
                     : “0” (loops)   \
   );                     \

     return;
}
Listato 4
Scarica subito una copia gratis

Una risposta

  1. Avatar photo Maurizio 10 Ottobre 2016

Scrivi un commento

Seguici anche sul tuo Social Network preferito!

Send this to a friend