Varying MCU clock speed - the easy way

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.
Post Reply
mnfisher
Valued Contributor
Posts: 1276
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 122 times
Been thanked: 643 times

Varying MCU clock speed - the easy way

Post by mnfisher »

I was experimenting with measuring the power consumption of a PIC MCU.

I wondered how this would vary with clock speed - and there is always a trade-off with more speed needing more power...

Using a PIC16F887 - the internal clock can be set at speeds up to 8MHz - but an external clock can be up to 20MHz,

I wrote a simple blinkie - and connected a pin to a logic analyser (with a 1ms delay in the loop)

In the chip options I set clock to XC - and I connected a function generator to pin OSC1 (and ground) I added a capacitor between OSC2 and ground - but this didn't seem to be necessary. I also set clock speed as 20MHz

I set the function generator to a sine wave at 20MHz and on turning the signal on got a neat set of pulses output, I could reduce the clock speed (and even overclock 24MHz worked for a handy 20% speed boost :-) Not recommended for use in the real world though!)

Setting the clock to 10Mhz - gave a 2ms pulse as expected - and the chip was happy at alll the clock speeds I tested (down to 32kHz)

I also tested a PIC12F1840 - and the best power savings are to be had by using sleep wherever possible, a low speed clock and reducing the voltage - and the PIC was using about 300nA when sleeping ! For battery applications this is good - however other times what is needed is speed and power consumption is less of a concern...

As most (many?) oscilloscopes have a signal generator - this can be fairly easy to implement and use...

Martin

chipfryer27
Valued Contributor
Posts: 1367
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 330 times
Been thanked: 475 times

Re: Varying MCU clock speed - the easy way

Post by chipfryer27 »

Hi Martin

You are starting to practise Dark Arts :)

You really can get drawn into getting a chip to consume almost nothing and clocks play a big part. Many chips can be configured to run at different speeds on-the-fly so you could set your chip to sleep using a very low power and frequrency oscillator (e.g. 32KHz) then when it is time to do something more fun have the chip use a higher speed more suited to the task in hand.

Peripherals also play a part and some need a clock during sleep, which can often be accommodated. Certain Timers can be configured to use an external crystal, thereby providiing a clock during sleep.

You have ruined Christmas for me as I was planning to do some similar tests over the period to what you have done, now no need :lol:

Regards

mnfisher
Valued Contributor
Posts: 1276
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 122 times
Been thanked: 643 times

Re: Varying MCU clock speed - the easy way

Post by mnfisher »

It's my Christmas gift to the world.

I call it the TimeSink (Pat Pending)

Which reminds me - Wacky Races.... See how powerful it is😎

Martin

alanwms
Posts: 118
Joined: Fri Dec 04, 2020 2:29 pm
Has thanked: 24 times
Been thanked: 7 times

Re: Varying MCU clock speed - the easy way

Post by alanwms »

Interesting speed versus consumption. I always wonder about speed, and if the clock is correctly set. My verification is to simply flash an LED with 1 second intervals. If it isn't flashing at 1 second intervals, the clock speed setting is incorrect. Kinda verifies for me.

mnfisher
Valued Contributor
Posts: 1276
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 122 times
Been thanked: 643 times

Re: Varying MCU clock speed - the easy way

Post by mnfisher »

The classic blinkie. Always good - especially when approaching a new MCU.

As Iain mentioned - it is possible to alter the clock speed on the fly. We were looking at battery powered options - so low power was a primary concern.

Martin

mnfisher
Valued Contributor
Posts: 1276
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 122 times
Been thanked: 643 times

Re: Varying MCU clock speed - the easy way

Post by mnfisher »

And - I found a small hitch with this...

Testing a PIC 12F1840 and it was quite happy to run off the clock signal. No power needed :-) Those batteries should last and last!

Martin

Post Reply