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

ccp2 pwm

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Byeon woo sung



Joined: 24 Jul 2018
Posts: 1

View user's profile Send private message

ccp2 pwm
PostPosted: Tue Jul 24, 2018 5:50 pm     Reply with quote

I use pic18f65k40.
I would like to get a pwm waveform use ccp2 with rb4 port.
But i can't get waveform. (low signal).
Can you see what's wrong?

Code:
#include <18F65K40.h>
#device ADC = 10
#fuses HS,NOWDT,NOPROTECT,CLKOUT,PPS1WAY
#use delay(clock= 16000000 )
#BYTE PORTE = 0XF8D
#BYTE TRISE = 0XF85

#pin_select CCP2OUT=PIN_E4

void main()
{
    int i= 124;
   
    setup_oscillator(OSC_HFINTRC_16MHZ);
    setup_adc(ADC_CLOCK_DIV_32);
    setup_adc_ports(NO_ANALOGS);
    setup_comparator_1(NC_NC);
    setup_comparator_2(NC_NC);
    setup_comparator_3(NC_NC);
   
    setup_timer_2(T2_DIV_BY_4,250,1);
    setup_ccp2(CCP_PWM);
 
    set_tris_e(0x00);
   
    while(TRUE)
    {
        set_pwm2_duty(i);   
    }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 25, 2018 1:08 am     Reply with quote

This line is the problem:
Quote:
setup_timer_2(T2_DIV_BY_4,250,1);

With the K40 series PICs, you need to specify the clock source as internal:
Code:
setup_timer_2(T2_CLK_INTERNAL | T2_DIV_BY_4, 250, 1);
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Wed Jul 25, 2018 1:42 am     Reply with quote

Well done PCM_Programmer.

This is one of those 'caveat' ones. On most of the older PIC16/18 PWM's, the clock source for timer2 is fixed (FOSC/4). No choices. If you look at the diagram for the timer, a direct connection. On the K40 and a few other newer PIC's the timer can be operated from other sources. In this case 9 sources. ZCD_OUT, CLKREF_OUT, SOSC, MFINTOSC, LFINTOSC, HFINTOSC, FOSC, FOSC/4, and an external PIN (PPS selectable). The default 'erased' state, selects one of the combinations that is 'reserved', so the timer won't work...
The T2_CLK_INTERNAL settings selects FOSC/4, giving normal operation.

I must admit to having played with PWM's clocked off an external pin, and it is quite a useful ability. Smile
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