PDA

View Full Version : changing osc on the fly



viewgsm
- 3rd February 2010, 20:12
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

tenaja
- 3rd February 2010, 22:00
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.)

mackrackit
- 3rd February 2010, 22:29
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...

tenaja
- 3rd February 2010, 23:01
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.

rmteo
- 3rd February 2010, 23:53
....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.

http://home.earthlink.net/~rmteo/sitebuildercontent/sitebuilderpictures/a_69b.jpg


....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.

mackrackit
- 4th February 2010, 00:31
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...

rmteo
- 4th February 2010, 00:56
...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.

mackrackit
- 4th February 2010, 01:48
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 :rolleyes:

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.

rmteo
- 4th February 2010, 15:30
But I am not an engineer :rolleyes:

I look at the graph as showing the primary frequency.

Makes sense then. :o

Charles Linquis
- 5th February 2010, 01:46
If you need to run at 4Mhz on the internal OSC, put this in your 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.

Art
- 5th February 2010, 17:36
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.

rmteo
- 5th February 2010, 18:04
Wouldn't a delay need to know clock speed to work correctly?

Dave
- 5th February 2010, 21:46
Art, What would you use as a reference in that situation?

Dave Purola,
N8NTA

Bruce
- 5th February 2010, 23:48
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.

Art
- 5th February 2010, 23:54
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.

rmteo
- 6th February 2010, 00:43
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.

Art
- 6th February 2010, 01:45
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.

rmteo
- 6th February 2010, 02:10
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.

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.

Charles Linquis
- 6th February 2010, 03:30
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.

rmteo
- 6th February 2010, 03:37
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.

viewgsm
- 6th February 2010, 04:01
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

Art
- 6th February 2010, 08:30
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).

Charles Linquis
- 6th February 2010, 15:13
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.