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

Issues with low frequency software pwm and duty cycle

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



Joined: 07 May 2011
Posts: 40
Location: Bulgaria

View user's profile Send private message

Issues with low frequency software pwm and duty cycle
PostPosted: Thu Jul 19, 2012 11:06 am     Reply with quote

I want to generate a PWM with frequency from 3Hz to 30Hz. Also duty cycle from 100uS to 2mS with 50uS steps. I use the following program but the lowest on time is 116 when width=1 and the step is more than 120us. For example if width=2, on time is 235uS etc. I want to vary the on time with 50uS steps - 100,150,200,250...2000.
Compiler version is 4.120.
Code:

#include <18f2431.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT,NODEBUG,BROWNOUT,NOCPD,NOWRT
#use delay(clock=4000000)
#include <LCD20x4.c>

#define PWM_PIN  PIN_B1

#define TIMER0_PRELOAD 153 

#define LOOPCNT freq         

int8 status_enter_prev,status_up_down_main,f=1,frequency;
int16 width=1,freq,duty;

void buttons_freq_up_down()
{   
    if(!input(PIN_C0) && status_enter_prev==0) //freq up
    {
      f++;
      if(f>28)
      f=28;
      delay_ms(50);
    }
    if(!input(PIN_C3) && status_enter_prev==0) //freq down
    {
      f--;
      if(f<1)
      f=1;   
      delay_ms(50);
    } 
     
    if(input(PIN_C0) && input(PIN_C3))
    {               
      status_enter_prev=0;
    }
   
    while(!input(PIN_C0))  // Loop here until button released.
    {
      delay_ms(25);
    }
   
    while(!input(PIN_C3))  // Loop here until button released.
    {
      delay_ms(25);
    }
           
}

void buttons_duty_up_down()
{     
    if(!input(PIN_C1) && status_up_down_main==0) //duty up
    {
      status_up_down_main=1;
      //delay_ms(50);           
      width++;
      if(width>17)
      width=17;
    }
    else if(!input(PIN_C4) && status_up_down_main==0) //duty down
    {
      status_up_down_main=1;
      //delay_ms(50);           
      width--;
      if(width<1)
      width=1;
    } 
    if(input(PIN_C1) && input(PIN_C4))
    {
         status_up_down_main=0;
    }
   
    while(!input(PIN_C1))  // Loop here until button released.
    {
      delay_ms(25);
    }
   
    while(!input(PIN_C4))  // Loop here until button released.
    {
      delay_ms(25);
    }
   
}

void frequency_change()
{

if(f==1)
{
freq=2800;
frequency=3;
}

if(f==2)
{
freq=2100;
frequency=4;
}

if(f==3)
{
freq=1680;
frequency=5;
}

if(f==4)
{
freq=1380;
frequency=6;
}

if(f==5)
{
freq=1190;
frequency=7;
}

if(f==6)
{
freq=1040;
frequency=8;
}

if(f==7)
{
freq=920;
frequency=9;
}

if(f==8)
{
freq=840;
frequency=10;
}

if(f==9)
{
freq=765;
frequency=11;
}

if(f==10)
{
freq=695;
frequency=12;
}

if(f==11)
{
freq=645;
frequency=13;
}

if(f==12)
{
freq=600;
frequency=14;
}

if(f==13)
{
freq=560;
frequency=15;
}

if(f==14)
{
freq=525;
frequency=16;
}

if(f==15)
{
freq=495;
frequency=17;
}

if(f==16)
{
freq=460;
frequency=18;
}

if(f==17)
{
freq=440;
frequency=19;
}

if(f==18)
{
freq=420;
frequency=20;
}

if(f==19)
{
freq=400;
frequency=21;
}

if(f==20)
{
freq=382;
frequency=22;
}

if(f==21)
{
freq=366;
frequency=23;
}

if(f==22)
{
freq=350;
frequency=24;
}

if(f==23)
{
freq=337;
frequency=25;
}

if(f==24)
{
freq=324;
frequency=26;
}

if(f==25)
{
freq=311;
frequency=27;
}

if(f==26)
{
freq=300;
frequency=28;
}

if(f==27)
{
freq=290;
frequency=29;
}

if(f==28)
{
freq=280;
frequency=30;
}

}


void duty_change()
{

if(width==1)
{
duty=116;
}

if(width==2)
{
duty=235;
}

if(width==3)
{
duty=350;
}

if(width==4)
{
duty=465;
}

if(width==5)
{
duty=585;
}

if(width==6)
{
duty=710;
}

if(width==7)
{
duty=830;
}

if(width==8)
{
duty=950;
}

if(width==9)
{
duty=1070;
}

if(width==10)
{
duty=1200;
}

if(width==11)
{
duty=1320;
}

if(width==12)
{
duty=1450;
}

if(width==13)
{
duty=1550;
}

if(width==14)
{
duty=1670;
}

if(width==15)
{
duty=1800;
}

if(width==16)
{
duty=1920;
}

if(width==17)
{
duty=2020;
}

}

#INT_RTCC 
void tick_interrupt(void);

void main()
{
lcd_init();
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_1 | RTCC_8_BIT);
set_timer0(TIMER0_PRELOAD);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
 
while(true)
 {
   buttons_freq_up_down();
   buttons_duty_up_down();
   frequency_change();
   duty_change();
 
   lcd_gotoxy(1,3);
   printf(lcd_putc,"Frequency=%u [Hz] ",frequency);
   lcd_gotoxy(1,4);
   printf(lcd_putc,"ON Time=%lu [uS]  ",duty);
       
 }
 
}

#INT_RTCC
void tick_interrupt(void)
{
static int16 loop = LOOPCNT;
static int16 pulse;

set_timer0(get_timer0() + TIMER0_PRELOAD);

if(--loop == 0)
  {
   loop = LOOPCNT;
   pulse = width;
  }

if(pulse)
  {
   output_high(PWM_PIN);
   pulse--;
  }
else
  {
   output_low(PWM_PIN);
  }

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jul 19, 2012 11:41 am     Reply with quote

Quote:
I want to vary the on time with 50uS steps.

It's not going to happen with a 4 MHz oscillator frequency. That gives 1 us
per instruction cycle. It takes 50 instructions of overhead just to handle
the interrupt. Your user code inside the isr will increase this even more.

Try increasing your oscillator frequency. Then you will have a chance
to make it work.
Mike Walne



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

View user's profile Send private message

PostPosted: Tue Jul 24, 2012 11:14 am     Reply with quote

Do you have to use EXACTLY 50us steps?

Could you, for instance, use 32us or 64us?

Mike

EDIT

Seriously, you can use the hardware PWM built into the PIC.

You set up the PWM generator so that:-

(1) The PWM duty creates a mark period with the desired ON time.
(2) The timer2 interrupt operates with a period significantly greater that your maximum on time.

Use the interrupt to control the PWM:-

(a) Enable the PWM for a single period.
(b) Disable the PWM long enough to give the desired repeat period.
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