nanukanu
Posts: 9
Joined: Fri Feb 06, 2015 7:38 pm

code issue with calling fuction

Sun Feb 08, 2015 10:31 am

Hi,

This is my second question to the forum. In my last post, i got my 4x4 matrix keypad working correctly to show inputs on screen in one line. Now my next question is how can i post this keypad input on lcd. I have 16x2 lcd which i am able to use and display text i supply thru code. I need to merge these two in one so that i can capture input from keypad and then display it to the lcd. Please help me out. the codes are as below:-

Keypad code:-

Code: Select all

import RPi.GPIO as GPIO

Last edited by nanukanu on Tue Feb 10, 2015 10:38 am, edited 1 time in total.

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: code issue with calling fuction

Sun Feb 08, 2015 2:45 pm

combining your two bits of code but without attempting to test or look for errors etc, you could possibly use as a starter:

Code: Select all

import RPi.GPIO as GPIO
import time
import sys

GPIO.setmode (GPIO.BOARD)

MATRIX = [ [1,4,7,'*'],
           [2,5,8,0],
           [3,6,9,'#'],
           ['A','B','C','D']]

ROW = [31,33,35,37]
COL = [32,36,38,40]

LCD_RS = 27
LCD_E  = 17
LCD_D4 = 22
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 18


LCD_WIDTH = 16
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80
LCD_LINE_2 = 0xC0


E_PULSE = 0.00005
E_DELAY = 0.00005

def lcd_init():
  lcd_byte(0x33,LCD_CMD)
  lcd_byte(0x32,LCD_CMD)
  lcd_byte(0x28,LCD_CMD)
  lcd_byte(0x0C,LCD_CMD)
  lcd_byte(0x06,LCD_CMD)
  lcd_byte(0x01,LCD_CMD)

def lcd_string(message):

  message = message.center(LCD_WIDTH," ")

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def lcd_byte(bits, mode):
  GPIO.output(LCD_RS, mode)

  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4, True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5, True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6, True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7, True)

  time.sleep(E_DELAY)
  GPIO.output(LCD_E, True)
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)
  time.sleep(E_DELAY)

  GPIO.output(LCD_D4, False)
  GPIO.output(LCD_D5, False)
  GPIO.output(LCD_D6, False)
  GPIO.output(LCD_D7, False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4, True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5, True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6, True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7, True)

  time.sleep(E_DELAY)
  GPIO.output(LCD_E, True)
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)
  time.sleep(E_DELAY)


try:
  for j in range(4):
      GPIO.setup(COL[j], GPIO.OUT)
      GPIO.output(COL[j], 1)

  for i in range (4):
      GPIO.setup(ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)


  GPIO.setmode(GPIO.BCM)     
  GPIO.setup(LCD_E, GPIO.OUT) 
  GPIO.setup(LCD_RS, GPIO.OUT)
  GPIO.setup(LCD_D4, GPIO.OUT)
  GPIO.setup(LCD_D5, GPIO.OUT)
  GPIO.setup(LCD_D6, GPIO.OUT)
  GPIO.setup(LCD_D7, GPIO.OUT)

  lcd_init()
  while(True):
    for j in range (4):
      GPIO.output(COL[j],0)

      for i in range(4):
        if GPIO.input (ROW[i]) == 0:
          lcd_byte(LCD_LINE_1, LCD_CMD)
          lcd_string(MATRIX[i][j])
          #print (MATRIX[i][j]),
          #sys.stdout.flush()
          time.sleep(1)
          while (GPIO.input(ROW[i]) == 0):
            pass

      GPIO.output(COL[j],1)
            
except KeyboardInterrupt:
    GPIO.cleanup()
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”