|
|
View previous topic :: View next topic |
Author |
Message |
Olufola
Joined: 19 May 2009 Posts: 21
|
Sensor Network with I2C |
Posted: Tue May 19, 2009 1:44 pm |
|
|
I am currently working on a project involving sensor network. Eleven PICs are to be used in the network. I have been trying to use I2C to implement the network in such a way that I have ten slaves and one master. The whole idea is to have ten discrete sensors (slaves) communicating with central base station (master).
Addressing the slaves has been a big problem. I can only broadcast on the network. I cannot uniquely request for data from individual slaves. I saw somewhere that no one has ever successfully implemented correct addressing with the CCS compiler. I do not know if this is true or not.
Assembly code was said to have worked. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 19, 2009 2:18 pm |
|
|
Quote: | Addressing the slaves has been a big problem. |
1. Post the PIC that you are using for the slaves.
2. Post the #use i2c() statement for a few of the slaves, so we can see
the addresses that you are using.
3. Post your compiler version. |
|
|
Olufola
Joined: 19 May 2009 Posts: 21
|
|
Posted: Tue May 19, 2009 2:37 pm |
|
|
Thanks,
1. I am using PIC18F4520 for all the slaves and the master.
2. #use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=104)
3. I am using PCW.
I will soon post the full slave and master code.
Once again, Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 19, 2009 2:56 pm |
|
|
Quote: | 2. #use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=104) |
Post several of the #use i2c() statements for your slaves, so I can see
the slave address on each one. |
|
|
Olufola
Joined: 19 May 2009 Posts: 21
|
|
Posted: Tue May 19, 2009 3:51 pm |
|
|
I mistakenly posted a reply without logging in. My last reply was attributed to a guest. Some of the slave #use are:
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=97)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=98)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=99)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=100)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=101)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=102)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=103)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=104) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 19, 2009 3:55 pm |
|
|
Quote: | #use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=97)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=98)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=99)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=100)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=101)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=102)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=103)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=104) |
The slave addresses shown in bold are not correct. The slave addresses
must be an even number. |
|
|
Olufola
Joined: 19 May 2009 Posts: 21
|
Slave address |
Posted: Wed May 20, 2009 2:16 am |
|
|
I tried using two slave PICs just to ensure that the addressing problem is solved before using ten. The following is the Master code that I wrote. The problem with this is that data is received from the first slave after with the nothing else happens (as if the program hangs). These were the first codes I used and the crap that I posted yesterday were the ones that I resorted to when this one that I originally expected to work did not ( I have not joined this forum then anyway)
I will be glad anyone can help me scrutinize this code for any errors. I might be biased.
Code: |
#include <18F4520.h>
#device adc=8
byte stad,gook;
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV25 //Brownout reset at 2.5V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES NOSTVREN //Stack full/underflow will not cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,fast,sda=PIN_C4,scl=PIN_C3,force_hw)
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
// TODO: USER CODE!!
i2c_start();
delay_ms(5);
i2c_write(0xc1);
delay_ms(5);
while(true)
{
i2c_start();
delay_ms(5);
stad = i2c_isr_state();
if(stad==0 ||stad==0x80){ printf("Address Match Received From Slave 1 \n\r"); }
gook = i2c_read();
delay_ms(5);
printf("Received Data from slave 1 is %3u\n\r",gook);
output_high(pin_d6);
delay_ms(50);
output_low(pin_d6);
delay_ms(50);
delay_ms(100);
//------------------------
i2c_start();
delay_ms(5);
i2c_write(0xd1);
delay_ms(5);
stad = i2c_isr_state();
if(stad==0 || stad ==0x80)
{ printf("Address Match Received From Slave 2 \n\r"); }
gook = i2c_read();
delay_ms(5);
printf("Received Data from slave 2 is %3u\n\r",gook);
output_high(pin_d4);
delay_ms(50);
output_low(pin_d4);
delay_ms(50);
//--------------------
}
i2c_stop();
delay_ms(10);
} |
|
|
|
Ttelmah Guest
|
|
Posted: Wed May 20, 2009 10:04 am |
|
|
The obvious thing is that you never 'stop' the first transaction.
A repeated 'start', is legitimate on a single I2C device, normally used as a way of switching bus direction, but a repeated start, with a device already selected?....
I2C, needs care on a number of things.
Make sure you ack/nack as required on data transfers. Failing to switch for the last byte, is a common error.
Stop, when you have finished.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 20, 2009 11:16 am |
|
|
This is supposed to be the i2c Master program, but it's full of code
for the slave program. It has calls to i2c_isr_state(), but that routine
is supposed to be called inside a slave interrupt routine.
I suggest that you start over. Try to make small Master and Slave
test programs. Get those programs working. Then expand the code.
Here is how to do it:
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
See if you can make that test work. |
|
|
|
|
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
|