LCD Bargraph code?

augustinetez

Well-Known Member
Most Helpful Member
On the off chance, does anybody have some simple code already written that will take a 0-5V voltage applied to the ADC input and convert it to a bargraph on a 16x2 or 20x4 LCD?

For the 16F1827 PIC.

I found one written for Proton Basic but their free trial compiler seems to be no longer available and of course it won't work in the Oshonsoft compiler without a total rewrite.
 
I've only got the Oshonsoft compiler at the moment and it's been way too long since doing any Basic programming.

4 bit PortB and ADC on Port A, doesn't matter which pin.

Thanks Ian.
 
Ian, can you define the first eight (or 5) characters to be 1 to 5 pixels (if memory serves well) so you can have a 1 pixel bar?

Mike.
 
Yeah! I do that, but it looks rubbish This is the way Proton Basic do it and it actually looks better..

The characters are 1 space away from each other..

I have two types ( now three ) one is a 16 character box and the position moves from one side to the other.. Like a slider..
 
I made it "muck about-able" Any size, up to 20 chars 3 or 5 bar resolution It now accepts 0 ~ 100%

Enjoy... I do like the 3 line type.. But now its up to you.. I enjoyed doing this..
 

Attachments

Sometimes.. On a Sunday when all else is a sleep.. I can write pages of code ( mostly C ) and surprise myself that no syntax or compile errors have occurred.. It bowls me over sometimes..

Because I have a full copy of Proteus.. I can test immediately..
 

I'd be more than surprised

Always a curly bracket missing, or confusion with '=' and '=='
 
For reasons I haven't worked out yet, the first Bar graph file refused to work on the '1827 test bed so I ported it across to my 16F886 test bed.

Likewise it also refused to co-operate.

Just to make sure I hadn't completely lost the plot I ran the 'Hello world' script in the same file (commenting out all the bar graph code) which ran fine, which is when I noticed the 'All_Digital' command and applied it to the bar graph code - success. Still to try it on the '1827.

My amended code below:
C:
'For 16F886 4MHz clock and using R/W bit in LCD interface

Define CONFIG = 0x20c4
Define CONFIG2 = 0x3fff

#define LCD_LINES = 4
#define LCD_CHARS = 20
#define LCD_BITS = 4
#define LCD_DREG = PORTB
#define LCD_DBIT = 0
#define LCD_RSREG = PORTB
#define LCD_RSBIT = 6
#define LCD_EREG = PORTB
#define LCD_EBIT = 4
#define LCD_RWREG = PORTB
#define LCD_RWBIT = 5
#define LCD_COMMANDUS = 5000  'delay after LCDCMDOUT, default value is 5000
#define LCD_DATAUS = 100  'delay after LCDOUT, default value is 100
#define LCD_INITMS = 100  'delay used by LCDINIT, default value is 100
'the last three Define directives set the values suitable for simulation; they should be omitted for a real device

#define ADC_Clk = 3
#define ADC_Sample_uS = 50

All_Digital

TRISB = 0

ANSEL = %00000100  'configure all PORTA pins except RA2 as digital I/O's

Dim buffer(16) As Byte
Dim data As Word
Dim dumm As Word


Lcdinit 0

Lcddefchar 0, %00000, %00000, %00000, %00000, %00000, %00000, %00000, %00000
Lcddefchar 1, %10000, %10000, %10000, %10000, %10000, %10000, %10000, %10000
Lcddefchar 2, %10100, %10100, %10100, %10100, %10100, %10100, %10100, %10100
Lcddefchar 3, %10101, %10101, %10101, %10101, %10101, %10101, %10101, %10101


main:


    ADC_Read 2, data
    dumm = data / 21
    dumm = dumm * 100 / 48
    Lcdcmdout LcdLine1Home
    Lcdout " BAR GRAPH ", #dumm, " "

    Lcdcmdout LcdLine2Home
    Call bar(data)
    
Goto main
End                                               

Proc bar(dat As Word)
    Dim idx As Byte
    Dim pos1 As Byte
    Dim pos2 As Byte
    
    pos1 = dat / 21
    pos2 = (pos1 Mod 3)
    pos1 = pos1 / 3
    
    For idx = 0 To 15
        buffer(idx) = 0x20
    Next idx
    For idx = 1 To pos1
        buffer(idx - 1) = 3
    Next idx
    If pos2 = 0 Then
        buffer(idx - 1) = 0x20
    Else
        buffer(idx - 1) = pos2
    Endif
    
    For idx = 0 To 15
        Lcdout buffer(idx)
    Next idx

End Proc                                         

'All_Digital
'Lcdinit LcdCurBlink
'loop:
'Lcdout "Hello world!"
'WaitMs 1000
'Lcdcmdout LcdClear
'WaitMs 1000
'Goto loop
 
I think it was just for porting over to the 16F886 - it has ANSEL and ANSELH - I forgot about the last one, will change it tomorrow and drop out the 'All_digital' to see what happens.
 
Swapped out 'All_digital' for the added 'ANSELH = 0' in the 16F886 code and it worked, will have another go with the 16F1827 version a little later.

Also just a quick note on a bug in the Oshonsoft Basic Compiler versions when using the sim with an LCD (confirmed by Vlad after I sent the report).

When you load the sim which is using the LCD module, it asks if you want to use the connection/port details from the loaded program - this works fine if you use this form of 'Define xxxx'.

However, if you use '#define xxx', it doesn't work and you have to manually configure the LCD module every time.
 
The '#' is an escape character primarily for C for the pre-processor... Basic doesn't use them..

If you look at ALL his examples they are not used... BUT!! I would have thought an error would occur
Also the #define IS the line in the help file.. I think the help file is cocked up..
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…