CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

Calculating maximum duty value for set_pwm_duty()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
40inD



Joined: 30 Jul 2007
Posts: 106
Location: Moscow, Russia

View user's profile Send private message

Calculating maximum duty value for set_pwm_duty()
PostPosted: Fri Dec 08, 2017 1:08 am     Reply with quote

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: 12647

View user's profile Send private message

PostPosted: Fri Dec 08, 2017 2:29 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 08, 2017 3:21 am     Reply with quote

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: 12647

View user's profile Send private message

PostPosted: Fri Dec 08, 2017 4:43 am     Reply with quote

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: 5827
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Dec 08, 2017 6:51 am     Reply with quote

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:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group