Hardware PWM (HPWM) Clairification on 18F45K22


Results 1 to 9 of 9

Threaded View

  1. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Hardware PWM (HPWM) Clairification on 18F45K22

    Demon,

    The word "define" is a PBP statement, and PBP is not case sensitive. So it doesn't matter what case DeFiNe is.
    What follows "define" is passed on to the assembler, and MPASM IS case sensitive.

    Any defines that PBP is expecting to see, must be UPPERCASE because that's what PBP is looking for.
    Defines used in your own assembly language routines can be any case, as long as your code is looking for the same case.

    neondales's defines have the correct case , even though they are unnecessary and use the wrong timers.

    neondale,

    You don't need any of what you posted, except for the ANSEL's.
    And since you are just fading LED's, the CCP's can all run off of the same Timer, which is TIMER2 by default.

    Here's a program that starts up CCP1-4 at 3Khz - 50% dutycycle.
    Note that the default location for CCP3 is PORTB.5.
    You can move it to PORTE.0 by changing the CCP3MX_ configuration bit.
    If you move it ... change the CCP3 defines to match.
    Code:
    ;----[Device Configuration]--(See manual section 4.9)---------------------------
    #CONFIG
        __config  _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_ON_1H & _PRICLKEN_ON_1H & _FCMEN_ON_1H & _IESO_ON_1H
        __config  _CONFIG2L, _PWRTEN_OFF_2L & _BOREN_SBORDIS_2L & _BORV_190_2L
        __config  _CONFIG2H, _WDTEN_ON_2H & _WDTPS_32768_2H
        __config  _CONFIG3H, _CCP2MX_PORTC1_3H & _PBADEN_OFF_3H & _CCP3MX_PORTB5_3H & _HFOFST_ON_3H & _T3CMX_PORTC0_3H & _P2BMX_PORTD2_3H & _MCLRE_EXTMCLR_3H
        __config  _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    #ENDCONFIG
    
    ;----[DEFINEs]------------------------------------------------------------------
    DEFINE OSC 4               ; Oscillator is 4Mhz
    
    DEFINE CCP1_REG PORTC      ; Tell PBP which pins the CCP outputs are on
    DEFINE CCP1_BIT 2
    DEFINE CCP2_REG PORTC
    DEFINE CCP2_BIT 1
    DEFINE CCP3_REG PORTB
    DEFINE CCP3_BIT 5
    DEFINE CCP4_REG PORTD
    DEFINE CCP4_BIT 1
    
    ;----[Aliases]------------------------------------------------------------------
    LED       PIN 0       ; LED on PORTB.0
    
    ;----[Variables]----------------------------------------------------------------
    Duty      VAR BYTE    ; Duty Cycle 
    Chan      VAR BYTE    ; HPWM Channel
    FadeDir   VAR BYTE    ; Fade Direction (1=up, -1=down)
    
    ;----[Constants]----------------------------------------------------------------
    Freq      CON 3000    ; HPWM Frequency
    MaxBright CON 180     ; Maximum Fade Brightness
    
    ;----[Initialize]---------------------------------------------------------------
    OSCCON = %01010000    ; 4Mhz Internal OSC
    
    ANSELA = 0            ; All Digital
    ANSELB = 0
    ANSELC = 0
    ANSELD = 0
    ANSELE = 0
    
    FadeDir = 1           ; Start fading up
    Duty = 127            ; 50% dutycycle
    ;GOTO Fade1            ; Run Fading Routine
    
    HPWM 1, Duty, Freq    ; start the Hardware PWM channels
    HPWM 2, Duty, Freq
    HPWM 3, Duty, Freq
    HPWM 4, Duty, Freq
    
    ;----[Main Program Loop]--------------------------------------------------------
    Main:
        TOGGLE LED
        PAUSE 500
    GOTO Main
    
    
    ;----[Fade all channels up and down]---------------------------------------------
    Fade1:
        FOR Chan = 1 TO 4
            HPWM Chan, Duty, Freq
        NEXT Chan
        IF Duty = MaxBright THEN FadeDir = -1
        IF Duty = 0 THEN FadeDir = 1
        Duty = Duty + FadeDir
        PAUSE 10
    GOTO Fade1
    Uncomment the GOTO Fade1 to run the Fade routine.
    Last edited by Darrel Taylor; - 23rd June 2013 at 00:04.
    DT

Similar Threads

  1. manual hardware PWM setup on 16F1824
    By comwarrior in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 31st January 2011, 19:34
  2. Hardware PWM Question?
    By jhorsburgh in forum General
    Replies: 1
    Last Post: - 18th August 2008, 04:07
  3. Inverting the o/p of hardware PWM
    By AndrewC in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 7th August 2008, 20:36
  4. 3 Hardware PWM.
    By sirvo in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 30th January 2008, 13:21
  5. pics with 3 hardware PWM ports
    By CBUK in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th August 2004, 01:14

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts