|
|
View previous topic :: View next topic |
Author |
Message |
khienpzo
Joined: 26 Nov 2012 Posts: 3
|
I2C 1 Master 2 Slave |
Posted: Mon Nov 26, 2012 7:56 pm |
|
|
Hi, I am doing one project communication 1 master and 2 slave i2c read data from ADC 2 slave master and sent to the computer. I refer to the code of "PCM programmer" but I have not read the ADC data.
Master code:
Code: |
#include <16f877a.h>
#include <def_877a.h>
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4000000)
#use i2c(MASTER,SDA=PIN_C4,SCL=PIN_C3,fast)
#use RS232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define slave_address1 0x10
#define slave_address2 0x20
//========================
char data1,data2 =0;
unsigned int16 control;
//=========================
#INT_RDA
void ngatrs232()
{
char c;
c = getc();
if (c=='a')
{
putc(255);
putc(255);
delay_ms (10);
}
if (c== 'b' )
{
control = 1;
}
if (c== 'f' )
{
control = 2;
}
}
void main()
{
control=0;
data1 =0;
data2 =0;
enable_interrupts(INT_RDA);
ext_int_edge(H_TO_L);
enable_interrupts (GLOBAL);
delay_ms(50);
set_tris_D(0x00);
set_tris_C(0x80);
while(true)
{
i2c_start();
i2c_write(slave_address1);
data1 = i2c_read(0);
i2c_stop();
delay_ms(10);
i2c_start();
i2c_write(slave_address2);
data2 = i2c_read(0);
i2c_stop();
delay_ms(10);
if (control == 1)
{
putc(data1);
putc(data2);
delay_ms(10);
}
}
} |
code Slave 1
Code: |
#include <16f877a.h>
#include <def_877a.h>
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4000000)
#use i2c (SLAVE,SDA=PIN_C4,SCL=PIN_C3,address=0x10,force_hw) // khai bao chuan truyen thong su dung//
//=====================================================
int8 adc_result;
#INT_SSP
void ssp_interrupt()
{
int8 incoming, state;
state = i2c_isr_state();
if(state < 0x80) // Master truyen du lieu
{
incoming = i2c_read();
}
if(state >= 0x80) // Master nhan du lieu tu Slave
{
i2c_write(adc_result);
}
}
//======================================
void main ()
{
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);
adc_result = read_adc();
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(true)
{
adc_result = read_adc();
delay_ms(500);
}
} |
code Slave 2
Code: |
#include <16f877a.h>
#include <def_877a.h>
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4000000)
#use i2c (SLAVE,SDA=PIN_C4,SCL=PIN_C3,address=0x20,force_hw) // khai bao chuan truyen thong su dung//
//=====================================================
int8 adc_result;
#INT_SSP
void ssp_interrupt()
{
int8 incoming, state;
state = i2c_isr_state();
if(state < 0x80) // Master truyen du lieu
{
incoming = i2c_read();
}
if(state >= 0x80) // Master nhan du lieu tu Slave
{
i2c_write(adc_result);
}
}
//======================================
void main ()
{
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);
adc_result = read_adc();
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(true)
{
adc_result = read_adc();
delay_ms(500);
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Tue Nov 27, 2012 12:40 am |
|
|
You don't tell us what your problem actually is....
However killer code is doing two putc's and a delay in the INT_RDA. Multiple problems with this:
1) If data arrives while you are doing this, this _will_ hang the UART (add ERRORS to the RS232 declaration, and the compiler will add code to clear this).
2) Implies that all interrupts _will_ be disabled in the delays in the main....
Basically don't do this. If you must send from inside the interrupt, use a buffered TX. Or just use a flag and do the transmission in the external code. Never delay for more than a very few uSec in an ISR....
Other problem, is that you must read from the I2C, in state 0x80. Study the example code.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Nov 27, 2012 6:02 am |
|
|
also ...
be sure to have the correct value resistors for the I2C bus lines.Usually 4k7 or 3k3 will work.
hth
jay |
|
|
khienpzo
Joined: 26 Nov 2012 Posts: 3
|
reply I2C |
Posted: Tue Nov 27, 2012 7:38 am |
|
|
I use resistor 2k2 for I2C line. |
|
|
|
|
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
|