PWM question

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
mazur50
Posts: 68
Joined: Thu Dec 04, 2008 5:19 am

PWM question

Post by mazur50 » Sat Apr 04, 2009 9:22 am

Can anyone tell me why i am getting only 80Hz out this when the the frequency is set to 1000

Code: Select all


Device = 18F1220                             // Setup the device/clock information
Clock = 8
Config OSC = INTIO2

Dim 
    Brake_Mosfet As PORTB.3,                // Declare the Brake_Mosfet pin
    ABS_Pot As PORTA.0,                     // Declare the ABS_Pot pin
    ABS_Brake_Switch As PORTA.1,            // Declare the ABS_Switch pin
    Full_Brake_Switch As PORTA.2,           // Declare the   Full_Brake_Switch pin
    Release_Brake_Switch As PORTA.3,        // Declare the Release_Brake_Switch pin
    Throttle_Pot As PORTB.0,                // Declare Throttle_Pot pin
    Duty As Byte,

Function Get_PWM_Sample() As word           //  Funtion to grab the ADC sample
    result = ADC.Read(0)                    //  Grab an ADC sample from channel 0
    result = (((result + 5) * 100) / 1024)           //  Outputs Percent of ADC value
End Function

ADCON1 = %11101110

Input(Throttle_Pot)
Input(ABS_Pot)
Output(Brake_Mosfet)
OSCCON = %01110110 

  While true
        If
          (Release_Brake_Switch = 0) And (ABS_Brake_Switch = 0) And (ADC.Read(4) < 9)
        Then
            High(PORTB.6)
            High(PORTB.4)
            Low(PORTB.5)
            PWM.SetFreq(1000)
            Duty = get_pwm_sample
            PWM.SetDutyPercent(Duty)
        Else
                PWM.Stop
              Break
        
        EndIf
        Wend


   

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Sat Apr 04, 2009 10:55 pm

Whenever I deal with the internal oscillator settings (OSCCON and OSCTUNE) I do a test to check if the desired frequency is really achieved.

You have set up for 8MHz. Do a small loop with a delay and blink an LED at a rate you can perceive well, such as 1/2 sec or 1 second duration. The compiler creates very accurate delays in software based on your clock setting.

Code: Select all

While 1=1
   DelayMS(500)
   LED = Not(LED)
Wend
Also, you should be setting your OSCCON and OSCTUNE registers at the very start of your code and with a small delay afterward. Read the datasheet and you will see there is a short period to allow the internal oscillator to come up to speed.

mazur50
Posts: 68
Joined: Thu Dec 04, 2008 5:19 am

Post by mazur50 » Sat Apr 04, 2009 11:31 pm

So I think I got the problem I was doing somthing wrong on the hardware side but I did notice I am only getting like 70 percent duty cycle max

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Sat Apr 04, 2009 11:58 pm

0%, 25%, 50%, 75% and 100% duty should be available at all valid PWM frequencies. Only Duty-Cycle resolution is affected for different frequencies (allowing finer percentage adjustment).

At 8MHz the lowest possible frequency for the PWM module is 488Hz. You can get more info about this on the WIKI:
http://www.sfcompiler.co.uk/wiki/pmwiki ... hUser.PWM2

Post Reply