;**************************** ; Double-handshake SERIAL IN from a Stamp II ; not interrupt or timer driven - can work with any pins ; uses flow-control handshakes ; displays results on port b ; ; serial mode is: non-inverted 8 data bits, no parity ; ; core serial code from Microchip Application Note AN593 ; by Stan D'Souza ; ; modified to work with Stamp II & 16F84A ; with added rx LED & handshakes - benz y2k ; solid at 9600 baud ;**************************** list p=16f84 radix hex ;**** CPU Equates *********** STATUS equ 0x03 porta equ 0x05 portb equ 0x06 ;**** bit Equates *********** c equ 0 ;**** constants ************* clockrate equ .4000000 baudrate equ .9600 fclk equ clockrate/4 baudconst equ ((fclk/baudrate)/3-2) ;**** general registers ***** count equ 0x10 rcreg equ 0x11 delay equ 0x12 mcount equ 0x13 ncount equ 0x14 ;**** defines *************** #define heyhey porta,4 ; stamp handshake in #define _hs porta,3 ; handshake pin #define _rx porta,2 ; receive serial pin #define rled porta,1 ; rx in progress LED pin #define zled porta,0 ; busy with other things... ; ORG 0x000 ; processor reset vector goto main ; go to beginning of program ; ; *** pause routine ****** pause movlw 0x2f movwf mcount loadn movlw 0x2f movwf ncount decn decfsz ncount,f goto decn decfsz mcount,f goto loadn return ; ; *** receive serial ************ startrec bcf _hs ;*** give me a byte! handshake pin 2 to 0 incf delay,f ; fix LSB quirk receive btfsc _rx ; wait 'til r is 0 goto receive ; that's right bcf rled ; LED signals rx in session rxbaudwait decfsz delay, F ; wait baudrate times goto rxbaudwait movlw baudconst ; reset delay part I movwf delay ; reset delay part II bsf _hs ; ****** reset handshake decfsz count, F ; decrement number of bits goto recvNextBit ; if not 0 then get next bit ;**** party's over **** movlw .9 movwf count ; reset 8 bits receive movf rcreg,w ; result file reg to W movwf portb ; display on portb bsf rled ; turn off rx led return recvNextBit bcf STATUS, c ; clear carry bit btfsc _rx ; skip next instruction if rx is low (clear) bsf STATUS, c ; set carry bit rrf rcreg, F ; rotate out of carry! goto rxbaudwait ; go again ; **** end rx serial *********************************************** ; ; **** here is main ************************************************ main movlw b'11110100' ; all but porta pins 1 2 4 are inputs tris porta ; define porta movlw b'00001000' movwf porta ; clear porta except out handshake movlw 0x00 ; portb is output tris portb ; what I said. clrf portb ; clear portb movlw baudconst movwf delay ; set up delay first time movlw .9 movwf count ; setup to grab 8 bits ; ***** loop to get bytes & display ********** loopdis ;get data btfsc heyhey ; is heyhey = 1? call startrec ; then get byte ; call pause ; pause if wanted bcf zled ; LED signals rx in session ; get data? btfsc heyhey ; is heyhey = 1? call startrec ; then get byte ; call pause ; pause if wanted bsf zled ; LED signals rx in session ;get data? btfsc heyhey ; is heyhey = 1? call startrec ; then get serial byte ; call pause ; pause if wanted goto loopdis ; end