|
小弟面对一些mplab source code 问题。
关与用hamming code来detect error,
在theory part 我还明白,但放到MPlab就完全不了
希望有人能大概解释一下
在下感激不尽
以下就是program code
Hamming code generator
;
;Since our communication is one way, we'll use forward error correction
;to detect single bit errors. To do this, we'll add code bits to our
;ten bit results for a total of 14 bits. These code bits will be in bit
;positions 13, 12, 11, and 10.
;Code bit P1 will be in position D13 and will be the exclusive OR of:
; P1=D9+D8+D6+D5+D3+D1
;Code bit P2 will be in position D12 and will be the exclusive OR of:
; P2=D9+D7+D6+D4+D3+D0
;Code bit P4 will be in position D11 and will be the exclusive OR of:
; P4=D8+D7+D6+D2+D1+D0
;Code bit P8 will be in position D10 and will be the exclusive OR of:
; P8=D5+D4+D3+D2+D1+D0
;The data will be packed as: P1 P2 P4 P8 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
;************************************************************************
(TRANSMITTER PART)
HammCode
clrf HCREG ;Calculate first code bit
movlw 0x20 1=D9+D8+D6+D5+D3+D1
btfsc ACCH,1
incf HCREG,f
btfsc ACCH,0
incf HCREG,f
btfsc ACCL,6
incf HCREG,f
btfsc ACCL,5
incf HCREG,f
btfsc ACCL,3
incf HCREG,f
btfsc ACCL,1
incf HCREG,f
rrf HCREG,f
btfsc STATUS,C ;if summation results in odd number
iorwf ACCH,f ;set code P1 to a 1
clrf HCREG ;Calculate second code bit
movlw 0x10 ; P2=D9+D7+D6+D4+D3+D0
btfsc ACCH,1
incf HCREG,f
btfsc ACCL,7
incf HCREG,f
btfsc ACCL,6
incf HCREG,f
btfsc ACCL,4
incf HCREG,f
btfsc ACCL,3
incf HCREG,f
btfsc ACCL,0
incf HCREG,f
rrf HCREG,f
btfsc STATUS,C ;if summation results in odd number
iorwf ACCH,f ;set code P2 to a 1
clrf HCREG ;Calculate third code bit
movlw 0x08 ; P4=D8+D7+D6+D2+D1+D0
btfsc ACCH,0
incf HCREG,f
btfsc ACCL,7
incf HCREG,f
btfsc ACCL,6
incf HCREG,f
btfsc ACCL,2
incf HCREG,f
btfsc ACCL,1
incf HCREG,f
btfsc ACCL,0
incf HCREG,f
rrf HCREG,f
btfsc STATUS,C ;if summation results in odd number
iorwf ACCH,f ;set code P4 to a 1
clrf HCREG ;Calculate fourth code bit
movlw 0x04 ; P8=D5+D4+D3+D2+D1+D0
btfsc ACCL,5
incf HCREG,f
btfsc ACCL,4
incf HCREG,f
btfsc ACCL,3
incf HCREG,f
btfsc ACCL,2
incf HCREG,f
btfsc ACCL,1
incf HCREG,f
btfsc ACCL,0
incf HCREG,f
rrf HCREG,f
btfsc STATUS,C ;if summation results in odd number
iorwf ACCH,f ;set code P8 to a 1
return
(RECEIVER PART)
hamm_decode
clrf HCVECT ;clear vector register
clrf HCREG ;Calculate first code bit
; P1=D9+D8+D6+D5+D3+D1+P1
btfsc BUFF_HIGH,5
incf HCREG,f
btfsc BUFF_HIGH,1
incf HCREG,f
btfsc BUFF_HIGH,0
incf HCREG,f
btfsc BUFF_LOW,6
incf HCREG,f
btfsc BUFF_LOW,5
incf HCREG,f
btfsc BUFF_LOW,3
incf HCREG,f
btfsc BUFF_LOW,1
incf HCREG,f
rrf HCREG,f
btfsc STATUS,C ;if summation results in odd number
bsf HCVECT,0 ;set vector bit 0 to a 1
clrf HCREG ;Calculate second code bit
; P2=D9+D7+D6+D4+D3+D0+P2
btfsc BUFF_HIGH,4
incf HCREG,f
btfsc BUFF_HIGH,1
incf HCREG,f
btfsc BUFF_LOW,7
incf HCREG,f
btfsc BUFF_LOW,6
incf HCREG,f
btfsc BUFF_LOW,4
incf HCREG,f
btfsc BUFF_LOW,3
incf HCREG,f
btfsc BUFF_LOW,0
incf HCREG,f
rrf HCREG,f
btfsc STATUS,C ;if summation results in odd number
bsf HCVECT,1 ;set vector bit 1 to a 1
clrf HCREG ;Calculate third code bit
; P4=D8+D7+D6+D2+D1+D0+P4
btfsc BUFF_HIGH,3
incf HCREG,f
btfsc BUFF_HIGH,0
incf HCREG,f
btfsc BUFF_LOW,7
incf HCREG,f
btfsc BUFF_LOW,6
incf HCREG,f
btfsc BUFF_LOW,2
incf HCREG,f
btfsc BUFF_LOW,1
incf HCREG,f
btfsc BUFF_LOW,0
incf HCREG,f
rrf HCREG,f
btfsc STATUS,C ;if summation results in odd number
bsf HCVECT,2 ;set vector bit 2 to a 1
clrf HCREG ; Calculate fourth code bit
; P8=D5+D4+D3+D2+D1+D0+P8
btfsc BUFF_HIGH,2
incf HCREG,f
btfsc BUFF_LOW,5
incf HCREG,f
btfsc BUFF_LOW,4
incf HCREG,f
btfsc BUFF_LOW,3
incf HCREG,f
btfsc BUFF_LOW,2
incf HCREG,f
btfsc BUFF_LOW,1
incf HCREG,f
btfsc BUFF_LOW,0
incf HCREG,f
rrf HCREG,f
btfsc STATUS,C ;if summation results in odd number
bsf HCVECT,3 ;set vector bit 3 to a 1
movf HCVECT,f ;test vector
btfsc STATUS,Z ;if zero then no error - otherwise, flip bit
return
movf HCVECT,w ;get the Hamming vector
call hcode_shuffle ;remap code
movwf HCVECT
clrf MASKH ;initialize mask registers
clrf MASKL ;
bsf STATUS,C |
|