16F876 RS-232 working as intended

The heart and soul of my airsoft turret project will be getting the PC to send serial data to the microcontroller that will control the pan, tilt, and rate of fire of the airsoft gun.  Today I successfully setup the 16F876 for async communication with my PC.

The code below configures porta for digital I/O and configures the USART for 19200 continuous async communication.  The test program monitors for a recieved byte and echoes it back to the reciever.

;
;
;
;
list p=16F876
include p16f876.inc

RX_Word equ 23
TW_Word equ 24
dataL equ 25

org 00
;initialize
start
bsf status,rp0
bcf status,rp1
;configure Porta pins as digital I/O
movlw 0x06
movwf adcon1
;configure porta as outputs
movlw B’00000000′
movwf trisa
;configure USART in async
bcf status,rp0
bcf status,rp1
movlw B’10010000′  ;async channel:  port is on, 8 bit transfer
movwf rcsta   ;continuous recieving, no address detect
bsf status,rp0
bcf status,rp1
movlw B’00100100′  ;setup async channel:  transmit enable, 8 bit
movwf txsta   ;high speed baud rate
movlw 0x0c   ;setup baudrate of 19200 with 4 meg crystal
movwf spbrg
bcf status,rp0
bcf status,rp1
;program start
main
clrf dataL
settle
decfsz dataL,F
goto settle

movf rcreg,w
movf rcreg,w
movf rcreg,w
loop call recieve
movwf txreg
goto loop

recieve btfss pir1,rcif
goto recieve
movf rcreg,w
return

transwt bsf status,rp0
wthere btfss txsta,trmt
goto wthere

clrf status
return
end

Sadly Microsoft doesn’t include hyperterminal with Vista or Win7.  To test communications I downloaded and used Realterm. It’s a free application and more then sufficient for testing and diagnostic purposes.

2 thoughts on “16F876 RS-232 working as intended

Leave a Reply

Your email address will not be published. Required fields are marked *