| View previous topic :: View next topic |
| Author |
Message |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Sun Jul 15, 2012 1:15 pm |
|
|
Let me step through a couple of other problems:
1) The I2C_STATE value _increments_ with each byte, and INT_SSP can only send one byte.You need:
| Code: |
char message[]="ABC";
#INT_SSP
void ssp_interrupt(void) {
int8 incoming, state;
state = i2c_isr_state();
if(state <= 0x80) // Master is sending data _must_ have = here
{
incoming = i2c_read();
}
if(state >= 0x80) // Master is requesting data from slave
{
i2c_write(message[state-0x80];
}
}
|
Then remember you need to add a terminating null to the string at the master (or tranfer the four bytes a three byte 'string' has). data needs to be large enough for this.
Best Wishes |
|
 |
semmoor
Joined: 09 May 2012 Posts: 46 Location: KSA
|
|
Posted: Sun Jul 15, 2012 3:46 pm |
|
|
| jeremiah wrote: | | try only NACK'ing the last byte you read in the master code. You are NACK'ing all 3. Usually when you NACK a read byte, that is the master telling the slave it is finished. |
I did as you said but i saw weird messages on the RS232 i couldn't understand !! |
|
 |
jeremiah
Joined: 20 Jul 2010 Posts: 1426
|
|
Posted: Sun Jul 15, 2012 6:11 pm |
|
|
| That goes back to what Ttelmah said about strings. |
|
 |
semmoor
Joined: 09 May 2012 Posts: 46 Location: KSA
|
|
Posted: Mon Jul 16, 2012 8:33 am |
|
|
| jeremiah wrote: | | That goes back to what Ttelmah said about strings. |
I also did the same but i don't know what is the real problem behind that.
I will do my best to solve this problem maybe i have some issue with my wiring or anything else like that!!! , anyways thank you guys for helping me. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Mon Jul 16, 2012 9:34 am |
|
|
Basic 'C'.
In C, a 'string' is a _null terminated_ array of characters.
So a 'three character string', _requires_ four characters of storage.
When you print a 'string', the print function, walks through the characters, printing them, _till_ it sees the 'null'.
Problem is you are trying to put three characters into a three character array, _lacking the null termination_. When you print this, the print will also try to print all the other stuff in RAM afterwards, keeping going, till it just happens to find a null. Hence loads of garbage.
Best Wishes |
|
 |
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Mon Jul 16, 2012 11:44 am |
|
|
The other side of what Ttelmah is saying is also true - if you stuff a 3 character "string" into a 3 character array (using sprintf for example), the location following the 3 character array will get the null string terminator stuffed in it so if that is another variable etc, that will also get corrupted.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
 |
sseidman
Joined: 14 Mar 2005 Posts: 159
|
|
Posted: Mon Jul 16, 2012 2:30 pm |
|
|
Why are you running two slaves? Are the slaves being used solely for ADC, or are they doing something else.
If the slaves are limited to ADC, have you considered simply using an I2C ADC, instead of building one? This way, you don't have to write any slave code. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9658 Location: Greensville,Ontario
|
|
Posted: Mon Jul 16, 2012 3:05 pm |
|
|
| heck, make it simpler yet....use a PIC with 2 or more ADC channels ! With proper signal conditioning analog is fine for 100'.We all know I2C is NOT a long distance bus.. |
|
 |
semmoor
Joined: 09 May 2012 Posts: 46 Location: KSA
|
|
Posted: Mon Jul 16, 2012 3:08 pm |
|
|
I even tried what all of you have said, but still i keep seeing strange characters.
this is the slave after new changes:
| Code: |
#include <16F877.h>
#device adc=8
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x14)
#include <string.h>
#INT_SSP
void ssp_interrupt()
{
int8 incoming, state;
state = i2c_isr_state();
if(state < 0x80) // Master is sending data
{
incoming = i2c_read();
}
if(state >= 0x80) // Master is requesting data from slave
{
i2c_write('A'); //send first char to slave
i2c_write('B'); //send second char to slave
i2c_write('C'); //send third char to slave
i2c_write('\0'); //send null terminating string
}
}
//======================================
void main ()
{
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1)
{
}
}
|
and the master after changing:
| Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
#define SLAVE1_WRT_ADDR 0x14
#define SLAVE1_READ_ADDR 0x15
//====================================
void main()
{
char data[4]; //stores data sent by slave
int c=0; //counter
while(1)
{
i2c_start();
i2c_write(SLAVE1_READ_ADDR);
data[0] = i2c_read(); delay_ms(100); //stores first char
data[1] = i2c_read(); delay_ms(100); //stores second char
data[2] = i2c_read(); delay_ms(100); //stores third char
data[3] = i2c_read(0); delay_ms(100);//stores last char sent by slave,
i2c_stop();
printf(" [ %s ] \r\n", data); //now display completed string on RS232
delay_ms(500);
}
}
|
Last edited by semmoor on Mon Jul 16, 2012 3:19 pm; edited 1 time in total |
|
 |
semmoor
Joined: 09 May 2012 Posts: 46 Location: KSA
|
|
Posted: Mon Jul 16, 2012 3:13 pm |
|
|
| sseidman wrote: | Why are you running two slaves? Are the slaves being used solely for ADC, or are they doing something else.
If the slaves are limited to ADC, have you considered simply using an I2C ADC, instead of building one? This way, you don't have to write any slave code. |
no forget about the ADC i'm not using it in this project , this code originally was written for ADC i just made few changes to make slave sends string instate of ADC value to the master. |
|
 |
semmoor
Joined: 09 May 2012 Posts: 46 Location: KSA
|
|
Posted: Mon Jul 16, 2012 3:17 pm |
|
|
| I know I could have used external I2C ADC chip to handle all this, but I'm just learning how to communicate between two microcontrollers master/slave that's all, but still failing. |
|
 |
|