Atmega328P I2C LCD problem - Page 1
Forum section: electronics / general

author: dan3460
author_rank: Frequent Contributor
timestamp: January 08, 2017, 03:40:05 pm
content: Waiting for some chips to come I started writing my first program using Atmel Studio. I used a Arduino Nano that has the same chip to test. I grabbed from the internet a LCD library by David Gironi to setup a 2x16 LCD. Screen worked perfectly on the Arduino. Friday I got the actual chips 328Ps and set them up on the breadboard, using the same 16Mhz clock that the Arduino uses. Loaded the program and everything was slow, changed the fuses for a 8Mhz with no divide by 8. To test I have a LED that blinks every second triggered by an interrupt that shows me that the program is working, and it is blinking at that rate.My problem is that the LCD now misses letters and the positioning randomly is in the wrong place. So probably the problem is generated on the LCD library and some of the problem is that there are instructions beyond my understanding such as this one:Code:static inline void _delayFourCycles(unsigned int __count){if ( __count == 0 )__asm__ __volatile__( "rjmp 1f\n 1:" ); // 2 cycleselse__asm__ __volatile__ ("1: sbiw %0,1" "\n\t""brne 1b" // 4 cycles/loop: "=w" (__count): "0" (__count));}Now, I'm not sure if when I was using the Nano, it was using the external clock, it may have been using the internal clock. At a much slower clock the LCD has enough time to react.Is there another LCD using I2C library somewhere.

author: obiwanjacobi
author_rank: Super Contributor
timestamp: January 09, 2017, 08:11:46 am
content: If your timing is off -because it runs a different clock speed, switching to another library wont make a difference.Anyway, There is a default Arduino library and AdaFruit has an LCD library but I'm not sure they support I2C.This is my take:https://atl.codeplex.com/SourceControl/latest#Source/Code/Arduino Libraries/ATL_HD44780/examples/I2CLCD/I2CLCD.ino

author: dan3460
author_rank: Frequent Contributor
timestamp: January 09, 2017, 12:39:55 pm
content: I'm not programing on Arduino, I used to and I used the LCDI2C libraries they have.What I will do is to make a copy of the project that I have, and start playing with the way that the delays are handled on the library.

author: dan3460
author_rank: Frequent Contributor
timestamp: January 11, 2017, 12:25:06 am
content: In case someone have this same problem, the solution is easy.I pulled my logic analyzer to see what was going on. Connected the I2C channels and the lines going to the LCD. I have attached to pictures of the logic analyzer, the first one is where I saw the glitch. As you can see the D4,D5 and D6 lines changed state while the e signal was up. You can see that more clearly in the closeup.jpg. By looking at the I2C signal, there is no problem for the PC8574 keeping up the conversation with the MCU, but clearly the LCD could not react fast enough to the command and they were been over run. The solution is to slow down a bit the I2C clock, which can be changed at the beginning of the TWIMASTER.CCode:**************************************************************************/#include <inttypes.h>#include <compat/twi.h>#include "i2cmaster.h"/* define CPU frequency in Mhz here if not defined in Makefile */#ifndef F_CPU#define F_CPU 16000000UL#endif/* I2C clock in Hz */#define SCL_CLOCK 15000L <<-------- Line to experiment with./*************************************************************************Initialization of the I2C bus interface. Need to be called only once*************************************************************************/void i2c_init(void)