Code: Select all
#if _clock=8 'maximum USART rate=57600
OSCCON.6=1 'internal oscilator is at 8MHz
OSCCON.5=1
OSCCON.4=1
#elseif _clock=4 'maximum USART rate=19200
OSCCON.6=1 'internal oscilator is at 4MHz
OSCCON.5=1
OSCCON.4=0
#elseif _clock=2 'maximum USART rate=9600, Delayms is doubled
OSCCON.6=1 'internal oscilator is at 2MHz
OSCCON.5=0
OSCCON.4=1
#elseif _clock=1 'maximum USART rate=4800, Delayms is quadruple
OSCCON.6=1 'internal oscilator is at 1MHz
OSCCON.5=0
OSCCON.4=0
#endif
What I did not expect was that the USART would work so well, with 1mhz there were zero bad characters in several hundred recorded at the 4800 baud declared, I had expected for some reason that the actual baudrate would be different than the declared rate, but obviously that would only be the case for software serial routines.
I also wanted to see if I could work below 1mhz but the Clock declare will not accept decimals with a leading zero or a decmal point, although it was happy with decimals above 1.
With this in mind I added a test to my code above to see if it would work down at 32KHz:
Code: Select all
#elseif _clock=3.2
OSCCON.6=0 'internal oscilator is at 31.250 KHz
OSCCON.5=0
OSCCON.4=0