reading a byte

Status
Not open for further replies.

simple&cheap

New Member
I need to read the 3rd. byte of a 8 byte frame and depending what's in that byte determines what I do next.
I think this modified code will read it but I not sure how to determine what it is

example
if the byte has 0x00 in it I do something
if the byte has 0x47 in it I do something else

;read the 3rd. byte of a 8 byte frame
;each frame is 16.6 ms long including a 5.4ms to 7ms.interframing gap used for sync
;each byte is 1.2ms to 1.4ms long including a 104us start bit and 264us to 464us stop bit
;start bits and stop bits are inverted

#include "p16F676.inc"

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _MCLRE_OFF & _CPD_OFF

errorlevel -302
cblock 0x20


Byte ; holds the Byte we are reading on the LANC line

ReadError ; bit 0 is used to hold a 1 if there is a read error
Bytes_Jump ;used to skip 1st. 2 bytes (should be a better way
Byte0 ; holds Byte0
Byte1 ; holds Byte1
Byte2 ; holds Byte2
Byte3 ; holds Byte3
Byte4 ; holds Byte4
Byte5 ; holds Byte5
Byte6 ; holds Byte6
Byte7 ; holds Byte7

BitCount ; counter used in bit counting
Count ; counter
Temp ; tempory register
d1 ; used for timer
d2
d3

endc

#DEFINE LANC PORTC,4 ;USED TO READ DATA AND SEND COMMANDS
org 0
goto Start ; jump to Start

;////////////////////////////////////SETTING I/O FOR READ/WRITE SUB///////////////////////////////////////////
;ReadLANC SET PORTC,4 AS INPUT FOR READING, WriteLANC SET PORTC,4 AS OUTPUT FOR SENDING COMMANDS

ReadLANC:
bsf STATUS,RP0 ;BANK 1
movlw B'00010110' ;PORTC,2,3 ALWAYS INPUT
movwf TRISC ;PORTC,4 INPUT WHEN READING
bcf STATUS,RP0 ;BANK 0
return

WriteLANC:
bsf STATUS,RP0 ; BANK 1
movlw B'00000110' ; PORTC,4 OUTPUT WHEN WRITTING
movwf TRISC
bcf STATUS,RP0 ; BANK 0
return
;-----------------------------------------------------------------------------------------------;
; The waitHalf sub delays the program by 48uS. This is used when the start bit of each byte is ;
; found so we are reading the bits in the middle, not on the transistion. Half a bit length is ;
; actually 52uS, but most lines of code take 1uS, these lines of code in the program have been ;
; worked to make sure the delay is correct. ;
;-----------------------------------------------------------------------------------------------;
WaitHalf:
movlw .14
movwf Count
nop

WaitHalfLoop:
decfsz Count
goto WaitHalfLoop
return
;????????????????????????????????SHOULD BE A BETTER WAY THAN THIS???????????????????????????????????

; Delay = 0.0022 seconds ; should put you @ the middle of stop bit of 2nd. byte
movlw 0xB7
movwf d1
movlw 0x02
movwf d2
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay_0

;2 cycles
goto $+1
return

;-----------------------------------------------------------------------------------------------;
; ReadByte is called to read a byte on the LANC line, the byte is stored in the Byte register ;
; The bits we read are inverted before they are stored. ;
;-----------------------------------------------------------------------------------------------;
ReadByte:

clrf Byte ; clear the Byte register
btfsc LANC ; skip when the LANC line drops to 0
goto $-1

call WaitHalf ; wait for 50uS

btfsc LANC ; make sure the LANC line is still 0
bsf ReadError,0 ; set the read error flag

; now we are in the middle of the start bit, so we wait for 104uS
; so we are in the middle of Bit0

call WaitHalf ; wait for 48uS (about half of a bit length)
call WaitHalf ; wait for 48uS (about half of a bit length)
nop
nop
nop
nop
nop
nop ; wait another 5uS


; CheckBit is looped 8 times in order to read the 8 bits of the byte

movlw .8 ; put 8 into W
movwf BitCount

CheckBit:
rrf Byte,1 ; rotate the Byte register right
bcf Byte,7 ; clear bit 7 of the Byte register incase the Carry flag has been rotated into it

btfss LANC ; is the LANC line HIGH
bsf Byte,7 ; if the LANC line was 'LOW' clear bit 7 of the Byte register

call WaitHalf ; wait for 52uS (half of a bit length)
call WaitHalf ; wait for 52uS (half of a bit length)

decfsz BitCount ; have we read all 8 bits
goto CheckBit
return

Start:

;//////////////////////////////TURN COMPARATOR OFF//////////////////////////////////////////
bcf STATUS,RP0 ;BANK 0
bcf STATUS,RP1 ; register page 0
movlw 0X07 ;SET UP W TO TURN COMPARATOR OFF
movwf CMCON
;////////////////////////////SET UP I/O////////////////////////////////////////////////////
bSf STATUS,RP0 ; BANK 1
movlw b'00001000' ;CONFIG I/O
movwf TRISA ;2,3,4 INPUTS
movlw b'00010110' ;CONFIG I/O LANC INPUT @ POWER!!!!!!!!!!!!!!!!!!!
movwf TRISC ;TO ALL OUTPUTS
;/////////////////////////////TURN OFF A/D/////////////////////////////////////////////////
MOVLW B'00000000' ; ALL DIGITAL
MOVWF ANSEL ;IS IN BANK 1

Sync:
call ReadLANC ; set for reading

Sync2:
clrf Count ; clear the Count register

SyncLoop:
btfss LANC ; see if the LANC line is high
goto Sync2

; If the Count register gets to zero, we know we are in the interframe
; gap. 256 x 5uS (the time for the loop) = 1280uS. There is only about
; 200uS between bytes on the LANC line!

decfsz Count ; decrement the Count register
goto SyncLoop

call Bytes_Jump ;jump the 1st 2 bytes to read 3rd bytes
call ReadByte ;read the 8 bits of the 3rd. bytes

;???????????????????????????????????????????????????????????????????????????
; if the 3rd. bytes has 0x00 in it do something
; if the 3rd. bytes has 0x47 in it do something else
;???????????????????????????????????????????????????????????????????

end
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…