;******************************************************************* ; This gets a character from serial rx and sends HEX out tx ; plus outputs the char on port B for reference / digital ;******************************************************************* ; LIST p=16C84 ; PIC16C84 is the target processor include "P16c84.inc" clkrate equ .4000000 ; 4 Mhz clock rate bdrate equ .2400 ; 2400 baud fclk equ clkrate/4 ; baudfig equ ((fclk/bdrate)/3-2) ;must be a 8 bit value baud2 equ baudfig/2 ;half bit time ra equ 5 ; I/O port A rb equ 6 ; I/O port B count equ 0x10 ; temp count txreg equ 0x11 ; temp tx register deltmp equ 0x12 ; delay temp storage switchc equ 0x15 ; used as temp register #define tx PORTA,1 ; the bit for serial tx #define rx PORTA,4 ; the bit for serial rx ; ; ;******************************************************************** ; Test Program ;********************************************************************* reset ;everything starts at 0 start movlw B'11000101' ;whatever option movlw B'00000000' ;RB0-7 0=outputs tris rb ; set them movlw B'00010000' ;RA4 is 1=input tris ra ; set them movlw 0 main call getrx ;rx will wait till you send something movwf PORTB ;put it out on port B for checking movwf switchc swapf switchc ;do the upper nibble first movf switchc,w call ascii ;and print the letter call sendtx swapf switchc ;swap it back movf switchc,w call ascii call sendtx movlw 0x20 ;send a space character call sendtx goto main ascii andlw 0x0F ;cut off upper bits addlw 0x30 ;make it start with ascii zero '0' movwf deltmp ;store, because microchip geeks are backwards movlw 0x3A ;we subtract from 0x3A subwf deltmp,0 ;deltmp - W = result put in w btfsc STATUS,C ;C=0 if result is negative 0x30 - 0x39 addlw 0x07 ;move us up to ascii 'A' (0x41) addlw 0x3A ;make it what it was before return sendtx movwf txreg ;store our data byte was in w bcf tx ;tx = LOW, start bit here movlw baudfig ;set up our delay movwf deltmp movlw .9 ;set up bit counter movwf count txwait decfsz deltmp goto txwait ;wait for bit time to end movlw baudfig ;set up bit delay again movwf deltmp decfsz count ;see if we are done yet goto nextbit bsf tx ;tx = HIGH - 2 stop bits txwait1 decfsz deltmp goto txwait1 ;wait for bit time to end movlw baudfig ;set up bit delay again movwf deltmp txwait2 decfsz deltmp goto txwait2 ;wait for bit time to end return nextbit rrf txreg ;shift it >>>> to c btfss STATUS,C ;skip if c = HIGH goto setlo bsf tx ;tx = HIGH goto txwait setlo bcf tx ;tx = LOW goto txwait getrx movlw baud2 ;set up HALF bit delay movwf deltmp movlw .9 ;set up bit counter movwf count getrxl btfsc rx ;watch for LOW on rx line, start bit goto getrxl halfbt decfsz deltmp ;move out 1/2 bit time goto halfbt movlw baudfig ;set up full bit delay movwf deltmp rxwait decfsz deltmp ;lets wait for this bit to pass by goto rxwait movlw baudfig ;set up bit delay again movwf deltmp decfsz count ;are all bits in yet? goto rxgo movf txreg,w ;put result into w return ;we don't wait for stop bits, no reason to rxgo bcf STATUS,C ;clear the carry bit always btfsc rx ;skip if rx is LOW bsf STATUS,C ;set the carry if rx was HIGH rrf txreg ;shift C >>>> into our holding reg goto rxwait ;go do the next bit ;we sense the bit's state in the ;middle of a bit time. That's why ;we did the half bit time delay. END