CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

use i2c question!
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 14, 2006 9:31 pm     Reply with quote

Quote:
while(1)
{
i2c_start();
i2c_write(0xa0)
i2c_write("a")
i2c_stop();
}

1. You don't have an i2c_write() statement for the address of the
byte in the eeprom. You need to add this statement. See the
program below.
2. You need to put the letter in single quotes, like this: i2c_write('a');
See the program below.

Also:
1. Don't use a letter such as 'a' for the data, because it can be confused
with the address of the chip (0xa0). Use some other letter for the data.

See the program below which shows some corrections for your code.
The following program works OK with EX_SLAVE.C:
Code:

#include <16F877.H>
#fuses XT, NOWDT, PROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
   
//====================================
void main()
{
int8 data;

// Write the letter 'B' to the slave board.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_write('B');
i2c_stop();

// Read from the slave board and display the data.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
printf("read %c \n\r", data);
     
while(1);     
}     
WHUNG.JOHN
Guest







TKS U PROGRAMMER
PostPosted: Fri Sep 15, 2006 12:01 pm     Reply with quote

PCM programmer wrote:
Quote:
while(1)
{
i2c_start();
i2c_write(0xa0)
i2c_write("a")
i2c_stop();
}

1. You don't have an i2c_write() statement for the address of the
byte in the eeprom. You need to add this statement. See the
program below.
2. You need to put the letter in single quotes, like this: i2c_write('a');
See the program below.

Also:
1. Don't use a letter such as 'a' for the data, because it can be confused
with the address of the chip (0xa0). Use some other letter for the data.

See the program below which shows some corrections for your code.
The following program works OK with EX_SLAVE.C:
Code:

#include <16F877>
#fuses XT, NOWDT, PROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
   
//====================================
void main()
{
int8 data;

// Write the letter 'B' to the slave board.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_write('B');
i2c_stop();

// Read from the slave board and display the data.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
printf("read %c \n\r", data);
     
while(1);     
}     


****************************************************
TKS u very much pcm progammer ,i learned i2c communication
agreement with u ,my progamm corrections as follws;

master progam:
#include <16F77.h>
#fuses XT, NOWDT, PROTECT, BROWNOUT, PUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3,SLOW)

//====================================
void main()
{

WHILE(1)
{

i2c_start();
i2c_write(0xA0);
DELAY_MS(100);
i2c_write('5');
DELAY_MS(100);
i2c_write('H');
DELAY_MS(500);
i2c_stop();
DELAY_MS(200);

}
}

slave progamm:
include <16F77.h>
#use delay(clock=4000000)
#fuses XT,NOWDT
#include "lcd.c"
#use i2c(SLAVE,address=0XA0,sda=PIN_C4,scl=PIN_C3,slow)


INT8 GG;

void main()
{
int i;
LCD_INIT();

WHILE(1)
{
IF (I2C_POLL()==TRUE)
{
GG=i2c_read();
LCD_GOTOXY(1,1); PRINTF(LCD_PUTC,"\fGG=%C",GG);
}

}

}

the progamm is right ,and lcd panel can display number right,
i think to simplify,do u (pcm progammer) have time teach?
gramercy for pcm progammer .
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Sep 15, 2006 12:27 pm     Reply with quote

Quote:
The progam is right. I think to simplify.

It looks very simple right now. You only have two lines of i2c code in
the slave.

I don't really want to teach i2c. I like to help people solve immediate
problems, so their project can move forward. I don't want to do the
whole project for them.
domdom



Joined: 06 Sep 2006
Posts: 29

View user's profile Send private message

PostPosted: Wed Mar 21, 2007 8:19 pm     Reply with quote

Hi, PCM_programmer,

Does this code work for master 16f877a? I pulled up two SDA and SCL with 4.7K resistor and program the slave chip(16f877a) with EX_SLAVE.c (without any modification)
But i failed to get the communication working. Kindly please give some comment

Code:

#include <16F877>
#fuses HS, NOWDT, PROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
   
//====================================
void main()
{
int8 data;

// Write the letter 'B' to the slave board.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_write('B');
i2c_stop();

// Read from the slave board and display the data.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
printf("read %c \n\r", data);
     
while(1);     


PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 21, 2007 8:49 pm     Reply with quote

It should work with Ex_Slave.c. It's basically similar to the master code
that I posted in this link:
http://www.ccsinfo.com/forum/viewtopic.php?t=28097&start=9

Make sure that you:

1. Have SDA on the Slave connected to SDA on the master.

2. Have SCL on the Slave connected to SCL on the master.

3. Have a ground connection between the two boards.

4. Have one pull-up resistor on SDA, and one on SCL.


If it doesn't work, then post your compiler version.
domdom



Joined: 06 Sep 2006
Posts: 29

View user's profile Send private message

PostPosted: Thu Mar 22, 2007 6:18 am     Reply with quote

Hi PCM programmer,

I program the master.c code in my master PIC , and EX_SLAVE.c in my slave PC.
the output of terminal is like below:

[img]
http://img144.imageshack.us/img144/5611/outputuc3.gif
[/img]

The code seems like not working. May be the baudrate i used?
Can you check again?!
domdom



Joined: 06 Sep 2006
Posts: 29

View user's profile Send private message

PostPosted: Sat Mar 24, 2007 11:43 pm     Reply with quote

Hi PCM programmer,

Did you see the url? what is my problem for my window terminal?

Or may be the code is not working?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 24, 2007 11:59 pm     Reply with quote

You have a problem with sending serial data to Hyperterminal.
Even if the i2c code produces bad output, you should still see
the text in this printf statement:
Code:
printf("read %c \n\r", data);

You should see the word "read" in the Hyperterminal window.
I don't see it.

Try this very simple program, just to test if you can talk to PIC
by typing characters into the HyperTerminal window:
http://www.ccsinfo.com/forum/viewtopic.php?t=29538&start=6

Modify the PIC #include file and #use delay() statement
as required to fit your actual hardware.
You shoul be able to type characters and have the PIC send
them back to HyperTerminal, where they are displayed.
funmix



Joined: 27 Mar 2007
Posts: 33

View user's profile Send private message

PostPosted: Sat Mar 31, 2007 11:06 am     Reply with quote

Hi PCM programmer,

the example you use for master i2c is 16f877a, i changed the 16877A to 18f4520. It doesn't work.

Is there any errata for this chip?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 31, 2007 12:24 pm     Reply with quote

There are many i2c erratas on the A1 silicon of the 18F4520.
http://ww1.microchip.com/downloads/en/DeviceDoc/80209f.pdf

This page says there are three revisions: A1, B2, and B3.
http://www.microchip.com/stellent/idcplgidcplg?IdcService=SS_GET_PAGE&nodeId=1335&dDocName=en010297

To find the silicon revision of your PIC, connect it to an ICD2 and
then select "Connect" from the Programmer menu in MPLAB.
It will display the revision. That's for Microchip tools. The CCS
tools probably also do it.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group