![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
bionicbot
Joined: 05 Aug 2010 Posts: 5 Location: UK
|
dsPIC33fj256MC710 Master- Slave with I2C |
Posted: Wed Sep 29, 2010 10:54 am |
|
|
Dear All,
I have some problem with using two of dspic33fj256mc710 for communication as master and slave. I need to send data from master to on-off led in slave, but it doesn't work. I've tried to read from many posts and apply to my work but it's still the same problem. This is my master code.
Code: |
#include <33FJ256MC710.h>
#fuses XT,PR_PLL,NOWDT,NOCOE,NODEBUG, NOWRTB, NOPUT, NOWRTSS, NOWRT, NOPROTECT, NORSS
#use delay(crystal=10000000,clock=20000000)
//#use rs232(baud=9600,uart1)
#use i2c(master, I2C1,force_hw)
#define slave01 0x52
BYTE read_slave(BYTE address)
{
BYTE data;
i2c_start();
i2c_write(address);
i2c_write(0x00);
i2c_start();
i2c_write(address+1);
data = i2c_read(0);
i2c_stop();
return(data);
}
void write_slave(BYTE address,BYTE data)
{
i2c_start();
i2c_write(address);
i2c_write(0x00);
i2c_write(data);
i2c_stop();
}
void main(void)
{
int8 count;
set_tris_g(0x0F);
setup_adc_ports(no_analogs);
while(TRUE)
{
for(count=0;count<4;count++)
{
write_slave(slave01,count);
// on-off led in master to check the data is corrected
if(bit_test(count,0))
{
output_high(PIN_G6);
}
else
output_low(PIN_G6);
if(bit_test(count,1))
output_high(PIN_G7);
else
output_low(PIN_G7);
delay_ms(1000);
}
}
}
|
And my slave code is:
Code: |
#include <33FJ256MC710.h>
#fuses XT,PR_PLL,NOWDT,NOCOE,NODEBUG, NOWRTB, NOPUT, NOWRTSS, NOWRT, NOPROTECT, NORSS
#use delay(crystal=10000000,clock=20000000)
//#use rs232(baud=9600,uart1)
#use i2c(SLAVE, I2C1, address=0x52)
BYTE address,buffer[0x10],data1;
#INT_SI2C
void ssp_interupt()
{
BYTE incoming, state;
state = i2c_isr_state();
output_high(PIN_G6);
output_high(PIN_G7);
if(state < 0x80)
{
incoming = i2c_read();
if(state == 1)
address = incoming;
if(state == 2)
{
data1 = incoming;
// on-off led by the data has been sent
if(bit_test(data1,0))
output_high(PIN_G6);
else
output_low(PIN_G6);
if(bit_test(data1,1))
output_high(PIN_G7);
else
output_low(PIN_G7);
}
}
if(state == 0x80)
{
i2c_write(buffer[address]);
}
}
void main(void)
{
int8 count;
//set_tris_g(0x0F);
setup_adc_ports(no_analogs);
enable_interrupts(INTR_GLOBAL);
enable_interrupts(INT_SI2C);
enable_interrupts(INT_MI2C);
while(TRUE)
{
}
}
|
I also used the oscilloscope to check the signal on SDA and SCL from master, it's correct on both of them. But when I put the command to on-off LED in interupt function of slave, it doesn't work. I tried to set address for slave at 0xA0 and 0x52 as I read from many post but it's still the same problem. I'm afraid that the interupt it doesn't work. Anybody have idea about this problem, please let me know.
Thanks in advance. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19382
|
|
Posted: Wed Sep 29, 2010 3:21 pm |
|
|
The lines operating the LEDs, will stay high most of the time in the slave. You are setting them high at the start of the interrupt handler. Remember the handler will be called for the 'stop' transaction, as well as for the data transactions, so the lines may be driven low for a short moment but will then go high again...
That the interrupt is called for 'stop', is not obvious in a lot of the paperwork. Most of the timing diagrams terminate at this point, and just a few of the data sheets add the comment that this happens...
Set the lines high at the start of the main, and remove the lines setting them high at the start of the interrupt.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
bionicbot
Joined: 05 Aug 2010 Posts: 5 Location: UK
|
|
Posted: Wed Sep 29, 2010 5:05 pm |
|
|
Ttelmah, Thank you very much for reply. Sorry for post a bit mistake of the code about the two lines in the interupt function. The two lines which on LED, I just put it to check the inturupt fnc work or not and I forgot to remove befor I put on the web. When I run this code LED doesn't on as we require, that mean why I thought that the interupt function doesn't work. I'm not sure about setting the interupt function it's correct? Do you have any idea? Thank again. |
|
![](templates/subSilver/images/spacer.gif) |
bionicbot
Joined: 05 Aug 2010 Posts: 5 Location: UK
|
|
Posted: Wed Sep 29, 2010 5:17 pm |
|
|
Ttelmah, I have changed the code as you have commented but LEDs are still not working. This is my code.
Code: |
#INT_SI2C
void ssp_interupt()
{
BYTE incoming, state;
state = i2c_isr_state();
if(state < 0x80)
{
incoming = i2c_read();
if(state == 1)
address = incoming;
if(state == 2)
{
data1 = incoming;
}
}
if(state == 0x80)
{
i2c_write(buffer[address]);
}
}
void main(void)
{
int8 count;
//set_tris_g(0x0F);
setup_adc_ports(no_analogs);
enable_interrupts(INTR_GLOBAL);
enable_interrupts(INT_SI2C);
enable_interrupts(INT_MI2C);
while(TRUE)
{
// on-off led by the data has been sent
if(bit_test(data1,0))
output_high(PIN_G6);
else
output_low(PIN_G6);
if(bit_test(data1,1))
output_high(PIN_G7);
else
output_low(PIN_G7);
}
}
|
I'm not sure about setting the interrupt, it's correct? Because when I turned on LED in the interuppt function, LEDs don't work.
Thanks again. |
|
![](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
|