View previous topic :: View next topic |
Author |
Message |
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
Finding the Minimum frequency value that the PIC can generat |
Posted: Thu Mar 16, 2023 12:56 am |
|
|
Hello
CCS C 5.115
The code I use is below. The compiler gives a warning message when I add this line I was wondering about. How does he do this calculation? What equation can I use to get this value? So what is the equation that finds the minimum and maximum value it can measure?
Code: |
#include <18F57Q43.h>
#use delay(internal = 64MHz)
#use pwm(output=PIN_C0, TIMER=2, frequency=1Hz, duty=50, PWM_OFF)
WARNING:
________________
More info: PWM period: 2,05 ms, Frequency: 488,281 Hz, Resolution: 10,00 bits
|
_________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Thu Mar 16, 2023 3:24 am |
|
|
Key answer. Datasheet.....
The point is that the maximum division available from Timer2, is /(128 *256).
Now the timer by default is always sourced from FOSC/4. If you want to,
you can run it instead of the LFOSC, but you have to do this yourself.
It is telling you that from Fosc/4, the minimum frequency that it can
generate is 64000000/(4*256*128) = 488.28Hz.
Now there is a cheat way of running it instead off the LFINTOSC. This gives
31000(very nominal though), so a division of 128*242, would give your
1Hz. So if you use:
#use pwm(output=PIN_C0, TIMER=2, frequency=516Hz, duty=50, PWM_OFF)
The PWM division will be setup correctly (16000000/31000 = 516).
Then just change timer2 afterwards to clock off LFOSC, instead of Fosc/4.
setup_timer_2(T2_DIV_BY_128 ! T2_CLK_LFINTRC, 241,1);
This will give you a 1Hz PWM.
The 'point' of a PWM though is to generate a high frequency signal without
using processor resources. Honestly if you want a 1Hz PWM, you'd be much
better, (assuming the code does need some internal timing), to have
something like a 50Hz 'tick' interrupt, use this for your timings, and add
to this an output toggle every 25 interrupts. |
|
![](templates/subSilver/images/spacer.gif) |
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Thu Mar 16, 2023 5:06 am |
|
|
Yes, I checked, thank you. Well, the period formula used in an 8-bit processor is below.
Period = (Tosc x 4) x (TMR2 Prescale value) x (PR2 + 1)
Fosc/2 instead of just Fosc/4 on 16-bit processors. Is the rest calculation the same? For example, I cannot go below 488Hz with an 8-bit processor. But I'm assuming I'm using 140MHz on a 16-bit processor. Is there any problem then? dsPIC33EV256GM104
Period = (Tosc x 4) x (TMR2 Prescale value) x (PR2 + 1)
= (( 1/140Mhz)*4) x 256 * 65535 +1 = ~ 480 ms _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9168 Location: Greensville,Ontario
|
|
Posted: Thu Mar 16, 2023 5:46 am |
|
|
my option...
In the past, to generate a 1Hz, 50%DC signal, I just add a good RTC module.Aside from the obvious time keeping use,they can be used as a 1Hz interrupt.Nowadays most projects have an LCD so I use the RTC to trigger the screen refresh.
While it adds a small hardware cost, it works very well.
Perhaps you can use it ? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Thu Mar 16, 2023 6:20 am |
|
|
Your original post was for a 8bit processor, not a 16bit one.
On the 16bit processors everything changes. Timer2 is not involved at all.
These have separate PWM modules, and they normally have 16bit counters
not 8bit ones. There is a separate complete data sheet, just for the PWM
modules. DS70645. The maximum prescaler is only /64. However the PWM
is fed of Fosc, not Fosc/2 (it is not the 'peripheral' feed). So off the 140MHz,
the standard 16bit modules could give:
140000000/(64*65536) = 33.37Hz minimum.
The choice of Fosc, is because they want to allow you to select really fast
PWM's.
You can also generate a PWM, with the output compare modules. These
can be fed from different clocks, so you setup the clock for a particular
timer, and specify these to be fed off this. One option with this is to
setup a module in cascade mode, and then run the output compare
off the second timer here. Allows enormous divisions. |
|
![](templates/subSilver/images/spacer.gif) |
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Thu Mar 16, 2023 7:50 am |
|
|
The minimum frequency value I am trying to produce is 12.25Hz.
One more thing, MCUs pull almost 10mA current. Are there any MCUs that pull lower current? It is important that the supply is 5V. _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9168 Location: Greensville,Ontario
|
|
Posted: Thu Mar 16, 2023 8:35 am |
|
|
Probably, but a lot depends on what peripherals you need to have and operation speed, as well can the PIC be put into 'sleep' mode ?
Then there's the 'stuff' you add to the PIC, like pullups, LEDs, etc. All those can be 'tweaked' for lower current. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Thu Mar 16, 2023 8:39 am |
|
|
Just lower the clock speed.
The consumption is almost linearly connected to the clock rate.
You can do that PWM frequency using the output compare, but you'd have
to do the calculations yourself, not with #use pwm.
Run at 50MHz, and you could do that directly using the PWM, and your
power would be down below 5mA. |
|
![](templates/subSilver/images/spacer.gif) |
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Thu Mar 16, 2023 10:32 am |
|
|
How so, did I understand you right? If I do 50mhz, the current pull by the processor drops from 100mA to 5mA. Is it true? How do I access the current pull by a PIC in the datasheet. _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Thu Mar 16, 2023 10:55 am |
|
|
No. You said it drew "almost 10mA". It drops close to linearly. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9168 Location: Greensville,Ontario
|
|
Posted: Thu Mar 16, 2023 10:58 am |
|
|
For the PIC18F57Q43, section 47.3.2, 'electrical specs'..... page 890, for the PDF I just downloaded.
They're rated while PIC VDD is 3 volts, so will be more at 5.
Also all modules are enabled so if you can disable (shutdown) a module you'll save power.
There are dozens of ways to reduce current, overall energy, some in hardware, others in software, all depend on what you need the PIC to do. |
|
![](templates/subSilver/images/spacer.gif) |
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Thu Mar 16, 2023 1:35 pm |
|
|
I am generating frequency from 8 bit input. The maximum frequency value is 3.125khz. There is nothing else I do. How do I go about reducing the current it draws for such a device? Can you share a simple code example?
I am using internal 64MHz. Then, if I use an average of 5V calculated at 3V, for example, it draws about 10 or 15mA of current. Is it true? Is this current information now the current drawn by my processor? Or how can I measure it on the circuit. Can I read this value from compiler or processor? _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9168 Location: Greensville,Ontario
|
|
Posted: Thu Mar 16, 2023 2:29 pm |
|
|
two ways to read the actual current being drawn by the PIC 'project'
1) use a DMM, set to current mode, in series between power supply and the PIC
2) put a low ohm precision (1% or even better .1%) resistor in series and measure the voltage on both sides, divide the difference (the voltage drop) by the resistor value and that's the current. You can cut code to use the PIC's ADC to read the 2 voltages. You'll have to run some 'calibration' tests to confirm your hardware and software are accurate though. |
|
![](templates/subSilver/images/spacer.gif) |
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Fri Mar 17, 2023 1:05 am |
|
|
I connected the voltmeter in series with the power supply. 110mA is going through.
I want to generate frequency in the range of minimum 12.25hz to 3125hz with PWM. What path will I follow? What are you talking about using normal PWM? Is there any documentation about it? _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19358
|
|
Posted: Fri Mar 17, 2023 3:08 am |
|
|
What is the chip actually doing when you read this?.
Is this measured on the 5v supply, or a line feeding a regulator to give
the 5v?.
That is 3* what the chip itself should draw, just processing but operating
no outputs. Says that you have something attached to the chip creating
a load.
Remember if this is through a regulator, the regulator itself will draw power.
The datasheet is the key documentation for _everything_. |
|
![](templates/subSilver/images/spacer.gif) |
|