STM32F407Disc : Write data on LCD

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

STM32F407Disc : Write data on LCD

Post by nikhiledutech » Fri Dec 29, 2017 10:38 am

Hello,
So i am currently interfacing STM32f4 with LCD(16*2) display. So have already tried few code available on github but its not working. So i have written few functions to write character on LCD display. But problem lies in the fact that output is not getting displayed on LCD.
below i have given my code.

"""
import pyb
from pyb import Pin, delay, millis
import time

#LCD Commands
LCD_CLEAR = 0x01 # DB0: clear display
LCD_HOME = 0x02 # DB1: return to home position

LCD_OFF = 0X08
LCD_ON = 0X0C
CURSOR_ON = 0X0E
CURSOR_OFF = 0X0C
CURSOR_BLINK = 0X0F

LCD_SHIFT_LEFT = 0x18
LCD_SHIFT_RIGHT = 0x1C
CURSOR_SHIFT_LEFT = 0X10
CURSOR_SHIFT_RIGHT = 0X14

LCD_4BIT_1LINE = 0x20 #4-bit interface, 1 line, 5*7 Pixels
LCD_4BIT_2LINE = 0x28 #4-bit interface, 2 lines, 5*7 Pixels
LCD_8BIT_1LINE = 0X30 #8-bit interface, 1 line, 5*7 Pixels
LCD_8BIT_2LINE = 0X38 #8-bit interface, 2 lines, 5*7 Pixels




#LCD_PIN_DECLARATION
rs = pyb.Pin('PB1', Pin.OUT_PP, Pin.PULL_UP)
rw = pyb.Pin('PB4', Pin.OUT_PP, Pin.PULL_UP)
en = pyb.Pin('PB5', Pin.OUT_PP, Pin.PULL_UP)
d0 = pyb.Pin('PE8', Pin.OUT_PP, Pin.PULL_UP)
d1 = pyb.Pin('PE9', Pin.OUT_PP, Pin.PULL_NONE)
d2 = pyb.Pin('PE10', Pin.OUT_PP, Pin.PULL_NONE)
d3 = pyb.Pin('PE11', Pin.OUT_PP, Pin.PULL_NONE)
d4 = pyb.Pin('PE12', Pin.OUT_PP, Pin.PULL_NONE)
d5 = pyb.Pin('PE13', Pin.OUT_PP, Pin.PULL_NONE)
d6 = pyb.Pin('PE14', Pin.OUT_PP, Pin.PULL_NONE)
d7 = pyb.Pin('PE15', Pin.OUT_PP, Pin.PULL_NONE)



# ASK25_LCD_INIT
def ASK25_LCD_Init(): # ASK25_LCD_INIT

rs.low() #Clear RS RW EN
rw.low()
en.low()
pyb.udelay(15) #15us

ASK25_LCD_WRITE_COMMAND(LCD_CLEAR)
delay(2)
ASK25_LCD_WRITE_COMMAND(LCD_CLEAR)
delay(4)

ASK25_LCD_WRITE_COMMAND(LCD_8BIT_2LINE)
delay(1)

ASK25_LCD_WRITE_COMMAND(LCD_HOME)
pyb.udelay(50)

ASK25_LCD_WRITE_COMMAND(LCD_OFF)
ASK25_LCD_WRITE_COMMAND(LCD_ON)
ASK25_LCD_WRITE_COMMAND(CURSOR_ON)
delay(50)




def LCD_Clear_Values():
d0 = 0
d1 = 0
d2 = 0
d3 = 0
d4 = 0
d5 = 0
d6 = 0
d7 = 0

def hal_write_8bits(command):
# """Writes 8 bits of data to the LCD."""
rw.low()
d0.value(command & 0x01)
d1.value(command & 0x02)
d2.value(command & 0x04)
d3.value(command & 0x08)
d4.value(command & 0x01)
d5.value(command & 0x02)
d6.value(command & 0x04)
d7.value(command & 0x08)



def ASK25_LCD_Enable():
#"""Pulse the enable line high, and then low again."""
en.low()
pyb.udelay(1)
en.high()
pyb.udelay(1) # Enable pulse needs to be > 450 nsec
en.low()
pyb.udelay(100) # Commands need > 37us to settle


# ASK25_LCD_Write_COMMAND
def ASK25_LCD_WRITE_COMMAND(command): # ASK25_LCD_Write_COMMAND
Lcd_Data = command
rs.low() #Clear rs
rw.low() #Clear rw
LCD_Clear_Values() #Clear Old Value
hal_write_8bits(command)
ASK25_LCD_Enable()



# ASK25_LCD_Write_DATA
def ASK25_LCD_Write_Data(character): # ASK25_LCD_Write_DATA
Lcd_Data = character

ASK25_LCD_WRITE_COMMAND(CURSOR_OFF)
rs.high() #Set RS
rw.low() #Set RW
LCD_Clear_Values() #Clear Old Values
hal_write_8bits(character)
ASK25_LCD_Enable()




#MAIN PROGRAM
ASK25_LCD_Init()
ASK25_LCD_Write_Data(0x32)


"""
So can anyone help me to improve with this code...

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: STM32F407Disc : Write data on LCD

Post by pythoncoder » Fri Dec 29, 2017 5:25 pm

There are at least two MicroPython drivers for Hitachi HD44780 LCD's. This one from Dave Hylands. There is also an asynchronous one I wrote which may be found here.
Peter Hinch
Index to my micropython libraries.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: STM32F407Disc : Write data on LCD

Post by nikhiledutech » Mon Jan 01, 2018 6:45 am

Okay. While using your asynchronous mode code, their is an import error at time of importing uasyncio module i.e "" import uasyncio as asyncio ""
I have downloaded your project, and searched for the module "uasyncio" its not appearing. So how to get this module or any other alternative ?

Thanks

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: STM32F407Disc : Write data on LCD

Post by pythoncoder » Mon Jan 01, 2018 8:45 am

There are some instructions here. The same principle applies to installing any of the standard MicroPython libraries.
Peter Hinch
Index to my micropython libraries.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: STM32F407Disc : Write data on LCD

Post by nikhiledutech » Mon Jan 01, 2018 8:53 am

Okay, i read ReadMe file and downloaded the uasyncio (asynchronous lib.) .Now i am facing a new problem.
As shown in the alcd test example, i have imported all the modules given below.
import uasyncio as asyncio
import utime as time
from alcd import LCD, PINLIST
Now, whenever i write next instruction " lcd = LCD(PINLIST,cols=16)" , it gives me this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "alcd.py", line 81, in __init__
AttributeError: 'module' object has no attribute 'get_event_loop'
"""
So its showing module object has no attribute 'get_event_loop'. So what should i do now ?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: STM32F407Disc : Write data on LCD

Post by pythoncoder » Mon Jan 01, 2018 9:09 am

Something has gone wrong with the installation of uasyncio. This is what I see on a Pyboard:

Code: Select all

>>> import uasyncio
>>> dir(uasyncio)
['_socket', 'StopLoop', 'EventLoop', 'start_server', 'uasyncio', 'IORead', 'sleep', 'IOWriteDone', 'ensure_future', 'wait_for', 'set_debug', 'sleep_ms', 'IOReadDone', 'SleepMs', 'core', 'coroutine', 'TimeoutObj', 'type_gen', 'SysCall', 'utimeq', 'select', 'DEBUG', '__name__', 'wait_for_ms', '__path__', 'TimeoutError', 'SysCall1', 'StreamReader', 'IOWrite', 'log', 'get_event_loop', 'time', 'Task', 'PollEventLoop', 'StreamWriter', 'open_connection', 'uerrno']
>>> 'get_event_loop' in dir(uasyncio)
True
Later today I'll test the uasyncio installation procedure on a Pyboard running the standard firmware.
Peter Hinch
Index to my micropython libraries.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: STM32F407Disc : Write data on LCD

Post by pythoncoder » Mon Jan 01, 2018 10:29 am

OK, I've tried this on a Pyboard V1.0. I flashed today's standard firmware and followed the installation instructions in the uasyncio tutorial, copying the directories and their contents to /flash. This was successful.

So I'm foxed as to your problem. Alas I haven't got an STM32F407Disc to exactly replicate your installation.
Peter Hinch
Index to my micropython libraries.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: STM32F407Disc : Write data on LCD

Post by nikhiledutech » Mon Jan 01, 2018 10:35 am

Okay. i get a FALSE value.
import uasyncio as asyncio
>>> dir (asyncio)
['inspect', 'asyncio', '__name__', '__file__']
Whenever i followed this link https://github.com/peterhinch/micropyth ... UTORIAL.md to install uasyncio .
But in this command micropython -m upip install -p ~/syn micropython-uasyncio i get error of micropython command not found.
edutech@edutech-desktop:~/nikhil/upython_stm/micropython/syn$ micropython -m upip install -p ~/syn micropython-uasyncio
micropython: command not found
I tried to install with the help of pip utility. And here is the output which i get in the terminal.
edutech@edutech-desktop:~/nikhil/upython_stm/micropython/syn$ pip install micropython-uasyncio
Collecting micropython-uasyncio
Downloading micropython-uasyncio-1.4.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-0W1VQO/micropython-uasyncio/setup.py", line 7, in <module>
import optimize_upip
ImportError: No module named optimize_upip

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-0W1VQO/micropython-uasyncio/
I am currently using Ubuntu 14.04. So how to install this without error ? I tried to follow document, but their its not mentioned properly how to install it. A single command is given with creating a folder ~/sys.
So can you help me in installing uasyncio either with upip or pip utility.

Thanks :)
Last edited by nikhiledutech on Tue Jan 02, 2018 9:26 am, edited 2 times in total.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: STM32F407Disc : Write data on LCD

Post by nikhiledutech » Tue Jan 02, 2018 4:59 am

I think this is the same problem which you too faced while py board.
Your issue link https://forum.micropython.org/viewtopi ... 4#p16284

And more thing i want to bring in notice is that, I am booting from flash not SDcard.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: STM32F407Disc : Write data on LCD

Post by nikhiledutech » Tue Jan 02, 2018 10:51 am

Hiii Sir,

I manually created a sub-folder named "uasyncio" in flash drive and pasted init.py , core.py, queues.py, synchro.py in that sub folder.

So now when i use your lcd example, its working :) Thankyou sir for help.

But do let me know, I did right step by creating sub folder in flash and pasting necessary files (i.e __init__,core,queues,synchro) OR is their any easier way ?

And as your program is for 4-bit mode, can i implement 8-bit mode by making necessary changes ? And do you have any idea why my synchronous LCD program didn't worked?

Once again THANKYOU :)

Post Reply