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

How to stop the PWM and control its operation?

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



Joined: 25 Aug 2005
Posts: 65
Location: Huntington Beach, CA

View user's profile Send private message

How to stop the PWM and control its operation?
PostPosted: Wed Sep 28, 2005 12:04 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

Re: How to stop the PWM and control its operation?
PostPosted: Wed Sep 28, 2005 1:03 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 28, 2005 1:04 am     Reply with quote

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"?
PostPosted: Wed Sep 28, 2005 9:35 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 28, 2005 2:13 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Wed Sep 28, 2005 2:15 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Sep 29, 2005 12:01 am     Reply with quote

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

View user's profile Send private message

Error with the "else" statement.
PostPosted: Thu Sep 29, 2005 1:09 am     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Thu Sep 29, 2005 1:12 am     Reply with quote

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

View user's profile Send private message

Thanks
PostPosted: Thu Sep 29, 2005 1:23 am     Reply with quote

Thanks all.
These lessons are sticking.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Sep 29, 2005 6:17 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Sat Oct 01, 2005 12:26 am     Reply with quote

Oh, sorry, I used to look at 16F876 datasheet. Laughing
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sat Oct 01, 2005 6:56 am     Reply with quote

Joshua Lai wrote:
Oh, sorry, I used to look at 16F876 datasheet. Laughing


And on the 16F876 it is on C2 as well Razz
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