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 pass the PWM stream as a variable?

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



Joined: 21 Mar 2018
Posts: 70

View user's profile Send private message

How to pass the PWM stream as a variable?
PostPosted: Mon Aug 25, 2025 5:33 pm     Reply with quote

In the example below, I would like to pass the PWM stream as a variable to a function that contains the pwm_set_duty_percent() function.

Code:

#include <16F18044.h> // device file
#include <stdint.h> // to use uint
#use delay(internal = 32MHZ)
#use pwm(FREQUENCY = 31.25kHz, PWM_OFF, STREAM = IN2, output=PIN_C2)
#use pwm(FREQUENCY = 31.25kHz, PWM_OFF, STREAM = IN1, output=PIN_C1)
void move_direction(uint8_t const DIRECTION);
int main(void)
{
 while(TRUE)
 {
  move_direction(IN1);
  delay_ms(1000);
  move_direction(IN2);
  delay_ms(1000);
 }
 return(0);
}
void move_direction(uint8_t const DIRECTION)
{
 pwm_set_duty_percent(DIRECTION, 500);
}


The values of IN1 and IN2 appear to have the values of 2 and 1, respectively. The DIRECTION variable generates the error "A numeric expression must appear here". Compiler is 5.121. Thanks for having a look.
gaugeguy



Joined: 05 Apr 2011
Posts: 333

View user's profile Send private message

PostPosted: Tue Aug 26, 2025 5:53 am     Reply with quote

use if ... else or switch inside your move_direction function
Ttelmah



Joined: 11 Mar 2010
Posts: 19935

View user's profile Send private message

PostPosted: Tue Aug 26, 2025 9:36 am     Reply with quote

Yes.
The point is that stream names are preprocessor directives. Solved at
compile time to generate different code. So they always have to be handled
as gaugeguy says by passing your own value, that you then use an if to
select the different possibilities with.
kyleighterry



Joined: 29 Aug 2025
Posts: 1

View user's profile Send private message

PostPosted: Fri Aug 29, 2025 4:45 am     Reply with quote

gaugeguy wrote:
use if ... else or switch inside your move_direction function

like i mean use if else
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