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

18F4520 PWM2 problem

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



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

18F4520 PWM2 problem
PostPosted: Sat May 29, 2010 10:09 am     Reply with quote

Hi all,
I'm generating a pulse for a transformer with 16F628 using the code below
Code:
    for(;;){
      output_low(LED);
      delay_us(25);
      output_high(LED);
      delay_us(30);
    }

So, I wanna do the same thing using 18F4520's PWM pin.
I'm using as PWM the CCP2 out of 18F4520.
And the code is here:
Code:
#separate
void SwitchingStart(void)
{
   long duty = 552;
   output_low(PIN_C1);
   setup_ccp2(CCP_PWM);
   setup_timer_2(T2_DIV_BY_4,109,1); // 18181 Hz
   set_pwm2_duty(duty); // 54%
}

#separate
void SwitchingStop(void)
{
   setup_ccp2(CCP_OFF);
   output_low(PIN_C1); // ccp2
   setup_timer_2(T2_DISABLED,0,1);
}


The ccp2 pin is connected through a 1k resistor to an NPN transistor's base.

But maybe I could not calculate the timer parameters and on the out of the transformer there are not any voltage. It worked with 16f628-code.

Where am I wrong?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 29, 2010 1:55 pm     Reply with quote

Create a very simple test program like this, except for your PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=41512&start=1
Test it. If it doesn't work, then post the program. The program must
show the #fuses, #use delay, etc. It must be compilable.
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Sat May 29, 2010 5:33 pm     Reply with quote

The simple program works without problem. But when I change the timer and duty values, it does not work.

Working code:
Code:
#include <18F4520.h>
#device *=16
#device adc=10
#FUSES WDT128                  //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PROTECT                  //Code protected from reads
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV27                   //Brownout reset at 2.7V
#FUSES PUT                      //No Power Up Timer
#FUSES CPD                      //Data EEPROM Code Protected
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES NOIESO                   //Internal External Switch Over mode enabled
#FUSES NOFCMEN                  //Fail-safe clock monitor enabled
#FUSES NOPBADEN                 //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES EBTR                     //Memory protected from table reads
#FUSES EBTRB                    //Boot block protected from table reads
#FUSES CPB                      //Boot Block Code Protected
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOMCLR                   //Master Clear pin disabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(clock=32M,restart_wdt) // 4 x 8Mhz

void main(void)
{
setup_ccp2(CCP_PWM);   
setup_timer_2(T2_DIV_BY_1, 255, 1);       
set_pwm2_duty(128);

while(1){restart_wdt();}
}


values calculated by pic wizard:
Code:
setup_ccp2(CCP_PWM);   
setup_timer_2(T2_DIV_BY_4, 109, 1);       
set_pwm2_duty(552);
The program does not work with these values above.

Last edited by tesla80 on Sat May 29, 2010 5:38 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 29, 2010 5:38 pm     Reply with quote

Post the PWM frequency and duty cycle (in percent) that you want.
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Sat May 29, 2010 5:41 pm     Reply with quote

I want to generate the same signal which the code below generates
Code:
    for(;;){
      output_low(LED);
      delay_us(25);
      output_high(LED);
      delay_us(30);
    }


I think it is 18181 Hz 54%
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 29, 2010 5:52 pm     Reply with quote

Quote:
setup_timer_2(T2_DIV_BY_4, 109, 1);
set_pwm2_duty(55L);

I think if you change the duty cycle value to 55L, it might work better.
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Sat May 29, 2010 6:04 pm     Reply with quote

Thanks, I can't see the same voltage on seconder of the transformer but there is change.

What means the L ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 29, 2010 7:04 pm     Reply with quote

It puts it in 10-bit PWM mode (instead of 8-bit).

Actually 55L isn't correct for your desired duty cycle of 54%.

I should have said to use 238L for 10-bit mode or use 59 for 8-bit mode.
Example:
Code:

setup_pwm2_duty(59);  // 8-bit PWM mode

or
Code:

setup_pwm2_duty(238L);  // 10-bit PWM mode
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sun May 30, 2010 2:56 am     Reply with quote

This is what I learned
Thanks PCM programmer
Code:
    set_timer2(0);

 //// the timer2 post scaler is not used for PWM
 /// Ex PIR2=9 osc=8mhz t2_div_by_1 freq=200khz
///  value = Fosc / (Fpwm * 4 * T2DIV) - 1  basically clock/4/(Fpwm*t2 divisor)-1
///  where  value and T2DIV go into setup_timer_2(T2_DIV_BY_X, value aka PIR2, 1);
///  now to get the resolution high we want PIR2 at about 255
//// this isn't always possible depending on the clock aka Fosc so resolution is often lost
//// even when the pre scaler is the the miniumum of t2_Div_by_ 1
///  Ex t2DIV =1 Tosc 8mhz Fpwm=200khz PIR2+1=10 basically for 50% use 5 (8 bit) or 20L (10 bit)
///  value = Desired_Duty_Cycle% * (PIR2+1) 8bit or Desired_Duty_Cycle% * (PIR2+1)*4 10bit
///  set_pwm1_duty(value);
/// Duty cycle  is 8bits or 10 bits depending if an int8 or int16 is passed
/// Ex setup_timer_2 ( T2_DIV_BY_1, 9, 1); for fpwm of 200khz at 8mhz
/// for 50% duty use set_pwm1_duty(5) 8bit or  set_pwm1_duty(20L) 10bit
///
setup_timer_2 ( T2_DIV_BY_1, 9, 1);
setup_ccp1(CCP_PWM);///// setup PWM to be on CCP1
//set_pwm1_duty(5); ////// Ex 50% is 5/(9+1)
set_pwm1_duty(20L); ////// Ex 50% is 20/(4*(9+1))


The actual precision is not the full 8 bits or 10 bits due to the loss of precision in setting PIR2
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Sun May 30, 2010 3:12 pm     Reply with quote

PCM programmer wrote:
Example:
Code:

setup_pwm2_duty(59);  // 8-bit PWM mode

or
Code:

setup_pwm2_duty(238L);  // 10-bit PWM mode

Hi, thanks,

There are difference between 10-bit variant and the 8-bit one.
10-bit variant generates 26us "1" and 20us "0".
8-bit variant generates 30us "1" and 25us "0".

In 10-bit variant the transistor heats up.
In 8-bit , everything is what I want.

Best regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun May 30, 2010 3:38 pm     Reply with quote

When I wrote that answer, I wasn't at my office and I was tired from
working in the sun all afternoon. Sorry if it wasn't an exact answer.
I won't do that again.
tesla80



Joined: 23 May 2007
Posts: 81

View user's profile Send private message

PostPosted: Sun May 30, 2010 3:53 pm     Reply with quote

Actually, It was exact answer.
Thanks again for your help.
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