'18f4520 8MHz 19-10-2017 AllDigital Define CONFIG1L = 0x00 Define CONFIG1H = 0x08 'INT OSC Define CONFIG2L = 0x1e Define CONFIG2H = 0x00 Define CONFIG3L = 0x00 Define CONFIG3H = 0x80 Define CONFIG4L = 0x80 'Set for HVP Define CONFIG4H = 0x00 Define CONFIG5L = 0x0f Define CONFIG5H = 0xc0 Define CONFIG6L = 0x0f Define CONFIG6H = 0xe0 Define CONFIG7L = 0x0f Define CONFIG7H = 0x40 ''Define SIMULATION_WAITMS_VALUE = 1 'Comment in for SIM out for PIC Define CLOCK_FREQUENCY = 8 Define SINGLE_DECIMAL_PLACES = 2 Define STRING_MAX_LENGTH = 60 Symbol pot = PORTA.0 'adc Symbol yled = PORTA.1 'yled Symbol rled = PORTA.2 'rled 'SET PIN IN/OUT TRISA = %00101001 TRISB = %00000000 TRISC = %00000001 '0=button 'SET BITS ON/OFF PORTA = %00000000 'ON/OFF PORTB = %00000000 PORTC = %00000000 PORTE = %00001000 'POSS MCLR RE3 ADCON0 = 0x03 'adc ADCON1 = 0x0e ADCON2 = %10100100 PIR1 = 0 PIR2 = 0 PIE1 = 0 PIE2 = 0 PIE1.RCIE = 1 'rxd Intr , used for GPS serial input work IPR1 = 0 IPR2 = 0 RCON.IPEN = 1 'this MUST be included when usinh Interrupts OSCCON = %01110010 'internal 8Mhz clock WaitMs 10 'SPI Define SPI_CS_REG = PORTA Define SPI_CS_BIT = 5 Define SPI_SCK_REG = PORTC Define SPI_SCK_BIT = 3 Define SPI_SDI_REG = PORTC 'not used, but left in so OSH does not burp Define SPI_SDI_BIT = 4 Define SPI_SDO_REG = PORTC Define SPI_SDO_BIT = 5 Symbol lcd_reset = PORTA.4 Symbol lcd_mode = PORTC.6 '1=DATA ,0=COMMAND [D/C] 'bin2asc buffers, NOTE: fixed location in memory Dim ascbfr4 As Byte @ 0x0c0 Dim ascbfr3 As Byte @ 0x0c1 Dim ascbfr2 As Byte @ 0x0c2 Dim ascbfr1 As Byte @ 0x0c3 Dim ascbfr0 As Byte @ 0x0c4 Dim b2aval As Word @ 0x0c5 Dim temp3 As Word @ 0x0c7 Dim v0 As Word 'adco count Dim tot As Word 'demo count total Dim vlong As Long 'maths 'preloaded lcd/txd msgs Dim menu1 As String Dim menu2 As String Dim menu3 As String Dim menu4 As String Dim menu5 As String Dim menu6 As String Dim msg1 As String 'any msg data will be passed via this string Dim msg2 As String Dim ascval As String Dim rxdchr As String Dim rxdmsg As String Dim rxdbyt As Byte 'RXD char Dim x As Byte Dim y As Byte Dim row As Byte 'lcd row pointer Dim xp As Byte 'normal font horiz Dim yp As Byte 'vert lcd location Dim xx As Byte 'big font hor Dim yy As Byte 'vert Dim cnt As Byte 'demo number display count Dim cp As Byte Dim fp As Byte Include "font1.bas" 'this file nust be in the same folder as this program code 'test messages, your project msgs go here, more if needed 'test msgs, note: 14 characters/row And 6 rows with this fonts1.bas menu2 = "EVEN=V: 0123456789.0123456789.0123456789.0123456789." menu1 = "EXAMPLE OF HORIZONTAL SCROLLING ON THE 5110" menu3 = "THE REMOTE UNIT IS MOVING IN A NORTHERLY DIRECTION" menu4 = "AT AN ALTITUDE OF 1234 METES ABOVE THE REFERENCE POINT" msg1 = "" ''Hseropen 9600 start: 'do a lcd Reset Gosub rst_lcd '5110 states very ealy RESET is important SPIPrepare Gosub cmd5110 'init lcd WaitMs 10 'Enable High 'unrem these for serial input Interrupts, eg: GPS 'Enable Low main: 'Gosub rd_adc0 'Gosub demo_cntr 'examples of messages msg1 = menu1 'assign menu string to msg1 string Call scroll_msg(0, 0, msg1) msg1 = menu2 'assign menu string to msg1 string Call scroll_big_msg(0, 0, msg1) 'dummy loop loop2: Toggle PORTB.7 '''Goto loop2 Goto main End ''writes the msg To the lcd at xp [column] And yp {row] Proc scroll_big_msg(xp As Byte, yp As Byte, msg1 As String) y = Len(msg1) fp = 0 For cp = 1 To y lcd_mode = 0 'command SPICSOn SPISend 0x80 SPISend 0x44 SPICSOff lcd_mode = 1 'data row = 0 msg2 = MidStr(msg1, cp, 14) For x = 1 To 9 ascval = MidStr(msg2, x, 1) Gosub big2lcd Next x ascval = " " Gosub big2lcd lcd_mode = 0 'command SPICSOn SPISend 0x80 SPISend 0x45 SPICSOff lcd_mode = 1 'data row = 1 msg2 = MidStr(msg1, cp, 14) For x = 1 To 9 ascval = MidStr(msg2, x, 1) Gosub big2lcd Next x ascval = " " Gosub big2lcd If fp = 0 Then WaitMs 1000 fp = 1 Else WaitMs 300 Endif Next cp End Proc ''writes the msg To the lcd at xp [column] And yp {row] Proc scroll_msg(xp As Byte, yp As Byte, msg1 As String) y = Len(msg1) fp = 0 For cp = 1 To y lcd_mode = 0 'command SPICSOn SPISend 0x80 SPISend 0x40 SPICSOff lcd_mode = 1 'data row = 0 msg2 = MidStr(msg1, cp, 14) For x = 1 To 14 ascval = MidStr(msg2, x, 1) Gosub chr2lcd Next x ascval = " " Gosub chr2lcd lcd_mode = 0 'command SPICSOn SPISend 0x80 SPISend 0x40 SPICSOff lcd_mode = 1 'data row = 1 msg2 = MidStr(msg1, cp, 14) For x = 1 To 14 ascval = MidStr(msg2, x, 1) Gosub chr2lcd Next x ascval = " " Gosub chr2lcd If fp = 0 Then WaitMs 1000 fp = 1 Else WaitMs 300 Endif Next cp End Proc ''writes the msg To the lcd at xp [column] And yp {row] Proc wr_msg(xp As Byte, yp As Byte, msg1 As String) lcd_mode = 0 'command xp = xp * 6 xp = 0x80 Or xp yp = 0x40 Or yp SPICSOn SPISend xp SPISend yp SPICSOff lcd_mode = 1 'data y = Len(msg1) For x = 1 To y ascval = MidStr(msg1, x, 1) Gosub chr2lcd 'in font1.bas Next x End Proc ''writes the msg in BIG font to the lcd at xp [column] And yp {row] Proc wr_msg_big(xp As Byte, yp As Byte, msg1 As String) row = yp 'save row cnt lcd_mode = 0 'command xx = xp * 8 xx = 0x80 Or xp yy = 0x40 Or yp SPICSOn SPISend xx SPISend yy SPICSOff lcd_mode = 1 'data y = Len(msg1) For x = 1 To y ascval = MidStr(msg1, x, 1) Gosub big2lcd 'in font1.bas Next x lcd_mode = 0 xx = xp * 8 row = row + 1 xx = 0x80 Or xp yy = 0x40 Or yp yy = yy + 1 SPICSOn SPISend xx SPISend yy SPICSOff lcd_mode = 1 'data y = Len(msg1) For x = 1 To y ascval = MidStr(msg1, x, 1) Gosub big2lcd 'in font1.bas Next x End Proc 'clears all normal font row xp Proc clr_row(yp As Byte) xp = 0 lcd_mode = 0 xp = xp * 6 xp = 0x80 Or xp yp = 0x40 Or yp SPICSOn SPISend xp SPISend yp SPICSOff lcd_mode = 1 SPICSOn For x = 0 To 83 'all pixels across SPISend 0x00 Next x SPICSOff End Proc 'initialise 5110 and set lcd writing direction cmd5110: lcd_reset = 0 WaitMs 100 lcd_reset = 1 WaitMs 10 lcd_mode = 0 'cmd mode WaitMs 10 SPICSOn SPISend 0x21 WaitUs 100 SPISend 0xb8 'contrast was b0 WaitUs 100 SPISend 0x04 WaitUs 100 SPISend 0x14 'lcd bias WaitUs 100 SPISend 0x20 WaitUs 100 SPISend 0x0c '' 09 now normal data write mode WaitUs 100 SPICSOff WaitMs 5 ''' 'this is for setting top left of lcd and bit write direction. lcd_mode = 0 xp = xp * 6 xp = 0x80 Or xp yp = 0x40 Or yp SPICSOn SPISend xp SPISend yp SPICSOff WaitMs 5 ''' Return 'resest the lcd rst_lcd: lcd_reset = 1 WaitMs 10 lcd_reset = 0 WaitMs 10 lcd_reset = 1 Return 'convert any binary value from 0000h To ffffh To 0000 To 65535 decimal 'just name the binary word as b2aval and call this subr and the ASCII 'result will be in ascbfr4,3,2,1,0, ready for your LCD or UART 'just pop the DP in the output to the LCD [as shown above] bin2asc: ascbfr4 = b2aval / 10000 temp3 = b2aval Mod 10000 ascbfr3 = temp3 / 1000 temp3 = b2aval Mod 1000 ascbfr2 = temp3 / 100 temp3 = temp3 Mod 100 ascbfr1 = temp3 / 10 ascbfr0 = temp3 Mod 10 'results are BCD so 'convert To ascii For lcd Or uart ascbfr4 = ascbfr4 Or 0x30 ascbfr3 = ascbfr3 Or 0x30 ascbfr2 = ascbfr2 Or 0x30 ascbfr1 = ascbfr1 Or 0x30 ascbfr0 = ascbfr0 Or 0x30 Return