'-------------------------------------------------------------------------------- 'name : I2C_LCD_tst.bas 'purpose : Character LCD (Strawberry LINUX), I2C I/F 'micro controller : Mega1284 '-------------------------------------------------------------------------------- '-------------------------------------------------------------------------------- Rem Used ports ' ' LCD I/F (TWI = hardware I2C) ' Hardware I2C pins on Mega1284 are ' ' PC0 : SCL ' PC1 : SDA '-------------------------------------------------------------------------------- $regfile = "m1284pdef.dat" $crystal = 8192000 ' 8.192MHz Xtal clock $hwstack = 64 $swstack = 64 $framesize = 40 Const Contrast = &B0010_0000 ' LCD contrast (from &B0000_0000 to &B0011_1111) Const I2caddr = &B0111_1100 ' I2C slave address (= &B0011_1110) x 2 ' (address x 2) is needed for I2C protocol. $lib "i2c_twi.lbx" ' Include BASCOM I2C library (for hardware I2C = TWI) Rem I2C I/O pin definition ' Since this program uses hardware I2C, i.e. TWI, ' hardware I2C pins on Mega1284 are defined. Config Scl = Portc.0 ' SCL Config Sda = Portc.1 ' SDA Rem type definition of variables ' for working Dim I As Integer Dim B As Byte Dim S As String * 32 Dim Saddr As Word Dim Ns As Integer Dim Is0 As Integer Dim Bs0 As Byte Dim Twid1 As Byte , Twid2 As Byte Rem program start ' I2C BUS initialize Config Twi = 100000 ' 100kHz Clock rate of hardware I2C = TWI I2cinit Waitms 40 ' wait LCD ready from its power ON ' LCD initialize Gosub Init_twi_lcd ' LCD initialize Gosub Twi_cls ' Clear LCD Screen S = "M1284 TWI LCD" Gosub Print_twi ' print String S to LCD Gosub Nextline ' goto lowerline S = "Starting" Gosub Print_twi ' print String S to LCD Wait 3 Gosub Twi_cls ' Clear LCD Screen S = "Number display" Gosub Print_twi For I = 1 To 10 S = Str(i) S = S + " " Gosub Nextline ' locate cursor to &H40 address Gosub Print_twi Wait 1 Next I Gosub Topline S = "Test END " Gosub Print_twi End Rem subroutines ' Data out to I2C bus ' Send 2 bytes (control byte (Twid1) and data (Twid2) byte) I2cout: I2cstart I2cwbyte I2caddr ' send I2C address I2cwbyte Twid1 ' send control byte ( = RS bit) I2cwbyte Twid2 ' send Instruction or Data ' (If RS bit in the preceding control byte = 1, then this byte = Data. ' If RS bit = 0, then this byte is interpretted as Instruction.) I2cstop Return ' Initialize LCD Init_twi_lcd: Twid1 = &B0000_0000 ' set RS bit = 0 in control byte (RS=0 indicates Instruction transfer) Twid2 = &B0011_1000 ' function set, 8-bit I/F, 2-line display Gosub I2cout Waitus 30 Twid2 = &B0011_1001 ' Extended mode function set, 8-bit I/F, 2-line display, instruction table 1 Gosub I2cout Waitus 30 Twid2 = &B0001_0100 ' adjust internal OSC frequency, 1/5 bias Gosub I2cout Waitus 30 B = Contrast And &H0F B = B Or &B0111_0000 Twid2 = B ' set contrast lower 4-bit Gosub I2cout Waitus 30 B = Contrast Shift B , Right , 4 B = B And &H03 B = B Or &B0101_1100 Twid2 = B ' set contrast higher 2-bits, display ON, & power ON Gosub I2cout Waitus 30 Twid2 = &B0110_1100 ' follower control Gosub I2cout Waitms 300 Twid2 = &B0011_1000 ' function set, instruction table -> 0 Gosub I2cout Waitus 30 Twid2 = &B0000_1100 ' display ON, cursor off Gosub I2cout Waitus 30 Twid2 = &B0000_0110 ' entry mode set: incremental addressing Gosub I2cout Waitus 30 Return ' Clear screen Twi_cls: Twid1 = &B0000_0000 ' set RS = 0 in control byte (RS=0 indicates Instruction transfer) Twid2 = &B0000_0001 'clear display Gosub I2cout Waitms 2 Twid2 = &B0000_0010 ' reset cursor position Gosub I2cout Waitus 30 Return ' Print strings S to LCD Print_twi: Saddr = Varptr(s) ' string variable S top address Ns = Len(s) ' string length For Is0 = 1 To Ns Bs0 = Inp(saddr) ' get a byte in the string Gosub Lcdchar ' print one char. Incr Saddr Next Is0 Return ' print a character Bs0 to LCD Lcdchar: Twid1 = &B0100_0000 ' set RS bit = 1 in control byte (RS=1 indicates Data transfer) Twid2 = Bs0 Gosub I2cout Return ' to lowerline Nextline: Twid1 = &B0000_0000 ' set RS = 0 in control byte (RS=0 indicates Instruction transfer) Twid2 = &HC0 ' set DDRAM address to &H40 Gosub I2cout Return ' to upperline Topline: Twid1 = &B0000_0000 ' set RS = 0 in control byte (RS=0 indicates Instruction transfer) Twid2 = &H80 ' set DDRAM address to &H00 Gosub I2cout Return