• 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: Proton_Hpwm_Macros.zip

    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 "HPWM.inc"                              ' 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 HPWM.inc file is fully commented, therefore extensions or changes should be relatively straightforward.
  • Recent Activity


    Mysterious PORTB problem

    Thread Starter: xldaedalus

    I'm using Proton+ to develop firmware for a product with switches. The MCU is an 18F26K22. Most of the switches reside on PORTB. I am NOT using a...

    xldaedalus Today, 03:21 Go to last post

    PIC18F1330 PLL problem

    Thread Starter: rcurl

    I'm just starting out on a new project that uses a PIC18F1330. I haven't used this specific chip before, so I began by trying to blink an LED just...

    tumbleweed Today, 11:13 Go to last post

    Pic16f18877 oread

    Thread Starter: evoortman

    Hi, On a PIC16F18877 the OREAD command doesn't seem to work. The code is working on a PIC16F1939. Both controllers use 32MHz int osc. If i...

    See_Mos Today, 11:19 Go to last post

    Lin bus and auto baud detection

    Thread Starter: Tim

    Lin bus looks to be a great system for an application i'm working on. One of the main things is the slave can be really simple without needing a...

    towlerg Yesterday, 15:13 Go to last post