3C : LCD I2C

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 268
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

3C : LCD I2C

Post by Dan81 »

Hello

I'm still using "Customize Component Code" (only for Flowcode v4)

I'll post the code soon.

There'll be all the usual "Component Macros" with an I2C management.

I use a classic LCD with a GPIO (from Microchip) : MCP23008.

The only rule is to respect the schematic below.
lcd_mcp.jpg
lcd_mcp.jpg (28.11 KiB) Viewed 44584 times
Daniel

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: 3C : LCD I2C

Post by Benj »

Hi Dan,

Great idea for an article. We actually use the SPI version of this output expander on the EB057 and EB058 gLCD to allow for the LCD pins to be controlled from a single E-blocks port. The SPI version is pretty much identical but because of the overhead for I2C communications the SPI version will run a little faster. With a slow device like the LCD the I2C method shouldn't experience any slowdown.

Many thanks.

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 268
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

Re: 3C : LCD I2C

Post by Dan81 »

How to use I2C LCD.

INTRODUCTION

I2C bus is very interesting cause it needs only 2 lines of the micro-controller.
You can find lots of components (RTC, thermometer, Led dimmer, …).

The bus frequency is 100kHz, writing a register will take about 300us.
It could be a bit slow for some components, but for some others it is quick enough.

I was working with an RTC (DS1307) and needed to display the time.
Using a classic LCD will waste 6 (or 10) pins of my Pic and a real I2C LCD was expensive enough

Then I decided to use an I2C GPIO (MCP 23008) from Microchip.

For me, the code customization with version 4 of Flowcode is very interesting cause all the code stays with your Flowchart.

On top of that, I wanted to use this new component identical to Matrix_Multimedia (with simulation).

End of chat.


TECHNICAL

For hardware the only rule is to respect the schematic given above ( close to the one given by Matrix M in the LCD help)

For the Software,
Choose your LCD as usual (2x16 , 4x20,…), the macros are identical (and simulable).

The supplementary thing is to add a I2C component and to add the macro I2C "Init" BEFORE the LCD init.
Note : I2C is only "Hardware" , the software mode is not supported (it could be).

Note2 : It is not necessary to provide a port for connection, if you leave "unconnected", when you compile, you will get 2 warnings which tell you "some components are not connected" but everything will go well.



Remarks :
- I have done the same thing with a PCF8574A and a real I2C LCD (BATRON BTHQ21605AV-YETF-LED04-I2C from Farnell , a little detail : the char set is not standard)
- You can do the same thing with the keyboard.

Daniel

PS : thanks for the feedback
Attachments
I2C_LCD_mcp.fcc
(9.6 KiB) Downloaded 1594 times
I2C_LCD_mcp.fcf
(28.62 KiB) Downloaded 1883 times

ncc1502
Posts: 48
Joined: Mon Apr 07, 2008 9:18 pm
Has thanked: 23 times
Been thanked: 8 times
Contact:

Re: 3C : LCD I2C

Post by ncc1502 »

Has anybody done the same work for the LCD on an SPI port expander?
Better safe then sorry

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 268
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

Re: 3C : LCD I2C

Post by Dan81 »

Hello Bercioiu

The same flowchart as above but with a PCF8574A.

Daniel
Attachments
LCD-I2C-PCF8574a.fcf
only tested with Proteus7.10
(not hard tested)
(29.97 KiB) Downloaded 1483 times

bercioiu
Posts: 161
Joined: Sun Feb 25, 2007 10:56 am
Location: bucharest, romania
Has thanked: 90 times
Been thanked: 34 times
Contact:

Re: 3C : LCD I2C

Post by bercioiu »

Thank you for the reply!
In the custom component I see a new macro "OUT7" how can I use that? It is for P7 on the pcf? If yes, how can I use that in the flowchart? And one more question: I see that PCF is a little different from MCP , in the PCF I didn`t see the reset pin, so, what modification have to make to your schematic if I want to use PCF instead MCP?
Thank you again!

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 268
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

Re: 3C : LCD I2C

Post by Dan81 »

Hello Bercioiu

It is (quite) the same schematic.
Image3.jpg
Image3.jpg (18.61 KiB) Viewed 42621 times
The macro "OUT7" was a project , it is empty (sorry).

Daniel

bercioiu
Posts: 161
Joined: Sun Feb 25, 2007 10:56 am
Location: bucharest, romania
Has thanked: 90 times
Been thanked: 34 times
Contact:

Re: 3C : LCD I2C

Post by bercioiu »

Hi DAN!
First, thanks for your help so far!
I cant compile your file with LCD for PCF 8574 (LCD-I2C-PCF8574a-1.fcf), when I compile it return me the following errors:

Code: Select all

LCD-I2C-PCF8574a-1.c(145): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(146): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(147): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(148): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(149): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(150): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(151): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(152): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(153): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(154): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(155): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(156): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(157): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(158): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(159): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(160): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(161): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(162): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(163): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(164): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(168): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(169): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(177): Illegal symbol name: %
LCD-I2C-PCF8574a-1.c(224): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(225): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(226): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(227): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(228): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(229): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(230): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(231): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(233): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(234): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(235): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(236): Illegal symbol name: 3
LCD-I2C-PCF8574a-1.c(237): Illegal symbol name: 3

36 errors detected
Error: preprocessing error

failure
What can I do to compile it?
Thanks!

L.E. I forgot to mention that I use Flowcode 5 with all updates (5.5.2.1).

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 268
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

Re: 3C : LCD I2C

Post by Dan81 »

Hello Bercioiu

This custom code is for Flowcode 4.

Daniel

bercioiu
Posts: 161
Joined: Sun Feb 25, 2007 10:56 am
Location: bucharest, romania
Has thanked: 90 times
Been thanked: 34 times
Contact:

Re: 3C : LCD I2C

Post by bercioiu »

There is a way to modify this custom code for Flowcode V5 ? What is the different between V4 and V5?
Thank you anyway!

Mouine
Posts: 10
Joined: Sun May 13, 2012 1:13 pm
Location: France
Contact:

Re: 3C : LCD I2C

Post by Mouine »

Hello Dan81,

I try to complie the code with flowcode V5 but it not work, do you have one for it ?
I try it with flowcode V4 and i it work perfectly.

Thanks

MilekNS
Posts: 8
Joined: Tue Mar 11, 2014 11:09 pm
Has thanked: 1 time
Contact:

Re: 3C : LCD I2C

Post by MilekNS »

Dan81 wrote:Hello Bercioiu

The same flowchart as above but with a PCF8574A.

Daniel

Hello Dan. I'm trying to control i2c LCD with PCF8574 but on Atmel AVR (atmega168) MCU.
I managed to open LCD-I2C-PCF8574a.fcf in Flowcode AVR 5.3 to see basic commands. I realized the main substance of "code" but there is one thing that I don't understand at all.
Physical Connection of LCD ?! That LCD (your is 20x4 and mine is 16x2, anyway standard) should be connected to I2C pins of MCU, ( in my case those are 27-SDA and 28-SCL), Vcc and GND, 4 wire total, and as far I understand those should be the only 4 pins used for LCD. I have 4 pin IDC connector for i2c with those 4.
But when I opened LCD-I2C-PCF8574a.fcf under LCD connections I saw that almost entire PORT B is used ?!
From PB0-PB3 for data bits of lcd and PB4-RS and PB5-EN.
During Simulation I see on "view chip" that states of those pins are being changed from LOW to HI, randomly.
Question is What to do with those ?? I need those pins of PORT B for something else, those are my "inputs on plc board" and I can't allow their state to change without any signal on real inputs, I need those pins completely free from lcd controll .
If I "remove" those connection of LCD to Port B in Flowcode I get warnings and error during compile? :|
I have not made ​​any real hardware for LCD yet, until i get anything logical in simulation , main MCU board is finished and tested for everything else except I2C communication to others "slaves"
// sorry bcs my probably bad english

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 268
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

Re: 3C : LCD I2C

Post by Dan81 »

Hello Milek

Customisation was made for Flowcode v4 (different with Flowcode v5).
Connections to portB are not really made (it is a I2C bus) .
You can move connections to "unconnected" and the compilation will be OK with Flowcode4.

If you can manage a LCD with C code, it is enough easy to manage it through a PCF (but a bit long !) .

Moving the code to Flowcode5 is not useful because Flowcode6 is the current version.

Sorry

Daniel

MilekNS
Posts: 8
Joined: Tue Mar 11, 2014 11:09 pm
Has thanked: 1 time
Contact:

Re: 3C : LCD I2C

Post by MilekNS »

Hi Dan.

So does it mean that if I open FC, chose my MCU target (mega168) and make exactly same Flowcode diagram like yours, completely from scratch but in FC V5.3 it will not work because such a diagram is intended for V4 and same one goes bad in v5? I did that already and simulation works ok, LCD writes all those stuff from diagram, numbers, letters, characters, it compiles to C (with warning about LCD unconnected ) but not in .hex
I don't understand? Is it not the same in recent versions, that for example some block like "calculation", or "decision" are different in V4 and V5? Problem for me is that I don't understand #C, that's why I'm using FC ( also pretty hard to get around with it ) to program something "simple" by drawing diagrams.

Is there any other simple solution how to make my i2c LCD to work in V5, but w/o moving to V6 ? :roll:
If there is a new version of some SW (like in this case FC-V6) than the previous one should not just get obsolete easy like that? Ok, I know, it's "easy" to work in FC for some simple task, but when there is a problem like "how to control i2c LCD" there is no any specific tutorial about that, help file is unusable for that. There no any webpage here, or tutorial where I can simply see do this, this and this and it will work? thanks and sorry to bothering you.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: 3C : LCD I2C

Post by Benj »

Hello,

If you look in the I2C component help file for v5 then there is information about how to use the I2C bus and how to read / write data.

There should also be a page on Wikipedia regarding I2C which should provide some insight on the protocol.

I2C is a fairly simple protocol so once you get the basics it should be possible to talk to just about any I2C device using the same principals.

I hope you can appreciate that we cannot write specific tutorials for all of the millions of different devices out there. If you want to document how you get on then this would be great and would potentially help out other users in a similar situation.

MilekNS
Posts: 8
Joined: Tue Mar 11, 2014 11:09 pm
Has thanked: 1 time
Contact:

Re: 3C : LCD I2C

Post by MilekNS »

hello Benj

I2C component help file was first thing to look. But...for someone who works as anesthesiologist in hospital it doesn't help me a lot... :D electronics is something that interests me a side, and i don't have that much knowledge. For me it was a great success even to make PCB! :lol: I have build some mcu projects before but only those where I found finished .hex and I was interested to learn how to program something that I could use around my house, in garage, just anything, C was the problem, nooo way I could learn that easy, but making something by "drawing diagram" sounds as great solution!
I understand that i2c use two wires for communication, an that's practically all.
"
Sending Data
Output a start condition //Starts the device listening to the I²C bus.
Transmit the External Device address with the LSB cleared. //Enables the device and sets to write mode.
Transmit the Internal address. //Sets the address to start writing data to.
Transmit a byte of data. //Device will acknowledge and increment internal address until page limit is reached
....
Transmit a byte of data. //Device will acknowledge and increment internal address until page limit is reached
Output a stop condition //Stops the device listening to the I²C bus.
Alternatively use the Send_Byte_Transaction macro.


This part of help file doesn't tell me anything, i see it's "example" for some memory chip but any way i don't know what to do with that, what blocks in diagram i have to use, where to put those and how to set all features.

I made one diagram based on Dan81 example.
Result: Simulation-ok , LCD ( in simulation ) show what i wanted to see, simple word "text" and then characters behind. Compile to C-OK, compile to .hex-OK Problem: as in my first comment i saw that states of pins on POrtB are being changed (view chip) , Dan said "Connections to portB are not really made" but i just sent hex to mcu and because i have some LED's attached to PortB i see they are blinking, ( I didn't made i2c board with that 8574 chip until i get something that could work ) so connections to portB are really made. If disconnect LCD from portB (in flowcode), simulation still work but with warning about unconnected device, compile to C-ok, compile to .hex doesn't work.
I'm really desperate about this. I understand it should be probably easy but for someone who understands the issues and I'm not in this story. :roll:

I wanted to attach that diagram files but it's on my home pc but i saved some *screen shoots* on my flash drive,
These days I'm on duty in hospital so I will not be able to deal with fc until I get back to home town.
sorry because long text, sorry because probably "tips&tricks" is wrong places to write all this. :roll:
Attachments
I put together three screen shoots into one image
I put together three screen shoots into one image
1slika.jpg (401.39 KiB) Viewed 40617 times

MilekNS
Posts: 8
Joined: Tue Mar 11, 2014 11:09 pm
Has thanked: 1 time
Contact:

Re: 3C : LCD I2C

Post by MilekNS »

Still nothing. :cry:

Should I open new thread?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: 3C : LCD I2C

Post by Benj »

Hello Mike,

Sorry for the delay, could you attach your program so we can have a look at the compilation errors? What version of Flowcode are you using?

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 268
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

Re: 3C : LCD I2C

Post by Dan81 »

Hello Milek

I've modified the flowchart for AVR and Flowcode5.
It seems to be OK in simulation , but I can't test it in real .

You will have to modify the chip configuration and quartz frequency.

Daniel
Attachments
I2C_LCD_pcf_FC5_Avr.fcf_avr
not Hard tested
(30.8 KiB) Downloaded 1052 times

MilekNS
Posts: 8
Joined: Tue Mar 11, 2014 11:09 pm
Has thanked: 1 time
Contact:

Re: 3C : LCD I2C

Post by MilekNS »

hello Dan81.

It was some time ago since I visited this forum (and used FC at all) last time and i was happy to see the answer :)

I have tried simulation and build hex, both works now, on pc and hex is generated, but my problem also is hardware check, the wiring of my pcb it's not same as matrix multimedia lcd and little schematic you have posted earlier in this thread.

address 0x20=Ok, connections for data bus d4-d7 lines=OK, RS pin of lcd=OK, but my RW is connected to p5 and EN of lcd to p6, backlight to P7 (npn transistor)

i just need information where in custom code to find that part where i can change order of connection between PCF and LCD because my pcf8574 is smd chip and i have no any other way to make changes except in code. cutting my pcb traces and soldering wires arround doesn't look as option.

chip configuration and quartz frequency , no problem ,they are already same :)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

EDIT: TODAY i took my pcb and looked around , changes on board were minimal so I have decided to make them.
now connections are same,

PCF-LCD
-----------
PO--D4
P1 --D5
P2 --D6
P3 --D7
P4 --RS
P5 --En
backlight is ON because it's connected to Vcc and gnd.
a2,a1,a2 are all tied to GND so address is 0x20, everything ( I think) as it should be but something is not OK. on my LCD just after power up first row of 16 is filled like in picture attached (it's not my picture, i have just find it on internet because it's same issue as mine)
Attachments
kvadratici LCD.jpg
(50.62 KiB) Downloaded 28187 times

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 268
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

Re: 3C : LCD I2C

Post by Dan81 »

Hello Milek

Have you put pull up resistors on clock and data lines ?

Which hardware are you using ?

Daniel

KevinH
Posts: 19
Joined: Sun Mar 18, 2012 9:00 pm
Has thanked: 8 times
Been thanked: 6 times
Contact:

Re: 3C : LCD I2C

Post by KevinH »

Check the requirements of the contrast pin3.
For other LCD's I have used the wiring pin3 direct to the supply pins will often not work.

Place a 10k preset between +5 and GND and take the wiper to pin3. As you adjust the setting it should go from all pixel black to a blank display, and somewhere in between the characters 'may' burst into view.

MilekNS
Posts: 8
Joined: Tue Mar 11, 2014 11:09 pm
Has thanked: 1 time
Contact:

Re: 3C : LCD I2C

Post by MilekNS »

Dan81 wrote:Hello Milek

Have you put pull up resistors on clock and data lines ?

Which hardware are you using ?

Daniel
Yeap, there are pull-up on scl and sda but 3,9k, i hope it's fine bcs i don't have higher values at this moment but there are still pull-up's, maybe i could add to them one more in series to get 7k8 for both data lines?

I'm using my own design, a board with ATmega168 ( internal 8mhz). Everything else (mostly very simple stuff) i have tried on that board is working except i don't know how to use i2c :) Dan Your code work in simulation and it's ok 99% but connections is probably different than what I need. Also I was wondering is there any chance to "kill" pcf8574 by sending some wrong commands during all these tests ?!but anyway I did make those changes for RW and En pins of LCD but it didn't worked...

I was also looking for some finished hex code for atmega 168 (or atmega8) just to burn it to mcu and check is my pcf8574 board is ok at all and i have found this http://mmdolze.users.sourceforge.net/us ... h-twi.html it has same wiring except back light where mine is npn transistor and not pnp BUT there is no hex included and i can't get it no chance. :oops:
KevinH wrote:Check the requirements of the contrast pin3.
For other LCD's I have used the wiring pin3 direct to the supply pins will often not work.

Place a 10k preset between +5 and GND and take the wiper to pin3. As you adjust the setting it should go from all pixel black to a blank display, and somewhere in between the characters 'may' burst into view.
I thought of it so that part is 100% ok, i have some other board with tiny2313 programmed just to scroll text around on LCD (standard 4b parallel connection) and contrast on pin3 is adjusted to be ok. Then I switch my lcd to PCB With 8574 and get those square like from picture above but they are visible because i set same value of POT on both boards.

KevinH
Posts: 19
Joined: Sun Mar 18, 2012 9:00 pm
Has thanked: 8 times
Been thanked: 6 times
Contact:

Re: 3C : LCD I2C

Post by KevinH »

First I'm very new to Flowcode so am using this as part of my learning how programs are constructed, so I'm of little help here! BUT, I now recall having a similar problem with an LCD many years ago (Using STE bus and Arcom Basic) which I eventually located as the LCD's power up/ reset time. The issue was the LCD was taking greater than 1 second to initialise, so was ignoring my my set-up codes during this time.

Try an initial delay of say 2 seconds and see if this helps, if it does gradually reduce it until it faults and choose a reliable delay greater than this.

The other delay that others have reported as an issue is the pulse on the 'E' line which seems to require some 10mS, however I can't recall having this issue, but did 'play' with many delay values to get things right.

The simulation would not find this type of issue, so there is hope.....

MilekNS
Posts: 8
Joined: Tue Mar 11, 2014 11:09 pm
Has thanked: 1 time
Contact:

Re: 3C : LCD I2C

Post by MilekNS »

Dan81, I was just looking your last FC diagram and under LCD component/ custom code / component macros

Defines, RawSend (there is one more RawSend below ?), Start, Clear, Enable_pulse and Backlight are true (customized) and further under:

Start /Edit code I see there are lines of Wdt_delay_ms() ?

As i remember My WDT fuse bit is not ON :roll: i need WDT turned on to get this (maybe) working, right ?
I would try that right now but my isp programmer is at my home, not close, so i just asking at least to know is that possible issue?

Post Reply