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

Ramp-Up PWM with Interrupt

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



Joined: 06 Aug 2015
Posts: 10

View user's profile Send private message

Ramp-Up PWM with Interrupt
PostPosted: Mon Aug 10, 2015 1:01 am     Reply with quote

Hi Guys,

I'd like to implement a Ramp-Up PWM signal using timer interrupts. I've already done this with the PWM CCP module and would like to do this with interrupts this time.

I'm using pic16f877a operating at 4MHz.

Here's what I have so far: the code below generates a PWM with 10% duty and operates at 10mS Period. Duty is adjusted by varying the count2 variable (currently at 1). What I am attempting is to have the duty cycle ramp up to 100% in small increments.

any help/criticism is greatly appreciated. Thanks.

I would like to thank "Ttelmah" with help writing the code below.

Code:
#include <16F877A.h>
#device adc=8
#FUSES NOWDT,HS,PUT,NOPROTECT,NODEBUG,NOBROWNOUT,NOLVP,NOCPD,NOWRT
#use delay(clock=4M)

int count1=0,count2=0;


#int_timer0
void timer0_isr(void) {    //Int rountine....decrements counters
   if (count1) --count1;
   if (count2) --count2;
}

void main(void) {
  setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_4); // Timer setup for 1mS ticks
  enable_interrupts(GLOBAL);
  enable_interrupts(INT_TIMER0);
   
  count2=1; // Sets Duty...Currently On for 1mS, so Duty=10%
   
  do {
       if (count1==0) {
          output_toggle(PIN_B1) ;
          count1=10;
      }
      if (count2==0) {
          output_toggle(PIN_B1);
          count2=10;
      }
  }while (1);
}
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Aug 10, 2015 11:44 am     Reply with quote

what is the control objective of your code?

can you post a schematic that provides insight into what you are trying to do as a result of the design?
Micro_Guy



Joined: 06 Aug 2015
Posts: 10

View user's profile Send private message

PostPosted: Mon Aug 10, 2015 3:21 pm     Reply with quote

Currently I just have an Oscilloscope connected to Output B1 to monitor the PWM.

The objective will be to have an LED dim on. I realize i will have to play with the period and transition time to achieve the desired dimming.

Thanks.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Aug 10, 2015 4:03 pm     Reply with quote

why do you not use the hardware CCP /PWM which is so much simpler and less interrupt demanding to operate?
Micro_Guy



Joined: 06 Aug 2015
Posts: 10

View user's profile Send private message

PostPosted: Mon Aug 10, 2015 7:55 pm     Reply with quote

I will end up using the hardware CCP/PWM and I'm already quite familiar with how ccp/pwm works.

I am trying to implement this with an interrupt to better understand how interrupts work. This is purely experimental to satisfy my curiosity and further my understanding.
RoGuE_StreaK



Joined: 02 Feb 2010
Posts: 73

View user's profile Send private message

PostPosted: Mon Aug 17, 2015 10:31 pm     Reply with quote

If I'm interpreting the above code correctly, you'll have to completely rethink the method if you want to achieve your aim.
I believe(!) that what the code above does is essentially count down and then reset two counters, with one one count behind the other, so one hits "0" and toggles the pin, then 1ms later the other hits zero and re-toggles the pin. Then there's 9 interrupts from that last toggle before the first counter reaches "0" again, so on for 1ms and off for 9ms.

To use the same interrupt, you'd have to be counting from 100 (or is it from 99?), and one possible method could be (not thought through) to increment counter2 by 1 every time it gets reset, and decrement counter1 by one every time it gets reset.
That might not be thought out correctly, but essentially what you want to achieve is a 1/100 toggle, then a 2/100 toggle, then 3/100, 4/100, etc etc

Any number of ways to flay a feline
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