+ Post New Thread
Results 1 to 5 of 5
  1. #1
    Newbie level 3
    Points: 550, Level: 4

    Join Date
    Dec 2009
    Posts
    3
    Helped
    0 / 0
    Points
    550
    Level
    4

    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:
    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);
        }   
       
       }
    
        }

    •   Alt24th December 2009, 23:16

      advertising

        
       

  2. #2
    Newbie level 3
    Points: 550, Level: 4

    Join Date
    Dec 2009
    Posts
    3
    Helped
    0 / 0
    Points
    550
    Level
    4

    Re: 16f877a pwm no output

    With some code changes, I figured out that if I use just set_pwm1_duty(j) or set_pwm2_duty(j) in code it looks like working.(I think Proteus can't show duty cycle changes, but shows %50 duty cycle pwm signal). So how can I use both of the pwm pins?
    This picture shows the pwm signal I need to create:



    •   Alt25th December 2009, 22:07

      advertising

        
       

  3. #3
    Full Member level 2
    Points: 3,342, Level: 13
    Achievements:
    7 years registered
    s_cihan_tek's Avatar
    Join Date
    Mar 2006
    Location
    Turkey
    Posts
    126
    Helped
    64 / 64
    Points
    3,342
    Level
    13

    Re: 16f877a pwm no output

    Hi,

    You simply can't generate a sinusoidal PWM using this method.

    In your for loops, you are updating the duty cycle several times before one period of the PWM is completed. The comparator will miss lots of cycles and output useless PWM signals.

    You can use the timer interrupts and a look-up table containing the duty cycle values corresponding to the different instants of a sine wave instead.

    I don't know how your circuit will use these PWMs, but it's better to use just one PWM channel and output a single PWM signal whose average duty cycle is 128. You don't need to use both PWM channels.

    Test your code in a breadboard or something, don't rely only on Proteus.



    •   Alt25th December 2009, 23:12

      advertising

        
       

  4. #4
    Newbie level 3
    Points: 550, Level: 4

    Join Date
    Dec 2009
    Posts
    3
    Helped
    0 / 0
    Points
    550
    Level
    4

    Re: 16f877a pwm no output

    Thanks for reply. My circuit is a 20kHz H-bridge driven by sinus pwm. For that, I need 2 pwm channels. Pic creats sinus pwms, pwms go to 2 ir2110 mosfet drivers then 4 mosfets make switching.
    Because of 20Khz, LUT will have many values in it. I think LUT is not an option in this situation. Or am I wrong?



  5. #5
    Full Member level 2
    Points: 3,342, Level: 13
    Achievements:
    7 years registered
    s_cihan_tek's Avatar
    Join Date
    Mar 2006
    Location
    Turkey
    Posts
    126
    Helped
    64 / 64
    Points
    3,342
    Level
    13

    Re: 16f877a pwm no output

    One period of the sine wave is 20000us
    One period of the PWM is 50us

    This means that you need a LUT with 400 values for a full period. Since you are using a different PWM signal for each half-period, you just need 200 values, which is NOT many.



--[[ ]]--