View previous topic :: View next topic |
Author |
Message |
40inD
Joined: 30 Jul 2007 Posts: 112 Location: Moscow, Russia
|
Calculating maximum duty value for set_pwm_duty() |
Posted: Fri Dec 08, 2017 1:08 am |
|
|
Is it possible to calculate at runtime the maximum value for set_pwmX_duty function using Fosc and timer settings? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Fri Dec 08, 2017 2:29 am |
|
|
It is the PR2 value plus one times four, minus 1. So if you have:
setup_timer_2(T2_DIV_BY_1, 199, 1);
The PR2 value is the 199.
Full on = ((199+1)*4)-1 = 799.
So 0 to 799. |
|
|
40inD
Joined: 30 Jul 2007 Posts: 112 Location: Moscow, Russia
|
|
Posted: Fri Dec 08, 2017 3:21 am |
|
|
How to get T2_DIV_BY_1 or T2_DIV_BY_4 or something else in program at runtime? I mean, the program needs to get timer settings, calculate max duty value and use it on it's own purposes. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Fri Dec 08, 2017 4:43 am |
|
|
They are just numbers.
There are only three typically /1, /4 & /16.
So something like:
Code: |
int1 select_ratios(int16 div_reqd)
{
int8 factor, prescale;
if (div_reqd>4096)
return FALSE; //can't do this division
if (div_req'd==0)
return FALSE; //can't do this division /0!...
if (div_reqd>1024)
{
prescale=T2_DIV_BY_16;
factor=div_reqd/16;
}
else if (div_reqd>256)
{
prescale=T2_DIV_BY_4;
factor=div_reqd/4;
}
else
{
prescale=T2_DIV_BY_1;
factor=div_reqd;
}
setup_timer_2(prescale,factor-1,1);
return(TRUE);
}
|
Obviously a lot of granularity, but shows the sort of thing needed. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Dec 08, 2017 6:51 am |
|
|
Those 'numbers' can be acquired by reading the appropriate register and then decoding the bits. Have a read of the datasheet for your PIC, look under the timer section, there will be a chart with what bits control what function. Within your program locate the register using GETENV , ead the 'timer control register' save into a variable ,then parse or decode the bits.
I leave the actual coding for you to do, that way you'll get first hand knowledge !
Jay |
|
|
|