SF Observations

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Doj
Posts: 362
Joined: Wed Apr 11, 2007 10:18 pm
Location: East Sussex

SF Observations

Post by Doj » Tue Aug 28, 2007 3:55 pm

I am playing with the Preprocessor and found these interesting(to me!) observations.

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
The compiler recognises and works down to 1MHz and on testing the Delayms routines they showed, as expected, that below the minimum supported frequency(in this case 4mhz) the delays were realated in their extra length, ie at 2mhz they were twice as long, 1mhz 4 times as long.

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
I then did a test with Delayms to see if the routine would work accurately with the computed value for a 1 second delay of 4000000/31250=7.8 approx, rounded up to 8, a Delayms(8) did give a very good 1 second delay as hoped, great for running on li-poly cells at minimum quiescent current.

Post Reply