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

Using E1 as PWM output on 18F45K22 affects delay_ms()

 
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: 64

View user's profile Send private message

Using E1 as PWM output on 18F45K22 affects delay_ms()
PostPosted: Wed Apr 16, 2025 3:31 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 17, 2025 5:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 17, 2025 12:46 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 17, 2025 1:15 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 17, 2025 2:01 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 18, 2025 12:13 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 21, 2025 9:47 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 21, 2025 2:42 pm     Reply with quote

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.
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