|
|
View previous topic :: View next topic |
Author |
Message |
ArMo
Joined: 26 Dec 2007 Posts: 3
|
i2c between PIC's 16F74 |
Posted: Wed Dec 26, 2007 2:17 pm |
|
|
Hi,
I'm have some problem with my communicaton i2c between PIC16F74's...
I try to write one simple code for write/read between pic's, but my code don't read nothing.
Can someone show me where I have gone wrong? I'm a little confused.
//MASTER CODE:
#include <16F74.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#use delay(clock=16000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Slow,sda=PIN_C4,scl=PIN_C3)
//=============== GLOBAIS ======================
char s[]= {"- write -"};
char buffer[0x20];
char data;
int i,j;
//============================================
void led()
{
output_bit(PIN_A3,0);
delay_ms(1000);
output_bit(PIN_A3,1);
delay_ms(1000);
output_bit(PIN_A3,0);
}
void buffer()
{
if(j<25)
{
buffer[j]=data;
j++;
}
else j=0;
}
void main()
{
port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
// TODO: USER CODE!!
while(1)
{
//================= WRITE =====================
if(!input(PIN_A0)) // write in slave
{
i2c_start(); // Begin
i2c_write(0xa0); // Control Byte (write)
i2c_write(0x20); // Address
for (i=0;i<10;i++)
{
i2c_write(s[i]); // Data (10 char's)
delay_ms(100);
}
i2c_stop(); // end
led(); // Led
}
//============= READ ===================
if(!input(PIN_A2)) // read in slave
{
i2c_start(); // begin
i2c_write(0xA0);
i2c_write(0x20);
i2c_start();
i2c_write(0xA1);
for(j=0;j<25;j++)
{
data = i2c_read();
buffer();
}
i2c_stop();
led();
}
}
}
// SLAVE CODE:
#include <16F74.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //Crystal osc (>4mhz)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#use delay(clock=16000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,address=0xA0)
//============= GLOBAIS ==================
BYTE buffer[0x20];
char c;
int8 data;
char r[]="- Ok -";
int i=0;
int j;
//=========( BUFFER )========================
/*
void buffer()
{
if (i<25> state=0: Address match received with R/W bit clear
{
}
*/
//=========( STATE<80 )======================
if(state<0x80>1)
{
data=rx;
c=data;
putc(c);
buffer();
}
//=====( STATE=80 )=======================
if(state==0x80) // Address match received with R/W bit set
{
i2c_start();
i2c_write(0x20);
for (j=0;j<10;j++)
{
i2c_write(r[j]);
delay_ms(100);
}
i2c_stop();
}
//=====( STATE<FF )==================
/*
if(state<0xFF) //Transmission completed and acknowledged
{
}
*/
}
void main ()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while (1)
{
}
}
//--------------------------------------------------------------------
Thank you for your time.
ArMo
Last edited by ArMo on Thu Dec 27, 2007 6:03 am; edited 7 times in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 26, 2007 2:24 pm |
|
|
Quote: | if (i<25> state=0: |
The code in your post is messed up, because of a setting in the forum.
To fix it, do this:
Click on the word "Profile", in the upper left corner of the CCS forum
page. Then edit your user profile and select the box to disable HTML.
Set it for "No":
Quote: | Always allow HTML: No |
Then edit your post, delete the code, and paste in new code.
This will fix the problem. Then the code will look OK. |
|
|
ArMo
Joined: 26 Dec 2007 Posts: 3
|
|
Posted: Wed Dec 26, 2007 2:39 pm |
|
|
I do it... that's all rigth now ??? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 26, 2007 2:44 pm |
|
|
Quote: | if (i<25> state=0: Address match received with R/W bit clear
if(state<0x80>1)
|
It's still not fixed.
1. You need to edit your original post. Delete all the code.
2. Copy and paste in new code, from your editor (MPLAB or CCS).
3. Look below the posting window. Make sure that this box is selected:
Quote: | x Disable HTML in this post |
4. Then press the Submit button. |
|
|
ArMo
Joined: 26 Dec 2007 Posts: 3
|
|
Posted: Thu Dec 27, 2007 6:04 am |
|
|
I did it ... it´s ok ?? PLEASE HELP ME |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 27, 2007 1:26 pm |
|
|
The code is not posted OK. Look at it. Many statements are messed up.
I suggest that you give up on posting the code. Instead, try the
following:
Use the CCS example file code, Ex_Slave.c, for the Slave PIC.
It's in this directory:
Quote: |
c:\Program Files\picc\Examples\Ex_Slave.c
|
Use the code in this post for the Master PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=32368&start=3 |
|
|
|
|
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
|