Teensy 3.1 (3.0) with LCD 20x4

Post details of your projects here.
jepalza
Posts: 1
Joined: Mon Jan 02, 2017 8:37 pm

Teensy 3.1 (3.0) with LCD 20x4

Postby jepalza » Mon Jan 02, 2017 8:45 pm

Hello:
First, I feel my bad English, but I am Spanish, and I am using google translate and dictionary

This is my first project in teensy 3.1 (valid in 3.0) to use a 20x4 LCD in 4bits mode. I am using the project of this thread viewtopic.php?f=8&t=739 with tweaks to adapt to the teensy and to 20x4 (the original is 16x2).
The connection scheme comes from an arduino project, not mine.

In Spanish (In case the automatic translation is not well understood)
Este es mi primer proyecto en teensy 3.1 (valido en 3.0) para emplear un LCD de 20x4 en modo 4bits. Estoy usando el proyecto de este hilo ..... con retoques para adecuar al teensy y a 20x4 (el original es de 16x2).
El esquema de conexión proviene de un proyecto de arduino, no es mio.



Code: Select all

#include "MK20DX128.bas" ' teensy 3.1 (3.0)

#define E  OUT(4) ' Operation enable signal. Falling edge triggered.
#define RW OUT(3) ' Read/Write select signal, R/W=1: Read R/W: =0: Write
#define RS OUT(2) ' Register select signal. RS=0: Command, RS=1: Data

#define DB7 OUT(12) ' Four high order bi-directional three state data bus lines.
#define DB6 OUT(11)
#define DB5 OUT(10)
#define DB4 OUT( 9)

SUB WAIT_US(time as integer)
   dim start as integer

   start = TIMER
   while ((TIMER - start) < time)
   loop
END SUB

' 4-bit Initialization:

' send a command to the LCD controller
SUB LCD_CMD(D as integer)
   RS = 0 ' RS=LOW : Command
   RW = 0 ' RW=LOW : Write

   ' Send upper 4 bits
   DB7 = D and &H80
   DB6 = D and &H40
   DB5 = D and &H20
   DB4 = D and &H10
   E = 1 ' enable pulse width >= 300ns
   E = 0 ' Clock enable: falling edge

   ' Send lower 4 bits
   DB7 = D and &H08
   DB6 = D and &H04
   DB5 = D and &H02
   DB4 = D and &H01
   E = 1 ' enable pulse width >= 300ns
   E = 0 ' Clock enable: falling edge
END SUB


' write line 0 or line 1 of the LCD display
SUB WRITE_LINE(line_num as integer, linea(20) as string)
   dim i, j as integer
   
   ' LCD 20x4
   if line_num = 3 then i = &Hd4
   if line_num = 2 then i = &H94
   if line_num = 1 then i = &HC0
   if line_num = 0 then i = &H80

   ' set the position
   LCD_CMD(i)
   WAIT_US(50)

   RS = 1 ' RS=HIGH : Data
   RW = 0 ' RW=LOW : Write

   i = 0
   WHILE (i < 20) and (linea(i) > 0)
      j = linea(i)
      i += 1
      
      ' Send upper 4 bits
      DB7 = j and &H80
      DB6 = j and &H40
      DB5 = j and &H20
      DB4 = j and &H10
      E = 1 ' enable pulse width >= 300ns
      E = 0 ' Clock enable: falling edge

      ' Send lower 4 bits
      DB7 = j and &H08
      DB6 = j and &H04
      DB5 = j and &H02
      DB4 = j and &H01
      E = 1 ' enable pulse width >= 300ns
      E = 0 ' Clock enable: falling edge

      WAIT_US(40) 'wait > 37 us
   LOOP
   
END SUB


' write a character to where ever the current position is
' NOTE: remember to delay 40 us between characters
SUB LCD_DATA(D as integer)
   RS = 1 ' RS=HIGH : Data
   RW = 0 ' RW=LOW : Write

   ' Send upper 4 bits
   DB7 = D and &H80
   DB6 = D and &H40
   DB5 = D and &H20
   DB4 = D and &H10
   E = 1 ' enable pulse width >= 300ns
   E = 0 ' Clock enable: falling edge

   ' Send lower 4 bits
   DB7 = D and &H08
   DB6 = D and &H04
   DB5 = D and &H02
   DB4 = D and &H01
   E = 1 ' enable pulse width >= 300ns
   E = 0 ' Clock enable: falling edge
END SUB


SUB INIT_LCD

   ' TEENSY 3.1 pins    
   OUTPUT(4) ' E
   OUTPUT(3) ' RW
   OUTPUT(2) ' RS
   OUTPUT(12) ' D7
   OUTPUT(11) ' D6
   OUTPUT(10) ' D5
   OUTPUT( 9) ' D4

   E = 0
   RS = 0
   RW = 0

   wait(50) ' Wait > 40 msec after power is applied

   DB7 = 0 ' put 0x30? (o 0x03?) on the output port
   DB6 = 0
   DB5 = 1
   DB4 = 1
   E = 1   ' enable pulse width >= 300ns
   E = 0   ' one set/clr = 958 ns
   wait(5) ' must wait 5ms, busy flag not available

   E = 1
   E = 0
   WAIT_US(200) ' must wait 160us, busy flag not available

   E = 1
   E = 0
   WAIT_US(200) ' must wait 160us, busy flag not available

   E = 1
   E = 0
   WAIT_US(200) ' can check busy flag now instead of delay

   
   LCD_CMD(&H28) ' Function set: 4-bit/2-line
   WAIT_US(40) 'wait > 37 us

   LCD_CMD(&H28) ' Function set: 4-bit/2-line
   wait(1) 'wait > 37 us

   'LCD_CMD(&H0F) '0 0 0 0 1 D C B Display ON/OFF control, display on, cursor on and blinking
   LCD_CMD(&H0C) '0 0 0 0 1 D C B Display ON/OFF control, display on, no cursor
   WAIT_US(40) 'wait > 37 us
      
   LCD_CMD(&H01)'0 0 0 0 0 0 0 1 Display Clear
   wait(2) 'wait > 1.52 ms
   
   LCD_CMD(&H06)'0 0 0 0 0 1 D S Entry Mode Set
   WAIT_US(40) 'wait > 37 us

   LCD_CMD(&H02)'0 0 0 0 0 0 1 0 Return Home
   wait(2) 'wait > 1.52 ms

END SUB
' /**********************************************************/

MAIN:
   dim c, i, line_num as integer
   dim S(20) as string
   
   print "Coridium Wireless LCD Example"
   print "   Enter lines to display"
   
   INIT_LCD
   WRITE_LINE(0,"**CORIDIUM IS COOL**")
   WRITE_LINE(1,"0123456789ABCDEFGHIJ")
   WRITE_LINE(3,"TEENSY 3.1  LCD 20x4")
   
   line_num = 2
   i = 0
   S = ""
   
WHILE(1)

   c = RXD(0) ' read characters

   ' accumulate the string, lines of length greater than 20 will be split
   if c >= &H20
      S(i) = c
      i += 1
   endif

   ' if length is 20 or its a new line and length is greater than 0
   if (i = 20) or (i > 0 and ((c=&HA) or (c=&HD) ))
      print "linea:";line_num;" - "; S
      while i < 20
         S(i) = " " ' pad out with spaces
         i += 1
      loop

      WRITE_LINE(line_num, S)
      line_num+=1
      if line_num=4 then line_num=0
      i = 0
      S = ""
   endif

LOOP

Attachments
teensy-3.0-lcd_bb-303.png
teensy-3.0-lcd_bb-303.png (71 KiB) Viewed 1137 times
P1011853.JPG
P1011853.JPG (137.6 KiB) Viewed 1137 times

olzeke51
Posts: 264
Joined: Sat May 17, 2014 4:22 pm
Location: South Carolina
Contact:

Re: Teensy 3.1 (3.0) with LCD 20x4

Postby olzeke51 » Wed Jan 04, 2017 1:32 pm

Congradulations !! A good way to start the New Year..
Hope to see more.
Gary Olzeke51


Return to “Projects”

Who is online

Users browsing this forum: No registered users and 1 guest