Today 00:52
Forum: Proton Plus Compiler v3
Starter: johngb
Views: 0
Replies: 25
Go to last post By: xldaedalus
Today 00:21
Forum: Proton 24
Starter: johngb
Views: 0
Replies: 6
Yesterday 22:33
Forum: The Lounge
Starter: normnet
Views: 0
Replies: 2
Yesterday 19:04
Forum: Website / Forum Issues
Starter: johngb
Views: 0
Replies: 2
Yesterday 17:20
Forum: Proton Plus Compiler v3
Starter: joesaliba
Views: 0
Replies: 12
Yesterday 16:47
Forum: Analog-to-Digital Converter Module
Starter: CharlieBrown
Views: 0
Replies: 1
Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: LED Bargraph - HOLD PEAK - Help22 days old

  1. #1
    Join Date
    Dec 2005
    Posts
    48
    Thumbs Up
    Received: 0
    Given: 1
    Total Downloaded
    385.82 MB

    Default LED Bargraph - HOLD PEAK - Help

    Hi, I wrote this simple code to pilot 10 leds / bargraph for audio user.
    However, I would like to have the last "led on" as "peak hold".
    Can I have a help?
    Thank you.

    Stefano

    Code:
                        Device = 16F88
                        Xtal 10                    
                        All_Digital TRUE                                   
                      
                        Dim audioin As Word
                        Dim peakhold As Word
                        Dim audioled As Word
                        
                        Symbol led0= PORTB.0    'alias leds to port pins
                        Symbol led1= PORTB.1
                        Symbol led2= PORTB.2
                        Symbol led3= PORTB.3
                        Symbol led4= PORTB.4
                        Symbol led5= PORTB.5
                        Symbol led6= PORTB.6
                        Symbol led7= PORTB.7
                        Symbol led8= PORTA.4
                        Symbol led9= PORTA.3
                        
                    ' Define ADC parameters
                        Declare Adin_Res = 8           ' 8-bit result required
                        Declare Adin_Tad = FRC          ' RC OSC chosen
                        Declare Adin_Stime = 50         ' Allow 50us sample time
     
                        TRISA.4 = 0
                        TRISA.3 = 0
                        TRISB = 0
                        ADCON1 = 000010              ' Set PORTA analog
    
    
                        PORTB = 0  
                        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''                                                     
    Main:                    
                        While 1=1
                       
                        audioin = ADIn 0
                        audioled = audioin
                        If audioled > 1 Then GoSub led
                        Wend
                        
    led:
                        If audioled > 30 Then 
                        High led0 
                        Else 
                        Low led0
                        EndIf
                        
                        
                        If audioled > 77 Then        
                        High led1
                        Else 
                        Low led1
                        EndIf
                        
                        If audioled > 122 Then      
                        High led2
                        Else 
                        Low led2
                        EndIf
                        
                        If audioled > 154 Then 
                        High led3
                        Else 
                        Low led3
                        EndIf
                        
                        If audioled > 178 Then
                        High led4
                        Else 
                        Low led4
                        EndIf
                        
                        If audioled > 198 Then
                        peakhold = 198
                        High led5
                        Else 
                        Low led5
                        EndIf
                        
                        If audioled > 216 Then 
                        High led6
                        Else 
                        Low led6
                        EndIf
                        
                        If audioled > 230 Then                     
                        High led7
                        Else 
                        Low led7
                        EndIf
                        
                        If audioled > 243 Then                     
                        High led8
                        Else
                        Low led8
                        EndIf
                        
                        If audioled > 254 Then                      
                        High led9
                        Else
                        Low led9
                        EndIf                    
                        Return
    
    
                        End
    Attached Images Attached Images

  2. #2
    Join Date
    Sep 2004
    Posts
    2,632
    Thumbs Up
    Received: 59
    Given: 18
    Total Downloaded
    2.27 GB

    Default Re: LED Bargraph - HOLD PEAK - Help

    I think it can be done but you have to use interrupts.
    Regards

    Joseph

  3. #3
    Join Date
    Mar 2012
    Posts
    1,332
    Thumbs Up
    Received: 49
    Given: 123
    Total Downloaded
    2.37 GB

    Default Re: LED Bargraph - HOLD PEAK - Help

    How long do you want the peak hold to stay on for?
    George

  4. #4
    Join Date
    Dec 2005
    Posts
    48
    Thumbs Up
    Received: 0
    Given: 1
    Total Downloaded
    385.82 MB

    Default Re: LED Bargraph - HOLD PEAK - Help

    Thanks for the answer.

    Something like 1 second i think.

  5. #5
    Join Date
    Feb 2002
    Posts
    3,234
    Thumbs Up
    Received: 229
    Given: 83
    Total Downloaded
    1.50 GB

    Default Re: LED Bargraph - HOLD PEAK - Help

    Here's a simple program that will display a voltage on 10 LEDs, with a simple peak mechanism.

    The peak LED will reset after a certain period of time, altered by a value within the $define cPeakAmount.

    The peak principle is straightforward. Store the maximum value read from the ADC and compare it with the current ADC reading. If the peak is greater than the current, display it. Reset the peak value after X amount of loop iterations, which can be measured as time depending on the delays used within the loop. In order to change the display to a bar instead of a dot, access the PORTs instead of the bits of the port, but the mechanism for peak stays the same.

    Code:
    ' A simple LED bar for voltage that also includes a simple peak LED display
    ' Written by Les Johnson for the Proton BASIC compiler
    '
        Device = 16F88
        Declare Xtal = 10            
    '    
    ' Define ADC parameters
    '
        Declare Adin_Res = 8            ' 8-bit result required
        Declare Adin_Tad = FRC          ' RC OSC chosen
        Declare Adin_Stime = 50         ' Allow 50us sample time
                          
        Dim tBarOrPeak As Bit           ' Holds 0 for standard voltage display, 1 if displaying the peak voltage
        Dim bBarValue As Byte           ' Holds the value to display
        Dim bPeakValue As Byte          ' Holds the peak value
        Dim bAudioIn As Byte            ' Holds the value from the ADC
        Dim wPeakSamples As Word        ' Holds the amount of ADC samples before the peak is removed
    '
    ' Define the pins used for the LEDs
    '        
    $define LED0 PORTB.0
    $define LED1 PORTB.1
    $define LED2 PORTB.2
    $define LED3 PORTB.3
    $define LED4 PORTB.4
    $define LED5 PORTB.5
    $define LED6 PORTB.6
    $define LED7 PORTB.7
    $define LED8 PORTA.3
    $define LED9 PORTA.4
    
    $define cPeakAmount 1000                    ' The amount of ADC samples to take before the peak is reset
    
    $define cDisplayVoltage 0                   ' Display the standard voltage on the LEDs
    $define cDisplayPeak 1                      ' Display the peak voltage on the LEDs
            
        GoTo Main                               ' Jump to the main program loop
    
    '----------------------------------------------------------------------------------
    ' Display the voltage between 0 to 255 on 10 LEDs
    ' Input     : bBarValue holds the value to display
    '           : tBarOrPeak holds 0 if normal bar display, or 1 if displaying the peak
    ' Output    : None
    ' Notes     : None
    '
    $define LED_Bar(pValue, pPeak) '
        bBarValue = pValue         '
        tBarOrPeak = pPeak         '
        GoSub _LED_Bar
                    
    _LED_Bar:
        If bBarValue = 0 Then Return            ' Do not display if the voltage value is 0
    
        If tBarOrPeak = cDisplayVoltage Then    ' Do we require a standard voltage display?
            Low PORTB                           ' \
            Low LED8                            ' | Yes. So extinguish the LEDs
            Low LED9                            ' /
        EndIf
        Select bBarValue                        ' Do a comparison on the voltage value
            Case 1 To 24                        ' Is it enough for LED0?
                Set LED0                        ' Yes. So illuminate LED0
            Case 25 To 49                       ' Is it enough for LED1?     
                Set LED1                        ' Yes. So illuminate LED1
            Case 50 To 74                       ' Is it enough for LED2?  
                Set LED2                        ' Yes. So illuminate LED2
            Case 75 To 99                       ' Is it enough for LED3?
                Set LED3                        ' Yes. So illuminate LED3
            Case 100 To 124                     ' Is it enough for LED4?
                Set LED4                         ' Yes. So illuminate LED4
            Case 125 To  149                     ' Is it enough for LED5? 
                Set LED5                        ' Yes. So illuminate LED5
            Case 15 To  174                      ' Is it enough for LED6? 
                Set LED6                        ' Yes. So illuminate LED6
            Case 175 To 199                     ' Is it enough for LED7?        
                Set LED7                        ' Yes. So illuminate LED7
            Case 200 To 224                     ' Is it enough for LED8?         
                Set LED8                        ' Yes. So illuminate LED8
            Case Else                           ' \ Otherwise... Illuminate LED9        
                Set LED8                        ' /
        EndSelect
        Return
            
    '----------------------------------------------------------------------------------                   
    ' The main program loop starts here
    '
    Main:
        ADCON0 = 0                              ' Make PORTA.0 an analogue input
        ADCON1bits_ADFM = 0                     ' Set justify result for 8-bit operation
        Low  PORTB                              ' \
        Low LED8                                ' | Set the LED pins output low
        Low LED9                                ' /
        bPeakValue = 0       
        tBarOrPeak = 0
        wPeakSamples = 0
    
        Do                                      ' Create a loop                
            bAudioIn = ADIn 0                   ' Get an ADC reading
            
            LED_Bar(bAudioIn, cDisplayVoltage)  ' Display the standard voltage on the LEDs
            
            If bAudioIn > bPeakValue Then       ' Is the ADC reading greater than the current peak value?
                bPeakValue = bAudioIn           ' Yes. So update the peak value
            EndIf
            LED_Bar(bPeakValue, cDisplayPeak)   ' Display the peak voltage on the LEDs
            
            Inc wPeakSamples                    ' Increment the peak samples value
            If wPeakSamples >= cPeakAmount Then ' Have we reached the time to reset the peak?
                Clear wPeakSamples              ' Yes. So reset the counter
                Clear bPeakValue                ' Clear the peak voltage value
            EndIf
            DelayMS 10
        Loop                                    ' Do it forever
    Here's a screenshot of the above program being simulated, and using a variable resistor for the voltage to the ADC so it can be adjusted and tested.

    16f88.jpg
    Last edited by Les; 1st November 2017 at 22:05.
    For more example programs for Proton and Proton24 or updates, please visit: Proton WIKI or Proton Files

  6. #6
    Join Date
    Feb 2002
    Posts
    3,234
    Thumbs Up
    Received: 229
    Given: 83
    Total Downloaded
    1.50 GB

    Default Re: LED Bargraph - HOLD PEAK - Help

    The line above:

    Case 15 To 174 ' Is it enough for LED6?

    Should be:

    Case 150 To 174 ' Is it enough for LED6?

    For some reason, the zero was cut off by the code editor of the thread text.

    Also... Change the lines:

    Case Else ' \ Otherwise... Illuminate LED9
    Set LED8 ' /


    To:

    Case Else ' \ Otherwise... Illuminate LED9
    Set LED9 ' /
    Last edited by Les; 2nd November 2017 at 08:14.
    For more example programs for Proton and Proton24 or updates, please visit: Proton WIKI or Proton Files

  7. #7
    Join Date
    Dec 2005
    Posts
    48
    Thumbs Up
    Received: 0
    Given: 1
    Total Downloaded
    385.82 MB

    Default Re: LED Bargraph - HOLD PEAK - Help

    Thank you Les,
    it's work fine, but I have a question:
    is it possible to have leds always HIGH ?
    Now is a "peak-meter", instead "VU with Peak"
    Cheers.
    Stefano

  8. #8
    Join Date
    Dec 2005
    Posts
    48
    Thumbs Up
    Received: 0
    Given: 1
    Total Downloaded
    385.82 MB

    Default Re: LED Bargraph - HOLD PEAK - Help

    Sorry Les,but what I'm looking for is this: in order to change the display to a bar instead of a dot, access the PORTs instead of the bits of the port, but the mechanism for peak stays the same.

    CanI have a help?
    Thank you.

    Stefano




  9. #9
    Join Date
    Feb 2002
    Posts
    3,234
    Thumbs Up
    Received: 229
    Given: 83
    Total Downloaded
    1.50 GB

    Default Re: LED Bargraph - HOLD PEAK - Help

    Instead of altering the individual bits of a port, you can use "ands" to clear pins, and "ors" to set pins. The code will need to be adjusted a little, but the principle stays the same. i.e. Store the highest value received and use that as the peak. Maybe or in the peak value so that pin becomes high and does not effect the other pin states.
    For more example programs for Proton and Proton24 or updates, please visit: Proton WIKI or Proton Files

  10. #10
    Join Date
    Feb 2002
    Posts
    3,234
    Thumbs Up
    Received: 229
    Given: 83
    Total Downloaded
    1.50 GB

    Default LED Bargraph with Peak Hold LED

    This question got me curious, so I've created a simple program, without any code tweaks, to create an LED bar and have a single LED peak mechanism that only lasts for so long. The code could be tweaked for better optimisation, but it seems to work OK.

    Because all the LEDs do not fit on a single PORT, Oring and Anding would have actually produced a lot more code, and made it more complex to read and understand, so this code uses simple port and bit manipulations.

    A mechanism could also be created within the code to make the peak LED drop down until it reaches the actual voltage. This would be done with another counter variable. Then when reached, it decrements the bPeakValue variable until it reaches bBarValue. The counter variable would be used to slow down the decrement rate.


    Code:
    ' A simple LED bar for Voltage that also includes a simple peak LED display
    ' This version performs a bar graph
    ' Written by Les Johnson for the Proton BASIC compiler
    '
        Device = 16F88
        Declare Xtal = 10            
    '    
    ' Define ADC parameters
    '
        Declare Adin_Res = 8            ' 8-bit result required
        Declare Adin_Tad = FRC          ' RC OSC chosen
        Declare Adin_Stime = 50         ' Allow 50us sample time
                          
        Dim bBarValue As Byte           ' Holds the value to display
        Dim bPeakValue As Byte          ' Holds the peak value
        Dim bAudioIn As Byte            ' Holds the value from the ADC
        Dim wPeakSamples As Word        ' Holds the amount of ADC samples before the peak is removed
    '
    ' Define the port and pins used for the LEDs
    '
    $define LED_Port PORTB
    $define LED0 PORTB.0
    $define LED1 PORTB.1
    $define LED2 PORTB.2
    $define LED3 PORTB.3
    $define LED4 PORTB.4
    $define LED5 PORTB.5
    $define LED6 PORTB.6
    $define LED7 PORTB.7
    $define LED8 PORTA.3
    $define LED9 PORTA.4
    
    $define cPeakAmount 1000                    ' The amount of ADC samples to take before the peak is reset
            
        GoTo Main                               ' Jump to the main program loop
    
    '----------------------------------------------------------------------------------
    ' Display the standard voltage between 0 to 255 on all 10 LEDs as a bar
    ' Input     : bBarValue holds the value to display
    ' Output    : None
    ' Notes     : None
    '
    $define LED_Voltage(pValue) '
        bBarValue = pValue      '
        GoSub _LED_Voltage
                    
    _LED_Voltage:
        Select bBarValue                         ' Do a comparison on the voltage value
            Case 0                               ' Is the value 0?
                LED_Port = $00                   ' \
                Clear LED8                       ' | Yes. So extinguish all the LEDs 
                Clear LED9                       ' / 
            Case 1 To  24                         ' Is it enough for LED0?
                LED_Port = $01                   ' \
                Clear LED8                       ' | Yes. So  LEDs
                Clear LED9                       ' /
            Case 25 To 49                        ' Is it enough for LED1?     
                LED_Port = $03                   ' \
                Clear LED8                       ' | Yes. So illuminate the LEDs
                Clear LED9                       ' /
            Case 50 To 74                        ' Is it enough for LED2?  
                LED_Port = $07                   ' \
                Clear LED8                       ' | Yes. So illuminate the LEDs 
                Clear LED9                       ' / 
            Case 75 To 99                        ' Is it enough for LED3? 
                LED_Port = $0F                   ' \
                Clear LED8                       ' | Yes. So illuminate the LEDs
                Clear LED9                       ' /
            Case 100 To 124                      ' Is it enough for LED4?
                 LED_Port = $1F                  ' \
                Clear LED8                       ' | Yes. So illuminate the LEDs
                Clear LED9                       ' /
            Case 125 To 149                      ' Is it enough for LED5? 
                LED_Port = $3F                   ' \
                Clear LED8                       ' | Yes. So illuminate the LEDs
                Clear LED9                       ' /
            Case 150 To 174                      ' Is it enough for LED6? 
                LED_Port = $7F                   ' \
                Clear LED8                       ' | Yes. So illuminate the LEDs
                Clear LED9                       ' /
            Case 175 To 199                      ' Is it enough for LED7?        
                LED_Port = $FF                   ' \
                Clear LED8                       ' | Yes. So illuminate the LEDs
                Clear LED9                       ' /
            Case 200 To 224                      ' Is it enough for LED8?         
                LED_Port = $FF                   ' \
                Set LED8                         ' | Yes. So illuminate the LEDs
                Clear LED9                       ' /
            Case 225 To 255                      ' Is it enough for LED9?        
                LED_Port = $FF                   ' \
                Set LED8                         ' | Yes. So illuminate the LEDs
                Set LED9                         ' /
        EndSelect
        Return
    
    '----------------------------------------------------------------------------------
    ' Display the peak voltage between 0 to 255 on one of 10 LEDs
    ' Input     : bPeakValue holds the value to display
    ' Output    : None
    ' Notes     : The Peak LED only lasts for as long as the amount of samples in wPeakSamples compared to cPeakAmount
    ' 
    $define LED_Peak(pValue) '
        bPeakValue = pValue  '
        GoSub _LED_Peak
                    
    _LED_Peak:
        Select bPeakValue                       ' Do a comparison on the voltage value
            Case 1 To 24                        ' Is it enough for LED0?
                Set LED0
            Case 25 To 49                       ' Is it enough for LED1?     
                Set LED1  
            Case 50 To 74                       ' Is it enough for LED2?  
                Set LED2
            Case 75 To 99                       ' Is it enough for LED3?
                Set LED3
            Case 100 To 124                     ' Is it enough for LED4?
                Set LED4
            Case 125 To 149                     ' Is it enough for LED5? 
                Set LED5
            Case 150 To 174                     ' Is it enough for LED6? 
                Set LED6
            Case 175 To 199                     ' Is it enough for LED7?        
                Set LED7
            Case 200 To 224                     ' Is it enough for LED8?         
                Set LED8
            Case 225 To 255                     ' Is it enough for LED9?       
                Set LED9
        EndSelect
        Inc wPeakSamples                        ' Increment the peak samples value
        If wPeakSamples >= cPeakAmount Then     ' Have we reached the time to reset the peak?
            Clear wPeakSamples                  ' Yes. So reset the counter
            Clear bPeakValue                    ' Clear the peak voltage value
        EndIf
        Return
            
    '----------------------------------------------------------------------------------                   
    ' The main program loop starts here
    '
    Main:
        ADCON0 = 0                              ' Make PORTA.0 analogue input
        ADCON1bits_ADFM = 0                     ' Set left justify result for 8-bit operation
        Low LED_Port                            ' \
        Low LED8                                ' | Set the LED pins output low
        Low LED9                                ' /
        bPeakValue = 0
        wPeakSamples = 0
    
        Do                                      ' Create a loop                
            bAudioIn = ADIn 0                   ' Get an ADC reading        
            LED_Voltage(bAudioIn)               ' Display the standard voltage on the LEDs        
            If bAudioIn > bPeakValue Then       ' Is the ADC reading grreater than the current peak value?
                bPeakValue = bAudioIn           ' Yes. So update the peak value
            EndIf
            LED_Peak(bPeakValue)                ' Display the peak voltage on an LED
                   
            DelayMS 10                          ' A small delay
        Loop                                    ' Do it forever
    Here's a screenshot of Isis simulating the above code. Notice the bargraph with the seperate LED for peak.

    LED_Bar.jpg
    Last edited by Les; 3rd November 2017 at 20:09.
    For more example programs for Proton and Proton24 or updates, please visit: Proton WIKI or Proton Files

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. [SOLVED !] Where's my bargraph gone?
    By david in forum Proton Plus Compiler v3
    Replies: 5
    Last Post: 12th October 2012, 00:42
  2. Sample and Hold Circuit Help!!
    By syee10 in forum The Lounge
    Replies: 4
    Last Post: 15th October 2010, 14:22
  3. Flickering bargraph
    By shantanu@india in forum Proton Plus Compiler v3
    Replies: 8
    Last Post: 11th November 2009, 14:49
  4. Data Books to Have and to Hold
    By fanie in forum The Lounge
    Replies: 12
    Last Post: 15th July 2008, 16:58
  5. [SOLVED !] Button - press and hold to function - 12f675
    By normie in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 12th November 2007, 01:31

Members who have read this thread : 39

Actions :  (Set Date)

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