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

HW i2c doesn't work when using 2 16f877. (1 as master, 1 as

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Pei-Yu Huang
Guest







HW i2c doesn't work when using 2 16f877. (1 as master, 1 as
PostPosted: Thu Dec 06, 2001 11:08 pm     Reply with quote

Hello,
I have writen this code to do a master slave i2c comunication between two PIC16F877.

******* Master **********
#include <16F877.H> // device header file from CCS-C
#fuses HS, NOWDT, NOBROWNOUT, NOPROTECT
#use delay(clock=20000000) // 20 MHz crystal oscillator
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use I2C(master, sda=PIN_C4, scl=PIN_C3, SLOW, NOFORCE_SW)


// ----- function prototyping ----------------------
void i2c_master();
void init_i2c();
// ----- function prototyping ----- finished ---------
void i2c_master(){
i2c_start();
i2c_write(0xa0);
i2c_write(0x45);
i2c_write(0x67);
i2c_stop();
delay_ms(20);
}

void init_i2c(){
output_float(PIN_C3);
output_float(PIN_C4);
}

#org 0x0800 // fixed address for main(), written in YZ_ICP
main() {
init_i2c();

enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);

while (TRUE) {
i2c_master();

output_low(PIN_A5);
delay_ms(500);
output_high(PIN_A5);
delay_ms(500);

}
}


************* Slave **************
#include <16F877.H> // device header file from CCS-C
#fuses HS, NOWDT, NOBROWNOUT, NOPROTECT
#use delay(clock=20000000) // 20 MHz crystal oscillator
#use I2C(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa0, SLOW, NOFORCE_SW)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

byte address, data0, data1;

#INT_SSP
void i2c_slave()
{
if(i2c_poll()==TRUE){
address=i2c_read();
data0=i2c_read();
data1=i2c_read();
}
}

#org 0x0800 // fixed address for main(), written in YZ_ICP
main() {

enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while (TRUE) {
output_low(PIN_A5);
delay_ms(125);
output_high(PIN_A5);
delay_ms(125);
}
}

************ end of code **************

Here are some problems:
1. The master blocked when executing line i2c_write(0xa0); .
Therefore, slave didn't get anything from master.
2. So, I tried using software i2c with master instead of using hardware with master. (Slave didn't change.) Here is the code for master by using software i2c. (RC6 as SCL, RC7 as SDA)

*********** Master ***********
#include <16F877.H> // device header file from CCS-C
#fuses HS, NOWDT, NOBROWNOUT, NOPROTECT
#use delay(clock=20000000) // 20 MHz crystal oscillator
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use I2C(master, sda=PIN_B7, scl=PIN_B6)

// ----- function prototyping ------------------------------
void i2c_master();
void init_i2c();
// ----- function prototyping ----- finished -------------
void i2c_master(){
i2c_start();
i2c_write(0xa0);
i2c_write(0x45);
i2c_write(0x67);
i2c_stop();
delay_ms(20);
}

void init_i2c(){
output_float(PIN_B6);
output_float(PIN_B7);
}

#org 0x0800 // fixed address for main(), written in YZ_ICP
main() {
init_i2c();
port_b_pullups(TRUE);

enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);

while (TRUE) {
i2c_master();

output_low(PIN_A5);
delay_ms(500);
output_high(PIN_A5);
delay_ms(500);
}
}

******* end of code ********
Then the slave could get something from master. But all he got was 0xA0. It's seems that he missed 0x45 and 0x67.

I am sure the i/o conncetion is ok. And the pullup resister was added. Would you please help me find out what's wrong with my code? Sorry for taking your time.

IDE Version: 3.2
PCB Version: 3.002
PCM Version: 3.025

Thank you for the support. Best regards.
Pei Yu Huang

PS: Sorry for my poor English.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1496
Renato and Davide
Guest







Re: HW i2c doesn't work when using 2 16f877. (1 as master, 1
PostPosted: Fri Dec 07, 2001 3:55 am     Reply with quote

:=Hello,
:=I have writen this code to do a master slave i2c comunication between two PIC16F877.
:=
:=******* Master **********
:=#include <16F877.H> // device header file from CCS-C
:=#fuses HS, NOWDT, NOBROWNOUT, NOPROTECT
:=#use delay(clock=20000000) // 20 MHz crystal oscillator
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
:=#use I2C(master, sda=PIN_C4, scl=PIN_C3, SLOW, NOFORCE_SW)
:=
:=
:=// ----- function prototyping ----------------------
:=void i2c_master();
:=void init_i2c();
:=// ----- function prototyping ----- finished ---------
:=void i2c_master(){
:=i2c_start();
:=i2c_write(0xa0);
:=i2c_write(0x45);
:=i2c_write(0x67);
:=i2c_stop();
:=delay_ms(20);
:=}
:=
:=void init_i2c(){
:=output_float(PIN_C3);
:=output_float(PIN_C4);
:=}
:=
:=#org 0x0800 // fixed address for main(), written in YZ_ICP
:=main() {
:=init_i2c();
:=
:=enable_interrupts(GLOBAL);
:=enable_interrupts(INT_SSP);
:=
:=while (TRUE) {
:=i2c_master();
:=
:=output_low(PIN_A5);
:=delay_ms(500);
:=output_high(PIN_A5);
:=delay_ms(500);
:=
:=}
:=}
:=
:=
:=************* Slave **************
:=#include <16F877.H> // device header file from CCS-C
:=#fuses HS, NOWDT, NOBROWNOUT, NOPROTECT
:=#use delay(clock=20000000) // 20 MHz crystal oscillator
:=#use I2C(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa0, SLOW, NOFORCE_SW)
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
:=
:=byte address, data0, data1;
:=
:=#INT_SSP
:=void i2c_slave()
:={
:=if(i2c_poll()==TRUE){
:=address=i2c_read();
:=data0=i2c_read();
:=data1=i2c_read();
:=}
:=}
:=
:=#org 0x0800 // fixed address for main(), written in YZ_ICP
:=main() {
:=
:=enable_interrupts(GLOBAL);
:=enable_interrupts(INT_SSP);
:=while (TRUE) {
:=output_low(PIN_A5);
:=delay_ms(125);
:=output_high(PIN_A5);
:=delay_ms(125);
:=}
:=}
:=
:=************ end of code **************
:=
:=Here are some problems:
:=1. The master blocked when executing line i2c_write(0xa0); .
:=Therefore, slave didn't get anything from master.
:=2. So, I tried using software i2c with master instead of using hardware with master. (Slave didn't change.) Here is the code for master by using software i2c. (RC6 as SCL, RC7 as SDA)
:=
:=*********** Master ***********
:=#include <16F877.H> // device header file from CCS-C
:=#fuses HS, NOWDT, NOBROWNOUT, NOPROTECT
:=#use delay(clock=20000000) // 20 MHz crystal oscillator
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
:=#use I2C(master, sda=PIN_B7, scl=PIN_B6)
:=
:=// ----- function prototyping ------------------------------
:=void i2c_master();
:=void init_i2c();
:=// ----- function prototyping ----- finished -------------
:=void i2c_master(){
:=i2c_start();
:=i2c_write(0xa0);
:=i2c_write(0x45);
:=i2c_write(0x67);
:=i2c_stop();
:=delay_ms(20);
:=}
:=
:=void init_i2c(){
:=output_float(PIN_B6);
:=output_float(PIN_B7);
:=}
:=
:=#org 0x0800 // fixed address for main(), written in YZ_ICP
:=main() {
:=init_i2c();
:=port_b_pullups(TRUE);
:=
:=enable_interrupts(GLOBAL);
:=enable_interrupts(INT_SSP);
:=
:=while (TRUE) {
:=i2c_master();
:=
:=output_low(PIN_A5);
:=delay_ms(500);
:=output_high(PIN_A5);
:=delay_ms(500);
:=}
:=}
:=
:=******* end of code ********
:=Then the slave could get something from master. But all he got was 0xA0. It's seems that he missed 0x45 and 0x67.
:=
:=I am sure the i/o conncetion is ok. And the pullup resister was added. Would you please help me find out what's wrong with my code? Sorry for taking your time.
:=
:=IDE Version: 3.2
:=PCB Version: 3.002
:=PCM Version: 3.025
:=
:=Thank you for the support. Best regards.
:=Pei Yu Huang
:=
:=PS: Sorry for my poor English.


I have had the same problem. I had used the wizard. The wizard had put the instruction: setup_spi (FALSE); in the main function of the master and slave. I've Removed the instruction (cancelled!!!) it works!!!.
I hope that this help you.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1497
Pei-Yu Huang
Guest







Re: HW i2c doesn't work when using 2 16f877. (1 as master, 1
PostPosted: Sun Dec 09, 2001 8:07 pm     Reply with quote

:=I have had the same problem. I had used the wizard. The wizard had put the instruction: setup_spi (FALSE); in the main function of the master and slave. I've Removed the instruction (cancelled!!!) it works!!!.
:=I hope that this help you.

Thank you for the suggestion. But I didn't use the setup_spi(FALSE) either in master code or slave code. Do you have any other idea what make the comunication fail? You are so kind. I'd appreciate.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1535
Davide
Guest







Re: HW i2c doesn't work when using 2 16f877. (1 as master, 1
PostPosted: Mon Dec 10, 2001 2:33 am     Reply with quote

:=Thank you for the suggestion. But I didn't use the setup_spi(FALSE) either in master code or slave code. Do you have any other idea what make the comunication fail? You are so kind. I'd appreciate.


Hi, I have noticed that you have mistaken to write the code of the slave.

just this is wrong:

#INT_SSP
void i2c_slave()
{
if(i2c_poll()==TRUE){
address=i2c_read();
data0=i2c_read();
data1=i2c_read();
}
}

Because when happens an interrupt it makes the first instruction only: address=i2c_read().
The first time it is correct (is really the address of slave), but the second time it is already mistaken (the second byte incoming is a data byte, not the slave address), the same thing for the third data, it is a data not the address of the slave.
.
One possible solution is:

byte i=0; //byte == int

#INT_SSP
void i2c_slave()
{
if(i2c_poll()==TRUE)
{
if (i==0) address=i2c_read();
else
{
if (i==1) data0=i2c_read();
else if (i==2) data1=i2c_read();
}
i++;
}
}

Is important assign the incoming data at the right variable.

Another example is EX_SLAVE.c in the EXAMPLE directory in the pic C compiler.

Hello and good job
___________________________
This message was ported from CCS's old forum
Original Post ID: 1536
Pei-Yu Huang
Guest







Re: HW i2c doesn't work when using 2 16f877. (1 as master, 1
PostPosted: Tue Dec 18, 2001 12:25 pm     Reply with quote

:=Hi, I have noticed that you have mistaken to write the code of the slave.
:=
:=just this is wrong:
:=
:=#INT_SSP
:=void i2c_slave()
:={
:=if(i2c_poll()==TRUE){
:=address=i2c_read();
:=data0=i2c_read();
:=data1=i2c_read();
:=}
:=}
:=
:=Because when happens an interrupt it makes the first instruction only: address=i2c_read().
:=The first time it is correct (is really the address of slave), but the second time it is already mistaken (the second byte incoming is a data byte, not the slave address), the same thing for the third data, it is a data not the address of the slave.
:=.
:=One possible solution is:
:=
:=byte i=0; //byte == int
:=
:=#INT_SSP
:=void i2c_slave()
:={
:= if(i2c_poll()==TRUE)
:= {
:= if (i==0) address=i2c_read();
:= else
:= {
:= if (i==1) data0=i2c_read();
:= else if (i==2) data1=i2c_read();
:= }
:= i++;
:= }
:=}
:=
:=Is important assign the incoming data at the right variable.
:=
:=Another example is EX_SLAVE.c in the EXAMPLE directory in the pic C compiler.
:=
:=Hello and good job

Thank you for the answer. Software master and hardware slave are now talking to each other very well. You are a very good teacher. :P
___________________________
This message was ported from CCS's old forum
Original Post ID: 1691
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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