'**************************************************************** '* Name : OP51320demo.BAS * '* Author : Ranjit Diol * '* Notice : Copyright (c) 2005 COMPSys * '* : All Rights Reserved * '* Date : 2/15/2005 * '* Version : 1.0.0 * '* Notes : Optrex 51320 COG LCD (5 volt) * '* : on-board Controller SED1565 * '* : COG30 dev board '* Compiler: PICBasicPro Ver 2.45 '**************************************************************** ' NOT FOR COMMERCIAL USE ' DISCLAIMER: This file is being released as non-commericial ' software. It is being provided "AS IS", neither the author, ' nor COMPSys shall be held liable for any damages caused ' by its use. ' ************** This code is written for a PIC16F876 ************** ' Other 28pin PICs can be used instead @ device pic16F876, xt_osc, wdt_on, pwrt_off, bod_off, lvp_off, cpd_off, wrt_off, protect_off ' **************************************************************************************** DEFINE LOADER_USED 1 'If using ISP DEFINE OSC 10 'Adjust to suit your design ADCON1 = 7 'Adjust ADC for your needs OPTION_REG.7 = 1 'PORTB pull-ups off (0=ON) the LCD uses these pins low portb.3 'keep reset pin low while initializing include "modedefs.bas" ' Set receive register to receiver enabled DEFINE HSER_RCSTA 90h DEFINE HSER_BAUD 19200 'Baud = 19200 'DEFINE HSER_SPBRG 15 'for 20mhz Must be changed depending on Osc. used, see PIC datasheet DEFINE HSER_SPBRG 7 'for 10Mhz ' Set transmit register to transmitter enabled DEFINE HSER_TXSTA 20h DEFINE HSER_CLROERR 1 'Hser clear overflow automatically 'Optrex 51320 pins ' 1 = n/c ' 2 = /CS ' 3 = /RST ' 4 = A0 ' 5 = /WR ' 6 = /RD ' 7-14 = Lcd Data ' 15 = Vdd (5v+) ' 16 = Vss (GND) ' 17 = Vout ' 18 = C3- ' 19 = C1+ ' 20 = C1- ' 21 = C2- ' 22 = C2+ ' 23 = V1 ' 24 = V2 ' 25 = V3 ' 26 = V4 ' 27 = V5 ' 28 = VR ' 29 = C86 (Low = 80 type mcu) ' 30 = IRS (High = use internal resistors) 'LCD pin assignments, variables and constants lcd_cs var portc.5 'LCD pin 2 lcd_rst var portc.1 'LCD pin 3 lcd_a0 var portc.2 'LCD pin 4 lcd_wr var portc.0 'LCD pin 5 lcd_rd var portc.4 'LCD pin 6 lcd_port var portb 'LCD pins 7-14 lcd_tris var trisb 'Make pins as output TRISC.0 = 0 TRISC.1 = 0 TRISC.2 = 0 TRISC.4 = 0 TRISC.5 = 0 'General variables used lcd_dat var byte lcd_stat var byte lcd_cmd var byte lcd_x var byte lcdx var byte lcd_y var byte lcd_byte var byte col var byte col_msb var byte col_lsb var byte row var byte page var BYTE line var byte x var byte y var byte i var byte k var byte fidx var byte ix var byte iy var byte ik var byte char var byte chr var byte ch var byte idx var byte nibh var byte nibl var byte 'For graphic routines lcd_in var byte lcd_out var byte lcd_cx var byte lcd_cy var byte lcd_cr var byte lcd_s var word lcd_x1 var byte lcd_y1 var byte lcd_x2 var byte lcd_y2 var byte tmp1 var byte tmp2 var byte PAGE = $B0 '%10110000 $B0 Set Page to address 0 col_msb = $10 '%00010000 'COL MSB col_lsb = $00 '%00000000 'COL LSB 'LCD Constants ADC con $A0 '%10100000 $A0 normal dir SEG0 to SEG131 (%10100001 $A1 = reverse dir) BIAS con $A2 '%10100010 $A2 = 1/9 bias SHL con $C8 '%11001000 $C8 = COM63->COM0 reverse ( $C0 = COM0->COM63 normal) PWR con $2F '%00101111 $2F (adjust as needed) REG con $26 '%00100110 $26 R0-2 VOLSET con $81 '%10000001 $81 VOLINIT CON $20 '$36 '$35 to $3F (to adjust contrast) VREF con $0E '%00001110 $0E ($36 = %00110110 ) LCDON CON $AF '%10101111 $AF Display ON LCDOFF CON $AE '%10101110 $AE Display OFF DISPOFF CON $A4 '%10100100 $A4 Entire display off (normal) DISPON CON $A5 '%10100101 $A5 Entire display ON REVON con $A7 '%10100111 $A7 Reverse display ON REVOFF con $A6 '%10100110 $A6 Normal display OFF RESET con $E2 '%11100010 $E2 Software reset PAGE0 con $B0 '%10110000 $B0 first line PAGE1 CON $B1 PAGE2 CON $B2 PAGE3 CON $B3 PAGE4 CON $B4 PAGE5 CON $B5 PAGE6 CON $B6 PAGE7 CON $B7 STARTLINE con $40 '%01000000 $40 Initial start line CHRSPC con 1 'Spacing in pixels between chars CWIDTH con 6 '5x7 char width (includes 1 blank pixel INITLINE var BYTE INITLINE =$40 ' %01000000 $40 Initial start line (DDRAM COM0) COLMSB CON $10 '%00010000 $10 Set COL MSB COLLSB CON $00 '%00000000 $00 Set COL LSB LCDWIDTH CON 128 LCDHEIGHT con 64 LCDVERT con 63 OFFSET con 4 PI con 3 LCDHORIZ con 127 + OFFSET TRISB = 0 'I2C EEprom pin assignments, variables and constants scl var PORTC.3 'I2C SCL for ext eeprom sda var PORTC.4 'I2C SDA for ext eeprom edat var BYTE 'Data byte to be written ctl con $A0 'EEPROM control code 'Initialization reset high lcd_cs low lcd_rst low lcd_rst pause 200 high lcd_rst high lcd_rst low lcd_cs hserout ["Starting...",13,10] GOTO main 'Skip over the subroutines 'Bitwise Serial I/O Commands 'Shiftin lcd_sid, lcd_sck, MSBPRE, [dat] 'Shiftout lcd_sid, lcd_sck, MSBFIRST,[dat] '********** Subroutines *********** 'LCD initialization LCD_init: low lcd_rst pauseus 50 high lcd_rst lcd_cmd = reset '$E2 gosub lcdcmd lcd_cmd = BIAS '$A2 gosub lcdcmd lcd_cmd = ADC '$A0 gosub LCDcmd lcd_cmd = SHL '$C8 gosub lcdcmd lcd_cmd = REG '$26 gosub lcdcmd lcd_cmd = VOLSET '$81 gosub lcdcmd lcd_cmd = VOLINIT '$20 gosub lcdcmd lcd_cmd = PWR '$2F gosub lcdcmd lcd_cmd = lcdon '$AF gosub lcdcmd 'lcd_cmd = $30 'gosub lcdcmd 'lcd_cmd = volset 'gosub lcdcmd 'lcd_cmd = $3D 'gosub lcdcmd return 'LCD Command assumes lcd_cmd has the command byte LCDcmd: high lcd_rd low lcd_a0 low lcd_cs low lcd_wr lcd_port = lcd_cmd high lcd_wr high lcd_cs return 'LCD data out assumes lcd_dat has the data byte LCDdata: high lcd_rd high lcd_a0 low lcd_cs low lcd_wr lcd_port =lcd_dat high lcd_wr high lcd_cs return LCDstatus: lcd_tris = $FF high lcd_wr low lcd_a0 low lcd_cs low lcd_rd lcd_stat = lcd_port high lcd_rd high lcd_cs lcd_tris = 0 return 'Assumes lcd_dat has the byte pattern to display LCDfill: for page = page0 to PAGE7 lcd_cmd = startline gosub lcdcmd lcd_cmd = page gosub lcdcmd lcd_cmd = $10 gosub lcdcmd lcd_cmd = offset gosub lcdcmd 'lcd_dat = %10011001 for fidx = OFFSET to LCDHORIZ 'Shiftout lcd_sid, lcd_sck, MSBFIRST,[lcd_dat] gosub LCDdata next fidx next page return LCDclr: lcd_dat = 0 gosub LCDfill return 'Assumes lcd_x, lcd_y and lcd_byte LCDByte: lcd_cmd=startline : gosub lcdcmd row = lcd_y/8 + page0 '$B0 to $B7 lcd_cmd=row : gosub lcdcmd lcdx = lcd_x + offset lcd_cmd = (COLMSB | (lcdx >> 4)) : gosub lcdcmd lcd_cmd = lcdx & $0F : gosub lcdcmd lcd_dat = lcd_byte gosub LCDdata return 'Place a character assumes lcd_x,lcd_y, and chr LCDPutChar: char = chr - $20 if chr > $4f then char = (chr - $50) idx = char*5 for ix = 1 to 5 if chr > $4F then gosub table2 idx=idx+1 else gosub table1 idx=idx+1 endif gosub lcdbyte lcd_x = lcd_x + 1 next ix lcd_x = lcd_x + 1 lcd_byte = 0 'blank pixel gosub lcdbyte return 'Goto a LCD position assumes lcd_x,lcd_y LCDGoTo: row = lcd_y/8 + page0 '$B0 to $B9 lcd_cmd=row : gosub lcdcmd lcd_cmd=startline : gosub lcdcmd lcdx = lcd_x + offset lcd_cmd = (colmsb | (lcdx >> 4)) : gosub lcdcmd lcd_cmd = lcdx & $0F : gosub lcdcmd Return '*********** END OF SUBROUTINES ************** Main: hserout ["LCD init...",13,10] gosub lcd_init gosub lcdclr ' ***Write some text*** 'Lcd_y is the vertical position and will be converted to the appropriate 'PAGE number (lines 1 to 8) by dividing lcd_y by 8 'Lcd_x is the horizontal position (0 to 99) 'The lookup table is used for printing each character lcd_y = 8 lcd_x = 16 for x = 0 to 17 lookup x,["Optrex F-51320 LCD"],chr gosub lcdputchar next x lcd_y = 16 lcd_x = 36 for x = 0 to 10 lookup x,["Graphic LCD"],chr gosub lcdputchar next x 'Display some 8x8 pixel icons lcd_y = 16 lcd_x =8 for x = 0 to 7 lookup x,[$C3,$A5,$99,$81,$99,$A5,$C3,$00],lcd_byte lcd_x = lcd_x + 1 'advance one pixel horiz gosub lcdbyte next x lcd_y = 16 lcd_x =20 for x = 0 to 7 lookup x,[$18,$18,$18,$18,$7E,$42,$24,$18],lcd_byte lcd_x = lcd_x + 1 'advance one pixel horiz gosub lcdbyte next x lcd_y = 16 lcd_x =103 for x = 0 to 7 lookup x,[$26,$46,$80,$90,$90,$80,$46,$26],lcd_byte lcd_x = lcd_x + 1 'advance one pixel horiz gosub lcdbyte next x lcd_y = 16 lcd_x =113 for x = 0 to 7 lookup x,[$FF,$81,$BD,$A5,$A5,$BD,$81,$FF],lcd_byte lcd_x = lcd_x + 1 'advance one pixel horiz gosub lcdbyte next x lcd_y = 32 lcd_x = 60 for x = 0 to 7 lookup x,[$60,$60,$1F,$03,$C6,$FC,$00,$00],lcd_byte lcd_x = lcd_x + 1 'advance one pixel horiz gosub lcdbyte next x 'Print some more text lcd_y = 48 lcd_x = 24 for x = 0 to 14 lookup x,["(c)COMPSys 2005"],chr gosub lcdputchar next x '******* END OF MAIN PROGRAM ************ fini: goto fini 'Loop forever '***** ASCII TABLE SUBROUTINES ********** 'ASCII table for 5x7 dots font Table1: lookup idx,[_ $00,$00,$00,$00,$00,_ ' 20 space $00,$00,$5f,$00,$00,_ ' 21 ! $00,$07,$00,$07,$00,_ ' 22 " $14,$7f,$14,$7f,$14,_ ' 23 # $24,$2a,$7f,$2a,$12,_ ' 24 $ $23,$13,$08,$64,$62,_ ' 25 % $36,$49,$55,$22,$50,_ ' 26 & $00,$05,$03,$00,$00,_ ' 27 ' $00,$1c,$22,$41,$00,_ ' 28 ( $00,$41,$22,$1c,$00,_ ' 29 ) $14,$08,$3e,$08,$14,_ ' 2a * $08,$08,$3e,$08,$08,_ ' 2b + $00,$50,$30,$00,$00,_ ' 2c , $08,$08,$08,$08,$08,_ ' 2d - $00,$60,$60,$00,$00,_ ' 2e . $20,$10,$08,$04,$02,_ ' 2f / $3e,$51,$49,$45,$3e,_ ' 30 0 $00,$42,$7f,$40,$00,_ ' 31 1 $42,$61,$51,$49,$46,_ ' 32 2 $21,$41,$45,$4b,$31,_ ' 33 3 $18,$14,$12,$7f,$10,_ ' 34 4 $27,$45,$45,$45,$39,_ ' 35 5 $3c,$4a,$49,$49,$30,_ ' 36 6 $01,$71,$09,$05,$03,_ ' 37 7 $36,$49,$49,$49,$36,_ ' 38 8 $06,$49,$49,$29,$1e,_ ' 39 9 $00,$36,$36,$00,$00,_ ' 3a : $00,$56,$36,$00,$00,_ ' 3b ; $08,$14,$22,$41,$00,_ ' 3c < $14,$14,$14,$14,$14,_ ' 3d = $00,$41,$22,$14,$08,_ ' 3e > $02,$01,$51,$09,$06,_ ' 3f ? $32,$49,$79,$41,$3e,_ ' 40 @ $7e,$11,$11,$11,$7e,_ ' 41 A $7f,$49,$49,$49,$36,_ ' 42 B $3e,$41,$41,$41,$22,_ ' 43 C $7f,$41,$41,$22,$1c,_ ' 44 D $7f,$49,$49,$49,$41,_ ' 45 E $7f,$09,$09,$09,$01,_ ' 46 F $3e,$41,$49,$49,$7a,_ ' 47 G $7f,$08,$08,$08,$7f,_ ' 48 H $00,$41,$7f,$41,$00,_ ' 49 I $20,$40,$41,$3f,$01,_ ' 4a J $7f,$08,$14,$22,$41,_ ' 4b K $7f,$40,$40,$40,$40,_ ' 4c L $7f,$02,$0c,$02,$7f,_ ' 4d M $7f,$04,$08,$10,$7f,_ ' 4e N $3e,$41,$41,$41,$3e],lcd_byte ' 4f O return Table2: lookup idx,[_ $7f,$09,$09,$09,$06,_ ' 50 P $3e,$41,$51,$21,$5e,_ ' 51 Q $7f,$09,$19,$29,$46,_ ' 52 R $46,$49,$49,$49,$31,_ ' 53 S $01,$01,$7f,$01,$01,_ ' 54 T $3f,$40,$40,$40,$3f,_ ' 55 U $1f,$20,$40,$20,$1f,_ ' 56 V $3f,$40,$38,$40,$3f,_ ' 57 W $63,$14,$08,$14,$63,_ ' 58 X $07,$08,$70,$08,$07,_ ' 59 Y $61,$51,$49,$45,$43,_ ' 5a Z $00,$7f,$41,$41,$00,_ ' 5b [ $02,$04,$08,$10,$20,_ ' 5c \ $00,$41,$41,$7f,$00,_ ' 5d ] $04,$02,$01,$02,$04,_ ' 5e ^ $40,$40,$40,$40,$40,_ ' 5f _ $00,$01,$02,$04,$00,_ ' 60 ` $20,$54,$54,$54,$78,_ ' 61 a $7f,$48,$44,$44,$38,_ ' 62 b $38,$44,$44,$44,$20,_ ' 63 c $38,$44,$44,$48,$7f,_ ' 64 d $38,$54,$54,$54,$18,_ ' 65 e $08,$7e,$09,$01,$02,_ ' 66 f $0c,$52,$52,$52,$3e,_ ' 67 g $7f,$08,$04,$04,$78,_ ' 68 h $00,$44,$7d,$40,$00,_ ' 69 i $20,$40,$44,$3d,$00,_ ' 6a j $7f,$10,$28,$44,$00,_ ' 6b k $00,$41,$7f,$40,$00,_ ' 6c l $7c,$04,$18,$04,$78,_ ' 6d m $7c,$08,$04,$04,$78,_ ' 6e n $38,$44,$44,$44,$38,_ ' 6f o $7c,$14,$14,$14,$08,_ ' 70 p $08,$14,$14,$18,$7c,_ ' 71 q $7c,$08,$04,$04,$08,_ ' 72 r $48,$54,$54,$54,$20,_ ' 73 s $04,$3f,$44,$40,$20,_ ' 74 t $3c,$40,$40,$20,$7c,_ ' 75 u $1c,$20,$40,$20,$1c,_ ' 76 v $3c,$40,$30,$40,$3c,_ ' 77 w $44,$28,$10,$28,$44,_ ' 78 x $0c,$50,$50,$50,$3c,_ ' 79 y $44,$64,$54,$4c,$44,_ ' 7a z $00,$08,$36,$41,$00,_ ' 7b { $00,$00,$7f,$00,$00,_ ' 7c | $00,$41,$36,$08,$00,_ ' 7d } $10,$08,$08,$10,$08,_ ' 7e ~ $78,$46,$41,$46,$78],lcd_byte ' 7f ¦ '$7e,$43,$40,$43,$7e 'Ü '$7e,$43,$42,$43,$7e 'Ö '$1e,$21,$21,$71,$21 'Ç '$3c,$42,$53,$53,$72 'Ð return end