/********************************************************************* * * Funzioni protocollo ONEWIRE * ********************************************************************/ #include "compiler.h" #include "onewire.h" #include /********************************************************************* * Funzione: unsigned char OWReset(void) * * Input: Nessuno * Output: 1 = Dispositivo presente * 0 = Dispositivo non presente * Descrizione: Reset ONWIRE * ********************************************************************/ unsigned char OWReset(void) { unsigned char pd; pd = 0; TRISBbits.TRISB5 = 0; PORTBbits.RB5 = 0; Delay10TCYx(240); TRISBbits.TRISB5 = 1; Delay10TCYx(40); if (PORTBbits.RB5 ==0) pd = 1; Delay10TCYx(200); return pd; } /********************************************************************* * Funzione: void OWTX(unsigned char DATO) * * Input: Byte da inviare * Output: Nessuno * Descrizione: Invia un byte al dispositivo ONWIRE * ********************************************************************/ void OWTX(unsigned char DATO) { unsigned char j; for (j=0;j<8;j++) { if ((DATO >> j) & 0x01) { TRISBbits.TRISB5 = 0; PORTBbits.RB5 = 0; Delay10TCYx(3); TRISBbits.TRISB5 = 1; Delay10TCYx(37); TRISBbits.TRISB5 = 1; Delay10TCYx(1); } else { TRISBbits.TRISB5 = 0; PORTBbits.RB5 = 0; Delay10TCYx(40); TRISBbits.TRISB5 = 1; Delay10TCYx(1); } } } /********************************************************************* * Funzione: unsigned char OWRX(void) * * Input: Nessuno * Output: Byte ricevuto * Descrizione: Riceve un byte dal dispositivo ONWIRE * ********************************************************************/ unsigned char OWRX(void) { unsigned char j,a,b; a=0; b=0; for (j=0;j<8;j++) { PORTBbits.RB5 = 0; TRISBbits.TRISB5 = 0; Delay10TCYx(2); TRISBbits.TRISB5 = 1; Delay10TCYx(2); a = PORTBbits.RB5; a <<= j; b |= a; Delay10TCYx(36); } return b; } /********************************************************************* * Funzione: unsigned char OWRX1(void) * * Input: Nessuno * Output: Bit ricevuto * Descrizione: Riceve un bit dal dispositivo ONWIRE * ********************************************************************/ unsigned char OWRX1(void) { unsigned char a; a=0; PORTBbits.RB5 = 0; TRISBbits.TRISB5 = 0; Delay10TCYx(2); TRISBbits.TRISB5 = 1; Delay10TCYx(2); a = PORTBbits.RB5; Delay10TCYx(36); return a; }