Hi All,
I have never used the USART of the PIC 16F877A and I used to use Serin2 in a loop to check for packets of 8 bytes which came to control a PTZ video camera. As the pic had other things to do you can imagine it missed messages from time to time.
I tried using the sample program USART.bas but when I connected the RX to the serial program of Microcode Studio and sent single bytes out and then wrote a serin2 command to send rcreg back, the value was always different and many times the same value arrived back when I entered a different one.
I was sending my data to the RX via com1 and receiving using serout2 via com2.
This was my code:
@ device hs_OSC, wdt_on, pwrt_on, protect_off
'OPEN USART
define osc 20
Include "modedefs.bas"
RCSTA = %10010000 ' Enable USART receive
SPBRG = 31 ' Set baud rate to 4800 (20MHz clock)
INTCON = %11000000
PIE1.5=1
trisc.6=0
TRISC.7=1
TRISA.0=0
test var porta.0
counter var word
BAUD CON 16390
loop:
on interrupt goto myroutine
if counter= 5000 THEN 'just to keep the pic busy to make sure the
toggle portd.0 ' interrupts are working.
COUNTER=0
else
counter=counter+1
endif
INTCON = %11000000
goto loop
goto loop
'********************************************
disable
myroutine:
if PIR1.5=1 then
serout2 test,BAUD,[dec rcreg,10,13] 'send the value to com2
serout2 test,BAUD,["PIR1.5=",BIN PIR1.5,10,13]
IF RCSTA.1=1 THEN
serout2 test,BAUD,["BUFFER OVERRUN",10,13] 'Just to see if overflow occured
ENDIF
endif
resume
enable
'********************************************
end
Best Regards
Christopher in Santiago
Bookmarks