My Problems using the serial port on the PIC

I ran into very time-consuming difficulties using the serial port on the PIC, which ended up being solved by a relatively simple exchange (thanks to Cindy Jeffers for the idea). When I first hooked up my PIC serial port, I was getting gibberish. Garbled text was being spat out, but it was predictable gibberish, so it was as if it was running through a translator. The problem ended up being with the clock. Apparently, the using a 20 MHz crystal on the PIC does not produce a reliable enough clock to make serial communications work properly. I fixed it by using a 4 MHz crystal, but the ideal way would be to use a 'powered oscillator'. Tom Igoe has a little blurb about it on his site.

Code

I ended up working with Cindy Jeffers' serial test program, which was written in PICBasic:

include "modedefs.bas"

define osc 4

'variable to receive data   
inputData var byte 

input portc.7
output porta.0
output porta.1
output porta.2
output porta.3
output portb.0
output portb.7

aVar var byte
aVar = "a"      '97

bVar var byte
bVar = "b"      '98

low porta.1
low porta.2
low porta.3
low porta.4
high portb.7

main:
    serout portc.6, n9600, ["            key please: "]       
    serin portc.7, n9600, inputData   
    if inputData = aVar then
        high porta.0
        high portb.0
        low porta.1
    else
        high porta.1
        low porta.0
        low portb.0
    endif
    
    serout portc.6, n9600, ["your key: "]       
    serout portc.6, n9600, [inputData]     
goto main   


      

Links

Tom Igoe on Powered Oscillators
Test program for PIC serial port, written in PICBasic