Search
Enter Keywords:
Thursday, 09 September 2010
Home arrow Projects arrow Microchip Pic and dsPIC Programming arrow TW LCD Module for MikroBasic
Newsflash
Please be patient as data and information needs to be inputed . This is a fresh install of csaba.ch and will be updated more and more
 
TW LCD Module for MikroBasic Print E-mail
Written by Administrator   
Monday, 28 April 2008

This code demonstrates the 2 wire (two wire = TW) interface . It is not an I2C driven LCD allthough kind of similar.
The origianl design is by Myke Predko and then was ported to the MikroC platform by Rich Irace .
I then ported it to MikroBasic and MikroBasic for dsPIC and GCC for AVR Studio4 .

Here is the youtube video on how it works:

You need to a flashplayer enabled browser to view this YouTube video

 
program TW_LCD_Demo
' * Project name:
'     TW_LCD_Demo
' * Copyright:
'     Public Domain, 2008
' * Author   :
'     Csaba Zvekan
' * Revision History:
'     20080428:
'       - initial release.
' * Description:
'     This code demonstrates the two wire Serial LCD Interface originaly
'     designed Myke Predko. The main code here was written in MikroC by Rich Irace.
'     I took a different approach for the NybbleShift Routine. More like what
'     myke suggested  on http://www.myke.com/lcd.htm. Also changed some LCD
'    timing values.
'     Except at the end I keep Data lines high then low. Different from his
'     website stated where he keeps clock High then low (to enable "E"pin)
' * Test configuration:
'     MCU:             PIC18F452
'     Dev.Board:       EasyPIC4
'     Oscillator:      HS, 4 - 48.000 MHz   fully Tested
'     Ext. Modules:    TW_LCD board, Lcd_2x16
'     SW:              mikroBasic v6.0 or higher
' * NOTES:
'     - In order to have valid function, PORTx pin's must be connected to
'       pull-up/ or down resistors !
'     - LEDiodes on PORTx should be turned off !
 
include TW_LCD
 
dim text as string[16]
 
main:
'**********************************************************************
  ADCON0 =  %00000000
  ADCON1 =  %00000100       ' used to be 0b00000100
  PORTA =   %00000000       ' Start with Everything Low
  PORTB =   %00000000        '  Start with Everything Low
  PORTC =   %00000000        '  Start with Everything Low
  PORTD =   %00000000        '  Start with Everything Low
  PORTE =   %00000000
  INTCON =  %00000000       ' Disables Interrupts
 
  TRISA =   %00000000       ' ALL Outputs
  TRISB =   %00000000        ' All Outputs   PORTB6 and PORTB7 TW_LCD
  TRISC =   %00000000        ' ALL Outputs
  TRISD =   %00000000        ' ALL Outputs
  TRISE =   %00000000       ' ALL Outputs
'**********************************************************************
  'variables
  'none
 
'**********************************************************************
    TW_LCD_Init()                ' Initializes the two wire LCD Interface
 
    while true
    TW_LCD_CMD(Lcd_Clear)
    text="Initialized "
    TW_LCD_Out(1, 3,text)         ' Displays Message on Row 1
    Delay_ms(3000)
 
    text="Two Wire LCD"
    TW_LCD_Out(1, 2,text)         ' Displays Message on Row 1
    text="inf www.csaba.ch"
    TW_LCD_Out(2, 1,text)         ' Displays Message on Row 2
    Delay_ms(3000)
    TW_LCD_CMD(Lcd_Clear)
    TW_LCD_CMD(Lcd_Underline_On)
    TW_LCD_CMD(Lcd_Blink_Cursor_On)
    text="Cursor Blink On"
    TW_LCD_Out(1, 1,text)         ' Displays Message on Row 1
    Delay_ms(3000)
    TW_LCD_CMD(Lcd_Cursor_Off)
    text="Now Off"
    TW_LCD_Out(2, 3,text)         ' Displays Message on Row 2
    Delay_ms(3000)
    text="same CMDs MikroE"
    TW_LCD_Out(2, 1,text)         ' Displays Message on Row 2
    TW_LCD_CMD(Lcd_Clear)
 
    text="Celsius 25.5"
    TW_LCD_Out(1, 2,text)         ' Displays Message on Row 1
    TW_LCD_Write(%11011111,1)    ' Displays the degree symbol °
    Delay_ms(3000)
    wend
end.
 

and then the TW.LCD module is here:

 
module TW_LCD
' * Project name:
'     module TW_LCD.pbas
' * Copyright:
'     Public Domain, 2008
' * Author   :
'     Csaba Zvekan
' * Revision History:
'     20080428:
'       - initial release.
' * Description:
'     This code demonstrates the two wire Serial LCD Interface originaly
'     designed Myke Predko. The main code here was written in MikroC by Rich Irace.
'     I took a different approach for the NybbleShift Routine. More like what
'     myke suggested  on http://www.myke.com/lcd.htm. Also changed some LCD
'    timing values.
'     Except at the end I keep Data lines high then low. Different from his
'     website stated where he keeps clock High then low (to enable "E"pin)
' * Test configuration:
'     MCU:             PIC18F452
'     Dev.Board:       EasyPIC4
'     Oscillator:      HS, 4 - 48.000 MHz   fully Tested
'     Ext. Modules:    TW_LCD board, Lcd_2x16
'     SW:              mikroBasic v6.0 or higher
' * NOTES:
'     - In order to have valid function, PORTx pin's must be connected to
'       pull-up/ or down resistors !
'     - LEDiodes on PORTx should be turned off !
symbol Clk  = PORTB.4         ' PORTB6 for the Clock Signal
symbol Data = PORTB.3          ' PORTB7 for the Data Signal
'* This is where you define the two lines for Clock / Data
'**********************************************************************
symbol row1  =  %00000000        '  Define on  LCD Row1 (0)
symbol row2  =  %01000000        '  Define on  LCD Row2 (64)
'symbol Lcd_Clear = %00000001    '  Define LCD_CLEAR
 
const ClockDelay as byte  = 160
dim  progarray as string [17]
'*****************************************************************
'* Shift Out the Nybble
'*****************************************************************
sub procedure NybbleShift(dim Nybble as byte, dim RS as byte)
dim i as byte
'**  Empty shift register by writing 6 zeroes to it
    Data = 0
    for i= 1 to 6       'Clear the Shift Register  by writing
        Clk  = 1        '6 times data 0
        Clk  = 0
    next i
'** Output  E-pin on LCD at the end of the 6th bit
        Data = 1
        Clk  = 1
        Clk  = 0
'** Output RS-Pin on LCD at 5th position
        Data = RS
        Clk  = 1
        Clk  = 0
'** Output the Nybble   highest bit first
      for i= 1 to 4
        if Nybble.3  = 1 then    ' look at bit MSB  lower nybble
           Data = 1
         else
           Data = 0
        end if
        Clk = 1
        Clk = 0
        Nybble= Nybble << 1
        next i
'** Finish Nybble write with E-pin High
        Data  = 1                ' E-pin goes high by pulling Data high
        delay_us(1)              ' 450nS for the delay
        Data  = 0
end sub
'**********************************************************************
'* Write a byte to LCD with option RS(xx,1) HIGH or RS(xx,0) LOW
'**********************************************************************
sub procedure TW_LCD_Write(dim LCDData as byte,dim RSValue as byte)
dim  j,t as byte
    t = 0
    j = 0
    j = LCDData                  'save LCDData to j
    NybbleShift((j >> 4)and 0x0F, RSValue )     'Shift out High Nybble
    'delay_us(ClockDelay)        ' 450nS for the clock only if necessary
    t = (LCDData << 4)
    t = (t >> 4)
    NybbleShift(t and 0x0F, RSValue)      'Shift out Low Nybble
    'delay_us(ClockDelay)        ' 450nS for the clock
    if ((LCDData AND 0xFC) + (RSValue)) = 0  then
        delay_ms(5)              ' to be adjusted even higher !!!
        else
        delay_uS(160)            ' to be adjusted even higher !!!
    end if
end sub
'**********************************************************************
'* Converts digital value to BCD
'**********************************************************************
sub function NumToChar(dim  valu as integer)as integer
   dim ch as integer
   if valu < 10 then
    ch=valu + 48                 ' 48 = '0'
   else
    valu=valu - 10
    end if                       ' 65 = A   in MIKROC
    ch=valu + 48                 ' 48 for MikroBasic
    result = ch
end sub
'***********************************************************************
'* LCD to BCD  displays byte in decimal as either 1, 2 or 3 digits
'***********************************************************************
sub procedure LCDtoBCD(dim valu as integer,dim digits as integer)
   dim d as integer
   dim ch as integer
   if digits = 3 then           ' Take 100's Digits
     d = valu/100                 ' Divide it by 100
     ch = NumtoChar(d)           ' Take number, convert it to ASCII
        ' lcdchar(ch)            ' number (0 - 9)
    TW_LCD_Write(ch, 1)           ' Send that value to display
   end if
   if (digits >1) then           ' Take the two lowest digits
      valu = valu mod 100
      d = valu/10
      ch = NumtoChar(d)
                                  ' lcdchar(ch)
    TW_LCD_Write(ch, 1)
   end if
   if (digits = 1)then           ' Take the least significant digit
      valu = valu mod 100
   end if
      d = valu mod 10
       ch = NumtoChar(d)
         ' lcdchar(ch)
    TW_LCD_Write(ch, 1)
end sub
'**********************************************************************
'* LCD Configuration TW_LCD     but it don't work !!!
'**********************************************************************
'sub procedure TW_LCD_Config(dim byref data_port as byte,dim Clk1, Data1 as byte)
'symbol Clk  = data_port.Clk1         ' PORTB6 for the Clock Signal
'symbol Data = data_port.Data1        ' PORTB7 for the Data Signal
'end sub
'**********************************************************************
'* Reset TW_LCD
'**********************************************************************
sub procedure TW_LCD_Reset()
    TW_LCD_Write(%00101000, 0)   ' Switch to 4-Bit mode 2 lines
    TW_LCD_Write(%00010000, 0)   ' Turn Off the Display
    TW_LCD_Write(%00000001, 0)   ' Clear LCD and Home Cursor
     TW_LCD_Write(%00000110, 0)   ' Move Cursor After Each Character
    'LCDWrite(%00010000, 0)      ' Shift Cursor without chasngin DD Reg
    'LCDWrite(%00001111, 0)      ' Turn On LCD and Enable Cursor & Blink
    TW_LCD_Write(%00001100, 0)   ' Turn On LCD and Disable Cursor& Blink
end sub
'************************************************************************
'* Initializes the Two Wire LCD
'************************************************************************
sub procedure TW_LCD_Init()
    delay_ms(20)
      NybbleShift(3, 0)          ' Init LCD
    delay_ms(5)
      NybbleShift(3, 0)          ' Init LCD
    delay_us(160)
       NybbleShift(3, 0)          ' Init LCD
    delay_us(160)
      NybbleShift(2, 0)          ' Set LCD 4 Bit Mode
    delay_us(160)
    TW_LCD_Reset()               ' Call Reset LCD
end sub
'************************************************************************
'* Sends LCD Commands to the TW_LCD Interface  same as MikroE
'************************************************************************
Sub procedure TW_LCD_CMD(dim TW_command as byte)
TW_LCD_Write(TW_command, 0)     ' Send out a TW_ LCD Command
end sub
'***********************************************************************
'* Displays String to the TW LCD
'*
'* When row is 1,  Characters are displayed on LCD Row 1  v 0
'* When row is 2, Characters are displayed on LCD Row 2   v 64
'***********************************************************************
sub procedure TW_LCD_Out(dim row as byte,dim character as byte, dim byref s as string [16])
  dim i as byte
  if row =1 then row = row1
  end if
  if row =2 then row = row2
  end if
    TW_LCD_Write(%10000000 + row + (character-1), 0)      'Move Cursor to the corresponding Row
  i = 0
  while (s[i] <> 0)        'Display the string
  TW_LCD_Write(s[i],1)
  inc(i)
  wend
end sub
end.
Last Updated ( Monday, 14 July 2008 )
 
< Prev   Next >

Main Menu
Home
Forum
Joomla! License
News
Blog
Links
Contact Us
Search
News Feeds
FAQs
Wrapper
Downloads
Shop
Projects
Games
OS Tips & Tricks
Sponsored Links
Joomla! Home
Joomla! Forums
OSM Home
Administrator
© 2010 The Csaba DIY and Electronic Site
Joomla! is Free Software released under the GNU/GPL License.