Pic18f46k20 hpwm issue


Yesterday 23:54
Forum: Proton Plus Compiler v3
Starter: rcurl
Views: 0
Replies: 2
Go to last post By: normnet
Yesterday 22:20
Forum: FineLineIDE
Starter: normnet
Views: 33570
Replies: 145
Yesterday 19:03
Forum: Proton Studio
Starter: Puffeltje
Views: 0
Replies: 16
Go to last post By: ChrisRowe
Yesterday 15:32
Forum: Proton Studio
Starter: ChrisRowe
Views: 0
Replies: 0
Yesterday 13:48
Forum: Proton Studio
Starter: ChrisRowe
Views: 0
Replies: 0
Yesterday 10:44
Forum: Proton Studio
Starter: wirecut
Views: 0
Replies: 2
Yesterday 08:29
Forum: The Lounge
Starter: towlerg
Views: 0
Replies: 4
+ Reply to Thread
Results 1 to 4 of 4

Thread: Pic18f46k20 hpwm issue167 days old

  1. #1
    Junior Member Elliot's Avatar
    Join Date
    Dec 2018
    Posts
    4
    Thumbs Up
    Received: 1
    Given: 0
    Total Downloaded
    304.56 MB

    0 Not allowed!

    Default Pic18f46k20 hpwm issue

    Hi,

    I'm literally pulling my hair out on this one, any help here would be much appreciated.

    For some reason the Duty cycle is stepped from 0 to about 10, then it stays the same. I should have a nice progressive 0-255 brightness change on my 7 segment LED display but only the first 10 are different.

    Is it the PIC configuration? I've read the datasheet a dozen times and searched the Forum, I just can't seem to get 0-255 out of the HPWM channel.

    Thanks

    Code:
    '----------------------------------------------------------------------------------------------------------------------------------------***
    '*  Name            : 
    '*  Author          : 
    '*  Notice          : 
    '*                  : 
    '*  Date            : 
    '*  Version_Current : 
    '*  Notes           :                                                   
    '*                  :                                                   
    '----------------------------------------------------------------------------------------------------------------------------------------***
    
    
    Device = 18F46K20
    
    
      Config_Start
        FOSC = INTIO67 ; Internal oscillator block, port function on RA6 and RA7
        FCMEN = OFF ; Fail-Safe Clock Monitor disabled
        IESO = OFF ; Oscillator Switchover mode disabled
        PWRT = Off ; PWRT enabled
        BOREN = Off ; Brown-out Reset enabled and controlled by software (SBOREN is enabled)
        BORV = 27 ; VBOR set to 2.7 V nominal
        WDTEN = OFF ; WDT is controlled by SWDTEN bit of the WDTCON register
        WDTPS = 1 ; 1:1
        MCLRE = Off ; MCLR pin enabled, RE3 input pin disabled
        HFOFST = OFF ; The system clock is held off until the HF-INTOSC is stable.
        LPT1OSC = OFF ; Disabled, T1 operates in standard power mode.
        PBADEN = OFF ; PORTB<4:0> pins are configured as digital I/O on Reset
        CCP2MX = PORTC ; CCP2 input/output is multiplexed with RC1
        STVREN = On ; Stack full/underflow will cause Reset
        LVP = OFF ; Single-Supply ICSP disabled
        XINST = OFF ; Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
        Debug = OFF ; Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
        Cp0 = OFF ; Block 0 (000800-003FFFh) not code-protected
        CP1 = OFF ; Block 1 (004000-007FFFh) not code-protected
        CP2 = OFF ; Block 2 (008000-00BFFFh) not code-protected
        CP3 = OFF ; Block 3 (00C000-00FFFFh) not code-protected
        CPB = OFF ; Boot block (000000-0007FFh) not code-protected
        CPD = OFF ; Data EEPROM not code-protected
        WRT0 = OFF ; Block 0 (000800-003FFFh) not write-protected
        WRT1 = OFF ; Block 1 (004000-007FFFh) not write-protected
        WRT2 = OFF ; Block 2 (008000-00BFFFh) not write-protected
        WRT3 = OFF ; Block 3 (00C000-00FFFFh) not write-protected
        WRTB = OFF ; Boot block (000000-0007FFh) not write-protected
        WRTC = OFF ; Configuration registers (300000-3000FFh) not write-protected
        WRTD = OFF ; Data EEPROM not write-protected
        EBTR0 = OFF ; Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
        EBTR1 = OFF ; Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
        EBTR2 = OFF ; Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
        EBTR3 = OFF ; Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
        EBTRB = OFF ; Boot block (000000-0007FFh) not protected from table reads executed in other blocks
      Config_End
    
    
      '64 MHz INTERNAL
      Xtal = 64
      OSCCON = %01110100  '16MHz INTERNAL Primary clock
      OSCTUNE = %01000000 '%x1xxxxxx = PLL ENABLED
      
      
      OSCTUNE.5 = 0
      OSCTUNE.4 = 0
      OSCTUNE.3 = 0
      OSCTUNE.2 = 1
      OSCTUNE.1 = 1
      OSCTUNE.0 = 1
      
      T2CON = %00000110
    
    
        
        Declare Hserial_Baud 115200
        Declare Hserial_RCSTA = %10010000  ' Enable serial port and continuous receive
        Declare Hserial_TXSTA = %00100000  ' Enable transmit and asynchronous mode2 
        Declare Hserial_Clear = On         ' OptionaLL2y clear the buffer before receiving
                                    
        
        
        All_Digital true
        
        Declare Bootloader OFF
        
    
    
        Declare Serial_Baud 2400                                'with a 16mhz 19200 baud
        Declare RsOut_Pin PORTB.7                                 
        'Declare Rsin_Pin PORTC.4                               
        Declare RsOut_Mode true                                 'we are not using a MAX232 so the data can be normal
        Declare RsIn_Mode true                                  'we are not using a MAX232 so the data can be normal
        Declare RsIn_Timeout 500                              'if the PIC get no data in this time then stop receiving
        Declare RsOut_Pace 2
        Declare All_Digital true
      
     
     '----------------------------------------------------------------------------------------------------------------------------------------****    
    
    
    
    
       
       Reminders off
       
       Declare Float_Display_Type = LARGE
       
    ReStart:
        
        ADCON0=%00101001
        ADCON1=%00000000              
        ADCON2=%10110101                  
    
    
    
    
        
        GoSub TRIS_ON
    
    
    '----------------------------------------------------------------------------------------------------------------------------------------***
    
    
    '---------------------------------------------------------------------------------------------------------------------------------------- 
    
    
    Symbol RBIF                    = INTCON.0
    Symbol INTF                    = INTCON.1
    Symbol T0IF                    = INTCON.2
    Symbol RBIE                    = INTCON.3
    Symbol INTE                    = INTCON.4
    Symbol T0IE                    = INTCON.5
    Symbol PEIE                    = INTCON.6
    Symbol GIE                     = INTCON.7
    
    
    
    
    SYMBOLS:                        ' Alias port pins
    
    
    Symbol PIC_Enable               = LATA.6
    
    
    
    
    Symbol LED_A                    = LATD.0
    Symbol LED_B                    = LATD.1
    Symbol LED_C                    = LATD.2
    Symbol LED_D                    = LATD.3
    Symbol LED_E                    = LATD.4
    Symbol LED_F                    = LATD.5
    Symbol LED_G                    = LATD.6
    Symbol LED_DP                   = LATD.7
    
    
    '---------------------------------------------------------------------------------------------------------------------------------------- 
    
    
    
    
    DIMS: 
    
    
    
    
    BITS:
    
    
    
    
    BYTES:
    
    
    Dim A1                              As Byte 
    
    
    WORDS:
    
    
    
    
    DWORDS:
    
    
    
    
    FLOATS:
    
    
    
    
    
    
    STRINGS:
    
    
    
    
    Set_Variables:
    
    
    PIC_Enable = 1
    
    
    
    
    ' MAIN MAIN MAIN Main Main Main Main Main Main Main
    
    
    Main:
    
    
    GoSub LED__All
    
    
    For A1 = 0 To 20
        HPWM 1, A1, 38000'pwm_frequency
        DelayMS 500
    Next A1
    
    
    GoSub LED__OFF
    
    
    
    
    GoTo Main
    
    
    ' MAIN MAIN MAIN Main Main Main Main Main Main Main
    
    
    
    
    '-----------------------------------
    
    
    TRIS_ON:
    
    
        TRISA=%10101111
        TRISB=%01110111
        TRISC=%10010001
        TRISD=%00000000
        TRISE.0 = 1
        TRISE.1 = 0
        TRISE.2 = 1
    
    
    
    
    Return
    
    
    '-----------------------------------
    
    
    LED__All:
    
    
    LED_A = 1
    LED_B = 1
    LED_C = 1
    LED_D = 1
    LED_E = 1
    LED_F = 1
    LED_G = 1
    LED_DP = 1
    
    
    Return
    
    
    '-----------------------------------
    
    
    LED__OFF:
    
    
    LED_A = 0
    LED_B = 0
    LED_C = 0
    LED_D = 0
    LED_E = 0
    LED_F = 0
    LED_G = 0
    LED_DP = 0
    
    
    Return
    '-----------------------------------

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,225
    Thumbs Up
    Received: 371
    Given: 131
    Total Downloaded
    1.58 GB

    0 Not allowed!

    Default PIC18F46K20 HPWM demo

    The HPWM commands are working as expected with the 18F46K20 device. The demo program below sweeps the duty cycle smoothly:

    Code:
    '
    '   /\\\\\\\\\
    '  /\\\///////\\\
    '  \/\\\     \/\\\                                                 /\\\          /\\\
    '   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
    '    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
    '     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
    '      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
    '       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
    '        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
    '                                  Let's find out together what makes a PIC Tick!
    '
    ' Demonstrate HPWM for channels 1 and 2 on a PIC18F46K20 device
    ' Written for the Proton BASIC Compiler by Les Johnson
    '
        Device = 18F46K20
        Declare Xtal = 16
    '
    ' Declare the pins used for CCP1 and CCP2 (HPWM)
    ' This is because some devices have PPS,
    ' and the compiler needs to know which pins to set as outputs etc...
    '    
        Declare HPWM1_Pin = PORTC.2
        Declare HPWM2_Pin = PORTC.1 
        
        Dim bDutyCyle As Byte
        
    Main:
        Do                                  ' Create a loop
            For bDutyCyle = 0 To 255        ' Create a loop for the duty cycle
                HPWM 1, bDutyCyle, 2000     ' Output a PWM signal from CCP1
                HPWM 2, bDutyCyle, 2000     ' Output a PWM signal from CCP2
                DelayMS 10                  ' Slow down a bit so we can see the duty change
            Next                            ' Close the duty loop
        Loop                                ' Do it forever
    And here's the sim displaying the PWM signals:

    HPWM.jpg



















    You need to simplify your program in order to make tests of the PWM signal, then add extras to the program until something unexpected happens, then you know what it is. Also, note that LEDs do not have a linear brightness, so a linear voltage will not illuminate the LED linearly, as it would with an incandescent bulb. This requires a look up table to give the correct voltage to the LED for the voltage outputted. I think it's called an AntiLog, and I remember writing one based upon a calculation many years ago, because each LED type has different characteristics.
    Last edited by top204; 3rd January 2019 at 10:56.

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,225
    Thumbs Up
    Received: 371
    Given: 131
    Total Downloaded
    1.58 GB

    0 Not allowed!

    Default Linearise an LED's brightness using an Antilog table

    Found it.

    Here's the simple program I did for the Amicus18 board that linearises an LED. The values for the calculation need to be changed for different types of LED, and even different families of LEDs, but if I remember, it worked well:

    Code:
    '
    '   /\\\\\\\\\
    '  /\\\///////\\\
    '  \/\\\     \/\\\                                                 /\\\          /\\\
    '   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
    '    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
    '     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
    '      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
    '       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
    '        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
    '                                  Let's find out together what makes a PIC Tick!
    '
    '
    ' Linearise a PWM driven LED
    ' Written for the Proton BASIC Compiler by Les Johnson
    '
        Include "Amicus18.inc"                  ' Set the compiler to use a PIC18F25K20 device operating at 64MHz
        Include "Amicus18_Hpwm8.inc"            ' Load the 8-bit PWM routines into the program
    
        Dim bIndex    As Byte
        Dim bLinValue As Byte
        Dim wIndex    As Word
        Dim wDcyValue As Word
        Dim fTemp     As Float
        Dim fGammaValue As Float = 0.85
    
    $define cTableSize  255
    $define cSteps      255
    
        Dim bLinTable[cTableSize] As Byte
    
    '--------------------------------------------------------------------
        GoTo Main                               ' Jump to the main program loop
    
    '--------------------------------------------------------------------
    ' Create a data array to linearise an LED's brightness for 8-bit PWM
    ' Input     : fGammaValue = gamma correction:  normally between 0.5 to 1.0
    '           : cTableSize holds the size of the array table
    '           : cSteps = Total PWM steps
    ' Output    : Array bLinTable is filled with the values
    ' Notes     : Leo Bodnar's antilogarithmic gamma correction algorithm
    '           : Equivalent to: wDcyValue = cSteps ^ (( (wIndex + 1) / cTableSize) ^ fGammaValue) + 0.3
    '
    Sub Create_LineariseTable()
        Clear bLinTable
        For wIndex = 1 To SizeOf(bLinTable) - 1   ' Make the loop start at 1 so we do not get a division by 0
            fTemp = Pow((wIndex / cTableSize), fGammaValue)
            wDcyValue = Pow(cSteps, fTemp)
            bLinTable[wIndex] = wDcyValue
        Next
    '
    ' Draw the curve on a serial terminal for visualisation
    '
    (*
        HRSOutLn "' fGammaValue = ", Dec2 fGammaValue
        For wIndex = 0 To SizeOf(bLinTable) - 1
            bLinValue = bLinTable[wIndex]
            Hrsout Dec3 bLinValue, "| "
            If bLinValue <= 1 Then
                HRSOut "*",13
            Else
                HRSOut Rep " "\(bLinValue / 4), "*", 13
            EndIf
        Next
    *)
    EndSub
    
    '--------------------------------------------------------------------
    ' Increase and decrease the brightness of 2 LEDs on CCP1 and CCP2 pins.
    ' One LED using the linearisation calculations and one using the index as the Duty
    '
    Main:
    
        Create_LineariseTable()                         ' Create the linearising array table
        Analog12_Open()                                 ' Open PWM 1 and 2
    
        Do
            '
            ' Brighten the LEDs gradually
            '
            For bIndex = 0 To SizeOf(bLinTable) - 1
                bLinValue = bLinTable[bIndex]           ' Read the duty cycle value from the array
                Analog1_Write(bLinValue)                ' Illuminate the LED on PWM 1 using the table value
                Analog2_Write(bIndex)                   ' Illuminate the LED on PWM 2 using the standard duty value
                DelayMS  10 
            Next
            '
            ' Extinguish the LEDs gradually
            '
            For bIndex = SizeOf(bLinTable) - 1 To 0 Step - 1 
                bLinValue = bLinTable[bIndex]           ' Read the duty cycle value from the array
                Analog1_Write(bLinValue)                ' Illuminate the LED on PWM 1 using the table value
                Analog2_Write(bIndex)                   ' Illuminate the LED on PWM 2 using the standard duty value
                DelayMS 10
            Next
        Loop
    With the above program, it can be changed simply to create a Dim As Code (flash memory) table, instead of a byte array. To do this, output the table values to a serial terminal from a calculation that is working to satisfaction, then copy the data from the terminal and make a table from them. Then use the Cread8 command to access the table.
    Last edited by top204; 3rd January 2019 at 10:54.

  6. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  7. #4
    Junior Member Elliot's Avatar
    Join Date
    Dec 2018
    Posts
    4
    Thumbs Up
    Received: 1
    Given: 0
    Total Downloaded
    304.56 MB

    1 Not allowed!

    Default Re: Pic18f46k20 hpwm issue


    • Declare HPWM1_Pin = PORTC.2 Declare HPWM2_Pin = PORTC.1






    It would seem that I didn't get the memo when this was implemented. Thank you Les, I have a working Second working HPWM channel!

  8. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Can't change duty cycle on CCP2 (PORTC.1) PIC18F46K20 HPWM 2
    By Elliot in forum Proton Plus Compiler v3
    Replies: 1
    Last Post: 22nd December 2018, 12:07
  2. [SOLVED !] Issue with HPWM
    By Beginner in forum Proton Plus Compiler v3
    Replies: 4
    Last Post: 14th February 2013, 11:08
  3. 18F2431 HPWM, PWM4 portb.5 issue
    By basparky in forum Proton Plus Compiler v3
    Replies: 3
    Last Post: 6th December 2012, 23:41
  4. PIC18F46K20 Can't read digital input on port D
    By normnet in forum Proton Plus Compiler v3
    Replies: 9
    Last Post: 20th May 2011, 20:20
  5. How to work with PIC18F46K20
    By TEEGEE in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 19th November 2008, 21:42

Members who have read this thread : 41

Actions :  (Set Date)

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

Posting Permissions

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