• PicŪ Basic


  • Fancy bargraph on a HD44780 based LCD display

    HD44780 based LCD displays are highly popular because of their flexibility at a very low price. Displaying a bargraph can be done by simply showing block-cursors but this gives a rather limited resolution of 16 values (or the number of segments that the display carries)

    By programming some self-designed characters into the display memory, an increased resolution of up to 48 values (for a 16 segment display) can be shown as viewed in the picture below. In addition, the space between two characters is nicely masked. This results in a smooth bargraph that can be utilized in many applications.



    The code as showed below shows a bargraph as function of an input voltage (0…5V) at pin 17 (AN0, 10-bit ADC) of the 16F88 PICmicro. In the case that a different display is chosen, simply change the SEGMENTS variable into the corresponding display segments.

    Code:
    'Simple routine for a high-resolution bargraph on a HD44780 based LCD display.
    'In this example, Input A0 is configured to read an input voltage between VSS and VDD 
    '(usually 0...5V) and display the relative level as a bargraph where 5V equals full-scale
    'reading. 
    
    
    'The routine works as follows:
    ' - Prior to the while-wend loop, the LCD screens special character memory is programmed 
    '   to enable the high-resolution readout
    '
    ' - First a 10-bit ADC read occurs (value between 0....1023)
    '
    ' - Then the calculations are done:
    '       - First the value per segment (SUM byte) is calculated by multiplying the ADC read 
    '         value with the maximum number of segments (in this case 16) and then divide it by
    '         max read / number of scale lines per segment (1024 / 3)
    '       - Then the number of full characters to display (FULL byte) is calculated  
    '       - Finally the partial (PARTIAL byte) characters (0, 1 or 2) are calculated by extracting
    '         the modulus of the restvalue
    
    ' - Finally, the result is displayed:
    '       - First print the number of "FULL" characters
    '       - Then print the "PARTIAL" character, which either is empty, 1 line or 2 lines
    '       - Finally, clear the remaining segments to ensure that no old information still on the display 
    '
    ' - And jump back to the beginning for a "real-time" readout
    '
    ' Mischa van Santen (PA1OKZ) - Netherlands
    
    
    'Config
    Device = 16F88
                                           
    Declare Xtal = 8
    All_Digital true
    Clear
                                 
    'Declare A/D convertor
    Declare Adin_Res = 10                         ' 10-bit result
    Declare Adin_Tad = FRC                        ' RC oscillator
    Declare Adin_Stime = 50                       ' 50us sample time
    
    
    'Variables
    Dim SEGMENTS        As Byte                   ' Holds the number of segments of the display
    Dim METER           As Word                   ' Holds A/D value for meter reading
    Dim FULL            As Byte                   ' Holds the quantity of "full" characters to print |||
    Dim PARTIAL         As Byte                   ' Holds the value 1, 2 or 3 for the partial character | or ||
    Dim SUM             As Byte                   ' Holds the result of the sum (METER * 16)/341
    
    
    'HD44780
    DelayMS 10                                    ' Settle the display
    Print $FE, $40                                ' HD44780 character program mode
    Print $00, $00, $00, $15, $15, $15, $15, $15  ' Character 0: Three lines  |||
    Print $00, $00, $00, $10, $10, $10, $10, $10  ' Character 1: One line     |
    Print $00, $00, $00, $14, $14, $14, $14, $14  ' Character 2: Two lines    ||
    Print $00, $00, $00, $00, $00, $00, $00, $00  ' Character 3: Scale         .
    
    
    TRISA  = $01                                  ' PortA.0 = Input           
    ANSELA = $01                                  ' PortA.0 = Analog 
    
    
    
    
    'Main
    SEGMENTS = 16                                 ' In this example we utilize a 16-segment display
    
    
    While 1 = 1                                   ' Do it forever
    
    
    'ADC reading
                 DelayUS 20                       ' Charging time ADC
                 METER = ADIn 0                   ' Pin 17 is ADC0 input @ 16F88
    
    
    'Calculate meter reading
                SUM = (METER * SEGMENTS) / 341    ' Meter value. Value 341 = 1024 / 3 (values per segment)
                FULL = SUM / 3                    ' Number of full characters |||
                PARTIAL = SUM // 3                ' Modulus restvalue (0, 1 or 2)
    
    
    'Display the bargraph          
                Print At 1, 1, Rep 0\FULL         ' Displays full characters ||| of the S-meter
                If PARTIAL > 0 Then Print PARTIAL ' Prints either the empty, | or || character
                Print Rep 3\SEGMENTS - FULL       ' Clears the remaining part of the level bar         
    
    
    Wend
                    
    End
    Of course, the ADC can be replaced by any input, such as up/down buttons, a rotary encoder or eventually an external device that outputs values to the PICmicro.

    In this example, the special characters have been individually programmed into the HD44780. This is the easiest approach to enable creative other bargraph designs.

    Another example that utilizes this approach is showed in the picture below, being a stereo V/U meter with a tuning indicator on top. This is possible since only 5 vertical dots are needed to show the V/U, leaving 3 dots unused. Since the maximum number of freely programmable characters of the HD44780 is violated here, it is mandatory to continuously update the content of its memory to achieve this.

  • Recent Activity

    hadv215-2509

    PicKit3 Programmer

    Thread Starter: Dave-S

    Trying to setup a PicKit3 Programmer. Has gone through the install programmer and the Dos screen flashes up but does not stay there, and it does not...

    hadv215 Today, 11:13 Go to last post
    Dave-S-350

    UMC Loader

    Thread Starter: Dave-S

    Trying to use the MicroCode loader, but when I select either Loader or Loader Options from the "View" menu it just opens my Melabs Programmer. How...

    Dave-S Today, 15:23 Go to last post