changing osc on the fly


Closed Thread
Results 1 to 23 of 23
  1. #1
    Join Date
    Jun 2007
    Posts
    15

    Question changing osc on the fly

    hi all,

    I need to change the clock speed on the fly or dinamicly in the code ,

    i am using pic18f4550 and i need to switch from 48mhz to 24 or even 4 mhz to drop power consumtion in certin setuaion, i can change config bits, but i am using the uart (hserin) ,hserout, also sound and need them to adaopt to the new clock

    any ideas ?

    thank you

  2. #2
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    For the uart, just change baudcon (or its equivelant) to manually adjust the baud rates to maintain your clock speed. There are easy to read charts in the datasheets.

    In my apps, I set up a sub for each clock speed I run at, and then gosub the appropriate sub each time I need to change clock speed. Each subroutine sets the osccon, baudcon, and any other delay related variables so all of my routines behave the same regardless of the actual running speed.

    For timed delays, you will have to either adjust the parameter, or make your own delay routines that use your own parameters. (For instance, instead of the built in delayms you would use a gosub DelayCorrectedms, which loops and calls delayus with an adjusted time...keeping in mind the added overhead of the basic code.)

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I may be wrong on this but...

    Power consumption is relative to voltage. Clock speed is relative to voltage.
    Faster the clock, higher the voltage.

    But with this type of PIC it will run at 2 volts with a 4MHz external.
    So, using the PLL the PIC can "virtually" run at 48MHz on 2 volts and not consume extra power.

    ???

    I run this PIC at 3.3 volts and 48MHz but I have not measured the amps...
    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    I may be wrong on this but...

    Power consumption is relative to voltage. Clock speed is relative to voltage.
    Faster the clock, higher the voltage.

    But with this type of PIC it will run at 2 volts with a 4MHz external.
    So, using the PLL the PIC can "virtually" run at 48MHz on 2 volts and not consume extra power.

    ???

    I run this PIC at 3.3 volts and 48MHz but I have not measured the amps...
    That is all true... but you will save even more power by putting the pic to sleep. A a high speed pic that is sleeping most of the time will draw less power than a 4mhz pic awake all the time.

  5. #5
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    ....Clock speed is relative to voltage.
    Not quite. There are minimum voltages required to run at specific frequencies but there is not a direct relationship between the 2.



    Quote Originally Posted by mackrackit View Post
    ....Power consumption is relative to voltage.

    But with this type of PIC it will run at 2 volts with a 4MHz external.
    So, using the PLL the PIC can "virtually" run at 48MHz on 2 volts and not consume extra power.
    You cannot run it at 48Mhz with a 2V supply. With a PIC18F4550 supply current can range from 0.8mA (4MHz INTRC, 2.0V) to as high as 50mA (48MHz EC, 5.0V), a range of more than 62:1 - see section 28.2 DC Characteristics: Power-Down and Supply Current of the data sheet.

    Using one of the Power Managed (idle/sleep) modes is the proper way to go. See Section 3.0.

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rmteo View Post
    Not quite. There are minimum voltages required to run at specific frequencies but there is not a direct relationship between the 2.
    OK. Not a direct relationship. I should have said clock speed and minimum voltatge.
    You cannot run it at 48Mhz with a 2V supply. With a PIC18F4550 supply current can range from 0.8mA (4MHz INTRC, 2.0V) to as high as 50mA (48MHz EC, 5.0V), a range of more than 62:1 - see section 28.2 DC Characteristics: Power-Down and Supply Current of the data sheet.

    Using one of the Power Managed (idle/sleep) modes is the proper way to go. See Section 3.0.
    Maybe not at 2 volts, but 2.5 is doable. Using the PICKIT2 for a PS, only goes down to 2.5. So I can not verify the 2 volts right now...
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    ...Maybe not at 2 volts, but 2.5 is doable. Using the PICKIT2 for a PS, only goes down to 2.5. So I can not verify the 2 volts right now...
    It is poor engineering practice to operate a device outside of the manufacturer's specs - take a look at the graph above where it shows that the minimum voltage is 4.2V for frequencies >25Mhz.

  8. #8
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rmteo View Post
    It is poor engineering practice to operate a device outside of the manufacturer's specs - take a look at the graph above where it shows that the minimum voltage is 4.2V for frequencies >25Mhz.
    But I am not an engineer

    I look at the graph as showing the primary frequency.
    I have not built anything to run on less than 3.3 volts. I do a lot at 3.3 though. Have for some time. Makes working with SD cards and other low power things much easier. One power supply.
    Dave
    Always wear safety glasses while programming.

  9. #9
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    But I am not an engineer

    I look at the graph as showing the primary frequency.
    Makes sense then.

  10. #10
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    If you need to run at 4Mhz on the internal OSC, put this in your code.



    Code:
    ASM
           movlw 0x62                              ; %0110 0010  = 4 Mhz, internal osc block
           movwf OSCCON
           movlw 0x80                              ; %1000 0000  = PLL disabled
           movwf OSCTUNE
    ENDASM
    I have a development board that runs at 40Mhz on a 10Mhz XTAL + PLL, but sometimes I need to slow things down to 4Mhz and run on the internal oscillator to see how they will run on "little" parts. This code does the trick.
    Charles Linquist

  11. #11
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    I was thinking of a routine that could detect the clock speed the pic is running at
    so that then the pic program could adjust serial speed, etc accordingly.

    Make a delay loop in picbasic and count the number of ticks a hardware timer
    makes during the delay loop.. and then the hardware timer value will be
    different for different clock speeds.

  12. #12
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Wouldn't a delay need to know clock speed to work correctly?

  13. #13
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    Art, What would you use as a reference in that situation?

    Dave Purola,
    N8NTA

  14. #14
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    If you have a PIC that supports Internal/External Oscillator Switchover then you shouldn't need a firmware solution to determine osc speed. You already know what it should be after you write to OSCCON & flip the System Clock Select bit.

    If you have one running on the internal oscillator, and you need to calibrate it, then Microchip has a few app notes for this using TMR1 with an external 32.768kHz watch type crystal. Or you could just output the internal clock on OSC2/CLKO and test it with an O-scope.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  15. #15
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rmteo View Post
    Wouldn't a delay need to know clock speed to work correctly?
    No, because you use a timer based on RC clock, which is always the same,
    but the delay is dependent on osc speed.

    So, start timer, execute delay, read timer.

    The fact that the delay varies is what I'm counting on.

  16. #16
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Lets say you have a 4MHz clock and you set a delay of 1mS (1000us). A timer (without pre/post scaler) will count to 1000 for the duration of the delay.

    Now drop the clock the 1MHz. The timer will now count to 250 (1000/4) each mS. However, the delay will increase to 4mS due to the slower clock, so the timer will again count to 1000.

  17. #17
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    On the 16F877/876 for example, Timer0 can increment on the rising or falling edge of pin RA4/TOCKI
    That way you can use an RC osc to control TIMER0 so varying the OSC for the microcontroller will
    change the speed of the pause delay, but not the speed of TIMER0.
    Art.
    Last edited by Art; - 6th February 2010 at 01:52.

  18. #18
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Art View Post
    On the 16F877/876 for example, Timer0 can increment on the rising or falling edge of pin RA4/TOCKI
    That way you can use an RC osc to control TIMER0 so varying the OSC for the microcontroller will
    change the speed of the pause delay, but not the speed of TIMER0.
    Art.
    That is a little different from this.
    Quote Originally Posted by Art View Post
    Make a delay loop in picbasic and count the number of ticks a hardware timer
    makes during the delay loop.. and then the hardware timer value will be
    different for different clock speeds.

  19. #19
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    In some cases, you can read the configuration bits to determine if you are running on the INT OSC, and whether or not any dividers or PLLs are in use. If you have any kind of external oscillator or RS-232 input, you can figure things out pretty easily.
    Charles Linquist

  20. #20
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    With VDD = 5.0V, supply current of PIC18F4550 at 4MHz is typically 2.5mA (5.0mA MAX). In sleep mode it is 0.1uA (2.0us MAX). See table 28-2 of the datasheet.

    So in sleep mode, current consumption is 1/25000 of that of run mode at 4MHz. To reduce power consumption - the intent of the OP - it makes more sense to use the Power Managed Modes of the device, which is what they were designed to do.

    Any of the available interrupt sources can cause the device to exit from the Sleep mode to Run mode. To enable this functionality, an interrupt source must be enabled by setting its enable bit in one of the INTCON or PIE registers. The exit sequence is initiated when the corresponding interrupt flag bit is set. You can also exit Sleep mode by WDT time out.

  21. #21
    Join Date
    Jun 2007
    Posts
    15


    Did you find this post helpful? Yes | No

    Thumbs up thanks all

    thank you all for your help,, here is what i did,

    i ran the cpu on lower speed than the 48mhz needed for the USB, which did the trick without the need to switch the clock on the fly.

    thanks again

  22. #22
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rmteo View Post
    That is a little different from this.
    and then the hardware timer value will be
    different for different clock speeds.
    The hardware timer value you read back after the pause routine will be different for different clock speeds (of the microcontroller osc).

  23. #23
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    For all you that are contemplating a common body of code running on many different PICs , I have a project where I have to deal with this on a constant basis. For a long time, we built hardware with a 20Mhz 8720. Later, the hardware changed to a 40Mhz 8723. The speed increase was necessary for some new functionality (the 12 bit A/D was handy too), but we really didn't want to support two code bases. Some customers had both models, which meant the same code had to run seamlessly on both. The boards (obviously) have nearly the same functionality, but upgrades dictated that some pin changes (at least on the PIC - not the board I/O) had to change as well.

    We even had a few intermediate boards that were running with an 8723 at 20Mhz. These were built when the '8720 was becoming obsolete and before we had code that was modified to run at 40Mhz.

    Our code can tell some of the things about the board it is on by reading the state of a couple of pins on startup (no we didn't plan it that way, it just works out).

    The 20 Mhz units are running with a 20Mhz XTAL, and the 40Mhz units are running on a 10Mhz XTAL with the 4X PLL running.

    We read the config bits to see if the PLL is enabled and set a "40Mhz" flag.
    The config bits also tell us we are running on an 8723 or 8720.

    Since the code is the same in all boards, the DEFINES are the same. In our code, the OSC is DEFINEd to 20.

    Using information from the config bits, we made a table that set up things properly as far as timer reload values (to keep the timer interrupts at a constant rate), Baud rate values, PR2 (PWM registers, etc), A/D clock (/32 or /64). The A/D conversion routines also had to be changed to deal with the two extra bits on the '23.

    Finally, we made a large number of GOSUB DELAYX , which replaced all the PAUSE STATEMENTS. For example: The PAUSE 100 would be replaced with
    GOSUB DELAY100. DELAY100 called another routine that contained PAUSE X,
    where 'X' was either 100 (for 20Mhz) or 200 (for 40Mhz).

    It can be done.
    Charles Linquist

Similar Threads

  1. Define osc 1
    By ronsimpson in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 1st October 2009, 06:35
  2. Changing bits in a byte for multiple output
    By tazntex in forum Serial
    Replies: 3
    Last Post: - 11th August 2008, 19:10
  3. Replies: 4
    Last Post: - 16th May 2008, 14:35
  4. Changing declared variables names on the fly
    By jessey in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th December 2006, 06:34
  5. Mode changing on the fly
    By Angus Anderson in forum GSM
    Replies: 1
    Last Post: - 28th November 2006, 08:58

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts