include ; ; modif by PF 01/12/2012 ; SWTXD equ PORTB ; Transmit pin port and pin SWTXDpin equ 4 TRIS_SWTXD equ TRISB ; Transmit pin tris and pin SWRXD equ PORTB ; Receive pin port and pin SWRXDpin equ 5 TRIS_SWRXD equ TRISB ; Receive pin tris and pin EXTERN DelayRXHalfBitUART0 EXTERN DelayRXBitUART EXTERN DelayTXBitUART EXTERN uartdata EXTERN BitCount UARTCODE CODE ;************************************************************************ ;* Function Name: LectdUART * ;* Return Value: char: received data or nothing if timeoout * ;* Parameters: void * ;* Description: This routine waits for a start bit, and then * ;* reads all 8-bits. * ;* Special Note: The user must provide two routines: * ;* DelayRXHalfBit(): * ;* DelayRXHalfBit should have: * ;* 5 Tcy for overhead * ;* 2 Tcy call to DelayRXHalfBit * ;* ? Tcy * ;* 2 Tcy for return from DelayRXHalfBit * ;* = (((2*OSC_FREQ)/(8*BAUDRATE))+1)/2 Tcy * ;* DelayRXBit(): * ;* DelayRXBit should have: * ;* 10 Tcy for overhead * ;* 2 Tcy call to DelayRXBit * ;* ? Tcy * ;* 2 Tcy for return from DelayRXBit * ;* = (((2*OSC_FREQ)/(4*BAUDRATE))+1)/2 Tcy * ;************************************************************************ LectUART banksel BitCount movlw 0x08 ; Count 8-bits movwf BitCount banksel SWRXD WaitChar1 btfsc INTCON,TMR0IF,1 ; important ! a=1 acces SFR goto sortie btfsc SWRXD,SWRXDpin ; Detect a start bit (low) goto WaitChar1 call DelayRXHalfBitUART0 ; Delay until middle of start bit banksel SWRXD btfsc SWRXD,SWRXDpin ; If not still low, then go back goto WaitChar1 ; and wait for start bit GetChar1 ; Loop to get all 8-bits call DelayRXBitUART ; Delay a full bit for middle of banksel SWRXD btfss SWRXD,SWRXDpin ; Set or clear the carry bit bcf STATUS,C ; according to the state of the btfsc SWRXD,SWRXDpin ; RXD I/O pin bsf STATUS,C banksel uartdata rrcf uartdata,F ; Rotate the carry into data decfsz BitCount,F ; Loop for 8-bits goto GetChar1 call DelayRXBitUART ; Delay a full bit time, middle of start bit call DelayRXHalfBitUART0 ; Delay half bit for end of start bit nop banksel uartdata movf uartdata,W,BANKED return ; Return received character sortie movlw 0x00 return GLOBAL LectUART END