Proton BASIC Compiler - Flexible Hpwm routines for 18F devices

  • PicŪ Basic

  • Flexible Hpwm routines for 18F devices

    I've created a selection of macros that allow much more flexibility for controlling the CCP peripherals operating as PWM on suitable 18F devices.

    These can be found here, along with a demo program:

    The demo program for use on an 18F device is also listed below:

    ' Hardware PWM routines that make use of the extra CCP peripherals contained on some devices
    ' It also allows frequencies upto 65535Hz
    ' Low end frequencies and high end frequencies depend on the oscillator value used.
    ' Frequencies may be approximate depending on the oscillator value used and the frequency required.
    ' This is not due to a limitation of the compiler, but of the peripheral itself.
    ' Note. All PWM peripherals must share a common frequency
    ' Only for 18F devices that have similar methods of setting up CCP periperals
    ' For use only with version onwards of the Proton Compiler.
        Device = 18F25K22                               ' Use a device that has 4 CCP peripherals
        Declare Xtal = 64                               
        Declare Optimiser_Level = 2
        Declare Dead_Code_Remove = 1
    ' Setup which pins are used by the CCP peripherals
    ' Note that for some of these, config fuse changes will be required
        Declare CCP1_Pin = PORTC.2
        Declare CCP2_Pin = PORTC.1   
        Declare CCP3_Pin = PORTB.5
        Declare CCP4_Pin = PORTB.0
        Include ""                              ' Load the HPWM macros into the program
        Dim bLoop As Byte
        Dim wFrequency As Word
        All_Digital = On
        For wFrequency = 0 To 65535  Step 64              ' Create a Loop to sweep through the possible frequencies
            HPWM1_SetFreq(wFrequency,127)                ' 50% duty at all possible frequencies
            HPWM2_SetFreq(wFrequency,127)                ' 50% duty at all possible frequencies
            HPWM3_SetFreq(wFrequency,127)                ' 50% duty at all possible frequencies
            HPWM4_SetFreq(wFrequency,127)                ' 50% duty at all possible frequencies
            DelayMS 1
        HPWM1_Stop()                                    ' Stop CCP1
        HPWM2_Stop()                                    ' Stop CCP2
        HPWM3_Stop()                                    ' Stop CCP3
        HPWM4_Stop()                                    ' Stop CCP4
    ' Start a single CCP PWM peripheral which will setup and start Timer 2
    ' Then only adjust the duty cycle of each CCP peripheral
    ' This is a much better method in both code and speed of operation
        HPWM1_SetFreq(40000,0)                          ' 0% duty at 40KHz and start CCP1
        HPWM2_Start()                                   ' Start CCP2
        HPWM3_Start()                                   ' Start CCP3
        HPWM4_Start()                                   ' Start CCP4
        For bLoop = 0 To 255                            ' Create a duty cycle Loop
            HPWM1_Duty(bLoop)                           ' Alter the duty cycle of CCP1
            HPWM2_Duty(bLoop)                           ' Alter the duty cycle of CCP2
            HPWM3_Duty(bLoop)                           ' Alter the duty cycle of CCP3
            HPWM4_Duty(bLoop)                           ' Alter the duty cycle of CCP4
            DelayMS 10 
        HPWM1_Stop()                                    ' Stop CCP1
        HPWM2_Stop()                                    ' Stop CCP2
        HPWM3_Stop()                                    ' Stop CCP3
        HPWM4_Stop()                                    ' Stop CCP4
     FOSC = HSHP           ' HS oscillator (high power > 16 MHz)
     PLLCFG = On           ' Oscillator multiplied by 4
     PRICLKEN = On         ' Primary clock enabled
     FCMEN = Off           ' Fail-Safe Clock Monitor disabled
     IESO = Off            ' Internal/External Oscillator Switchover mode disabled
     PWRTEN = On           ' Power up timer enabled
     BOREN = SBORDIS       ' Brown-out Reset enabled in hardware only (SBOREN is disabled)
     BORV = 190            ' Brown Out Reset Voltage set to 1.90 V nominal
     WDTEN = Off           ' Watch dog timer is always disabled. SWDTEN has no effect.
     WDTPS = 128           ' Watchdog Timer Postscale 1:128
     CCP2MX = PORTC1       ' CCP2 input/output is multiplexed with RC1
     PBADEN = Off          ' PORTB<5:0> pins are configured as digital I/O on Reset
     CCP3MX = PORTB5       ' P3A/CCP3 input/output is multiplexed with RB5
     HFOFST = On           ' HFINTOSC output and ready status are not delayed by the oscillator stable status
     T3CMX = PORTC0        ' Timer3 Clock Input (T3CKI) is on RC0
     P2BMX = PORTB5        ' ECCP2 B (P2B) is on RB5
     MCLRE = EXTMCLR       ' MCLR pin enabled, RE3 input pin disabled
     STVREN = Off          ' Stack full/underflow will not cause Reset
     LVP = Off             ' Single-Supply ICSP disabled
     XINST = Off           ' Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
     Debug = Off           ' Disabled
     CP0 = Off             ' Block 0 (000800-001FFFh) not code-protected
     CP1 = Off             ' Block 1 (002000-003FFFh) not code-protected
     CP2 = Off             ' Block 2 (004000-005FFFh) not code-protected
     CP3 = Off             ' Block 3 (006000-007FFFh) not code-protected
     CPB = Off             ' Boot block (000000-0007FFh) not code-protected
     CPD = Off             ' Data EEPROM not code-protected
     WRT0 = Off            ' Block 0 (000800-001FFFh) not write-protected
     WRT1 = Off            ' Block 1 (002000-003FFFh) not write-protected
     WRT2 = Off            ' Block 2 (004000-005FFFh) not write-protected
     WRT3 = Off            ' Block 3 (006000-007FFFh) not write-protected
     WRTC = Off            ' Configuration registers (300000-3000FFh) not write-protected
     WRTB = Off            ' Boot Block (000000-0007FFh) not write-protected
     WRTD = Off            ' Data EEPROM not write-protected
     EBTR0 = Off           ' Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
     EBTR1 = Off           ' Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
     EBTR2 = Off           ' Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
     EBTR3 = Off           ' Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
     EBTRB = Off           ' Boot Block (000000-0007FFh) not protected from table reads executed in other blocks

    The file is fully commented, therefore extensions or changes should be relatively straightforward.
  • Recent Activity


    Valid time in NMEA sentance

    Thread Starter: SimonJA

    Is there any way to know that a GPS nmea sentence has the correct time? I use a module to keep a pic rtc accurate but have noticed that when...

    david Yesterday, 20:40 Go to last post

    I'm over the moon with joy. :-)

    Thread Starter: Les

    I just had to show you this, because I am over the moon with it. LOL I've just about finished procedures on the Proton 8-bit compiler so I am busy...

    towlerg Yesterday, 16:13 Go to last post

    GPS speed logger for RC planes.

    Thread Starter: david

    Hi All, This is a simple project to allow the logging of RC plane speeds to help determine battery, motor and prop variables. The code is crude and...

    david Yesterday, 21:01 Go to last post