|
|
View previous topic :: View next topic |
Author |
Message |
Byeon woo sung
Joined: 24 Jul 2018 Posts: 1
|
ccp2 pwm |
Posted: Tue Jul 24, 2018 5:50 pm |
|
|
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
|
|
Posted: Wed Jul 25, 2018 1:08 am |
|
|
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: 19548
|
|
Posted: Wed Jul 25, 2018 1:42 am |
|
|
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. |
|
|
|
|
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
|