This file is part of OneWire routines
This is file "OneWireHAL-PIC16F-Turbo.c"/*============================================================================*/ /* Copyright (c) 2016-2019, Isaac Marino Bavaresco All rights reserved. isaacbavaresco@yahoo.com.br Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /*============================================================================*/ #include <string.h> #include <xc.h> #include "OneWire.h" #include "OneWireHAL-PIC16F.h" #include "Delay.h" /*============================================================================*/ #define CONVERT_us_TO_TCY(t) ((unsigned char)((t)*2.5)) /*============================================================================*/ #if defined USE_INLINE static inline void SetTimer( unsigned char t ) { T2CONbits.TMR2ON= 0; PR2 = CONVERT_us_TO_TCY( t ); TMR2 = 0; PIR1bits.TMR2IF = 0; } #else /* defined USE_INLINE */ #define SetTimer(t) do{T2CONbits.TMR2ON= 0;PR2=CONVERT_us_TO_TCY( t );TMR2=0;PIR1bits.TMR2IF=0;}while(0) #endif /* defined USE_INLINE */ /*============================================================================*/ #if defined USE_INLINE static inline void StartTimer( void ) { T2CONbits.TMR2ON= 1; } #else /* defined USE_INLINE */ #define StartTimer() do{T2CONbits.TMR2ON= 1;}while(0) #endif /* defined USE_INLINE */ /*============================================================================*/ #if defined USE_INLINE static inline int TimerExpired( void ) { return PIR1bits.TMR2IF; } #else /* defined USE_INLINE */ #define TimerExpired() (PIR1bits.TMR2IF) #endif /* defined USE_INLINE */ /*============================================================================*/ #if defined USE_INLINE static inline void EnterCritical( void ) { INTCONbits.GIE = 0; } #else /* defined USE_INLINE */ #define EnterCritical() do{INTCONbits.GIE=0;}while(0) #endif /* defined USE_INLINE */ /*============================================================================*/ #if defined USE_INLINE static inline void ExitCritical( void ) { INTCONbits.GIE = 1; } #else /* defined USE_INLINE */ #define ExitCritical() do{INTCONbits.GIE=1;}while(0) #endif /* defined USE_INLINE */ /*============================================================================*/ /*============================================================================*/ #if defined USE_INLINE static inline void OneWirePullDownOn( void ) { PORTAbits.RA0 = 1; }; #else /* defined USE_INLINE */ #define OneWirePullDownOn() do{PORTAbits.RA0=1;}while(0) #endif /* defined USE_INLINE */ /*============================================================================*/ #if defined USE_INLINE static inline void OneWirePullDownOff( void ) { PORTAbits.RA0 = 0; } #else /* defined USE_INLINE */ #define OneWirePullDownOff() do{PORTAbits.RA0=0;}while(0) #endif /* defined USE_INLINE */ /*============================================================================*/ #if defined USE_INLINE static inline void OneWirePullUpOn( void ) { PORTAbits.RA3 = 0; }; #else /* defined USE_INLINE */ #define OneWirePullUpOn() do{PORTAbits.RA3=0;}while(0) //#define OneWirePullUpOn() #endif /* defined USE_INLINE */ /*============================================================================*/ #if defined USE_INLINE static inline void OneWirePullUpOff( void ) { PORTAbits.RA3 = 1; } #else /* defined USE_INLINE */ #define OneWirePullUpOff() do{PORTAbits.RA3=1;}while(0) //#define OneWirePullUpOff() #endif /* defined USE_INLINE */ /*============================================================================*/ #if defined USE_INLINE #if defined USE_COMPARATOR static inline unsigned bit OneWireQueryBus( void ) { return CMCONbits.C2OUT; } #else /* defined USE_COMPARATOR */ static inline unsigned bit OneWireQueryBus( void ) { return PORTAbits.RA1; } #endif /* defined USE_COMPARATOR */ #else /* defined USE_INLINE */ #if defined USE_COMPARATOR #define OneWireQueryBus() (CMCONbits.C2OUT) #else /* defined USE_COMPARATOR */ #define OneWireQueryBus() (PORTAbits.RA1) #endif /* defined USE_COMPARATOR */ #endif /* defined USE_INLINE */ /*============================================================================*/ /*============================================================================*/ /*============================================================================*/ onewirestatus_t OneWireReset( void ) { onewireint_t Result; if( !OneWireQueryBus() ) return ONE_WIRE_STATUS_BUS_CONTENTION; SetTimer( 60 ); OneWirePullDownOn(); /* A */ Delay_us( 240 ); Delay_us( 240 ); EnterCritical(); OneWirePullDownOff(); /* B */ StartTimer(); while( OneWireQueryBus() == 0 && !TimerExpired() ) {} if( OneWireQueryBus() == 0 ) return ONE_WIRE_STATUS_BUS_CONTENTION; OneWirePullUpOn(); /* E */ Delay_us( 8 ); OneWirePullUpOff(); /* F */ Delay_us( 64 ); Result = (onewireint_t)OneWireQueryBus(); /* H */ ExitCritical(); Delay_us( 240 ); OneWirePullUpOn(); /* J/K */ Delay_us( 60 ); OneWirePullUpOff(); /* L */ Delay_us( 10 ); /* M */ if( !OneWireQueryBus() ) return ONE_WIRE_STATUS_BUS_CONTENTION; return Result == 0 ? ONE_WIRE_STATUS_OK : ONE_WIRE_STATUS_NO_RESPONSE; } /*============================================================================*/ onewirestatus_t OneWireWriteZero( void ) { if( !OneWireQueryBus() ) return ONE_WIRE_STATUS_BUS_CONTENTION; EnterCritical(); OneWirePullDownOn(); /* A */ Delay_us( 60 ); OneWirePullDownOff(); /* B0 */ Delay_us( 2 ); OneWirePullUpOn(); /* C0 */ Delay_us( 16 ); OneWirePullUpOff(); /* D0 */ ExitCritical(); Delay_us( 2 ); /* E0 */ if( !OneWireQueryBus() ) return ONE_WIRE_STATUS_BUS_CONTENTION; return ONE_WIRE_STATUS_OK; } /*============================================================================*/ onewirestatus_t OneWireWriteOne( void ) { if( !OneWireQueryBus() ) return ONE_WIRE_STATUS_BUS_CONTENTION; EnterCritical(); OneWirePullDownOn(); /* A */ Delay_us( 4 ); OneWirePullDownOff(); /* BR */ Delay_us( 2 ); OneWirePullUpOn(); /* D1 */ ExitCritical(); Delay_us( 67 ); OneWirePullUpOff(); /* E1 */ Delay_us( 2 ); /* F1 */ if( !OneWireQueryBus() ) return ONE_WIRE_STATUS_BUS_CONTENTION; return ONE_WIRE_STATUS_OK; } /*============================================================================*/ onewirestatus_t OneWireReadBit( void ) { onewirestatus_t Result; if( !OneWireQueryBus() ) return ONE_WIRE_STATUS_BUS_CONTENTION; SetTimer( 60 ); EnterCritical(); OneWirePullDownOn(); /* A */ StartTimer(); Delay_us( 4 ); OneWirePullDownOff(); /* BR */ Delay_us( 16 ); Result = OneWireQueryBus(); /* CR */ while( OneWireQueryBus() == 0 && !TimerExpired() ) {} if( OneWireQueryBus() == 0 ) { ExitCritical(); return ONE_WIRE_STATUS_BUS_CONTENTION; } OneWirePullUpOn(); /* F0 */ ExitCritical(); while( !TimerExpired() ) {} OneWirePullUpOff(); /* G0 */ Delay_us( 4 ); /* H0 */ if( !OneWireQueryBus() ) return ONE_WIRE_STATUS_BUS_CONTENTION; return Result; } /*============================================================================*/ onewirestatus_t OneWireInit( void ) { PORTAbits.RA0 = 0; TRISAbits.TRISA0 = 0; PORTAbits.RA3 = 1; TRISAbits.TRISA3 = 0; TRISAbits.TRISA1 = 1; #if defined USE_COMPARATOR TRISAbits.TRISA2 = 1; VRCONbits.VRR = 0; VRCONbits.VR = 1; VRCONbits.VROE = 0; VRCONbits.VREN = 1; CMCONbits.CM = 5; CMCONbits.C2INV = 1; #else /* defined USE_COMPARATOR */ CMCON = 0x07; #endif /* defined USE_COMPARATOR */ T2CONbits.TMR2ON = 0; T2CONbits.TOUTPS = 1; return ONE_WIRE_STATUS_OK; } /*============================================================================*/
file: /Techref/member/IMB-yahoo-J86/OneWirePIC16F-Turbo.c.htm, 10KB, , updated: 2019/8/19 09:14, local time: 2024/12/27 02:56,
owner: IMB-yahoo-J86,
18.224.54.118:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://linistepper.com/Techref/member/IMB-yahoo-J86/OneWirePIC16F-Turbo.c.htm"> <PRE></PRE></A> |
Did you find what you needed? |