CCS does not monitor this forum on a regular basis. Please do not post bug Reports on this forum. Send them to support@ccsinfo.com
Author Message
40inD

Joined: 30 Jul 2007
Posts: 106
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: 13269

 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: 106
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: 13269

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: 6157
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
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT - 6 Hours Page 1 of 1

 Jump to: Select a forum Software----------------General CCS C DiscussionCode LibraryEZ App Lynx Hardware----------------CCS ICD / Mach X / Load-n-Go
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum