View previous topic :: View next topic |
Author |
Message |
randy.shaffer
Joined: 21 Mar 2018 Posts: 70
|
How to pass the PWM stream as a variable? |
Posted: Mon Aug 25, 2025 5:33 pm |
|
|
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
|
|
Posted: Tue Aug 26, 2025 5:53 am |
|
|
use if ... else or switch inside your move_direction function |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19935
|
|
Posted: Tue Aug 26, 2025 9:36 am |
|
|
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
|
|
Posted: Fri Aug 29, 2025 4:45 am |
|
|
gaugeguy wrote: | use if ... else or switch inside your move_direction function |
like i mean use if else |
|
 |
|