;----------------------------------------------------------------------- ;name: coproc.asm ;description: ARobot coprocessor code. ;programmer: Aaron Richardson - Arrick Robotics (c) ;language: mpasm ;date: 2/11/99 ;edit: 1 ; ;function: ;This is a functional skeleton ARobot PIC coprocessor code. It turns on and ;off a red led and a green led. The code can be modified to complete ;any task that is desired for the coprocessor to accomplish. The same ;coprocessor test code that was used for the BSII coprocessor can be used on ;the master processor to control this coprocessor. The outcome will be the ;same as the BSII coprocessor. ; ;public routines: ; ;public variables: ;----------------------------------------------------------------------- list p=16f84, f=inhx8m, r=hex ;this file is the include file needed for the pic16f84. ;place it in the arobot directory. include "\arobot\p16f84.inc" ;include file. ;equates. ;I/O pin labels. net equ 4 ;network bit(port a). ;attach red led to pin 1, attach green led to pin 2.(with resistors). redled equ 2 ;led equates. grnled equ 3 sibbaud equ 0x043 ;baud rate(67 dec = 2400, 43 hex). ;32 = 4800(20 hex). ;15 = 9600(0F hex). ;6 = 19200. conid equ '2' ;controller id. ;variables. sibtmp1 equ 0x00c ;temp var for sib. sibtmp2 equ 0x00d ;temp var for sib. sibtmp3 equ 0x00e ;temp var for sib. temp1 equ 0x00f ;temp var for main. __CONFIG _CP_OFF & _WDT_OFF & _XT_OSC ;configure fuses. ;code preotect off. ;watch dog timer off. ;crystal oscliator. ;----------------------------------------------------------------------- ;this part of the code intializes the pic. ; setup: bsf status,rp0 ;set to bank 1. clrw ;clear w for trisa/b. movwf trisa ;clear trisa. movwf trisb ;clear trisb. bsf trisa,net ;set net to input. bcf status,rp0 ;set to bank 0. movlw 0x000 movwf portb ;clear ports. movwf porta bsf porta,redled ;turn off leds. bcf porta,grnled ;turn on green led for pwr. ;----------------------------------------------------------------------- ;this is the main part of the code. ; main: call sibwat ;wait for attention char. sublw '!' ;compare char with '!'. btfss status,z ;if zero then skip. goto main ;not attention char. main1: call sibwat ;get next char. sublw conid ;check conid. btfss status,z ;skip if zero. goto main ;talking to other coprocesor. call sibwat ;get next char. movwf temp1 ;save it for now. movlw 'R' ;check for R. subwf temp1,w ;check it. btfsc status,z ;skip if not R. goto red ;do red led. movlw 'G' ;check for G. subwf temp1,w ;check it. btfsc status,z ;skip if not G. goto green ;do green led. ;bad command found, send bad command character. sendb: movlw 'B' ;send out bad char. call sibput ;send it. goto main ;back to loop. ;valid command found, send acknowledge character. senda: movlw 'A' ;send out bad char. call sibput ;send it. goto main ;back to loop. ;----------------------------------------------------------------------- ;this routine turns on or off the red led. It gets the next character ;sent in on the network pin. If this character is a '1' then it turns ;the led on. If the character is a '0' then it turns the led off. If the ;character is something else then it jumps to sendb to signify a bad ;command found. ; red: call sibwat ;get next character. movwf temp1 ;save char in temp register. movlw '0' ;check for 0. subwf temp1,w ;check it. btfsc status,z ;skip if not 0. goto red0 movlw '1' ;check for 1. subwf temp1,w ;check it. btfsc status,z ;skip if not 1. goto red1 goto sendb ;bad command. red0: bsf porta,redled ;turn off red led. goto senda ;acknowledge command. red1: bcf porta,redled ;turn on red led. goto senda ;acknowledge command. ;----------------------------------------------------------------------- ;this routine turns on or off the green led. It gets the next character ;sent in on the network pin. If this character is a '1' then it turns ;the led on. If the character is a '0' then it turns the led off. If the ;character is something else then it jumps to sendb to signify a bad ;command found. ; green: call sibwat ;get next character. movwf temp1 ;save char in temp register. movlw '0' ;check for 0. subwf temp1,w ;check it. btfsc status,z ;skip if not 0. goto green0 movlw '1' ;check for 1. subwf temp1,w ;check it. btfsc status,z ;skip if not 1. goto green1 goto sendb ;bad command. green0: bsf porta,grnled ;turn off green led. goto senda ;acknowledge command. green1: bcf porta,grnled ;turn on green led. goto senda ;acknowledge command. ;----------------------------------------------------------------------- ;sibput - serial I/O put bit bang on network pin. ;puts byte found in w. does not destroy w. ;currently set for 2400 baud. ;delay determines speed: ; 3333us = 300 baud ; 832us = 1200 baud ; 416us = 2400 baud ; 208us = 4800 baud ; 104us = 9600 baud ;net is usually an input pin so it needs to be changed. sibput: clrf sibtmp1 sibput0: decfsz sibtmp1,f ;wait a bit. goto sibput0 bsf status,rp0 ;need to set net to output. bcf trisa,net ;set net to output. bcf status,rp0 bsf porta,net ;stays high while no transmission. movwf sibtmp1 ;copy w. movwf sibtmp2 ;save w. bcf status,c ;send start bit '0'. call sibput1 rrf sibtmp1,f ;send bit 0, lsb first. call sibput1 rrf sibtmp1,f ;send bit 1. call sibput1 rrf sibtmp1,f ;send bit 2. call sibput1 rrf sibtmp1,f ;send bit 3. call sibput1 rrf sibtmp1,f ;send bit 4. call sibput1 rrf sibtmp1,f ;send bit 5. call sibput1 rrf sibtmp1,f ;send bit 6. call sibput1 bcf status,c ;send bit 7 (zero). call sibput1 bsf status,c ;send stop bit '1'. call sibput1 bsf status,rp0 ;need to set net to input. bsf trisa,net ;set net to input. bcf status,rp0 movf sibtmp2,w ;restore w. return ;----------------------------------------------------------------------- ;put each bit on the net. ;(5 instruction cycles to get to sibwtw from here). sibput1: btfsc status,c ;skip if c is cleared. goto sibput2 ;jump if set. bcf porta,net ;lower net. goto sibwtw ;wait now. sibput2: bsf porta,net ;set net high. nop ;balance gotos if c is set. ;----------------------------------------------------------------------- ;sibwt whole cycle. ;wait whole cycle takes (baud*6)+8 cycles. ; sibwtw: movlw sibbaud ;setup counter with baud(1). movwf sibtmp3 ;move baud to tem register(1). sibwtw1: decfsz sibtmp3,f ;dec and skip if zero(1). goto sibwtw1 ;loop(2). ;(1 extra for skip) ;----------------------------------------------------------------------- ;sibwt half cycle. ;wait half takes (baud*3)+5 sibwth: movlw sibbaud ;setup counter with baud(1). movwf sibtmp3 ;move baud to tem register(1). sibwth1: decfsz sibtmp3,f ;dec and skip if zero(1). goto sibwth1 ;loop(2). ;(1 extra for skip) return ;done(2). ;----------------------------------------------------------------------- ;sibget - serial I/O get bit bang on network pin. ;gets byte from network pin and puts it in w. ;currently set for 2400 baud. ;delay determines speed: ; 3333us = 300 baud ; 832us = 1200 baud ; 416us = 2400 baud ; 208us = 4800 baud ; 104us = 9600 baud ;this routine assumes that net pin has just changed. ;assumes net pin is an input. sibget: clrf sibtmp2 ;clear input register. call sibwth ;sibwt half cycle. (middle of bit). call sibwtw ;sibwt whole cycle. bcf sibtmp2,net ;clear bit in storage. movf porta,w ;get porta. andlw 0x010 ;just get input bit. iorwf sibtmp2,f ;or it with storage. rrf sibtmp2,f ;shift it. call sibwtw ;sibwt whole cycle. bcf sibtmp2,net ;clear bit in storage. movf porta,w ;get porta. andlw 0x010 ;just get input bit. iorwf sibtmp2,f ;or it with storage. rrf sibtmp2,f ;shift it. call sibwtw ;sibwt whole cycle. bcf sibtmp2,net ;clear bit in storage. movf porta,w ;get porta. andlw 0x010 ;just get input bit. iorwf sibtmp2,f ;or it with storage. rrf sibtmp2,f ;shift it. call sibwtw ;sibwt whole cycle. bcf sibtmp2,net ;clear bit in storage. movf porta,w ;get porta. andlw 0x010 ;just get input bit. iorwf sibtmp2,f ;or it with storage. rrf sibtmp2,f ;shift it. call sibwtw ;sibwt whole cycle. bcf sibtmp2,net ;clear bit in storage. movf porta,w ;get porta. andlw 0x010 ;just get input bit. iorwf sibtmp2,f ;or it with storage. rrf sibtmp2,f ;shift it. call sibwtw ;sibwt whole cycle. bcf sibtmp2,net ;clear bit in storage. movf porta,w ;get porta. andlw 0x010 ;just get input bit. iorwf sibtmp2,f ;or it with storage. rrf sibtmp2,f ;shift it. call sibwtw ;sibwt whole cycle. bcf sibtmp2,net ;clear bit in storage. movf porta,w ;get porta. andlw 0x010 ;just get input bit. iorwf sibtmp2,f ;or it with storage. rrf sibtmp2,f ;shift it. call sibwtw ;sibwt whole cycle (7th bit). call sibwtw ;sibwt whole cycle (stop bit). rlf sibtmp2,f ;need 3 shifts. rlf sibtmp2,f rlf sibtmp2,f movf sibtmp2,w ;stick it in w. return ;done. ;----------------------------------------------------------------------- ;sibwat waits for the next char to start coming in on the wire then jumps ;to sibget. ; sibwat: btfss porta,net ;wait for bit to go low. goto sibget ;jump if low. goto sibwat ;loop. ;----------------------------------------------------------------------- ;end of coprocessor code. ;----------------------------------------------------------------------- end