Search
Enter Keywords:
Thursday, 09 September 2010
Home arrow Projects arrow dsPIC arrow TW LCD Module for MikroBasic dsPIC
Newsflash

PLease check out my DIY CNC mill & drill pictures and PROXXON MF-70 conversion on my music site. I will be transfering information very soon.

 
TW LCD Module for MikroBasic dsPIC Print E-mail
Written by Administrator   
Sunday, 04 May 2008

The following code is the TW LCD Interface code for the Microchip dsPIC Platform .

 

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

 
 
 
 
  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:             dsPIC30F6014A
'     Dev.Board:       EasyPIC4
'     Oscillator:      HS, 10 - 160.000 MHz   fully Tested
'     Ext. Modules:    TW_LCD board, Lcd_2x16
'     SW:              mikroBasic for dsPIC v5.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  = PORTD.6          ' PORTD6 for the Clock Signal
symbol Data = PORTD.7          ' PORTD7 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)
 
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 byte)as char
   dim ch as char
   if (valu < 10) then
    ch=valu + 48                 ' 48 = '0'
   else
    valu=valu - 10
    end if                       ' 65 = A
    ch=valu + 65
    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, 05 May 2008 )
 
< Prev

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.