1 title "PIC16F876A Unilink Interface by Werner Johansson (c) 2004-2005"
\r
2 subtitl "Definitions"
\r
3 list c=132,P=16F876a,R=DEC,F=inhx8m
\r
4 include "p16f876a.inc" ; Standard equates & Macros
\r
5 ERRORLEVEL 1,-302 ; Get rid of those annoying 302 msgs!
\r
7 ;----------------------------------------------------------------
\r
8 ; The Configuration Word
\r
9 __CONFIG _HS_OSC&_WDT_OFF&_PWRTE_OFF&_BODEN_ON&_LVP_OFF&_CPD_OFF&_WRT_OFF&_DEBUG_OFF&_CP_OFF
\r
11 ;----------------------------------------------------------------
\r
13 ;----------------------------------------------------------------
\r
16 ; 0.0 Very first "Fucking No Work!" version
\r
17 ; 0.1 Some receiving works with the new MCU using HW SPI, yay!
\r
19 ;----------------------------------------------------------------
\r
21 ; Unilink BUSON IN (blue) connected to RA4C2/T0CKI
\r
22 ; Unilink DATA (green) connected to RC4(SDI), RC5(SDO) via open collector driver (HW USART)
\r
23 ; Unilink BUSON OUT (blue) connected to RA5 via 100R resistor (this is for daisy-chaining)
\r
24 ; Unilink CLK (yellow) connected to RC3(SCK) (USART CLK)
\r
25 ; Unilink RST (lilac) connected to RB0(INT)
\r
26 ; RS-232 TX from computer connected to RC7/RX
\r
27 ; RS-232 RX to computer connected to RC6/TX
\r
28 ; RS-232 RTS from computer connected to RC2
\r
29 ; RS-232 RI and CTS to computer connected to RC1
\r
30 ; RS-232 INVALID signal from MAX connected to RC0
\r
32 ; Pin mapping as follows:
\r
34 ; RA0 Voltage sense (A/D in 0)
\r
35 ; RA1 Amp sense 1 (A/D in 1)
\r
36 ; RA2 Early-Startup sense/Blue LED drive (active low) (RA2 and RA3 have been reversed from the PCB layout!)
\r
37 ; RA3 Amp sense 2 (A/D in 3)
\r
38 ; RA4/T0CKI BUSON IN from Head Unit (can trigger interrupt on clocking?)
\r
39 ; RA5 BUSON OUT to cascade
\r
41 ; RB0 UNI RST in (interrupt triggered)
\r
42 ; RB1 Enable-PWR output to SEPIC converter
\r
43 ; RB2 Disable-AMP output
\r
44 ; RB3 Enable-AUX output to trigger analog switch relay
\r
45 ; RB4 Sign output from Amp sense 1/Red LED drive, active low
\r
46 ; RB5 Sign output from Amp sense 2/Green LED drive, active low
\r
47 ; RB6 ICSP/ICD reserved
\r
48 ; RB7 ICSP/ICD reserved
\r
50 ; RC0 INVALID input from MAX3235
\r
51 ; RC1 CTS and RI output
\r
53 ; RC3 Unilink clock input
\r
54 ; RC4 Unilink data input
\r
55 ; RC5 Unilink data output (drives inverted!)
\r
59 ;----------------------------------------------------------------
\r
60 ; File register usage
\r
62 ; Memory from 20h to 7fh (BANK 0 96 bytes)
\r
63 ; a0h to efh (BANK 1 80 bytes)
\r
64 ; 110h to 16fh (BANK 2 96 bytes)
\r
65 ; 190h to 1efh (BANK 3 96 bytes)
\r
66 ; 70-7fh, f0-ffh, 170-17fh and 1f0-1ffh share the same 16 bytes, for ISR!)
\r
70 Icount equ 2Dh ; Offset of string to print
\r
71 TxTemp equ 2Eh ; blahblah
\r
72 TxTemp2 equ 2Fh ; Blahblah2
\r
75 nop ; Leave the first location, just in case
\r
76 ; call Bootstrap ; Call Flash Load routine
\r
77 ; call LCDInit ; Initialize LCD I/F
\r
78 ; call IRQInit ; Set up and start the IRQ handler
\r
79 goto Main ; Run the main program loop (skip the IRQ handler)
\r
81 subtitl "IRQ Handler"
\r
82 ;----------------------------------------------------------------
\r
83 ; Interrupt handler always starts at addr 4.
\r
84 ; Let the entire handler stay here instead of branching away
\r
86 org 4 ; Must be on Address 4!
\r
87 retfie ; Interrupt return
\r
99 bsf STATUS,RP0 ; Hi Bank
\r
100 movlw 04h ; AD mode 4h, 3 ad inputs AN0, 1 and 3, not 2 (!) fix pcb.. :(
\r
102 movlw 0dfh ; RA5 should be outputs...
\r
103 ; movlw 0dbh ; RA2 & RA5 should be outputs...
\r
104 ; movlw 0fbh ; RA2 should be output...
\r
106 movlw 0c1h ; RB0, 6 & 7 are inputs.
\r
108 movlw 9dh ; RC0, 2, 3, 4, 7 are inputs
\r
109 ; movlw 0bdh ; RC0, 2, 3, 4, 7 are inputs
\r
111 bcf OPTION_REG,NOT_RBPU ; Turn on port B pull-up
\r
112 bcf STATUS,RP0 ; Restore Lo Bank
\r
114 bsf PORTB,1 ; Turn on SEPIC
\r
115 ; bsf PORTA,5 ; test ss shit
\r
117 bsf STATUS,RP0 ; Access bank 1
\r
118 bsf TXSTA,TXEN ; Enable UART TX
\r
119 movlw 31 ; Divisor for 9k6 @ 20MHz Fosc
\r
120 movwf SPBRG ; Store
\r
121 bcf STATUS,RP0 ; Back to bank 0
\r
123 bsf RCSTA,SPEN ; Enable serial port
\r
124 bsf RCSTA,CREN ; Enable UART RX
\r
127 ; movwf TXREG ; Test transmission
\r
132 ; btfss PORTB,0 ; Test RST bit
\r
135 ; bsf STATUS,RP0 ; Hi Bank
\r
136 ; bsf TRISA,2 ; RA2 back as input (turn off blue led)
\r
137 ; bcf STATUS,RP0 ; Lo Bank
\r
140 bcf SSPCON,SSPEN ;disable ssp
\r
145 btfsc PORTB,0 ; Test RST bit
\r
150 btfss PORTA,4 ; Test BUSON bit
\r
151 goto WaitBUSONHigh
\r
154 ; movlw 40h ; Latch on active-to-idle edge (needs ss crap?)
\r
155 movlw 00h ; Transfer on idle-to-active edge (and latch on active-to-idle)
\r
161 btfsc PORTC,3 ; SCK input must be low before enabling SSP according to errata sheet (!)
\r
164 ; movlw 34h ; fuxx0red ss mode crap
\r
165 movwf SSPCON ; Enable SPI slave mode with SCK IDLE low, no _SS control
\r
173 btfsc PORTB,0 ; Test RST bit
\r
176 btfss PORTA,4 ; Test BUSON bit
\r
180 btfss SSPSTAT,BF ; Test for buffer complete
\r
184 xorlw 0ffh ; Invert received data
\r
185 movwf TXREG ; Send it out to Async serial
\r
194 ; NOTE!!!! This is wrong, never drive the led outputs high as there are other open-collector drivers on these pins!!!
\r
195 ; This is just for debugging, must be removed when hooking things up to the board!!!
\r
197 ; IO is inverted logic (active low)
\r
198 ; btfsc PORTA,4 ; test buson
\r
199 btfss PORTC,4 ; test IO
\r
200 bcf PORTB,5 ; turn on green led
\r
201 ; btfss PORTA,4 ; test buson clear
\r
202 btfsc PORTC,4 ; test IO
\r
203 bsf PORTB,5 ; turn off green led
\r
205 ; CLK is true logic (active high)
\r
206 ; btfsc PORTB,0 ; Test RST bit
\r
207 btfsc PORTC,3 ; test clk
\r
208 bcf PORTB,4 ; turn on red led if RST high
\r
209 ; btfss PORTB,0 ; Test RST bit
\r
210 btfss PORTC,3 ; test clk
\r
211 bsf PORTB,4 ; turn off red led if RST low
\r
215 ;----------------------------------------------------------------------
\r
216 ; EE Data (64 bytes), located at 2100h
\r
219 ; data 0ffh, 0ffh, 0ffh, 0ffh, 001h, 023h, 045h, 067h
\r