;**********************************************************************
;
; Tutorial 12.1T - Manchester transmit routine, one byte packets.
;
;**********************************************************************
list p=16f818
__CONFIG _CP_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _CCP1_RB2 & _DEBUG_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_IO
#include <p16f818.inc>
;#define TXINV
;invert TX output so inactive is HIGH?
packet_len EQU 2 ;packet length 1 + 1 address byte
;TX pin is porta, 6
#ifndef TXINV
#define TXLOW BCF PORTA, 6
#define TXHIGH BSF PORTA, 6
#else
#define TXLOW BSF PORTA, 6
#define TXHIGH BCF PORTA, 6
#endif
;***** VARIABLE DEFINITIONS
cblock 0x20 ;start of general purpose registers
ncnt
bt
sum
mtx_buffer
mtx_buffer1
mtx_delay ; half_frame delay
count1 ;used in delay routine
count2
counta ;used in delay routine
countb ;used in delay routine
endc
org 0x0000
goto main
nop
nop
nop
retfie
main ; program starts here
banksel OSCCON ; Set oscilator to 4MHz
movlw b'01100000'
movwf OSCCON
bsf STATUS, RP0 ;select bank 1
movlw 0x00
movwf TRISA
clrf TRISB
bcf STATUS, RP0 ;select bank 0
clrf PORTA
clrf PORTB
call mtx_init
Loop
movlw 0xAA ;set packet address byte to 0xAA
movwf mtx_buffer
movlw 'N'
movwf mtx_buffer1
call mtx_send
call Delay20
movlw 'i'
movwf mtx_buffer1
call mtx_send
call Delay20
movlw 'g'
movwf mtx_buffer1
call mtx_send
call Delay20
movlw 'e'
movwf mtx_buffer1
call mtx_send
call Delay20
movlw 'l'
movwf mtx_buffer1
call mtx_send
call Delay20
movlw ' '
movwf mtx_buffer1
call mtx_send
call Delay20
goto Loop
;Manchester subroutines
; Author: el@jap.hu
; http://jap.hu/electronic/
mtx_init movlw .115 ; 350 usec
movwf mtx_delay
return
;
mtx_send
; send out buffer
outbuf movlw 0x14 ; 20xbit1, 1xbit0
header movwf count2
head0 call bit1
decfsz count2,F
goto head0
call bit0
movlw mtx_buffer
movwf FSR
movlw packet_len
movwf count1
movlw 0xff
movwf sum
;
outbu0 movf INDF,W
call update_sum
movf INDF,W
call outbyte
incf FSR,F
decfsz count1,F
goto outbu0
movf sum,W
call outbyte
; buffer is sent
return
update_sum ; fast CRC-8 algorithm with poly x^8+x^5+x^4+1
; executes in 23 cycles per update
xorwf sum,f
clrw
btfsc sum,7
xorlw 0x7a
btfsc sum,6
xorlw 0x3d
btfsc sum,5
xorlw 0x86
btfsc sum,4
xorlw 0x43
btfsc sum,3
xorlw 0xb9
btfsc sum,2
xorlw 0xc4
btfsc sum,1
xorlw 0x62
btfsc sum,0
xorlw 0x31
movwf sum
return
outbyte movwf bt
movlw 8
movwf count2
outby0 rlf bt,F
btfsc STATUS,C
goto outby1
call bit0
goto outby2
outby1 call bit1
outby2 decfsz count2,F
goto outby0
;
call bit1
; and bit0 - falls through to bit0 subroutine
;send a bit0
bit0 TXHIGH ; HIGH
call mtx_bitdel ; bit time delay
TXLOW ; to LOW transition
call mtx_bitdel ; bit time delay
return
;send a bit1
bit1 TXLOW ; LOW
call mtx_bitdel ; bit time delay
TXHIGH ; to HIGH transition
call mtx_bitdel ; bit time delay
return
; bit delay modified by NG
mtx_bitdel movf mtx_delay, W
movwf ncnt
ndelaya1 decfsz ncnt, F
goto ndelaya1
return
; end of Manchester routines
;Delay routines
Delay255 movlw 0xff ;delay 255 mS
goto d0
Delay100 movlw d'100' ;delay 100mS
goto d0
Delay50 movlw d'50' ;delay 50mS
goto d0
Delay20 movlw d'20' ;delay 20mS
goto d0
Delay5 movlw 0x05 ;delay 5.000 ms (4 MHz clock)
d0 movwf count1
d1 movlw 0xC7 ;delay 1mS
movwf counta
movlw 0x01
movwf countb
Delay_0
decfsz counta, f
goto $+2
decfsz countb, f
goto Delay_0
decfsz count1 ,f
goto d1
retlw 0x00
end