CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PWM Generation-Help

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Levism



Joined: 02 Aug 2015
Posts: 3

View user's profile Send private message

PWM Generation-Help
PostPosted: Sun Aug 02, 2015 12:15 pm     Reply with quote

Hello Gentlemen,

I am trying to create a program that allows a PIC16F876 to receive 0-5v from a 10k potentiometer through RA0. It converts this to a 1khz pwm signal that comes out of CCP1 and can have its duty cycle changed from 0 to 100% depending on the voltage received in RA0. I am very new to programing, having only a few class code samples and my recent studies to rely on. Below is the code I have come up with so far. When I test the .HEX on Proteus the Hz reading I get does not vary much from 252 Hz at all times, even when I tinker with the potentiometer. I would very much appreciate any insight as to how I can accomplish my goal.
Thanks in advance.
Code:

#include <16f876.h>
#device ADC=8
#use delay(clock=4000000)
#fuses XT,NOWDT,PUT,NOBROWNOUT,NOLVP

int conta=0;
int resultado=0;
long int ciclo=0;
int conv=0;

//--------------------------------------------------------------------

#INT_TIMER0
void trata_t0() {   

   resultado = read_adc( );
     conv=resultado/4;
     set_timer0(get_timer0());
         conta++;
if (conta==31) {
                conta=0;   
}
}
//---------------------------------------------------------------
void main (){

   SETUP_ADC_PORTS(RA0_ANALOG);
   setup_ADC(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);
 

   enable_interrupts(INT_TIMER0); 
   enable_interrupts(GLOBAL);   
 
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64);
   setup_timer_2 (T2_DIV_BY_16, 61, 1);
   setup_ccp1 (ccp_pwm);                 
   
   set_pwm1_duty ( 0 );
   
while(true){
     
      for (ciclo=0; ciclo<conv; ciclo++){
                                           set_pwm1_duty (ciclo); 
                                            delay_ms(50);
                                        }
       delay_ms(1000);
      }
}

_________________
Life is but dust.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 02, 2015 1:18 pm     Reply with quote

Quote:

the Hz reading I get does not vary much from 252 Hz at all times, even
when I tinker with the potentiometer

set_pwm1_duty() does not change the PWM frequency. It changes
the duty cycle. Example:
http://d32zx1or0t1x0y.cloudfront.net/2011/06/atmega168a_pwm_02_lrg.jpg
The frequency is constant but the duty cycle (pulse width) changes.
Levism



Joined: 02 Aug 2015
Posts: 3

View user's profile Send private message

Yes
PostPosted: Sun Aug 02, 2015 1:34 pm     Reply with quote

Thank you for pointing that out for me sir!
Now I realize that I need to get this frequency up to 1khz, for some reason I was mixing frequency with cycle in my mind. To do this I need to change the period in timer 2 correct?

setup_timer_2 (T2_DIV_BY_16, 61, 1);

I will try to measure the cycle and see what I got.
_________________
Life is but dust.
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Sun Aug 02, 2015 1:40 pm     Reply with quote

Change the prescaler. If you change the period value, the PWM resolution will change.
Levism



Joined: 02 Aug 2015
Posts: 3

View user's profile Send private message

Thanks for all the help!
PostPosted: Sun Aug 02, 2015 2:03 pm     Reply with quote

I got it guys, changed a few things as recommended!
Below is my final code:
Code:

#include <16f876.h>
#device ADC=8
#use delay(clock=4000000)
#fuses XT,NOWDT,PUT,NOBROWNOUT,NOLVP

int conta=0;
int resultado=0;
long int ciclo=0;
int conv=0;

//--------------------------------------------------------------------

#INT_TIMER0
void trata_t0() {   
resultado = read_adc();
set_timer0(get_timer0());
conta++;
if (conta==31) {
    conta=0;   
  }
}
//---------------------------------------------------------------
void main (){

   SETUP_ADC_PORTS(RA0_ANALOG);
   setup_ADC(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);
 

   enable_interrupts(INT_TIMER0); 
   enable_interrupts(GLOBAL);   
 
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64);
   setup_timer_2 (T2_DIV_BY_4, 61, 1);
   setup_ccp1 (ccp_pwm);                 
   
   set_pwm1_duty ( 0 );
   
   while(true){
     
   for (ciclo=0; ciclo<resultado; ciclo++){
        set_pwm1_duty (ciclo); 
        delay_ms(50);
       }
   delay_ms(1000);
   }
}


Thanks again for the great tips!
_________________
Life is but dust.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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