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

PWM 12f683

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



Joined: 20 Oct 2007
Posts: 9

View user's profile Send private message

PWM 12f683
PostPosted: Tue Oct 27, 2009 2:06 am     Reply with quote

Hi all,
i need to generate ~22KHz PWM with 50% duty cycle. I have connected LED on the CCP1 pin to the +5V. But the LED does shining only when i have
on the line below SETUP_TIMER_2(T2_DIV_BY_1,180,1); When i use SETUP_TIMER_2(T2_DIV_BY_1,91,1) the LED is turned off.

Please can you check my code:

void main()
{
setup_oscillator(OSC_8MHZ);
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_comparator(NC_NC);
set_tris_a(0b11111111);
setup_ccp1(CCP_PWM);
set_pwm1_duty(128); //50% duty cycle
SETUP_TIMER_2(T2_DIV_BY_1,91,1); //91=21 978Hz ,180=11 049Hz
set_tris_a(0b11111011);

while(1)
{
}
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Tue Oct 27, 2009 2:13 am     Reply with quote

Code:

#include <12F683.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOCPD                    //No EE protection
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES PUT                      //Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOFCMEN                  //Fail-safe clock monitor disabled

#use delay(clock=8000000)

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_1,90,1);
   setup_oscillator(OSC_8MHZ);

   setup_ccp1(CCP_PWM);
   set_pwm1_duty(182);
   setup_comparator(NC_NC);
   setup_vref(FALSE);

   while (1)
   {
            delay_cycles(1);
   }

}


Try this. Wink
Ttelmah
Guest







PostPosted: Tue Oct 27, 2009 3:12 am     Reply with quote

Try one critical change:

Either:
Code:

   setup_timer_2(T2_DIV_BY_1,90,1);
   setup_oscillator(OSC_8MHZ);

   setup_ccp1(CCP_PWM);
   set_pwm1_duty(182L); //Note the 'L'


or:
Code:

   setup_timer_2(T2_DIV_BY_1,90,1);
   setup_oscillator(OSC_8MHZ);

   setup_ccp1(CCP_PWM);
   set_pwm1_duty(45);


A search here in the forum, ill find a lot of explanations 'why', and a careful read of the manual for the pwm duty function wll also give the reason.

Best Wishes
Guest








PostPosted: Tue Oct 27, 2009 4:35 am     Reply with quote

Thanks guys,

Code:

   setup_timer_2(T2_DIV_BY_1,90,1);
   setup_oscillator(OSC_8MHZ);

   setup_ccp1(CCP_PWM);
   set_pwm1_duty(45);
 


I think this will not give 50% duty cycle, because duty time is derived from 8MHz oscillator and PWM frequency is derived from 2MHz ( after dividing by 4).
So if we want 50% duty cycle the code must contain
Code:

   setup_timer_2(T2_DIV_BY_4,90,1);

or
Code:

   set_pwm1_duty(180);

Please can you explain the letter L after the number ? I cannot find it in the help.

Milos
Ttelmah
Guest







PostPosted: Tue Oct 27, 2009 6:41 am     Reply with quote

The 45, _will_ give 50% duty cycle (close to). Try them as I posted them.

What 'L' means, will be explained in a general 'C' book, rather than by CCS.

The key is how numbers are handled.

The PWM, has two separate registers used to control the reset point. The first is an 8bit register, while the other is two extra 'bits' stored in the CCPCON register. If you don't put anything into these extra two bits, then the value put into the top 8bit register is used for the count, and is effectively multiplied by 4, for the comparisons. So in your case, the PWM, would count up to 91, if you only access the top register, and 45 or 46 would give 50%.

In the help file:
"Writes the 10-bit value to the PWM to set the duty. An 8-bit value may be used if the least significant bits are not required. If value is an 8 bit item, it is shifted up with two zero bits in the lsb positions to get 10 bits.".
So if the value you are sending is only an 8bit value, it is multiplied by 4 (shifted left 2 places), and used. 45%4 = 180. To write the 10bit value, you have to actually call the function with a value having more than 8bits.

So the question then becomes, 'How do I get the compiler to treat a number with less than 8 significant bits (a value < 255), as if it had more bits'?. This is what 'L' does. It tells the compiler to treat the value _as if it is a 'Long'_, no matter what size it actually is.

So you either want to pass 1/4 the value you want, without the 'L', or add the 'L' to the value.

Best Wishes
Milhaus



Joined: 20 Oct 2007
Posts: 9

View user's profile Send private message

PostPosted: Tue Oct 27, 2009 2:18 pm     Reply with quote

Hi Ttelmah,
Thank you very much for the explanation. Now I understand it !!!
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