https://toitlang.org/ logo
PCF8574 and HD44780
# help
e
I'm trying to use an LCD2004 with and PCF8574. Any help would be appreciated. I've thought of using VirtualPins is this the right direction ? import hd44780 show * import gpio import i2c import pcf8574 show * main: sda := gpio.Pin 2 scl := gpio.Pin 15 bus := i2c.Bus --sda=sda --scl=scl --frequency=100_000 device := bus.device 0x27 pcf := PCF8574 device ret := pcf.read // Reads status of all port and returns list. print "PCF read $ret" EN-PIN ::= gpio.VirtualPin :: pcf.set --pin=1 RS-PIN ::= gpio.VirtualPin :: pcf.set --pin=0 D4-PIN ::= gpio.VirtualPin :: pcf.set --pin=4 D5-PIN ::= gpio.VirtualPin :: pcf.set --pin=5 D6-PIN ::= gpio.VirtualPin :: pcf.set --pin=6 D7-PIN ::= gpio.VirtualPin :: pcf.set --pin=7 display := Hd44780 --type = Hd44780.LCD-20x4 --en = EN-PIN --rs = RS-PIN --d4 = D4-PIN --d5 = D5-PIN --d6 = D6-PIN --d7 = D7-PIN // Write text on third line, 6th column. display.write --row=2 --column=5 Hd44780.translate-to-rom-a-00 "โ†’toitโ†" // Special characters requires translate_to_rom_a_00. sleep --ms=2000 https://cdn.discordapp.com/attachments/1249402244747628697/1249402968172793967/rn_image_picker_lib_temp_eebf94e0-2762-49e4-bb0b-b0f3346c84a1.jpg?ex=66672cb0&is=6665db30&hm=a92fedd3f30002e5b70c57724a3d45481dfaa6f32edbe274db47791c67a754a1&
f
I haven't played with the
VirtualPin
yet, but I think your approach should work.
You probably need to change the lambdas though
They don't use the
value
parameter that is given to them. (At least I assume they get one)
Probably something like:
Copy code
EN-PIN gpio.VirtualPin ::
  if it:
    pcf.set --pin=1
  else:
    pcf.clear --pin=1
e
That makes sense. I added that, but still no characters showing. I need to check the pinout maybe.
f
I hope it's the pin out. The code looks otherwise correct
e
There seems to be an issue passing the pcf object to the class Hd44780. When i trigger a a virtual pin outside it works but the methods inside the class do not set or clear the pins.
f
Ok. I will try to have a look at the code a bit later
e
That d be great. I'm operating the PCF8574 with 3.3V and watching the pins output with a logic analyzer. I see the lambdas are being called, but the call doesn't reach the pcf. I looked at the code of the Hd44780 driver, but it looks good to me. Not sure what could be the problem.
f
Can you show the way you do the virtual pins?
e
Copy code
mport .hd44780
import gpio

import i2c
import pcf8574 show *

main:
  sda := gpio.Pin 21
  scl := gpio.Pin 22
  bus := i2c.Bus --sda=sda --scl=scl --frequency=100_000

  device := bus.device 0x27
  pcf ::= PCF8574 device
  devices := bus.scan
  devices.do: print "0x$(%02x it)"
  print pcf.read

  test-pin := gpio.VirtualPin ::
    if it:
      pcf.set --pin=2
    else:
      pcf.clear --pin=2

  test-pin.set 1
  sleep --ms=500

  EN-PIN := gpio.VirtualPin ::
    print "enable $it"
    if it:
      pcf.clear --pin=2
    else:
      pcf.set --pin=2
    sleep --ms=200
  RS-PIN ::= gpio.VirtualPin ::
    print "register $it"
    if it:
      pcf.set --pin=0
    else:
      pcf.clear --pin=0
  D4-PIN ::= gpio.VirtualPin ::
    if it:
      pcf.set --pin=4
    else:
      pcf.clear --pin=4
  D5-PIN ::= gpio.VirtualPin ::
    if it:
      pcf.set --pin=5
    else:
      pcf.clear --pin=5
  D6-PIN ::= gpio.VirtualPin ::
    if it:
      pcf.set --pin=6
    else:
      pcf.clear --pin=6
  D7-PIN ::= gpio.VirtualPin ::
    if it:
      pcf.set --pin=7
    else:
      pcf.clear --pin=7

  display := Hd44780
      --type = Hd44780.LCD-20x4
      --en = EN-PIN
      --rs = RS-PIN
      --d4 = D4-PIN
      --d5 = D5-PIN
      --d6 = D6-PIN
      --d7 = D7-PIN

  // Write text on third line, 6th column.
  display.write --row=2 --column=1  "like a toiger"
  sleep --ms=2000
that is my code right now
f
That's on me: you need to check
if it == 1:
You can also make your code simpler:
Copy code
pcf-pin pcf/PCF8574 n/int -> Lambda:
  return ::
    if it == 1: pcf.set n
    else: PCF.clear n
and then in your code:
Copy code
D4-PIN ::= pcf-pin pcf 4
  D5-PIN :: = ...
e
ok, but i dont think thats it. test-pin works, but the hd44780 driver doesnt seem to be able to trigger any state. i will try though with the other lambda form
f
Can you add a print to the lambda to see if it is called by the hd44780?
Oh. The print is already there
e
scratch that, it seems to work now
maybe
f
The constructor calls the pin immediately.
e
maybe it had something to do with the lambda
it seems to work now, i will try with all pins and share results
f
It shouldn't. But as long as it works now ๐Ÿ™‚
e
Code below works. I'd probably add a level shifter since the LCD I'm working with works with 5V, but surprinsingly it works. I had to invert the trigger in my case also.
Copy code
import gpio
import i2c
import hd44780 show *
import pcf8574 show *

pcf-pin pcf/PCF8574 n/int -> Lambda:
  return ::
    if it == 1: 
      pcf.clear --pin=n
    else: 
      pcf.set --pin=n

main:
  sda := gpio.Pin 21
  scl := gpio.Pin 22
  bus := i2c.Bus --sda=sda --scl=scl --frequency=100_000

  device := bus.device 0x27
  pcf ::= PCF8574 device

  EN-PIN ::= gpio.VirtualPin (pcf-pin pcf 2)
  RS-PIN ::= gpio.VirtualPin (pcf-pin pcf 0)
  D4-PIN ::= gpio.VirtualPin (pcf-pin pcf 4)
  D5-PIN ::= gpio.VirtualPin (pcf-pin pcf 5)
  D6-PIN ::= gpio.VirtualPin (pcf-pin pcf 6)
  D7-PIN ::= gpio.VirtualPin (pcf-pin pcf 7)

  display := Hd44780
      --type = Hd44780.LCD-20x4
      --en = EN-PIN
      --rs = RS-PIN
      --d4 = D4-PIN
      --d5 = D5-PIN
      --d6 = D6-PIN
      --d7 = D7-PIN

  display.write --row=0 --column=1  "Test line"
  sleep --ms=2000
f
Often 3.3V is enough to count as 1 for 5V devices.
You could create the virtual pin inside the pcf-pin function. That's what I intended to do...
e
Yes, that is a lot of duplicate code. I'll refactor this later. Thank you for the help @floitsch
f
Asked chatgpt: looks like .7*VCC is often accepted as 1. So 3.3V is borderline.
e
Good to know. I'm worrying also because I just measured the pins on the ESP and I get 5V there. That could potentially damage the ESP... maybe
The PCF module has pull ups to 5V
f
Strange.
I have read conflicting statements of whether the esp32 supports 5v on its pins.
e
It work right now, not sure for how long though
3 Views