'18lf2520 8MHz INT 5110 041017 1400 'Define SIMULATION_WAITMS_VALUE = 1 'Comment in for SIM out for PIC Define CLOCK_FREQUENCY = 8 Define SINGLE_DECIMAL_PLACES = 2 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 'SET PIN IN/OUT TRISA = %00000000 TRISB = %00000000 TRISC = %00000001 '0=button 'SET BITS ON/OFF PORTA = %00000000 'ON/OFF PORTB = %00000000 PORTC = %00000000 PORTE = %00001000 'POSS MCLR RE3 initialise_chip: OSCCON = %01110010 'internal 8Mhz clock WaitMs 10 AllDigital '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 'Define SPI_SDI_BIT = 4 Define SPI_SDO_REG = PORTC Define SPI_SDO_BIT = 5 'Define SPICLOCK_INVERT = 1 'Reverse signal 'Define SPICLOCK_STRETCH = 20 'Make the SPI signals slower Symbol lcd_reset = PORTA.4 Symbol lcd_cs = PORTA.5 'LCD Symbol lcd_data_command = PORTC.6 '=D/C Symbol lcd_sck = PORTC.3 Symbol lcd_sdo = PORTC.5 '= MOSI/SDO Symbol yled = PORTC.1 'yled 'START UP LED FLASHES yled = 1 WaitMs 1000 yled = 0 WaitMs 1000 definitions: Define STRING_MAX_LENGTH = 30 Dim i As Byte Dim j As Byte Dim k As Byte Dim li As Byte Dim lj As Byte Dim lk As Byte Dim si As Byte Dim sk As Byte Dim byte_to_send As Byte Dim x_value As Byte Dim y_value As Byte Dim character_to_send As Byte Dim char(8) As Byte Dim move_x As Byte Dim string_to_write As String Dim invert_display As Bit Dim wrap_text As Bit init: 'Start up flashes yled = 1 WaitMs 1000 yled = 0 WaitMs 1000 yled = 1 WaitMs 1000 yled = 0 WaitMs 1000 invert_display = 0 wrap_text = 0 WaitMs 100 Gosub init_lcd Gosub lcd_clear x_value = 2 'columns are individual pixels y_value = 0 'must be a row number, which is a multiple of 8 Gosub lcd_goto_xy Gosub draw_band x_value = 2 y_value = 1 Gosub lcd_goto_xy invert_display = 1 wrap_text = 0 string_to_write = "{ ABCDEFGHIJKLMNOPQRSTUVWXYZ" Gosub lcd_write_string invert_display = 0 x_value = 2 y_value = 3 wrap_text = 1 Gosub lcd_goto_xy string_to_write = "abcdefghijklmnopqrstuvwxyz" Gosub lcd_write_string x_value = 2 y_value = 5 Gosub lcd_goto_xy string_to_write = "123.45.67.89:0" Gosub lcd_write_string loop: High yled WaitMs 2000 Low yled WaitMs 2000 Goto loop End init_lcd: Low lcd_reset WaitMs 10 High lcd_reset Low lcd_data_command 'LOW=COMMAND byte_to_send = 0x21 'lcd extended commands Gosub lcd_write_byte byte_to_send = 0xb1 'set lcd contrast Gosub lcd_write_byte byte_to_send = 0x04 'set temp coefficient Gosub lcd_write_byte byte_to_send = 0x14 'lcd bias mode 1:48 Gosub lcd_write_byte byte_to_send = 0x0c 'put lcd into normal mode Gosub lcd_write_byte byte_to_send = 0x20 'dunno Gosub lcd_write_byte byte_to_send = 0x0c 'dunno Gosub lcd_write_byte WaitMs 5 High lcd_data_command 'HIGH=DATA Return lcd_write_byte: For li = 0 To 7 lk = 7 - li lj = 1 lj = ShiftLeft(lj, lk) lj = lj And byte_to_send If lj = 0 Then 'send a zero bit Low lcd_sdo Else 'send a one bit High lcd_sdo Endif 'toggle the clock line High lcd_sck ASM: nop Low lcd_sck Next li Return lcd_clear: 'put the cursor at 0,0 x_value = 0 y_value = 0 Gosub lcd_goto_xy 'put the lcd into data mode High lcd_data_command 'send 504 (48*84/8) blank pixels For i = 0 To 5 For j = 0 To 83 byte_to_send = 0x00 If invert_display = 1 Then byte_to_send = byte_to_send Nand 0xff Gosub lcd_write_byte Next j Next i 'just give everything a sec or two WaitMs 10 Return lcd_goto_xy: Low lcd_data_command byte_to_send = 0x80 byte_to_send = byte_to_send Or x_value Gosub lcd_write_byte byte_to_send = 0x40 byte_to_send = byte_to_send Or y_value Gosub lcd_write_byte High lcd_data_command Return lcd_get_character: 'set the default character to zero char(0) = 0x00 char(1) = 0x00 char(2) = 0x00 char(3) = 0x00 char(4) = 0x00 char(5) = 0x00 char(6) = 0x00 move_x = 0 Select Case character_to_send Case "A" char(0) = 01111110b char(1) = 00001001b char(2) = 00001001b char(3) = 01111110b move_x = 5 Case "B" char(0) = 01111111b char(1) = 01001001b char(2) = 01001001b char(3) = 00110110b move_x = 5 Case "C" char(0) = 00011100b char(1) = 00100010b char(2) = 01000001b char(3) = 01000001b move_x = 5 Case "D" char(0) = 01111111b char(1) = 01000001b char(2) = 01000001b char(3) = 00100010b char(4) = 00011100b move_x = 6 Case "E" char(0) = 01111111b char(1) = 01001001b char(2) = 01000001b move_x = 4 Case "F" char(0) = 01111111b char(1) = 00001001b char(2) = 00001001b move_x = 4 Case "G" char(0) = 00011100b char(1) = 00100010b char(2) = 01000001b char(3) = 01001001b char(4) = 01111010b move_x = 6 Case "H" char(0) = 01111111b char(1) = 00001000b char(2) = 00001000b char(3) = 01111111b move_x = 5 Case "I" char(0) = 01000001b char(1) = 01111111b char(2) = 01000001b move_x = 4 Case "J" char(0) = 00100000b char(1) = 01000000b char(2) = 01000001b char(3) = 00100001b char(4) = 00011111b move_x = 6 Case "K" char(0) = 01111111b char(1) = 00001000b char(2) = 00010100b char(3) = 01100011b move_x = 5 Case "L" char(0) = 01111111b char(1) = 01000000b char(2) = 01000000b move_x = 4 Case "M" char(0) = 01111111b char(1) = 00000001b char(2) = 00000010b char(3) = 00001100b char(4) = 00000010b char(5) = 00000001b char(6) = 01111111b move_x = 8 Case "N" char(0) = 01111111b char(1) = 00000100b char(2) = 00001000b char(3) = 00010000b char(4) = 01111111b move_x = 6 Case "O" char(0) = 00011100b char(1) = 00100010b char(2) = 01000001b char(3) = 01000001b char(4) = 00100010b char(5) = 00011100b move_x = 7 Case "P" char(0) = 01111111b char(1) = 00010001b char(2) = 00010001b char(3) = 00001110b move_x = 5 Case "Q" char(0) = 00011100b char(1) = 00100010b char(2) = 01000001b char(3) = 01010001b char(4) = 00100010b char(5) = 01011100b move_x = 7 Case "R" char(0) = 01111111b char(1) = 00011001b char(2) = 00101001b char(3) = 01000110b move_x = 5 Case "S" char(0) = 00100010b char(1) = 01000101b char(2) = 01001001b char(3) = 00110010b move_x = 5 Case "T" char(0) = 00000001b char(1) = 00000001b char(2) = 01111111b char(3) = 00000001b char(4) = 00000001b move_x = 6 Case "U" char(0) = 00111111b char(1) = 01000000b char(2) = 01000000b char(3) = 01111111b move_x = 5 Case "V" char(0) = 00000011b char(1) = 00011100b char(2) = 01100000b char(3) = 00011100b char(4) = 00000011b move_x = 6 Case "W" char(0) = 00111111b char(1) = 01000000b char(2) = 00100000b char(3) = 00011000b char(4) = 00100000b char(5) = 01000000b char(6) = 00111111b move_x = 8 Case "X" char(0) = 01100011b char(1) = 00010100b char(2) = 00001000b char(3) = 00010100b char(4) = 01100011b move_x = 6 Case "Y" char(0) = 00000011b char(1) = 00000100b char(2) = 01111100b char(3) = 00000111b move_x = 5 Case "Z" char(0) = 01111001b char(1) = 01001001b char(2) = 01001101b char(3) = 01001011b move_x = 5 Case "a" char(0) = 00100000b char(1) = 01010100b char(2) = 01010100b char(3) = 01111000b move_x = 5 Case "b" char(0) = 01111111b char(1) = 01000100b char(2) = 01000100b char(3) = 00111000b move_x = 5 Case "c" char(0) = 00111000b char(1) = 01000100b char(2) = 01000100b char(3) = 01000100b move_x = 5 Case "d" char(0) = 00111000b char(1) = 01000100b char(2) = 01000100b char(3) = 01111111b move_x = 5 Case "e" char(0) = 00111000b char(1) = 01010100b char(2) = 01010100b char(3) = 01011100b move_x = 5 Case "f" char(0) = 01111110b char(1) = 00001001b char(2) = 00000001b move_x = 4 Case "g" char(0) = 10011000b char(1) = 10100100b char(2) = 10100100b char(3) = 01111100b move_x = 5 Case "h" char(0) = 01111111b char(1) = 00000100b char(2) = 00000100b char(3) = 01111000b move_x = 5 Case "i" char(0) = 01000100b char(1) = 01111101b char(2) = 01000000b move_x = 4 Case "j" char(0) = 10000100b char(1) = 10000101b char(2) = 01111100b move_x = 4 Case "k" char(0) = 01111111b char(1) = 00010000b char(2) = 00010000b char(3) = 01101100b move_x = 5 Case "l" char(0) = 01000001b char(1) = 01111111b char(2) = 01000000b move_x = 4 Case "m" char(0) = 01111100b char(1) = 00000100b char(2) = 00000100b char(3) = 01111000b char(4) = 00000100b char(5) = 00000100b char(6) = 01111000b move_x = 8 Case "n" char(0) = 01111100b char(1) = 00000100b char(2) = 00000100b char(3) = 01111000b move_x = 5 Case "o" char(0) = 00111000b char(1) = 01000100b char(2) = 01000100b char(3) = 00111000b move_x = 5 Case "p" char(0) = 11111100b char(1) = 00100100b char(2) = 00100100b char(3) = 00011000b move_x = 5 Case "q" char(0) = 00011000b char(1) = 00100100b char(2) = 00100100b char(3) = 11111100b move_x = 5 Case "r" char(0) = 01111000b char(1) = 00000100b char(2) = 00000100b char(3) = 00000100b move_x = 5 Case "s" char(0) = 01001000b char(1) = 01010100b char(2) = 01010100b char(3) = 01010100b char(4) = 00100000b move_x = 6 Case "t" char(0) = 00111111b char(1) = 01000100b char(2) = 01000100b move_x = 4 Case "u" char(0) = 00111100b char(1) = 01000000b char(2) = 01000000b char(3) = 01111100b move_x = 5 Case "v" char(0) = 00011100b char(1) = 00100000b char(2) = 01000000b char(3) = 00100000b char(4) = 00011100b move_x = 6 Case "w" char(0) = 00111100b char(1) = 01000000b char(2) = 00100000b char(3) = 00010000b char(4) = 00100000b char(5) = 01000000b char(6) = 00111100b move_x = 8 Case "x" char(0) = 01101100b char(1) = 00010000b char(2) = 00010000b char(3) = 01101100b move_x = 5 Case "y" char(0) = 10001100b char(1) = 10010000b char(2) = 10010000b char(3) = 01111100b move_x = 5 Case "z" char(0) = 01100100b char(1) = 01010100b char(2) = 01001100b move_x = 4 Case "0" char(0) = 00111110b char(1) = 01000001b char(2) = 01000001b char(3) = 00111110b move_x = 5 Case "1", 1 char(0) = 01000010b char(1) = 01111111b char(2) = 01000000b move_x = 4 Case "2", 2 char(0) = 01100010b char(1) = 01010001b char(2) = 01010001b char(3) = 01001110b move_x = 5 Case "3", 3 char(0) = 01000010b char(1) = 01001001b char(2) = 01001001b char(3) = 00110110b move_x = 5 Case "4", 4 char(0) = 00011111b char(1) = 00010000b char(2) = 00010000b char(3) = 01111100b char(4) = 00010000b move_x = 6 Case "5", 5 char(0) = 00100111b char(1) = 01001001b char(2) = 01001001b char(3) = 00110001b move_x = 5 Case "6", 6 char(0) = 00111110b char(1) = 01001001b char(2) = 01001001b char(3) = 00110010b move_x = 5 Case "7", 7 char(0) = 00000001b char(1) = 01110001b char(2) = 00011001b char(3) = 00000111b move_x = 5 Case "8", 8 char(0) = 00110110b char(1) = 01001001b char(2) = 01001001b char(3) = 00110110b move_x = 5 Case "9", 9 char(0) = 00100110b char(1) = 01001001b char(2) = 01001001b char(3) = 00111110b move_x = 5 Case "." char(0) = 01100000b char(1) = 01100000b move_x = 3 Case ":" char(0) = 01100110b char(1) = 01100110b move_x = 3 Case 32 move_x = 4 Case "{" Gosub draw_wifi EndSelect Return send_character_to_lcd: 'put the lcd into data mode High lcd_data_command For i = 0 To 6 byte_to_send = char(i) If invert_display = 1 Then byte_to_send = byte_to_send Nand 0xff Gosub lcd_write_byte Next i Return lcd_write_string: sk = Len(string_to_write) - 1 For si = 0 To sk 'convert the requested character into an array of bits to display character_to_send = string_to_write(si) Gosub lcd_get_character 'check the character will fit on the screen k = x_value + move_x If k > 82 And wrap_text = 0 Then 'don't write anything more on this line x_value = 84 Else If k > 82 Then x_value = 2 y_value = y_value + 1 Gosub lcd_goto_xy Endif 'send the character to the lcd Gosub send_character_to_lcd 'update the "cursor x" position x_value = x_value + move_x Gosub lcd_goto_xy Endif Next si Return draw_band: x_value = 0 Gosub lcd_goto_xy 'put the lcd into data mode High lcd_data_command byte_to_send = 11110000b If invert_display = 1 Then byte_to_send = byte_to_send Nand 0xff For i = 0 To 83 Gosub lcd_write_byte Next i byte_to_send = 0xff If invert_display = 1 Then byte_to_send = byte_to_send Nand 0xff For i = 0 To 83 Gosub lcd_write_byte Next i byte_to_send = 00000111b If invert_display = 1 Then byte_to_send = byte_to_send Nand 0xff For i = 0 To 83 Gosub lcd_write_byte Next i Return draw_wifi: For i = 0 To 14 Select Case i Case 0, 14 byte_to_send = 00001000b Case 1, 13 byte_to_send = 00000100b Case 2, 12 byte_to_send = 00000010b Case 3, 11 byte_to_send = 00010010b Case 4, 10 byte_to_send = 00001001b Case 5, 9 byte_to_send = 00100101b Case 6, 8 byte_to_send = 10010101b Case 7 byte_to_send = 11010101b EndSelect If invert_display = 1 Then byte_to_send = byte_to_send Nand 0xff Gosub lcd_write_byte Next i x_value = x_value + 16 Gosub lcd_goto_xy Return