Strange Duty Cycle!!

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Strange Duty Cycle!!

Post by Francesco.C » Sat Jul 03, 2010 4:54 pm

Hi guys,

Can you please take a look at this code and tell me why I do not get the result I expect?

I am trying to controll a fan and a MOSFET device using PWM.
I have used 'PicCalc' for my calculation.

The set frequency is 1Khz with a changing duty for controlling the devices.

If for example I set the duty cycle at 200 I would expect to see on the Scope 20% ON and 80% OFF. This is because the Max Duty is 1000.

But on the Scope I see a 90% ON and 20% OFF.

I have tryed to find an answer in the PIC datasheet to ano avail.

Thanks in advance

Francesco


Code: Select all


// set clock and osc...
Device = 18F242
Clock = 4

 // Set values for a 1KHz PWM for a XT at 4MHz
 // Values  calculated using 'PicMultiCalc' program
 
#option PRESCALER = 4       // prescale value 
#option PR2_VALUE = 249      // prescale (PR2 register value)
#option MAX_DUTY = 1000      // max duty for this configuration


// bring options into the main program...
Const
   
   #if PRESCALER = 4
   PrescaleValue = $01,   
   #endif
   PR2Value = PR2_VALUE, 
   MaxDutyValue = MAX_DUTY 
                             
 '============== Set duty  for power output ======================
Sub SetPowerDuty(pDuty As Word)
     CCP1CON.5 = pDuty.1	
     CCP1CON.4 = pDuty.0	
     CCPR1L = pDuty 
End Sub
'=======================================================================

     
 '========== Set Duty  for COOLING FAN  ================================
Sub SetFanDuty(fDuty As Word)  '                                       =
   CCP2CON.5 = fDuty.1	       ' set Duty Cycle bits in register       =
   CCP2CON.4 = fDuty.0	       '                                       =
   CCPR2L = fDuty              '                                       =
End Sub                        '                                       =
'=======================================================================

 
 
// Prepare Ports for Operations  
  
Output(PORTC.2)               // make PORTC.2 an output for power (CCP1)
Output(PORTC.1)               // make PORTC.1 an output  for fan (CCP2)
Output(PORTB.4)               //L.E.D. Pin
 
// Prepare Option Registers for Operation

CCP1CON = %00001100           // set register CCP1 for PWM (Power)
CCP2CON = %00001100           // set register CCP2 for PWM  (Fan)
CCP2MUX(ON)                   // set pin C1 to MUX with CCP2
T2CON = PrescaleValue Or $04  // set prescaler, turn on TMR2 ($04)
PR2 = PR2Value                // set PR2  

	                           
// program start...
   
  
   SetPowerDuty(250)      // Set Duty for power output 
   SetFanDuty(100)      // Set duty for fan speed (Operational)
      
 While true 
  
Wend

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Sun Jul 04, 2010 11:28 am

It doesn't look like you're loading the duty cycle correctly. You're not getting the eight msb's into the CCPRxL register.

Try

Code: Select all

Sub SetPowerDuty(pDuty As Word)
     CCP1CON.5 = pDuty.1   
     CCP1CON.4 = pDuty.0   
     CCPR1L = byte(pDuty>>2)
End Sub
And likewise for CCP2

Francesco.C
Posts: 41
Joined: Thu Feb 26, 2009 6:54 pm
Location: UK

Post by Francesco.C » Sun Jul 04, 2010 6:09 pm

Thank You Jerry, it works. :D

Regards

Francesco

Post Reply