Searching \ for '[PIC]: F628 Asnyc Serial Input' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/microchip/ios.htm?key=serial
Search entire site for: 'F628 Asnyc Serial Input'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: F628 Asnyc Serial Input'
2002\02\18@133332 by Royce Simmons

picon face
Greetings All,

Does anyone have code for a F628 serial input routine?  I am having problems
making this
work.  I suppose lack of skill is the problem but I am confused with all the
register options.

Thanks,  Royce

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\02\18@155808 by Jinx

face picon face
This is a test of two routines that went into a security system.
Commenting's not too bad, some LCD delays not optimised
but it works OK. It was written in MPLAB and will compile. I
put the routines into a larger program and worked on them
there, it would be difficult to extract them now. And of course
it looks much prettier in Courier

==============================================

;rx_disp.asm
;get RS232 from PC, display
;Joe Colquitt, 5th November 2001

        list p=16F628
        #include <p16F628.inc>

;3.686400 MHz crystal. LCD Timing loops originally for 10MHz
;
;Baud rate = 9600 = 3686400/(64 x (5+1))
;
;Receive 10 bytes from PC and display on LCD screen
;
;LCD is Citizen 20 x 2 alphanumeric
;line 1 address = #00 +high bit = #80
;line 2 address = #40 +high bit = #c0
;
;;LCD pinout, 14-pin header - 1 Vss  2 V+
;                             3 Vlc  4 RS
;                             5 RW   6 E
;                             7 d0   8 d1
;                             9 d2  10 d3
;                            11 d4  12 d5
;                            13 d6  14 d7
;
;Porta.0  LCD d0
;      1      d1
;      2      d2
;      3      d3
;
;      4  LCD Register Select
;
;Portb.0  LCD Enable
;      1  USART Rx
;      2  USART Tx
;      3  LCD Read/Write
;
;      4  LCD d4
;      5      d5
;      6      d6
;      7      d7

rs     equ 0x04
en     equ 0x00
rw     equ 0x03

start  equ 0x00       ;program start vector
ram    equ 0x20       ;start of RAM

index  equ ram+0
cnt1   equ ram+1
cnt2   equ ram+2
cnt3   equ ram+3
cnt4   equ ram+4
endlp  equ 0x04       ;data fetch loop limit flag
temp1  equ ram+5
temp2  equ ram+6
rxhold equ ram+7
cksum  equ ram+8      ;checksum for transmitted data
fsrc   equ ram+9      ;fsr copy
dram   equ ram+0x0a   ;storage for transmitted data


bflag  equ 0x07       ;LCD busy flag

;LCD initialisation values         38h
;display on, no cursor blink       0ch
;entry mode - increment, no shift  06h

      __config _xt_osc & _wdt_off & _pwrte_on & _lvp_off & _boden_off

        org 0x00
        goto entry

entry    clrf   porta        ;...o oooo
        movlw  0x07
        movwf  cmcon
        clrf   status       ;set rp0, rp1 = 0 = Bank0

; Boot Baud Rate = 9600, No Parity, 1 Stop Bit

        bsf    status,rp0
        movlw  0x05         ;9600 baud @ 3.686400MHz
        movwf  spbrg
        movlw  b'00100000'  ;brgh = 0
        movwf  txsta        ;Async Transmission, set brgh
        movlw  b'10010000'  ;Async Reception
        clrf   status       ;back to page 0
        movwf  rcsta

        bsf    status,rp0
        bcf    status,rp1
        movlw  0x00
        movwf  trisa

        movlw  0x02         ;oooo ooio
        movwf  trisb

        movlw  b'00000010'  ; RX Lo, TX Hi
        movwf  portb

        movlw  0x80         ;portb pull-ups off (bit7)
        movwf  option_reg

        bcf    status,rp0
        bcf    status,rp1

        bcf    portb,en     ;clear control lines
        bcf    portb,rw
        bcf    porta,rs


d4d5     movlw  0x30         ;power-up value on data lines
        call   dataout
        nop
        bsf    portb,en     ;and write it to LCD
        nop
        nop
        nop
        bcf    portb,en
        movlw  0xc0         ;call 64 x 100us
        movwf  cnt1

        call   altin2
        incf   index,f
        movlw  0x02         ;loop for total of 3 writes
        xorwf  index,w
        btfss  status,z
        goto   d4d5

format   clrf   index        ;get LCD initialisation values
floop    nop
        nop
        movlw  0x02         ;get pattern from $02xx table
        movwf  pclath
        movf   index,w
        call   lcdinit      ;fetch data into W
        call   dataout
        nop
        bsf    portb,en     ;and write them to LCD
        nop
        nop
        nop
        bcf    portb,en
        nop
        call   busy         ;test Busy Flag
        incf   index,f
        movlw  0x03
        xorwf  index,w
        btfss  status,z
        goto   floop

getdata  movlw  0x48         ;set LCD address
        call   address
        clrf   index
        clrf   cksum
        movlw  0x29         ;base address for data storage
        movwf  fsr
        call   receive      ;get 20 bytes to display from PC

display  movlw  0x00         ;line 1, column 0
        call   address
        clrf   index
        movlw  0x29         ;set retrieval to base address
        movwf  fsr

line1a   movf   indf,w       ;get data byte from ram
        call   write
        incf   fsr,f        ;bump ram address
        incf   index,f
        movlw  0x06         ;check limit
        xorwf  index,w
        btfss  status,z
        goto   line1a

        movlw  0x20         ;space after 6 characters
        call   write

line1b   movf   indf,w
        call   write
        incf   fsr,f
        incf   index,f
        movlw  0x0d
        xorwf  index,w
        btfss  status,z
        goto   line1b

        movlw  0x20          ;space after 13 characters
        call   write

line1c   movf   indf,w
        call   write
        incf   fsr,f
        incf   index,f
        movlw  0x10
        xorwf  index,w
        btfss  status,z
        goto   line1c

        movlw  0x40
        call   address       ;line 2, column 0
        clrf   index         ;print "Pin# "
pinloop  movlw  0x02          ;get data from 0x2xx bank
        movwf  pclath
        movf   index,w
        call   pin
        call   write
        incf   index,f
        movlw  0x05
        xorwf  index,w
        btfss  status,z
        goto   pinloop

        clrf   index
line2    movf   indf,w
        call   write
        incf   fsr,f
        incf   index,f
        movlw  0x04
        xorwf  index,w
        btfss  status,z
        goto   line2

wait     goto   wait

receive  nop
        btfss  pir1,rcif    ;check "Rx full" IRQ bit
        goto   receive
        movf   rcreg,w
        movwf  indf         ;store in data ram
        addwf  cksum,f      ;add to checksum
        incf   fsr,f        ;bump counter
        incf   index,f
        movlw  0x14         ;count 20 bytes
        xorwf  index,w
        btfss  status,z
        goto   receive

        movf   cksum,w      ;send checksum back to PC
        movwf  txreg
txwait   bsf    status,rp0
        btfss  txsta,trmt
        goto   txwait
        bcf    status,rp0
        return

address  addlw  0x80         ;set high bit of address command
        call   dataout      ;put address on data lines
        nop
        bsf    portb,en     ;write it to LCD
        nop
        bcf    portb,en
        call   busy
        return

write    bsf    porta,rs     ;set RS for data write
        call   dataout
        bsf    portb,en     ;write it to LCD
        nop
        bcf    portb,en
        nop
        bcf    porta,rs
        nop
        call   busy
        nop
        return

point1   movlw  0x64         ;call 1ms routine 100 times
altin1   movwf  cnt3         ;(or enter here with W)
p1delay  call   msdelay
        decfsz cnt3,f
        goto   p1delay
        return

msdelay  movlw  0xf6         ;enter here for 1ms
        movwf  cnt1
altin2   clrf   cnt2         ;here for mults of 100us
inc      incfsz cnt2,f
        goto   inc
        incfsz cnt1,f
        goto   inc
        return

busy     bsf    portb,rw     ;set RW for Read
        nop
        call   msdelay
        bcf    portb,rw
        return

        bsf    status,rp0
        bcf    status,rp1
        movlw  0x80         ;make b7 an input
        movwf  trisb
        bcf    status,rp0
        bcf    status,rp1

        bsf    portb,en
rdbusy   btfsc  portb,bflag  ;loop until Busy Flag clear
        goto   rdbusy
        bcf    portb,en
        nop
        bcf    portb,rw

        bsf    status,rp0
        bcf    status,rp1
        movlw  0x00         ;b7 as output again
        movwf  trisb
        bcf    status,rp0
        bcf    status,rp1

        return

dataout  movwf  temp1        ;copy of data to send
        andlw  0x0f         ;mask out upper nybble, porta data in W
        movwf  temp2        ;copy
        movf   porta,w
        andlw  0xf0         ;clear porta lower nybble
        addwf  temp2,w
        movwf  porta        ;put data into porta lower nybble

        movf   temp1,w      ;get original
        andlw  0xf0         ;mask out lower nybble, portb data in W
        movwf  temp2        ;copy
        movf   portb,w
        andlw  0x0f         ;clear portb upper nybble
        addwf  temp2,w
        movwf  portb        ;put data into portb upper nybble
        return

org 200h

lcdinit  addwf  pcl,f
        retlw  0x38
        retlw  0x0c
        retlw  0x06

pin      addwf  pcl,f
        retlw  0x50         ;"PIN# "
        retlw  0x69
        retlw  0x6e
        retlw  0x23
        retlw  0x20

        end

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\02\18@161635 by Jinx

face picon face
Just a little fixer-upper in the previous post. The note at the
top says it's to receive 10 characters, but in fact the program
you see is set to receive 20. I was testing the LCD, not the
receive function

The program as it is now is back to the original 10 characters
in a 6 + 4 format. In case you were wondering

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\02\18@164018 by Tony Nixon

flavicon
picon face
Royce Simmons wrote:
>
> Greetings All,
>
> Does anyone have code for a F628 serial input routine?  I am having problems
> making this
> work.  I suppose lack of skill is the problem but I am confused with all the
> register options.
>
> Thanks,  Royce
>
> --
> http://www.piclist.com hint: The PICList is archived three different
> ways.  See http://www.piclist.com/#archives for details.

Bit Banged 9600, 1 stop, no parity.

#Define SkipIfRxLow     btfsc PORTB,RX
#Define Tx_Low          bcf PORTB,TX
#Define Tx_High         bsf PORTB,TX

;
; ---------------
; RECEIVE ROUTINE
; ---------------
;
RxRoutine       SkipIfRxLow             ; wait for start bit
       goto RxRoutine

       call HBDelay2           ; wait 1/2 bit length - 2 cycles
       movlw 9h
       movwf BCount

RxLoop  SkipIfRxLow             ; start bit is lost during routine
       goto RxHiBit

       bcf STATUS,C            ; receive start bit and 8 data bits
       goto RxLoBit

RxHiBit bsf STATUS,C
       nop
RxLoBit rrf RxHold,F
       call HBDelay4           ; wait 1/2 bit length - 4 cycles
       call HBDelay5           ; wait 1/2 bit length - 5 cycles
       decfsz BCount,F
       goto RxLoop

       movf RxHold,W
       return                  ; data returned in W Reg
;
; ----------------
; TRANSMIT ROUTINE
; ----------------
;
TxRoutine       movwf TxSend            ; temp store data to be sent
       Tx_Low                  ; do start bit
       call HBDelay5
       call HBDelay2

       movlw 8h                        ; 8 data bits to send
       movwf BCount

TxLoop  rrf TxSend,F
       btfsc STATUS,C
       goto TxHiBit

       nop
       Tx_Low
       goto DoneBit

TxHiBit Tx_High
       goto $ + 1

DoneBit call HBDelay5           ; wait 1 bit length
       call HBDelay5
       decfsz BCount,F
       goto TxLoop

       call DoRet
       nop
       Tx_High                 ; stop bit
       call HBDelay1
       call HBDelay
DoRet   return
;
; ----------------------------
; HALF BIT DELAY FOR 9600 BAUD
; ----------------------------
;
HBDelay nop
HBDelay1        nop
HBDelay2        goto $ + 1
HBDelay4        nop
HBDelay5        movlw 0x0D
       movwf BitWait
BW      decfsz BitWait,F
       goto BW
       nop
       nop
       nop
       return

--
Best regards

Tony

mICros
http://www.bubblesoftonline.com
spam_OUTsalesTakeThisOuTspambubblesoftonline.com

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\02\18@165311 by Tony Nixon

flavicon
picon face
Royce Simmons wrote:
>
> Greetings All,
>
> Does anyone have code for a F628 serial input routine?  I am having problems
> making this
> work.  I suppose lack of skill is the problem but I am confused with all the
> register options.

I just thought you might be using the UART, so try this code...


;
; SETUP PORTS ETC .....
;

;
; -----------------------
; DISABLE THE COMPARATORS
; -----------------------
;
       movlw b'00000111'
       movwf CMCON
;
; ------------------------------------
; SET BAUD RATE TO COMMUNICATE WITH PC
; ------------------------------------
; Boot Baud Rate = 9600, No Parity, 1 Stop Bit
; @ 4MHz
;
       bsf STATUS,RP0      ; RAM Page 1
       movlw d'25'         ; 9600 baud
       movwf SPBRG
       movlw b'00100100'   ; brgh = high (2)
       movwf TXSTA         ; enable Async Transmission, set brgh
       movlw b'10010000'   ; enable Async Reception
       bsf STATUS,RP0      ; RAM Page 0
       movwf RCSTA

       call DelayWt        ; waits a few mS to stabilise
;
; GET CHARACTER FROM PC (or whatever)
;
MainLoop call Receive       ; wait for data -> W
        movwf TXREG        ; echo it back
        goto MainLoop

;
; ----------------------------------------
; RECEIVE CHARACTER FROM RS232 OR INTERNAL
; ----------------------------------------
; This routine does not return until a character is received.

Receive   nop
         btfss PIR1,RCIF   ; check for received data
         goto Receive

         movf RCREG,W
         movwf RxHold      ; tempstore data
         return




--
Best regards

Tony

mICros
http://www.bubblesoftonline.com
.....salesKILLspamspam@spam@bubblesoftonline.com

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\02\18@203112 by Royce Simmons

picon face
Thanks to all that replied to my request.

Regards,  Royce

----- Original Message -----
From: Tony Nixon <Tony.NixonspamKILLspamENG.MONASH.EDU.AU>
To: <.....PICLISTKILLspamspam.....MITVMA.MIT.EDU>
Sent: Monday, February 18, 2002 4:38 PM
Subject: Re: [PIC]: F628 Asnyc Serial Input


> Royce Simmons wrote:
> >
> > Greetings All,
> >
> > Does anyone have code for a F628 serial input routine?  I am having
problems
> > making this
> > work.  I suppose lack of skill is the problem but I am confused with all
the
{Quote hidden}

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\02\19@183933 by Larry Kayser

flavicon
face
part 1 298 bytes content-type:text/plain; charset="us-ascii"; format=flowedAt 01:31 PM 2002/02/18 -0500, you wrote:
>Greetings All,
>
>Does anyone have code for a F628 serial input routine?  I am having problems
>making this
>work.  I suppose lack of skill is the problem but I am confused with all the
>register options.

This works, every time.

Larry


part 2 12910 bytes content-type:text/plain; charset="us-ascii"
;******************************************************************************
;kayserspamspam_OUTsympatico.ca
       LIST    p=16F628        ;PICF628 is the target processor
;
       #include        "P16F628.INC"   ;Include header file
;
       ErrorLevel      0       ;0 for Messages, Warnings, Errors
       ErrorLevel      -302    ;1 for Warnings, Errors
                               ;2 for Errors
                               ;-302 to suppress Page Message
                               ;
;enable _hs_osc, _pwrte_on, when programming the device
;******************************************************************************
               cblock  h'20'   ;bank 0  h'20' to h'7f'. 96 locations
               savew1  ;SAVEW1 *MUST* be at location h'20'!
               savestatus
               savepclath
               savefsr
               rx_data         ;the received byte from the serial UART
               tx_data         ;byte to be transmitted via UART
               endc
;******************************************************************************
               cblock  h'A0' ;bank 1
               savew2          ;SAVEW2 *MUST* be at location h'A0'.
               endc
;
       org     h'0000'         ;set code origin to beginning of rom start
       goto    initialize      ;we must get past interrupt vector at 0004
;
;INTERRUPT ROUTINE

       org     h'0004'         ;interrupt vector location
inthandler
       movwf   savew1          ;save w register! (at h'20', h'A0', etc.)
       movf    status,w                ;w now has copy of status
       clrf    status          ;ensure we are in bank 0 now!
       movwf   savestatus              ;save status
       movf    pclath,w                ;save pclath
       movwf   savepclath
       clrf    pclath          ;explicitly select Page 0
;
       movf    fsr,w
       movwf   savefsr         ;save fsr (just in case)
;
vector_to_interrupt
;
       btfsc   intcon,t0if             ;test to see which interrupt
       goto    service_t0if    ;needs servicing...
;
       btfsc   intcon,intf             ;there can be many different sources
       goto    service_intf    ;of interrupt...
                                       ;add as many checks here as you
                                       ;have possible interrupt sources....
;
service_t0if
       nop                             ;!!! or do something useful here...
;
t0if_done
               bcf     intcon,t0if             ;clear interrupt flag that caused interrupt.
               goto    intclean                ;restore and return from interrupt!
;
service_intf
               nop                     ;!!! or do something useful here...
;
intf_done       bcf     intcon,intf     ;clear flag that caused interrupt.
               goto    intclean        ;restore and return from interrupt!
;
intclean
               movf    savefsr,w
               movwf   fsr             ;restore fsr
;
               movf    savepclath,w
               movwf   pclath          ;restore pclath. (Page=original)
;
               movf    savestatus,w
               movwf   status          ;restore status! (bank=original)
;
               swapf   savew1,f        ;restore w from *original* bank!
               swapf   savew1,w        ;swapf does not affect any flags!
;
               retfie                  ;return from interrupt!
                                       ;gie is auto-re-enabled.
;
initialize                              ;initialize ports and registers

       bcf     status,rp0              ;first do page 0 stuff. yep, page 0

gie01   bcf     intcon,gie              ;turn gie off
       btfsc   intcon,gie              ;MicroChip recommends this check!
       goto    gie01                   ;without this check
                                       ;you are not sure gie is cleared!
       clrf    pir1            ;clear peripheral flags
;
       clrf    porta           ;clear all i/o registers...
       clrf    portb
;
       bsf     status,rp0      ;allow access to page 1 stuff!
;
       clrf    pie1            ;disable peripheral interrupts
;
       movlw   b'00000110'
       movwf   trisb           ;0=output  1=input
                               ; BUT for UART use RB1 and RB2 MUST be programmed as an INPUT!
;
       bsf     option_reg, not_rbpu    ;!rbpu! rb_pullup 0=enabled
                               ; 1=disabled. enabling is based on individual
                               ;port-latch values. currently pullups are
                               ;disabled.
       bsf     option_reg, intedg      ;intedg 0=inc on falling 1=inc on
                               ; rising edge. <<note: intedg and t0se use
                               ; opposite definitions!>>
                               ;currently set for rising edge detection.
       bcf     option_reg, t0cs        ;t0cs timer0clocksource 0=internal clkout
                               ;1=ra4/int. currently set for internal clkout

       bcf     option_reg, t0se        ;t0se timer0signaledge 0=inc on rising 1=inc
                               ; on falling edge.
                               ; <<note: intedg and t0se use opposite
                               ; definition!>>
       bcf     option_reg, psa ;psa prescaler assignment 0=tmr0 1=wdt
                               ;ps2-ps0 determine prescaler rate, which is
                               ;dependent also on whether tmr0 or wdt is
                               ;selected:
                       ;wdt from 0-7 is div by 1 2 4 8 16 32 64 128
                       ;tmr0 from 0-7 is div by 2 4 8 16 32 64 128 256
                       ;if wdt is assigned prescaler, then tmr0 is div by 1
                       ; here we will set prescaler to divide by 16 for tmr0
                       ; !!! set this any way you want.
                       ; This is just an example that works.
       bcf     option_reg,ps2  ;ps2
       bsf     option_reg,ps1  ;ps1
       bsf     option_reg,ps0  ;ps0

;intcon register: bit assignments
;
;enables... 1=enable 0=disable
;<7>=gie=global_int_enable
;<6>=peie=peripheral_int_enable
;<5>=t0ie=t0_int_enable (enables <2> t0if)
;<4>=inte=int_enable (rb0/int) (enables <1> intf)
;<3>=rbie=rb_int_enable (enables <0> rbif)
;
;intcon flags. software reset. 0=reset 1=flagged
;<2>=t0if=t0_int_flag
;<1>=intf=int_flag (rb0/int)
;<0>=rbif=rb_int_flag (rb7-rb4)

       clrf    intcon          ;in this example we have no interrupts used.
;
;pie1 peripheral interrupt enable 1 register:
; bit assignments. 1=enable  0=disable
;
;<7>=pspie=parallel_slave_port_int_enable
;<6>=adie=a/d_int_enable
;<5>=rcie=receiver_int_enable for uart (may use later)
;<4>=txie=transmit_int_enable for uart (may use later)
;<3>=sspie=sync_serial_int_enable
;<2>=ccp1ie=ccp1_int_enable
;<1>=tmr2ie=timer2_int_enable
;<0>=tmr1ie=timer1_int_enable
;
       clrf    pie1            ;no interrupts used in this example
;
;uart specific initialization
                               ;txsta=Transmit STAtus and control register.
                               ;take nothing for granted.
       bcf     txsta,csrc      ; <7> (0) don't care in asynch mode
       bcf     txsta,tx9       ; <6>  0  select 8 bit mode
       bsf     txsta,txen      ; <5>  1  enable transmit function
                               ;      *MUST* be 1 for transmit to work!!!
       bcf     txsta,sync      ; <4>  0 asynchronous mode.
                               ;      *MUST* be 0 !!!
                               ;      If NOT 0 the async mode is NOT selected!
                               ; <3>  (0) not implemented
       bsf     txsta,brgh      ; <2>  0 disable high baud rate generator !!!
                               ; lsk for 16F628
                               ; 1    (0) trmt is read only.
       bcf     txsta,tx9d      ; <0>  (0)  tx9d data cleared to 0.
;
;   For brgh=0       baudrate=Fosc/(64(spbrg+1))
;   So when brgh=0   spbrg_value = (xtal_freq/(baudrate*d'64'))-1

;   For brgh=1       baudrate=Fosc/(16(spbrg+1))
;   So when brgh=1   spbrg_value = (xtal_freq/(baudrate*d'16'))-1
;
xtal_freq       =       d'4000000'      ;crystal frequency in Hertz.
baudrate        =       d'19200'        ;desired baudrate.
;                               ;now calculate spbrg_value...
;spbrg_value    =       (xtal_freq/(baudrate*d'64'))-1
spbrg_value             =       (xtal_freq/(baudrate*d'16'))-1

;
       movlw   spbrg_value     ;set baud rate generator value
       movwf   spbrg
;
       bcf     status,rp0      ;allow access to page 0 stuff again. (normal)
;
;more uart specific initialization
;
                               ;rcsta=ReCeive STAtus and control register
;
       bsf     rcsta,spen      ; 7 spen 1=rx/tx set for serial uart mode
                               ;   !!! very important to set spen=1
       bcf     rcsta,rx9       ; 6 rc8/9 0=8 bit mode
       bcf     rcsta,sren      ; 5 sren 0=don't care in uart mode
       bsf     rcsta,cren      ; 4 cren 1=enable constant reception
                               ;!!! (and low clears errors)
                               ; 3 not used / 0 / don't care
       bcf     rcsta,ferr      ; 2 ferr input framing error bit. 1=error
                               ; 1 oerr input overrun error bit. 1=error
                               ;!!! (reset oerr by neg pulse clearing cren)
                               ;you can't clear this bit by using bcf.
                               ;It is only cleared when you pulse cren low.
       bcf     rcsta,rx9d      ; 0 rx9d input (9th data bit). ignore.
;
;If you are using a MAX232 that uses
;charge pumping, put a delay routine
;right HERE, a few seconds
;
;we need to initialize some things, so do it here.
;
       movf    rcreg,w         ;clear uart receiver
       movf    rcreg,w         ; including fifo
       movf    rcreg,w         ; which is three deep.
;
       movlw   0               ;any character will do.
       movwf   txreg           ;send out dummy character
                               ; to get transmit flag valid!
;
main
       bsf     intcon,gie      ;enable interrupts if you are using any!
;
loop
       call    ser_in          ;get UART input into W and rx_data
       call    transmitw       ;send W to the UART transmitter
       goto    loop            ;blithely echo characters forever...
;*****************************************************************
;SER_IN
ser_in
       btfsc   rcsta,oerr
       goto    overerror       ;if overflow error...
       btfsc   rcsta,ferr
       goto    frameerror      ;if framing error...
uart_ready
       btfss   pir1,rcif
       goto    ser_in          ;if not ready, wait...
;
uart_gotit
       bcf     intcon,gie      ;turn gie off.
       btfsc   intcon,gie
       goto    uart_gotit
;
       movf    rcreg,w         ;recover uart data
       bsf     intcon,gie      ;re-enable interrupts!!
       movwf   rx_data         ;save for later
       return
;
overerror
       bcf     intcon,gie      ;turn gie off.
       btfsc   intcon,gie      ;
       goto    overerror       ;
;
       bcf     rcsta,cren      ;pulse cren off...
       movf    rcreg,w         ;flush fifo
       movf    rcreg,w         ; all three elements.
       movf    rcreg,w
       bsf     rcsta,cren      ;turn cren back on.
                               ;this pulsing of cren
                               ;will clear the oerr flag.
       bsf     intcon,gie      ;enable interrupts.
       goto    ser_in          ;try again...
;
frameerror
       bcf     intcon,gie      ;turn gie off.
       btfsc   intcon,gie      ;
       goto    frameerror
;
       movf    rcreg,w         ;reading rcreg clears ferr flag.
       bsf     intcon,gie      ;enable interrupts.
       goto    ser_in          ;try again...
;
;TRANSMIT subroutine:
transmit
       movf    tx_data,w       ;copy tx_data to w.
transmitw
       btfss   pir1,txif
       goto    transmitw       ;wait for transmitter interrupt flag
gietx   bcf     intcon,gie      ;disable interrupts
       btfsc   intcon,gie      ;making SURE they are disabled!
       goto    gietx
       movwf   txreg           ;load data to be sent...
       bsf     intcon,gie      ;re-enable interrupts
       return
;
TransWt                 ; use to ensure that TX buffer is empty
       bsf     STATUS,RP0
TxWt    btfss TXSTA,TRMT        ; transmission is complete if hi
       goto    TxWt
       clrf    STATUS  ; RAM Page 0
       return
;
;----------------------------------------------------------------------
       end


part 3 136 bytes
--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email @spam@listservKILLspamspammitvma.mit.edu with SET PICList DIGEST in the body


More... (looser matching)
- Last day of these posts
- In 2002 , 2003 only
- Today
- New search...