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

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

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

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

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

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

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