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 support@ccsinfo.com

16F877A pwm trouble

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



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

16F877A pwm trouble
PostPosted: Mon Oct 19, 2009 6:25 am     Reply with quote

hi,
I'm using 16F877A to generate 38kHz for IR transmitter using pwm channel. I have used a 10Mhz crystal. CCS C version is 4.084.

My problem is at 50% duty voltage at pwm pin is about 2.4v
and at 30% duty voltage still 2.4v. At 5% duty voltage is about 1.6v.
Why isn't voltage changing according to duty?

And another problem is if I generate 1khz signal, 50% duty value is 512
but if I use 38khz the 50% duty value is 132. How does this happen?
And how to interface ADC value directly to pwm duty in this manner?
(if I use 1KHz I can directly use ADC value as duty).

Here is my code:
Code:

void main()
{
   INT count=0;
   setup_adc_ports (NO_ANALOGS) ;
   setup_adc (ADC_OFF) ;
   setup_psp (PSP_DISABLED) ;
   setup_spi (SPI_SS_DISABLED) ;
   setup_timer_0 (RTCC_INTERNAL|RTCC_DIV_1) ;
   setup_timer_1 (T1_DISABLED) ;
   setup_timer_2 (T2_DIV_BY_1, 65, 1) ;
   setup_ccp1 (CCP_PWM) ;
   setup_ccp2 (CCP_PWM) ;
   set_pwm1_duty (0) ;
   set_pwm2_duty (0) ;
   setup_comparator (NC_NC_NC_NC) ;
   setup_vref (FALSE) ;

   WHILE (1)
   {
      IF (input (PIN_B5)||input (PIN_A2))
      {
      output_high(PIN_B4);
         set_pwm1_duty (132) ; // 50% duty
         set_pwm2_duty (132) ;
      }

      ELSE
      {
      output_low(PIN_B4);
         set_pwm1_duty (13) ; // 5% duty
         set_pwm2_duty (13) ;
      }

      delay_us (600) ;
      set_pwm1_duty (0) ;
      set_pwm2_duty (0) ;
      delay_us (600) ;
     
   }

   // TODO: USER CODE!!
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 19, 2009 11:06 am     Reply with quote

Quote:
My problem is at 50% duty voltage at pwm pin is about 2.4v
and at 30% duty voltage still 2.4v. At 5% duty voltage is about 1.6v.
Why isn't voltage changing according to duty?

We are not mind readers. Tell us what external circuits are connected
to the PWM pin. Tell us the Vdd voltage of the PIC. Tell us how you
are measuring the PWM voltage. Is it the peak-to-peak voltage of the
PWM signal ? Are you using an oscilloscope to measure it ?


Quote:

And another problem is if I generate 1khz signal, 50% duty value is 512
but if I use 38khz the 50% duty value is 132. How does this happen?
And how to interface ADC value directly to pwm duty in this manner?
(if I use 1KHz I can directly use ADC value as duty).

Again, give full details. Show us the setup_timer_2() statements for
the 1 KHz mode and the 38 KHz mode. Post the set_pwm1_duty()
statement for each mode. Post the PIC's oscillator frequency for
each mode. Post the variable declarations of the duty cycle variable
(if a variable is used). Always post full details if you want an answer.
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Mon Oct 19, 2009 11:11 am     Reply with quote

PCM programmer wrote:
Quote:
My problem is at 50% duty voltage at pwm pin is about 2.4v
and at 30% duty voltage still 2.4v. At 5% duty voltage is about 1.6v.
Why isn't voltage changing according to duty?

We are not mind readers. Tell us what external circuits are connected
to the PWM pin. Tell us the Vdd voltage of the PIC. Tell us how you
are measuring the PWM voltage. Is it the peak-to-peak voltage of the
PWM signal ? Are you using an oscilloscope to measure it ?


Quote:

And another problem is if I generate 1khz signal, 50% duty value is 512
but if I use 38khz the 50% duty value is 132. How does this happen?
And how to interface ADC value directly to pwm duty in this manner?
(if I use 1KHz I can directly use ADC value as duty).

Again, give full details. Show us the setup_timer_2() statements for
the 1 KHz mode and the 38 KHz mode. Post the set_pwm1_duty()
statement for each mode. Post the PIC's oscillator frequency for
each mode. Post the variable declarations of the duty cycle variable
(if a variable is used). Always post full details if you want an answer.


Well oscillator frequency is 10MHZ all the time.
No external circuits. I just connected digital multimeter to pwm pin and measured it.
Posted code above. I used wizard to s\configure settings (pwm frequency initial duty,etc).
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 19, 2009 11:15 am     Reply with quote

You didn't post most of what I requested.

The meter is averaging the PWM signal. I don't know what the bandwidth
of the meter is. I wouldn't consider the meter to be a reliable method of
measuring the duty cycle. You need an oscilloscope for that.
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Mon Oct 19, 2009 11:18 am     Reply with quote

PCM programmer wrote:
You didn't post most of what I requested.

The meter is averaging the PWM signal. I don't know what the bandwidth
of the meter is. I wouldn't consider the meter to be a reliable method of
measuring the duty cycle. You need an oscilloscope for that.


vdd is 5v
And I don't know how to get that timer 2 thing you requested?

Anyway since DMM average voltage it gives corrct value for 50% pwm -->2.4v

But what about 30% and 5% ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 19, 2009 11:24 am     Reply with quote

All you have to do is copy and paste the line for setup_timer_2() from
your programs for 1 KHz operation, and for 38 KHz operation, into a post.

I said, how do we know what the bandwidth is for your meter ?
How do we know that it has a linear transfer function for converting
fractional duty cycles of a rectangular waveform into a DC voltage ?
We don't know.

I've answered all that I can on this thread. Someone else can
perhaps provide some more help.
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Mon Oct 19, 2009 11:28 am     Reply with quote

PCM programmer wrote:
All you have to do is copy and paste the line for setup_timer_2() from
your programs for 1 KHz operation, and for 38 KHz operation, into a post.

I said, how do we know what the bandwidth is for your meter ?
How do we know that it has a linear transfer function for converting
fractional duty cycles of a rectangular waveform into a DC voltage ?
We don't know.

I've answered all that I can on this thread. Someone else can
perhaps provide some more help.



this is for 38khz

setup_timer_2 (T2_DIV_BY_1, 65, 1) ;

this is for 1khz
setup_timer_2(T2_DIV_BY_16,155,1);


i dont have any info about multimeter.

i dont have it for 1khz,coz i actually didnt save main with Embarassed
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Mon Oct 19, 2009 12:18 pm     Reply with quote

aruna1 wrote:


this is for 38khz

setup_timer_2 (T2_DIV_BY_1, 65, 1) ;

this is for 1khz
setup_timer_2(T2_DIV_BY_16,155,1);


Those are the correct setup for a 38Khz and 1Khz pwm at 10 MHz. From your duty cycle values it looks like you are using 10 bit pwm instead of 8. If you want to directly drive the pwm with your 10 bit A/D you have to scale your A/D reading something like this:

Code:


long a_to_d_reading;
long duty_cycle;

//for 38Khz pwm
duty_cycle = (long) (a_to_d_reading*4*65/1024.0);

//for 1Khz pwm
duty_cycle = (long) (a_to_d_reading*4*155/1024.0);




You are measuring something that is not 30% or 5% most likely because you are not meeting the criteria in your program for generating a 30% or 5% signal. You are not going to know until you actually test your circuit with a scope as PCM Programmer pointed out.

If possible you should post a complete test program with the fuses. Right now we don't know whether you are using an external crystal or internal oscillator, or whether you set it up correctly.


Last edited by mkuang on Mon Oct 19, 2009 12:29 pm; edited 1 time in total
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Mon Oct 19, 2009 12:22 pm     Reply with quote

mkuang wrote:
aruna1 wrote:


this is for 38khz

setup_timer_2 (T2_DIV_BY_1, 65, 1) ;

this is for 1khz
setup_timer_2(T2_DIV_BY_16,155,1);


Those are the correct setup for a 38Khz and 1Khz pwm at 10 MHz. From your duty cycle values it looks like you are using 10 bit pwm instead of 8. If you want to directly drive the pwm with your 10 bit A/D you have to scale your A/D reading something like this:

Code:


long a_to_d_reading;
long duty_cycle;

//for 38Khz pwm
duty_cycle = (long) (a_to_d_reading*4*65/1024.0);

//for 1Khz pwm
duty_cycle = (long) (a_to_d_reading*4*155/1024.0);




You are measuring something that is not 30% or 5% most likely because you are not meeting the criteria in your program for generating a 30% or 5% signal. You are not going to know until you actually test your circuit with a scope as PCM Programmer pointed out.

If possible you should post a complete test program with the fuses. Right now we don't know whether you are using an external crystal or internal oscillator, or whether you set it up correctly.

Well problem is I don't have a oscilloscope.
And I wonder why PIC can't give pwm as I told it to do? I mean shouldn't PIC do as I command it through code?
bkamen



Joined: 07 Jan 2004
Posts: 1611
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Mon Oct 19, 2009 4:59 pm     Reply with quote

aruna1 wrote:

Well problem is I don't have a oscilloscope.
And I wonder why PIC can't give pwm as I told it to do? I mean shouldn't PIC do as I command it through code?


You really need a scope.

You have to keep in mind you're plugging a square wave into a meter that probably rolls off at 1000Hz at best.

your PIC is probably working fine. The way you're trying to measure the output is not fine.
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
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