View previous topic :: View next topic |
Author |
Message |
randy.shaffer
Joined: 21 Mar 2018 Posts: 63
|
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: 19779
|
|
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: 63
|
|
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: 327
|
|
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: 63
|
|
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: 19779
|
|
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. |
|
 |
|