keklik
Newbie level 3
16f877a pwm no output
I have written a code below to produce a 20kHz pwm to create a sinus wave at 50 Hz by changing duty cycle.
When I run the program it gave me no out puts on RC1 and RC2 pins. I got just logic 0. But pin RC0 was always logic 1. I couldn't find what wrong is.
Here is the code:
I have written a code below to produce a 20kHz pwm to create a sinus wave at 50 Hz by changing duty cycle.
When I run the program it gave me no out puts on RC1 and RC2 pins. I got just logic 0. But pin RC0 was always logic 1. I couldn't find what wrong is.
Here is the code:
Code:
#include <16f877a.h>
#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#use delay(clock=20000000) //crystal freq.
#use fast_io(c) //c port are outputs
void main()
{
int i;
int j=1; //just to sure not to get 0 at multiplication part
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
set_tris_c(0x00);
setup_CCP1(CCP_PWM);
setup_CCP2(CCP_PWM);
setup_timer_2(T2_DIV_BY_1,249,1);
set_pwm1_duty(j);
set_pwm2_duty(j);
while(1)
{
for (i=1;i<=100;i++) // 100 pwms at quarter of a sinus wave (first 5ms of sinus)
{
j=i*10;
set_pwm2_duty(j);
}
for (i=100;i>=1;i--) // 100 pwms at quarter of a sinus wave (second 5ms of sinus)
{
j=i*10;
set_pwm2_duty(j);
for (i=1;i<=100;i++) // 100 pwms at quarter of a sinus wave(third 5ms of sinus)
{
j=i*10;
}
set_pwm1_duty(j);
}
for (i=100;i>=1;i--) // 100 pwms at quarter of a sinus wave(forth 5ms of sinus wave)
{
j=i*10;
set_pwm1_duty(j);
}
}
}