View previous topic :: View next topic |
Author |
Message |
psv4
Joined: 29 Feb 2020 Posts: 1
|
18F4550 can't reach 50khz pwm |
Posted: Sat Feb 29, 2020 1:02 pm |
|
|
Hello,
I am new to pic and have a problem with getting 50khz pwm. When i set values to 50khz which is setup_timer_2(T2_DIV_BY_1, 9, 1) in 2mhz oscillator frequency,
proteus cant show on oscillator.
Then I am trying to increase oscillator frequency to get 16khz.
When I try to increase it gives me errors like option invalid Internal Osc Freq Wrong and Undefiened identifier OSC_16MHZ.
What values do I set to get a 50khz pwm ?
CCS C version:5.015
Code: | #include <18F4550.h>
#device ADC = 10
#fuses NOMCLR INTRC_IO
#use delay(clock = 16M)
unsigned long int deger;
void main(){
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_CCP2(CCP_OFF);
setup_oscillator(OSC_16MHZ);
setup_adc(ADC_CLOCK_DIV_8);
setup_adc_ports(ALL_ANALOG);
set_adc_channel (12);
setup_timer_2(T2_DIV_BY_1, 255, 1);
setup_ccp1(CCP_PWM);
delay_ms(100);
set_tris_b(0x01);
set_tris_c(0x00);
while(1){
deger = read_adc();
set_pwm1_duty(deger);
delay_ms(1);
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19591
|
|
Posted: Sat Feb 29, 2020 1:11 pm |
|
|
This is a read the data sheet one. The 18F4550, does not have a 16MHz
internal oscillator. Just 8MHz.
You can generate a 50KHz PWM from 8MHz:
setup_timer_2(T2_DIV_BY_1, 39, 1;
Will give 50KHz.
The settying you show:
setup_timer_2(T2_DIV_BY_1, 9, 1);
Will give 50kHz from 2MHz.
Your duty cycle value is allowed to go to the PR2 value ((the 9 or 39) +1) *4. No more. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9271 Location: Greensville,Ontario
|
|
Posted: Sat Feb 29, 2020 2:26 pm |
|
|
also..
if you ever decide to use the PIC in the 'real World', be SURE to read which clock configurations work with the internal USB peripheral !!!! |
|
|
|