View previous topic :: View next topic |
Author |
Message |
Micro_Guy
Joined: 06 Aug 2015 Posts: 10
|
Ramp-Up PWM with Interrupt |
Posted: Mon Aug 10, 2015 1:01 am |
|
|
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
|
|
Posted: Mon Aug 10, 2015 11:44 am |
|
|
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
|
|
Posted: Mon Aug 10, 2015 3:21 pm |
|
|
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
|
|
Posted: Mon Aug 10, 2015 4:03 pm |
|
|
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
|
|
Posted: Mon Aug 10, 2015 7:55 pm |
|
|
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
|
|
Posted: Mon Aug 17, 2015 10:31 pm |
|
|
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 |
|
|
|