Software solution to the flickering LED problem


+ Reply to Thread
Results 1 to 15 of 15
  1. #1
    Junior Member SebaG's Avatar
    Join Date
    Jul 2020
    Posts
    4
    Thumbs Up
    Received: 0
    Given: 0
    Total Downloaded
    6.6 KB

    0 Not allowed!

    Default Software solution to the flickering LED problem

    I made a simple LED brightness control circuit using PWM from a 12F1840 microcontroller. I read the voltage from the potentiometer and this value is the PWM level. The PWM signal is given on the N-MOS transistor. The most important thing is that I need to get a very smooth increase in brightness from zero to maximum using a potentiometer. There are two problems. 1. At low LED brightness, increasing the brightness does not occur smoothly, but in steps. 2. Due to the inaccuracy of the resistive path of the potentiometer, the diode flashes in some positions and does not shine with a steady light. The system is battery powered. MCP1661 has been used to provide a constant voltage during the discharge of the battery. Turning the ADC on and off is to save the battery. Do you have an idea how to solve these problems? Thanks in advance for your suggestions.

    Code:
    Device = 12F1840
    
    Clear
    
    Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF, BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
    Config2 WRT_OFF, PLLEN_OFF, STVREN_OFF, BORV_19, LVP_OFF
    '-------------------------------------------------------------------------------
    Xtal=32
    '-------------------
    'Optimiser_Level 3
    '-------------------
    OSCCON=110000     
    OSCCONbits_SPLLEN=1                         '8MHZ for OSCCONbits_SPLLEN=0 or 32MHz For OSCCONbits_SPLLEN=1.
    '-------------------
    Clear OPTION_REG
    '-------------------
    'APFCON=000001
    '-------------------
    ADCON1bits_ADFM=0                           '\0=Left justified. Six Least Significant bits of ADRESL are Set To 0 when the conversion Result is loaded. Read only ADRESH to obtain 8bit result
                                                '/1=Right justified. Six Most Significant bits of ADRESH are Set To 0 when the conversion Result is loaded.
    ADCON1bits_ADCS2=0                          '\
    ADCON1bits_ADCS1=1                          '  A/D Conversion Clock Select bits. 010=Fosc/32->1.0us @32MHz. 110=Fosc/32->2.0us @32MHz.
    ADCON1bits_ADCS0=0                          '/
    ADCON1bits_ADPREF1=1                        '\
    ADCON1bits_ADPREF0=1                        '/A/D Positive Voltage Reference Configuration bits. 11=VREF is connected to internal Fixed Voltage Reference (FVR) module. 
    '-------------------
    ANSELA=000011                            'Analog Select between Analog or Digital Function on pins RA<2:0>, respectively 0 = Digital I/O. Pin is assigned to port or digital special function.
                                                '1 = Analog input. Pin is assigned as analog input. Digital input buffer disabled. When setting a pin to an analog input, the corresponding TRIS bit
                                                'must be set to Input mode in order to allow external control of the voltage on the pin.
    '-------------------
    FVRCON=000000
    FVRCONbits_FVREN=1                          'Fixed Voltage Reference is enabled.
    FVRCONbits_ADFVR1=1                         '\
    FVRCONbits_ADFVR0=0                         '/10=ADC Fixed Voltage Reference Peripheral output is 2x (2.048V).
    '-------------------
    WPUA=0000                                '1=Pull-up enabled.
    All_Digital=1
    '-------------------
    TRISA=0011                               '1=PORTA pin configured as an INPUT (tri-stated).
    PORTA=1011
    '-------------------
    Symbol Batt_LEV PORTA.0
    Symbol Pot_VAL PORTA.1
    Symbol Fiber_PWM PORTA.2 
    Symbol Boost_EN PORTA.4
    Symbol LED_Red PORTA.5
    '-------------------
    Dim ADC_Raw As Byte
    Dim Duty As Byte
    '-------------------
    APFCONbits_CCP1SEL=0                        'Send PWM to RA2 (pin 5)
    PR2=111111                              'Set PWM period
    T2CON=000110                            'PWM On, Prescale clock by 16
    CCP1CON=001100                          'PWM single outout mode, active High
    CCP1ASbits_CCP1ASE=000000               'PWM Shutdown interaction disabled
    PWM1CON=000000                          'PWM Autostart, no delay
    PSTR1CON=010001                         'PWM P1A enabled, P1B disabled
    
    
    Main:
    GoTo Pot_ADC
    CCPR1L=ADRESH
    GoTo Main
    
    
    '==============================================================================================
    Pot_ADC:                                                                                
    '==============================================================================================
    ADCON0bits_CHS4=0                           '\
    ADCON0bits_CHS3=0                           '\
    ADCON0bits_CHS2=0                           ' 00001 =AN1.
    ADCON0bits_CHS1=0                           '/
    ADCON0bits_CHS0=1                           '/
    
    ADCON0bits_ADON=1                           'ADC switch ON.
        ADCON0bits_GO_DONE=1                    'Start of conversion.
            While ADCON0bits_GO_DONE=1          'loop during conversion (ADCON0bits_GO_DONE=1).
            Wend                                'terminate loop when ADCON0bits_GO_DONE=0.
    '    ADC_Raw=ADRESH                          '8bit result is written to ADC_Raw variable.
    ADCON0bits_ADON=0                           'ADC switch OFF
    Return
    '==============================================================================================
    PWM_:                                                                                
    '==============================================================================================
    PR2=0x3F
    T2CON=0x5
    TMR2=0x0
    ECCP1AS=0x0
    CCP1CON=0x2
    CCPR1H=0x0
    CCPR1L=0
    PSTR1CON=0x1
    PWM1CON=0x80
    Return
    '==============================================================================================
    Attached Thumbnails Attached Thumbnails pwm.jpg‎  

  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
    Prolific Poster charliecoultas's Avatar
    Join Date
    Sep 2010
    Posts
    1,472
    Thumbs Up
    Received: 51
    Given: 48
    Total Downloaded
    3.45 GB

    0 Not allowed!

    Default Re: Software solution to the flickering LED problem

    Hi SebaG

    In your routine Main, you GoTo Pot_ADC but then at the end of Pot_ADC you Return! Return goes back to where a Gosub was used, you haven't used Gosub. Look in the manual at Gosub, Return.

    You might try putting GoSub Pot_ADC instead of GoTo Pot_ADC. See if this makes a difference.

    Charlie

  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
    Junior Member SebaG's Avatar
    Join Date
    Jul 2020
    Posts
    4
    Thumbs Up
    Received: 0
    Given: 0
    Total Downloaded
    6.6 KB

    0 Not allowed!

    Default Re: Software solution to the flickering LED problem

    Hi, thanks for the answer. I don't think there was any GoSub instruction in Proton Basic when I wrote this program. I thought it didn't matter if we used GoSub or GoTo. You say it's not the same? Sorry, but I'm very weak at programming.

  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
    Prolific Poster charliecoultas's Avatar
    Join Date
    Sep 2010
    Posts
    1,472
    Thumbs Up
    Received: 51
    Given: 48
    Total Downloaded
    3.45 GB

    0 Not allowed!

    Default Re: Software solution to the flickering LED problem

    Gosub is short for "go and perform a subroutine". When the subroutine is finished, it "Returns" to the address given in the Gosub command. This means that a subroutine (Pot_ADC) can be called from more than one place.

    Does your program behave differently if you use GOSUB instead of GOTO?

    Charlie

  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.

  9. #5
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,966
    Thumbs Up
    Received: 96
    Given: 34
    Total Downloaded
    5.15 GB

    0 Not allowed!

    Default Re: Software solution to the flickering LED problem

    Seba,
    Charlie is correct.
    Here's a little more information
    When you use Gosub Somewhere, the compiler puts the current address of the program in special memory called a stack. The program then jumps to the address of the subroutine and does its task. At the end of the task the command Return removes the saved address from the stack and sends the operation back to where it last was to continue the program flow.
    Gosub ...and Return go hand in hand.
    If you used "goto somewhere" there is nothing in the stack to tell it where to go if you use a return.
    John

  10. 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.

  11. #6
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,390
    Thumbs Up
    Received: 30
    Given: 7
    Total Downloaded
    1.16 GB

    0 Not allowed!

    Default Re: Software solution to the flickering LED problem

    Hi,
    I don't see a call to PWM_ either, is it meant to be used?

    Regards,
    Bob

  12. 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.

  13. #7
    Junior Member SebaG's Avatar
    Join Date
    Jul 2020
    Posts
    4
    Thumbs Up
    Received: 0
    Given: 0
    Total Downloaded
    6.6 KB

    0 Not allowed!

    Default Re: Software solution to the flickering LED problem

    Hi,

    Thanks for the answers. This "GoTo" story is quite funny. 2 years ago, when I wrote this code, "GoTo" was working properly and this hex was uploaded to the PIC. When I recompiled my "GoTo" code today, the program didn't work properly anymore. Instead, it acted with the GoSub command. As I wrote - when I wrote this code about 2 years ago, there was no "GoSub" command in the compiler yet. The program from "GoSub", as I wrote, works, but just like mine with "GoTo" - in the initial range of the potentiometer rotation, the brightness of the LED is jumpy, let's say from 0-12 brightness levels, it takes place in steps, and then the brightness is regulated smoothly. Could the reason be the lack of averaging the reading from the potentiometer? Maybe averaging would also help to avoid spontaneous brightness changes (I do not touch the potentiometer knob then) in certain positions of the potentiometer slider. As for PWM, after its configuration, which is before the "Main" program, it is enough to rewrite the ADC value to the CCP1L register. Am I right?

    Sebastian

  14. 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.

  15. #8
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,390
    Thumbs Up
    Received: 30
    Given: 7
    Total Downloaded
    1.16 GB

    0 Not allowed!

    Default Re: Software solution to the flickering LED problem

    Hi,
    There has always been a Gosub command or at least for the 15 years I have been using PDS there has.
    If you have a pin spare you could use a serial LCD to show the analog value to see if it is altering and the frequency that you are sending.
    Also by the look of it you are sampling the analog value at a very fast rate, why not put a delay in and say sample at 10hz or so.

    Regards,
    Bob

  16. 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.

  17. #9
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,966
    Thumbs Up
    Received: 96
    Given: 34
    Total Downloaded
    5.15 GB

    0 Not allowed!

    Default Re: Software solution to the flickering LED problem

    Hi Sebastian,
    Whenever I use an A/D I use a simple averaging routine.
    When I'm by my computer later I'll write one for you.
    John

  18. 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.

  19. #10
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,966
    Thumbs Up
    Received: 96
    Given: 34
    Total Downloaded
    5.15 GB

    1 Not allowed!

    Default Re: Software solution to the flickering LED problem

    Hi Sebastian,
    Here's an example of a subroutine that brings in the value from the A/D and provides an average.
    There are many different ways to do this but I have written it without adding any new ideas such as procedures.
    Averaging routines can be very fancy but the one I have used is OK and would suit your purpose.
    Best wishes for your experiments,
    John

    Code:
    ;-------------------------------------------------------------------------------
    ;**** Added by Fuse Configurator ****
    ; Use the Fuse Configurator plug-in to change these settings
    
    ;-------------------------------------------------------------------------------
    ;**** Added by Fuse Configurator ****
    ; Use the Fuse Configurator plug-in to change these settings
    
    Device = 18F452
    
    Declare Reminders Off
    @ CONFIG_REQ = 0 ; Override Compiler's configuration settings
    Asm-
    Config OSC = HS    ;HS oscillator
    Config OSCS = OFF    ;Oscillator system clock switch option is disabled (main oscillator is source)
    Config PWRT = OFF    ;PWRT disabled
    Config BOR = On    ;Brown-out Reset enabled
    Config BORV = 20    ;VBOR set to 2.0V
    Config WDT = OFF    ;WDT disabled (control is placed on the SWDTEN bit)
    Config WDTPS = 128    ;1:128
    Config CCP2MUX = OFF    ;CCP2 input/output is multiplexed with RB3
    Config STVR = OFF    ;Stack Full/Underflow will not cause RESET
    Config LVP = OFF    ;Low Voltage ICSP disabled
    Config Debug = OFF    ;Background Debugger disabled. RB6 and RB7 configured as general purpose I/O pins.
    Config Cp0 = OFF    ;Block 0 (000200-001FFFh) not code protected
    Config CP1 = OFF    ;Block 1 (002000-003FFFh) not code protected
    Config CP2 = OFF    ;Block 2 (004000-005FFFh) not code protected
    Config CP3 = OFF    ;Block 3 (006000-007FFFh) not code protected
    Config CPB = OFF    ;Boot Block (000000-0001FFh) not code protected
    Config CPD = OFF    ;Data EEPROM not code protected
    Config WRT0 = OFF    ;Block 0 (000200-001FFFh) not write protected
    Config WRT1 = OFF    ;Block 1 (002000-003FFFh) not write protected
    Config WRT2 = OFF    ;Block 2 (004000-005FFFh) not write protected
    Config WRT3 = OFF    ;Block 3 (006000-007FFFh) not write protected
    Config WRTC = OFF    ;Configuration registers (300000-3000FFh) not write protected
    Config WRTB = OFF    ;Boot Block (000000-0001FFh) not write protected
    Config WRTD = OFF    ;Data EEPROM not write protected
    Config EBTR0 = OFF    ;Block 0 (000200-001FFFh) not protected from Table Reads executed in other blocks
    Config EBTR1 = OFF    ;Block 1 (002000-003FFFh) not protected from Table Reads executed in other blocks
    Config EBTR2 = OFF    ;Block 2 (004000-005FFFh) not protected from Table Reads executed in other blocks
    Config EBTR3 = OFF    ;Block 3 (006000-007FFFh) not protected from Table Reads executed in other blocks
    Config EBTRB = OFF    ;Boot Block (000000-0001FFh) not protected from Table Reads executed in other blocks
    Endasm-
    Declare Reminders On
    
    ;**** End of Fuse Configurator Settings ****
    ;-------------------------------------------------------------------------------
    Xtal = 4
    
    
    All_Digital =true
    
    Declare LCD_Type 0                'text type
    Declare LCD_DTPin PORTD.4        'assigns data lines to B4..7
    Declare LCD_ENPin PORTE.1        'enable pin
    Declare LCD_RSPin PORTE.0        'RS line pin
    Declare LCD_Interface 4          '4 or 8 line interface
    Declare LCD_Lines 2              'lines in the display
    Declare Float_Rounding = On
    Declare Float_Display_Type = LARGE
    
    Declare Adin_Res = 10           '10-bit result required
    Declare Adin_Tad = FRC          'RC OSC chosen
    Declare Adin_Stime = 50         'Allow 50us sample time
    'the next registers will need to be set depending on the pic you use, I've set for 18F452
    ADCON1  = %11001110             'set AN0 A/D the rest digital
    ADCON0  = %11000001             'enable A/D  internal oscillator
    
    TRISA = %00000001               'bitA.0
    TRISB = %00000000               'all outputs, you may change this though
    TRISC = %0                      'all outputs
    
    
    Dim ADrcvd      As Word         'the value rcvd from A/D
    Dim wTempRcvd   As Word         'used in calculation of average
    Dim I           As Byte         'loop counter
    
    GoTo Main
    
    DataIN:                         'a subroutine to bring in the value of a pot and average over 16 measures
         wTempRcvd = 0              'zero the storage counter
         For I = 1 To 16            'do it 16 times
            ADrcvd = ADIn 0         'bring in the value of the A/D
            DelayUS 2               'a little delay to make sure A/D recharges
            wTempRcvd = wTempRcvd + ADrcvd  'gradually build a total
         Next
         ADrcvd = wTempRcvd / 16    'find the average
    Return
    
    Main:
         GoSub DataIN               'visit the subroutine
         Print At 1,1,Dec ADrcvd,"   "   'print the value
         GoTo Main
    Notice I used 1 gosub and two goto.
    The first Goto was to jump over the subroutine and start with Main.
    The second Goto was to make an endless loop within Main.

    At any other time I would use Gosub to subroutines for two reasons.
    1) it makes the code easier to follow and track its operations
    2) it enables sections of code to be re-used.

  20. 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.

  21. #11
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,702
    Thumbs Up
    Received: 361
    Given: 173
    Total Downloaded
    2.08 GB

    0 Not allowed!

    Default Proton Compiler Median Filter Library Demo

    The Proton compiler has had the Gosub command from day one!

    A language without a call-return mechanism is not an option because they are paramount to all programs and even the early computer languages had a subroutine mechanism with a stack to hold the address'.

    John's averaging routine is nice and simply and will work nicely for an ADC input, however, the compiler also has a median filter library that is installed with it. All the compiler's libraries are installed in: "C:\Users\User Name\PDS\Includes"

    Here is a demo program showing the median filter working for random 16-bit (Word) values in an array:

    Code:
    '
    '   /\\\\\\\\\
    '  /\\\///////\\\
    '  \/\\\     \/\\\                                                 /\\\          /\\\
    '   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
    '    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
    '     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
    '      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
    '       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
    '        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
    '                                  Let's find out together what makes a PIC Tick!
    '
    ' Median filter library demo
    ' Written by Les Johnson for the Proton BASIC Compiler version 3.7.3.9 onwards
    '
        Device = 16F1939
        Declare Xtal = 16     
        
        Declare Hserial_Baud = 9600
        Declare HRSOut_Pin = PORTC.6
        
        Include "Median_Filters.inc"                ' Load the Median filter library into the program
    '
    ' Create some variables for the demo
    '    
        Dim bIndex As Byte                          ' Index for the elements of the array to be sorted
        Dim MedianValue As Word                     ' Holds the average (middle) value from the sorting
        Dim MyArray[11] As Word                     ' Holds the 16-bit values to be sorted
    
    '--------------------------------------------------------
    ' Transmit the decimal values from a 16-bit array to a serial terminal
    ' Input     : pArrayAddr holds the address of the array to transmit
    '           : pArraySize holds the size of the array
    ' Output    : None
    ' Notes     : None
    '
    Proc Array_Display16(ByRef pArrayAddr As Word, pArraySize As Byte)
        Repeat                                      ' Create a loop for the amount of elements in the array to display
            HRSOut Dec Ptr16(pArrayAddr++)          ' Transmit a decimal element value from the array passed
            Dec pArraySize                          ' Decrement the element count
            If pArraySize <> 0 Then HRSOut ","      ' If not at the end, transmit a comma character between the values
        Until pArraySize = 0                        ' Loop until all elements displayed
        HRSOut 13                                   ' Transmit a CR to move to the next line
    EndProc
    
    '--------------------------------------------------------
    ' Pass a randomly filled array to the median filter
    ' Display the unsorted array, then the sorted array, then the median (middle) value
    '
    Main:
        Seed $0345
    '
    ' Fill the array with random values to be sorted
    '
        For bIndex = 0 To 10
            MyArray[bIndex] = Random // 1023
        Next
        
        HRSOutLn "UnSorted Array:"
        Array_Display16(MyArray, SizeOf(MyArray))           ' Display the unsorted array
        
        MedianValue = Median16(MyArray, SizeOf(MyArray))    ' Sort the 16-bit array and return the median value
        
        HRSOutLn "\rSorted Array:"
        Array_Display16(MyArray, SizeOf(MyArray))           ' Display the sorted array
        
        HRSOutLn "\rMedian Value = ", Dec MedianValue       ' Transmit the average (median) value
    And here is a screenshot of the above demo program running in a simulator:

    Click image for larger version

Name:	Median_Filter_Demo.jpg
Views:	58
Size:	91.3 KB
ID:	3987





















    To use the median filter for ADC values, fill the array with the readings from the ADC, then call the appropriate Median procedure. It will return the average from the readings. i.e. The middle value.
    Last edited by top204; 24th July 2020 at 10:55.

  22. 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.

  23. #12
    Member John Lawton's Avatar
    Join Date
    Feb 2003
    Posts
    303
    Thumbs Up
    Received: 5
    Given: 22
    Total Downloaded
    1.44 GB

    0 Not allowed!

    Default Re: Proton Compiler Median Filter Library Demo

    If you are reading your ADC with only 8 bits accuracy. noise shouldn't be a problem unless you have a noisy circuit. If you are saying that controlling a LED at low output levels is stepped, well yes it will be. You really need much more than 8 bit resolution to give smooth control of LEDs especially if you don't have any law correction such as a lookup table to make the progression at low levels smoother.
    John Lawton

  24. 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.

  25. #13
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,702
    Thumbs Up
    Received: 361
    Given: 173
    Total Downloaded
    2.08 GB

    0 Not allowed!

    Default Proton Compiler LED Linearisation using an AntiLog algorithm

    John is right. LEDs are not linear in operation, so a linear voltage to them will not create a linear brightness level, as it does with an incandescent light bulb.

    I remember coming across this many years ago and studied some of the mechanisms to try to counter-act it, and I created this program to test the AntiLog algorithm I created to linearise an LED. The parameter pGamma can be changed in value when calling the procedure for the LED used, and once the correct Gamma Value is found, the RAM array can be sent to a serial terminal and the data copied from the terminal and used for a Flash Memory table, thus saving 255 bytes of RAM for the table, and a bulky algorithm calculation. Here's the code, from about 2012, altered to now use a procedure:

    Code:
    '
    '   /\\\\\\\\\
    '  /\\\///////\\\
    '  \/\\\     \/\\\                                                 /\\\          /\\\
    '   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
    '    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
    '     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
    '      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
    '       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
    '        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
    '                                  Let's find out together what makes a PIC Tick!
    '
    ' Linearise a PWM driven LED
    '
        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
    
    $define cTableSize 255
    $define cSteps     255
    
        Dim bLinTable[cTableSize] As Byte Heap
    
    '--------------------------------------------------------------------
    ' Create a data array to linearise an LED's brightness for 8-bit PWM
    ' Input     : pGamma holds the gamma correction value.  Normally between 0.5 to 1.0
    '           : cTableSize holds the size of the array table
    '           : cSteps holds the total number of PWM steps in the table (1 to 255)
    ' Output    : Array bLinTable is filled with the values
    ' Notes     : Leo Bodnar's antilogarithmic gamma correction algorithm
    '           : Equivalent to: wDcyValue = cSteps ^ (( (wIndex + 1) / cTableSize) ^ pGamma) + 0.3
    '
    Proc Create_LineariseTable(pGamma As Float)
        Dim fTemp     As Float
        Dim wDcyValue As Word
        Dim wIndex    As Word
    
        Clear bLinTable
        For wIndex = 1 To SizeOf(bLinTable) - 1
            fTemp = Pow((wIndex / cTableSize), pGamma)
            wDcyValue = Pow(cSteps, fTemp)
            bLinTable[wIndex] = wDcyValue
        Next
    '
    ' Draw the curve on a serial terminal for visualisation
    '
    (*
        HRSOutLn "' Gamma Value = ", Dec2 pGamma
        For wIndex = 0 To SizeOf(bLinTable) - 1
            bLinValue = bLinTable[wIndex]
            Hrsout Dec3 bLinValue, "| "
            If bLinValue <= 1 Then
                HRSOutLn "*"
            Else
                HRSOutLn Rep " "\(bLinValue / 4), "*"
            EndIf
        Next
    *)
    EndProc
    
    '--------------------------------------------------------------------
    ' Increase and decrease the brightness of two LEDs on CCP1 and CCP2 pins.
    ' One LED using the linearisation calculations and one using the index as the duty cycle so the difference can be seen
    '
    Main:
        Create_LineariseTable(0.85)                     ' Create the linearising array table
        Analog12_Open()                                 ' Open PWM 1 and 2
    
        Do
            '
            ' Illuminate 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
    Last edited by top204; 24th July 2020 at 19:36.

  26. 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.

  27. #14
    Junior Member SebaG's Avatar
    Join Date
    Jul 2020
    Posts
    4
    Thumbs Up
    Received: 0
    Given: 0
    Total Downloaded
    6.6 KB

    0 Not allowed!

    Default Re: Proton Compiler LED Linearisation using an AntiLog algorithm

    Thank you for your ideas. I work with this code as long as I have some spare time. Averaging helped in the 8bit PWM mode to eliminate spontaneous brightness jumps in certain positions of the potentiometer slider. As for the code linearizing the brightness of the led, I'm not sure. I mean, the code is great, thank you, but in my application it will probably not work, because everything will depend on how linear the potentiometer characteristic is and thus the ADC reading value. If there are any spikes in resistance, the end result may be worse than my initial one. It seems to me that this is an ideal solution, but when controlling with an encoder or buttons. Currently, I have increased the ADC and PWM reading resolution to 10bit and it's much better. Now I am starting the code responsible for averaging this result. Thanks a lot for your help!

  28. 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.

  29. #15
    Prolific Poster charliecoultas's Avatar
    Join Date
    Sep 2010
    Posts
    1,472
    Thumbs Up
    Received: 51
    Given: 48
    Total Downloaded
    3.45 GB

    0 Not allowed!

    Default Re: Proton Compiler LED Linearisation using an AntiLog algorithm

    You are welcome and thanks for the feedback. There are many people on this forum who are happy to help.

    Best regards
    Charlie

  30. 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. Flickering Problem
    By hassawfaa in forum Proton Plus Compiler v3
    Replies: 23
    Last Post: 27th July 2011, 14:24
  2. Flickering bargraph
    By [email protected] in forum Proton Plus Compiler v3
    Replies: 8
    Last Post: 11th November 2009, 13:49
  3. [SOLVED !] hardware/software uart problem
    By richardy in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 22nd July 2007, 23:28
  4. Solution To LCD Problems
    By nerginer in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 15th June 2006, 08:33
  5. [SOLVED !] Problem converting PBP code to Proton to fade LED
    By danielCA in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 11th July 2005, 01:05

Posting Permissions

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