View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
PWM in PIC12F615 |
Posted: Fri Nov 22, 2019 6:50 pm |
|
|
Hi, I am trying to implement a PWM in a pic12F615, with a frequency of 1Khz, but I am not able to obtain the PWM correctly. The duty cycle of the wave does not vary from 1 to 100% and oscillates. I can only several up to 15% of the duty cycle.
But if I use the same code in a pic16f887 if it works correctly!!
Here is my code:
Code: | #include <12F615.h>
#device ADC=10
#fuses INTRC_IO, NOWDT, PROTECT,NOPUT
#use delay(clock=4MHz)
int16 duty=0;
int Timer2=249;
int Poscaler=1;
void main() {
setup_timer_2(t2_div_by_4,Timer2,Poscaler); //Configuracion de Timer 2 para establecer frec. PWM a 1kHz
setup_ccp1(ccp_pwm); //Configurar modulo CCP1 en modo PWM
setup_adc_ports(all_analog); //Configurar ADC
setup_adc(adc_clock_internal);
set_adc_channel(0);
while(TRUE) {
delay_us(100);
duty=read_adc();
set_pwm1_duty(duty);
}
} |
Some of you can tell me what is my mistake? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 22, 2019 9:43 pm |
|
|
The loop delay is too short. I assume you're turning a trimpot with your
hand. Change it to 100 ms.
Quote: |
while(TRUE) {
delay_us(100);
duty=read_adc();
set_pwm1_duty(duty);
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Sat Nov 23, 2019 5:21 am |
|
|
also...
this..
Quote: | setup_adc(adc_clock_internal); | is wrong.
With a clock speed of 4MHz, Fosc/8 or Fosc/16 are valid. 'Internal' is for very slow PICs, sleeping.
Check datasheet, Chapter 9, Table 9-1, page 69 for 'how and why'.
I assume some 'Wizard' helped you ?
Jay |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Sat Nov 23, 2019 7:18 am |
|
|
Hi, PCM programmer and temtronic, that was it, thank you for your help. |
|
|
|