View previous topic :: View next topic |
Author |
Message |
randy.shaffer
Joined: 21 Mar 2018 Posts: 64
|
Using E1 as PWM output on 18F45K22 affects delay_ms() |
Posted: Wed Apr 16, 2025 3:31 pm |
|
|
Code: | #include <18F45K22.h>
#fuses NOWDT
#use delay(internal = 32MHZ)
//#use PWM(FREQUENCY=31.25kHz, OUTPUT=PIN_E0, PWM_OFF)
#use PWM(FREQUENCY=31.25kHz, OUTPUT=PIN_E1, PWM_OFF)
void main()
{
set_tris_b(0b1100000);
set_tris_e(0b0001000);
while(TRUE)
{// flash LED
output_high(PIN_B3);
delay_ms(500);
output_low(PIN_B3);
delay_ms(500);
}
} |
All is well with E0 or E2: 10-bit PWM resolution with flashing LED.
If changed to E1 (or any other), PWM resolution is only 8 bits with a limited duty range and the LED is on for about 3 sec and off for the same. I was hoping to use PIN_B0 with 10-bit resolution. Compiler is 5.119. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19782
|
|
Posted: Thu Apr 17, 2025 5:42 am |
|
|
The problem here its the difference between a CCP pin and a PWM pin.
The PWM you require will work fine on any of the CCP pins. So for example
PIN_D1. However #USE PWM, is having trouble configuring the ECCP module
correctly for the PWMx pins. E1, needs pulse steering to be used for P3B,
and it looks like #use does not know how to do this. It happily works onto
the A pins (so for example P1A, PIN_C2), but if you try to use any of the
steering pins, it only sets up 8bit operation and interferes with the
clock setup.
I would talk to CCS about this. But for now, the solution is to manually
use CCP3, and select pulse steering to the B pin (setup_ccp3). |
|
 |
randy.shaffer
Joined: 21 Mar 2018 Posts: 64
|
|
Posted: Thu Apr 17, 2025 12:46 pm |
|
|
Code: |
#include <18F45K22.h>
#fuses NOWDT
#use delay(internal = 32MHZ)
void main()
{
set_tris_b(0b1100000);
setup_timer_2(T2_DIV_BY_1, 255, 1);
setup_ccp3(CCP_PWM);
setup_ccp3(CCP_PULSE_STEERING_B);
set_pwm3_duty(512);
while(TRUE)
{// flash LED
output_high(PIN_B3);
delay_ms(500);
output_low(PIN_B3);
delay_ms(500);
}
}
} |
Thank you so much for the reply and help. This code was an attempt to get a 50% duty signal out of Pin B0, but it does not appear to work. Any guidance is greatly appreciated. |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 328
|
|
Posted: Thu Apr 17, 2025 1:15 pm |
|
|
You can't use separate setup_ccp3 statements. You have to OR the setup options together in one setup_ccp3 command. |
|
 |
randy.shaffer
Joined: 21 Mar 2018 Posts: 64
|
|
Posted: Thu Apr 17, 2025 2:01 pm |
|
|
Code: | #include <18F45K22.h>
#fuses NOWDT
#use delay(internal = 32MHZ)
void main()
{
set_tris_b(0b1100000);
setup_timer_2(T2_DIV_BY_1, 255, 1);
setup_ccp3(CCP_PWM | CCP_PULSE_STEERING_B);
set_pwm3_duty(512);
while(TRUE)
{// flash LED
output_high(PIN_B3);
delay_ms(500);
output_low(PIN_B3);
delay_ms(500);
}
} |
Thank you. Here is the updated code. No output at B0, though. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19782
|
|
Posted: Fri Apr 18, 2025 12:13 am |
|
|
TRIS.
The compiler does not 'know' what pin is involved, so does not setup the
TRIS for you. |
|
 |
randy.shaffer
Joined: 21 Mar 2018 Posts: 64
|
|
Posted: Mon Apr 21, 2025 9:47 am |
|
|
I tried to "single-out" B0 with the following, but still no PWM output at B0.
Code: | #include <18F45K22.h>
#fuses NOWDT
#use delay(internal = 32MHZ)
void main()
{
set_tris_b(0b00000001);
setup_timer_2(T2_DIV_BY_1, 255L, 1);
setup_ccp3(CCP_PWM | CCP_PULSE_STEERING_B);
set_tris_b(0b11111110);
set_pwm3_duty(512);
while(TRUE)
{// flash LED
output_high(PIN_B3);
delay_ms(500);
output_low(PIN_B3);
delay_ms(500);
}
} |
I contacted CCS about the original code, here is the response:
Quote: | With that device and pin B0 the compiler has to do a software PWM, this requires the use of the Timer's interrupt to generate the PWM. Which will interrupt code execution and effect the timing of the main() loop. There's no way to get around this when using a software PWM. Additionally because of this and the target frequency is on the high side for a SW PWM, it's spending the majority of the time in the ISR which is severely affecting the main() loop timing.
If you need that high of a frequency I highly recommend moving the output to one of the hardware PWM pins, which are pins C2, C1, B3, E0, B5, D1 and E2 on that device. If it has to be on pin B0 then I recommend lowering the frequency, for example frequencies of 3.096kHz and 1.953kHz will get the full resolution of the 8-bit Timer and not effect the main() loop timing to much.
Richard
CCS Support |
|
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 328
|
|
Posted: Mon Apr 21, 2025 2:42 pm |
|
|
Code: |
setup_ccp3(CCP_PWM | CCP_PULSE_STEERING_B);
|
This would be selecting PIN_E1. You would also need to set the TRIS for port E to make PIN_E1 an output.
The data sheet shows Pin B0 has no hardware connections for CCP or PWM. |
|
 |
|