| View previous topic :: View next topic |
| Author |
Message |
JimB
Joined: 25 Aug 2005 Posts: 65 Location: Huntington Beach, CA
|
| How to stop the PWM and control its operation? |
Posted: Wed Sep 28, 2005 12:04 am |
|
|
I put together the following to play with the PWM oscillator. I want to be able to have the PWM output only when the pushbutton is pressed ("0"). The while statement disables the start, but when the button is pushed the PWM output starts, but does not stop when released. I have single stepped through file and it does not stop at the while statement but goes down a line or two and then when it gets to the end, the debugger says it is running and the marker doesn't return. How does one stop the PWM output?
Also why doesn't the use of the PWM require anabling the PWM output pin?
| Code: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ICD=TRUE
#use delay(clock=20000000)
#define output_high(PIN_A5)
#define PUSH_BUTTON PIN_A5
main() {
while(input(PUSH_BUTTON));
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 250, 1);
set_pwm1_duty(125);
}
|
|
|
 |
Foppie
Joined: 16 Sep 2005 Posts: 138 Location: The Netherlands
|
| Re: How to stop the PWM and control its operation? |
Posted: Wed Sep 28, 2005 1:03 am |
|
|
I would try the following adjustments
| Code: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ICD=TRUE
#use delay(clock=20000000)
#define output_high(PIN_A5)
#define PUSH_BUTTON PIN_A5
main() {
while(1)
if (input(PUSH_BUTTON)) {
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 250, 1);
set_pwm1_duty(125);
}
else {
set_pwm1_duty(0);
}
}
|
Last edited by Foppie on Tue Oct 04, 2005 5:07 am; edited 1 time in total |
|
 |
newguy
Joined: 24 Jun 2004 Posts: 1946
|
|
Posted: Wed Sep 28, 2005 1:04 am |
|
|
Think about it. When you push the button, your code sets up the PWM to run. Now that it's running, you release the button. Where is there code to a) recognize the button release, and b) turn off the PWM when the button is released?
Edit: whoops! You beat me to the answer by a minute! |
|
 |
Guest
|
| What to do to cause a PWM to "stop"? |
Posted: Wed Sep 28, 2005 9:35 am |
|
|
Thanks for the help. My problem was, I just didn't see a direct way to stop the output. Setting the duty cycle to "0" didn't pop into my head as a typical way to accomplish this. One would think that the compiler manual would address this issue. I was thinking that the PWM had no way to loop back to the original while() and was considering a go_to label as an approach.
So is the PWM a non typical operation that does not behave as other C functions that loop back through the code?
Also, why doesn't the CC1 output pin require an in or out declaration? |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 28, 2005 2:13 pm |
|
|
| Quote: | | Also, why doesn't the CC1 output pin require an in or out declaration? |
Compile a test program and look at the .LST file to find the answer.
| Code: | .................... setup_ccp1(CCP_PWM);
0016: MOVLW FF
0017: MOVWF 21 // Set TrisC shadow register = all inputs
0018: BCF 21.2 // Set RC2 pin = output (in the shadow reg)
0019: MOVF 21,W // Copy shadow register to W
001A: BSF 03.5 // Bank 1
001B: MOVWF 07 // Write shadow register to physical TRISC
001C: BCF 03.5 // Bank 0
001D: BCF 07.2 // Set CCP1 pin to a Logic Low level.
001E: MOVLW 0C // Configure CCP1CON register for PWM mode.
001F: MOVWF 17
|
Answer: The CCS function inserts code to setup the TRIS correctly. |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Sep 28, 2005 2:15 pm |
|
|
Here's how I do it.
1. Setup the PWM
2. Make the pwm output and input
if button pressed
output
else
input |
|
 |
Joshua Lai
Joined: 19 Jul 2004 Posts: 42 Location: Malaysia, PJ
|
|
Posted: Thu Sep 29, 2005 12:01 am |
|
|
Like this can?
| Quote: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ICD=TRUE
#use delay(clock=20000000)
#define output_high(PIN_A5)
#define PUSH_BUTTON PIN_A5
short i;
main() {
while(1)
if (input(PUSH_BUTTON)); {
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 250, 1);
set_pwm1_duty(125);
}
else {
set_tris_c(0x03);
i=input(PIN_C3);
}
} | [/quote] |
|
 |
JimB
Joined: 25 Aug 2005 Posts: 65 Location: Huntington Beach, CA
|
| Error with the "else" statement. |
Posted: Thu Sep 29, 2005 1:09 am |
|
|
I cut and pasted the two examples above. When I compile them I always get an error highlighting the "else" word. The error message is
"A numeric expression must appear here." I just upgraded PCWH to version 3.235 with the same results. |
|
 |
Foppie
Joined: 16 Sep 2005 Posts: 138 Location: The Netherlands
|
|
Posted: Thu Sep 29, 2005 1:12 am |
|
|
in this line: | Code: | | if (input(PUSH_BUTTON)); { | the ; needs to go. so it will be | Code: | #include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ICD=TRUE
#use delay(clock=20000000)
#define output_high(PIN_A5)
#define PUSH_BUTTON PIN_A5
short i;
main() {
while(1)
if (input(PUSH_BUTTON)) {
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 250, 1);
set_pwm1_duty(125);
}
else {
set_tris_c(0x03);
i=input(PIN_C3);
}
} |
|
|
 |
JimB
Joined: 25 Aug 2005 Posts: 65 Location: Huntington Beach, CA
|
| Thanks |
Posted: Thu Sep 29, 2005 1:23 am |
|
|
Thanks all.
These lessons are sticking. |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Sep 29, 2005 6:17 am |
|
|
I have no idea why someone suggested that you do something with C3!
| Code: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ICD=TRUE
#use delay(clock=20000000)
#define PUSH_BUTTON PIN_A5
int8 trisc;
#locate trisc = 0x87
void main(void)
{
// Set up PWM
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 250, 1);
set_pwm1_duty(125);
// Make sure the PWM is off
bit_clear(trisc, 2);
while(1)
if (input(PUSH_BUTTON))
{
// Turn on PWM
bit_clear(trisc, 2);
}
else
{
// Turn off PWM
bit_set(trisc, 2);
}
} |
|
|
 |
Joshua Lai
Joined: 19 Jul 2004 Posts: 42 Location: Malaysia, PJ
|
|
Posted: Sat Oct 01, 2005 12:26 am |
|
|
Oh, sorry, I used to look at 16F876 datasheet.  |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sat Oct 01, 2005 6:56 am |
|
|
| Joshua Lai wrote: | Oh, sorry, I used to look at 16F876 datasheet.  |
And on the 16F876 it is on C2 as well  |
|
 |
|