![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
mvanvliet
Joined: 02 Jun 2009 Posts: 123 Location: The Netherlands
|
I2C with 16F690 slave to master problem |
Posted: Thu Mar 24, 2011 5:30 am |
|
|
I'm trying to send data between two 16F690 units, from the master to the slave and back, but when I send data to the slave the led goes on, so I think the data is received correct on the slave, but back to the master isn't working. Does anyone knows or sees the problem?
compiler version: 4.106
Master code:
Code: |
#include <16F690.H>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, PUT, NOMCLR, NOCPD, NOPUT
#use delay(clock=4000000)
#use i2c(MASTER, SDA=PIN_B4, SCL=PIN_B6)
#define SLAVE_WRT_ADDR 0xa0
#define SLAVE_READ_ADDR 0xa1
void main()
{
int8 data;
int8 receive = 0;
while(1)
{
//write to the slave board
i2c_start();
i2c_write(SLAVE_WRT_ADDR);
i2c_write(0x00);
i2c_write(10);
i2c_stop();
// Read from the slave board
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(SLAVE_READ_ADDR);
receive = i2c_read(0);
i2c_stop();
if(receive == 10)
{
delay_ms(200);
output_toggle(pin_b7);
}
}
}
|
Slave code:
Code: |
#include <16F690.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, PUT, NOMCLR, NOCPD, NOPUT
#use delay(clock=4000000)
#use i2c(SLAVE, SDA=PIN_B4, SCL=PIN_B6, ADDRESS=0xa0)
int8 incoming = 0, state;
#INT_SSP
void ssp_isr(void)
{
state = i2c_isr_state();
if(state < 0x80) // Master is sending data
{
incoming = i2c_read();
if(incoming == 10)
{
output_high(PIN_B7);
}
}
if(state >= 0x80) // Master is requesting data from slave
{
i2c_write(incoming);
}
}
void main ()
{
setup_adc_ports(NO_ANALOGS);//
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1)
{
}
}
|
Thanks! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19366
|
|
Posted: Thu Mar 24, 2011 6:17 am |
|
|
Your problem is 'incoming'.
When you try to read, you _write_ the register address to talk to. '0'. This is put into incoming. So the subsequent reply, will never be '10'.
This is why the normal method is to use a temporary variable 'dummy' (say), and have an array of actual storage locations, then set the address of the array from the first byte written (the address), and the contents from the second. If you then read back from the array, it is the address that gets set, by the address write, not the contents.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
mvanvliet
Joined: 02 Jun 2009 Posts: 123 Location: The Netherlands
|
|
Posted: Thu Mar 24, 2011 7:01 am |
|
|
Thanks for your reply! Do you mean something like this:
Code: |
#INT_SSP
void ssp_interupt (){
BYTE incoming, state;
state = i2c_isr_state();
if(state <= 0x80) //Master is sending data
{
incoming = i2c_read();
if(state == 1) //First received byte is address
address = incoming;
if(state == 2) //Second received byte is data
buffer[address] = incoming;
}
if(state == 0x80) //Master is requesting data
{
i2c_write(buffer[address]);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
mvanvliet
Joined: 02 Jun 2009 Posts: 123 Location: The Netherlands
|
|
Posted: Thu Mar 24, 2011 7:38 am |
|
|
It's working with this code in the slave:
Code: |
#include <16F690.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, PUT, NOMCLR, NOCPD, NOPUT
#use delay(clock=4000000)
#use i2c(SLAVE, SDA=PIN_B4, SCL=PIN_B6, ADDRESS=0xa0)
int8 incoming = 0, state;
BYTE address, buffer[0x10];
#INT_SSP
void ssp_isr(void)
{
state = i2c_isr_state();
if(state < 0x80) // Master is sending data
{
incoming = i2c_read();
if(state == 1) //First received byte is address
address = incoming;
if(state == 2) //Second received byte is data
buffer[address] = incoming;
}
if(state >= 0x80) // Master is requesting data from slave
{
i2c_write(buffer[address]);
}
}
void main ()
{
setup_adc_ports(NO_ANALOGS);//
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1)
{
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|