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 CCS Technical Support

PIC18F4550 PWM Issue
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Thu Jun 27, 2013 8:10 am     Reply with quote

Can you please give me example? I tried two methods and both did not work. Your help is appreciated
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Thu Jun 27, 2013 8:14 am     Reply with quote

aaronik19 wrote:
Can you please give me example? I tried two methods and both did not work. Your help is appreciated


Show your two methods. You will not learn anything by having us here doing it all for you.
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Thu Jun 27, 2013 8:24 am     Reply with quote

Ok the first one i already told it to you and you said that it was not good. By changing the frequency.

The second one is a little bit complicated. Assuming that you want to adjust the brightness to 80% in 5seconds. So first, i made a routine that every time is is called it will raise the pwm_percent by 5%. Now how many times this routine will be called depends on a ratio between the brightness needed and the time. In our example 16 times.

I believe that there is a simpler methods but i can't figure out the right one.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Jun 27, 2013 10:01 am     Reply with quote

aaronik19 wrote:
Ok the first one i already told it to you and you said that it was not good. By changing the frequency.

The second one is a little bit complicated. Assuming that you want to adjust the brightness to 80% in 5seconds. So first, i made a routine that every time is is called it will raise the pwm_percent by 5%. Now how many times this routine will be called depends on a ratio between the brightness needed and the time. In our example 16 times.

I believe that there is a simpler methods but i can't figure out the right one.

Changing the duty ratio in 5% jumps gives big changes in brightness at the lower levels.
For example, 10% to 15% is a 50% increase!
You can achieve smoother ramping by increasing or decreasing duty period in ones.
You then change the rate of ramping by adjusting the interval between steps.

Mike
temtronic



Joined: 01 Jul 2010
Posts: 9658
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jun 27, 2013 11:10 am     Reply with quote

also...
You should lookup info on 'lighting', specifically that intensity is not a linear function(ie: 3 volts in != 30% light). There are lots of websites devoted to this topic(hint: DMX lighting) that will help you.
Commercial lighting units can do this conversion automatically but since you've just grabbed a ballast and lamp, you'll have to 'do the math' to get the response you want.

hth
jay
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Sat Jun 29, 2013 6:13 am     Reply with quote

I made the principle requested after further testing using combination of previous, new value and delay values. Thanks to all for help and ideas.

Another issue is that i have a variable which i need to be very accurate in fact the values are such as 1.57, 2.69 etc. I used the int16 _fixed(2) but when i am reading this value with the hyperterminal on printf statement i am using the %3w to read this variable. When the value is 1.56, the hyperterminal is giving me 1.000. Is it important that if this variable needs to be added with variable2, variable2 needs to be of the same type ( int16 _fixed(2)

How i can solve this issue please? At the moment i am not in my workshop and the code that i had done till now is not on this laptop.
temtronic



Joined: 01 Jul 2010
Posts: 9658
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jun 29, 2013 8:36 am     Reply with quote

You should reread the printf() function help information again

If your variable is 123.56, %3w will display 123, as shown in the help file

You need to format as %6.2w to display as 123.56. There are 6 'positions'.
3 before the decimal point, the decimal point and 2 after the deciaml point.

I am however at a loss to understand how you created a fixed integer with 'fraction' from a floating point number....
please post how you did that.


hth
jay
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Sat Jun 29, 2013 8:43 am     Reply with quote

you are right temtronic. Sorry I managed to make the %3W without the decimal point! Thanks for your observation! I thought that it is something very small!

I will post the whole program when I tidy it a little bit. Hope that I post it by next Tuesday. In case that I need to add this floating decimal value with another variable, they must be both same type?
aaronik19



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

PostPosted: Mon Jul 01, 2013 7:53 am     Reply with quote

To keep my promise this is the actual program. 99% working good:

Code:
#include <pwm_new1.h>
#INCLUDE <math.h>

int16 ch1_current;
int16 ch1_newone;
int1 ch1_rampcheck;
int1 dim_active;
int16 ch1_delay;
int16 ch1_pwmvalue;
int16 ch1_loops;
int16 ch1_removesteps;
int secondloop;  // to add one second
void dimming_loop()
{
   IF(ch1_rampcheck == 1)
   {
      ch1_pwmvalue--;
   }

   ELSE
   ch1_pwmvalue++;
   
   IF(ch1_loops > 0 && dim_active == 1)
   {
      ch1_pwmvalue-= ch1_removesteps;
      pwm_set_duty_percent(ch1_pwmvalue);
   }
}

void dimming_setup()
{
   ch1_current = 1000;
   ch1_newone = 100;
   ch1_delay = 20;
   ch1_loops = ch1_current - ch1_newone;
   ch1_removesteps = (ch1_loops / ch1_delay);
   dim_active = 1;

   IF (ch1_current > ch1_newone)
   {
      ch1_pwmvalue = ch1_current;
      ch1_rampcheck = 1;
   }

   
   ELSE
   {
      ch1_pwmvalue = ch1_newone;
      ch1_rampcheck = 0;
   }
}

#INT_TIMER1    //every 104ms OVERFLOWS

void  TIMER1_isr(VOID)
{
   secondloop++;

   IF (secondloop == 10) //1 second loop
   {
      ch1_delay--;

      IF (ch1_delay == 0)
      {
         dim_active = 0;
         output_high (PIN_D1) ;
      }

      dimming_loop () ;
      secondloop = 0;
   }
}

void main()
{
   setup_ccp1 (CCP_PWM);
   setup_timer_2 (T2_DIV_BY_16, 125, 1);
   setup_timer_1 (T1_INTERNAL|T1_DIV_BY_8); //104 ms overflow
   enable_interrupts (INT_TIMER1) ;
   enable_interrupts (GLOBAL) ;
   dimming_setup () ;
   pwm_set_duty_percent (1000) ;
   delay_ms (2000) ;

   WHILE (TRUE)
   {
   }
}


The only issue that I have is that when the delay time is less than 4 seconds, the pwm value remains approx. 1000. If the delay time is for example 60, it works perfect.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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