; PIC Code to communicate with a ; PAK-I or PAK-II from AWC ; This code would also work ; for the PAK-III or PAK-IV ; if you used it to send the right commands ; This program assumes the PAK-II is connected ; to PORTB,6 (clock) and PORTB,5 (data). The PAK-II's ; Port A pin 4 is connected to an LED which will blink ; PROCESSOR 16C57 RADIX DEC INCLUDE ; ; ; For a 10MHz Pak this macro should delay about 10uS ; While 8uS is about the minimum, the PAK ; spec calls for 20uS minimum pulse at 10MHz (50kbps) ; and 10uS minimum at 20MHz (100kbps) ; These routines should delay half of the minimum ; There are many ways you could write a pause ; routine and in a real program, you may ; want to do something different ; If you use a longer time, you might not need to ; delay after sending each byte -- however at "top" ; speed, allow a little time between bytes ; and be sure to "wait" on commands that provide ; busy status ; All times based on a 20MHz clock ; Your milage will vary if your speed is different ; PAK timing is not critical, so if an interrupt ; or something slows this down, no big deal pakpause macro n movlw n movwf tick decfsz tick,F goto $-1 endm ; These numbers are approximate pakdelay equ 18 ; 9 for 20Mhz PAK; about 18 for 10Mhz PAK - higher is OK pakbdelay equ 16 ; delay between bytes - 8 for 20MHz; 16 for 10MHz PAK #define clk portb,6 #define dat portb,5 ; TRIS bits for data bit high or data bit low datahigh equ 0xBF datalow equ 0x9F tick equ 9 ; used by delay routine sbyte equ 10 ; communications byte paki equ 11 ; loop counter for PAK library out equ 12 ; This is used by the example code org 0 start ; set clock to output movlw datahigh tris portb ; Need to set internal pull up resistor on RB5 or use external pull up ; Always do a comm reset on the PAK before starting call pakreset ; Read PAK's ID just to prove it works movlw 8 call pakxmit call pakwait ; wait for ready call pakrcv ; W should equal 2A for a PAK-II ; Change test pin to an output movlw 0x14 ; IODIR call pakxmit movlw 0x10 call pakxmit pakloop movlw 0x16 ; write port command call pakxmit movf out,w call pakxmit ; toggle bit so LED will blink movlw 0x10 xorwf out,F ; reuse paki and sbyte here to delay so people can see the blink ; Delay = 255*255*delay movlw 0xFF movwf sbyte iloop movlw 0xFF movwf paki ploop pakpause 255 decfsz paki,F goto ploop decfsz sbyte,F goto iloop goto pakloop ; END of example code ; PAK LIBRARY BEGINS HERE ; Send a byte in W to the PAK pakxmit movwf sbyte movlw 8 movwf paki ; number of bits loop rlf sbyte,F ; MSB first btfsc STATUS,C goto xone movlw datalow tris portb bcf dat xone call pakclk ; pulse clock movlw datahigh tris portb decfsz paki,F goto loop pakpause pakbdelay ; give it some time between bytes clrw retlw 0 ; wait loop -- only works with commands ; that bring the first bit in as 0 pakwait btfsc dat goto pakwait retlw 0 ; Receive a byte from the PAK ; if you need to wait for the PAK to be ready ; you must call pakwait first ; return is in sbyte pakrcv clrf sbyte movlw 8 ; 8 bits movwf paki rloop bcf status,c btfsc dat bsf status,c rlf sbyte,F call pakclk ; pulse clock decfsz paki,F goto rloop retlw 0 ; Generate a clock pulse (could be a macro) pakclk bsf clk pakpause pakdelay bcf clk pakpause pakdelay retlw 0 ; This routine resets the PAK using the data ; line reset sequence ; Lots of pauses to be sure -- shouldn't need ; to do this but once pakreset clrf portb movlw datalow tris portb pakpause pakdelay bsf clk ; clock high pakpause pakdelay bsf dat ; data high pakpause pakdelay bcf clk ; clock low pakpause pakdelay movlw datahigh tris portb pakpause pakdelay retlw 0 ; Reset vector org 0x1FF goto start END