'bargraph program
'started with code in this thread
'http://www.picaxeforum.co.uk/showthread.php?18541-LCD-bargraph-analog-simulation
'removed 2nd lcd initialization, it was causing missing pixels with custom characters
'removed several pauses that I don't think were necessary
'added pauses after clear screen and home cursor commands
'original program didn't have proper scaling so
'I swapped out bargraph drawing routine for hippy's code in this thread
'http://www.picaxeforum.co.uk/showthread.php?19908-axe133-bargraph&p=188443&viewfull=1#post188443
'
'* Pin Assignments **************************************
'LCD pin 6 = SE = c.6 = pin 15 MPU
'LCD pin 4 = RS = c.7 = pin 16 MPU
'LCD pin 11 = D4 = b.4 = pin 10 MPU
'LCD pin 12 = D5 = b.5 = pin 11 MPU
'LCD pin 13 = D6 = b.6 = pin 12 MPU
'LCD pin 14 = D7 = b.7 = pin 13 MPU
'touch input c.0 Used for a source of variable input
'* Symbols ************************************************
#picaxe 18M2
Symbol CHARS = 20 ' 20 character display
symbol LcdSE = c.6 'strobe data into display
symbol LcdRS = c.7 'set low for instruction, high for character entry
symbol LcdByte = b0 'data sent to LCD routines
symbol j = b1 'scratch byte
Symbol pixelsOn = b2
Symbol charsOn = b3
Symbol charsToDo = b7
Symbol valueToGraph = b8 '0 - 255
symbol huns = b4 'hundreds digit in bintoascii
symbol tens = b5 'tens digit in bintoascii
symbol ones = b6 'ones digit in bintoascii
'* Main Routine *******************************************
EEPROM 0, (0,0,0,0,0,0,0,0,16,16,16,16,16,16,16,16,24,24,24,24,24,24,24,24,28,28,28,28,28,28,28,28,30,30,30,30,30,30,30,30,31,31,31,31,31,31,31,31)
'This list of data creates five user defined characters that are called to make partially filled blocks
'0=no lines, 1=one line, 2=two lines, 3=three lines, 4=four lines
dirsb = %11110000 'portB high nibble is LCD D7-D4
dirsc = %11000000 'c.6 c.7 are outputs
gosub LCD_Init 'get lcd ready
setfreq m16 'run at a higher clockspeed after lcd init
LcdByte = 64 'data will go to LCD custom character CGRAM
gosub LCD_WrIns
for j = 0 to 63 'write the data to LCD custom character CGRAM
read j, LcdByte
gosub LCD_WrChr
next j
valueToGraph = 0
do 'for demo purpose this is simply looped continously
inc valueToGraph '0 - 255
'valueToGraph = 3
lcdbyte = 128 'move to first line, first position of DDRAM
gosub lcd_wrins
gosub drawGraph
lcdbyte = 192 'move to second line, first position
gosub lcd_wrins
bintoascii valueToGraph, huns, tens, ones 'break down input number so it can be displayed
lcdbyte = huns 'display tens
gosub lcd_wrchr
lcdbyte = tens 'display tens
gosub lcd_wrchr
lcdbyte = ones 'display ones
gosub lcd_wrchr
loop
end
'drawGraph draws a bargraph the length of the constant CHARS
'the graph starts at the current cursor location
'call with valueToGraph from 0 to 255
'routine is based on code hippy posted on forum
drawGraph:
'pixelsOn = CHARS * 5 - 1 * valueToGraph / 255 + 1 '1 pixel will be printed when 0
pixelsOn = CHARS * 5 * valueToGraph / 255 'no pixel will be printed when 0
charsOn = pixelsOn / 5
pixelsOn = pixelsOn // 5
charsToDo = CHARS - charsOn
Do While charsOn > 0
lcdbyte = 255 '255 prints a solid block of 5x8
gosub lcd_wrchr
charsOn = charsOn - 1
Loop
If pixelsOn <> 0 Then
lcdbyte = pixelsOn
gosub lcd_wrchr
charsToDo = charsToDo - 1
End If
Do While charsToDo > 0
lcdbyte = 0 'prints a blank space
gosub lcd_wrchr
charsToDo = charsToDo - 1
Loop
return 'doDraph
'* Initialise LCD *****************************************
LCD_Init:
low LcdRS
low LcdSE
'wait for LCD to stabilise (>40ms after Vcc > 2.7V)
pause 100
'send 00110000 three times to initialise LCD by instruction
outpinsb = %00110000
pulsout LcdSE,1
pause 5 'wait at least 4.1 milliseconds
pulsout LcdSE,1
pause 1
pulsout LcdSE,1
pause 1
outpinsb = %00100000 'set to 4 bit mode
pulsout LcdSE,1
LcdByte = %00101000 'set interface
gosub LCD_WrIns
LcdByte = %00001100 'enable display, no cursor
gosub LCD_WrIns
LcdByte = %00000001 'clear and home cursor
gosub LCD_WrIns
LcdByte = %00000110 'set cursor direction: movement
gosub LCD_WrIns
return
'* Display Character On LCD *******************************
LCD_WrChr:
high LcdRS 'set RS high (write data)
outpinsb = LcdByte and %11110000
pulsout LcdSE,1
outpinsb = LcdByte * 16
pulsout LcdSE,1
return
'* Send Instruction to LCD ********************************
LCD_WrIns:
low LcdRS 'set RS low (write instruction)
outpinsb = LcdByte and %11110000
pulsout LcdSE,1
outpinsb = LcdByte * 16
pulsout LcdSE,1
return
'* Clear LCD **********************************************
LCD_Clear:
LcdByte = 1
gosub LCD_WrIns
pause 8 'wait at least 1.5 milliseconds after home or clear
return
'* Home Cursor *******************************************
LCD_Home:
LcdByte = 128
gosub LCD_WrIns
pause 8 'wait at least 1.5 milliseconds after home or clear
return
'* Move To Line 1 *****************************************
LCD_Line1:
LcdByte = 128
gosub LCD_WrIns
return
'* Move To Line 2 *****************************************
LCD_Line2:
LcdByte = 192
gosub LCD_WrIns
return
'* Move To Line 3 *****************************************
LCD_Line3:
LcdByte = 148
gosub LCD_WrIns
return
'* Move To Line 4 *****************************************
LCD_Line4:
LcdByte = 212
gosub LCD_WrIns
return