Jump to content
Sign in to follow this  
Andrew

I2C Circuit Help

Recommended Posts

I'm trying to write to a Matrix Orbital LK162-12 LCD screen from a PIC 16F628. This is my first electronics project, so hopefully, I'm just doing something silly.

I first tested the LCD from the computer and that all worked ok through the RS232 serial port. The problem is when I try to communicate from the PIC to the LCD over I2C.

I'm using the PICC Lite compiler and their sample I2C code. SDA is set up on Pin RA0 and SCL is set up on RA1. I'm not sure I am setting up the PULL-UP resistors correctly. I have a 10K resistor between VCC and RA0 and a second 10K resistor between VCC and RA1.

http://www.ping.be/~ping0751/i2cfaq/i2chardw.htm

I have an LED connected to RB0 set up to do blink codes so I know what line of code it failed at. The flashled method blinks the specified number of times with a 1 second pause between cycles. Here's a snippet of my code and the line it's failing at:

//initialize I2C

SCL_DIR = I2C_OUTPUT;

SDA_DIR = I2C_OUTPUT;

SDA = 0;

SCL = 0;

if(i2c_WriteTo(LCD)) flashled(1); //Talk to LCD device

DelayMs(200);

//ClearDisplay

if(i2c_PutByte(0xFE)==I2C_ERROR) flashled(2);

DelayMs(200);

if(i2c_PutByte(0x58)==I2C_ERROR) flashled(3);

DelayMs(200);

if(i2c_PutByte('A')==I2C_ERROR) flashled(4);

DelayMs(200);

if(i2c_PutByte('n')==I2C_ERROR) flashled(5);

DelayMs(200);

if(i2c_PutByte('d')==I2C_ERROR) flashled(6);

DelayMs(200);

if(i2c_PutByte('r')==I2C_ERROR) flashled(7);

DelayMs(200);

if(i2c_PutByte('e')==I2C_ERROR) flashled(8);

DelayMs(200);

if(i2c_PutByte('w')==I2C_ERROR) flashled(9);

DelayMs(200);

Any help would be greatly appreciated.

-Andrew Westberg

Share this post


Link to post
Share on other sites

I have no experience with PICC Lite or the LCD module you are using. However, here are some common issues to observe:

(1) Most LCD controller's init time (after power up or reset) is a bit long. Allow at least one second after startup before communicating with the LCD.

(2) Try explicitly setting the SCL pin to an output. Some compilers expect you to handle the TRIS register on your own.

(3) The first I2C byte that is sent usually involves the device address. I do not see that in your code, but it is hard to tell {could be the i2c_WriteTo(LCD) code}. Be sure that your device address is valid.

(4) I looked at the PICC docs and could not find anything on the I2C functions that you are using. So, I can't comment on them at all. You might want to see if they have a I2C_Start() function or something similar (used to init the I2C port). Also, an I2C_Stop() is usually called at the end of the communication.

Like this:

clear_screen()

{

i2c_start();

i2c_write(DEVICE_ADDR);

i2c_write(0xfe);

i2c_write(0x58);

i2c_stop();

delayms(5);

return;

}

This is not tested code. But it represents the sort of calls that are usually seen when talking to a I2C LCD device. Beyond this I will not be of much help. Good luck.

Share this post


Link to post
Share on other sites

CCS has a really good support forum for their product, Pic C. If you haven't tried asking there, I'd suggest putting a post as there's a lot of knowledge available to help. I've noticed that depending on compiler versions, they have a lot of people reporting bugs. Believe me, I've run into some of them!

http://www.ccsinfo.com/forum/

Michael

Share this post


Link to post
Share on other sites
(1) Most LCD controller's init time (after power up or reset) is a bit long. Allow at least one second after startup before communicating with the LCD.

I have a delay of 5 seconds in my code before any of the lines I posted are run.

(2) Try explicitly setting the SCL pin to an output. Some compilers expect you to handle the TRIS register on your own.

I'll try this suggestion and let you know what happens.

(3) The first I2C byte that is sent usually involves the device address. I do not see that in your code, but it is hard to tell {could be the i2c_WriteTo(LCD) code}. Be sure that your device address is valid.

I failed to show the line of code where LCD is defined along witht he 5 second delay in my above post. the LCD is defined as 0x50 which is the I2C address of the device.

(4) I looked at the PICC docs and could not find anything on the I2C functions that you are using. So, I can't comment on them at all. You might want to see if they have a I2C_Start() function or something similar (used to init the I2C port). Also, an I2C_Stop() is usually called at the end of the communication.

The code I'm using is located in the samples folder after installing PICC Lite. I'm attaching the documentation they included with the sample code. I have customized their example code to use the pins I wanted it to.

I'll look through the sample code to make sure it's doing a start and stop for me. If not, I may try modifying my code to do this explicitly. Thanks for all your good feedback.

i2c.txt

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×