Hey,

Thanks a lot. I now understand the address part. But even after the change, it 
does not work. It sends the sensor reading just once over the radio. The Base 
station blinks once and then stops. I do not understand this? I have wired both 
the device to the Atm128I2CMasterC. 

I have also taken Sergio's advice to call the isOwner() as well. The LEDs 
inside readDone() also just blink the first time to send the data and then 
stop. What could be causing this? Here is the updated code. Thanks a lot.

--------------------------------------------------------------------------
configuration BedAppC { }

implementation
{
        components BedC, MainC, LedsC, ActiveMessageC,
        new TimerMilliC(), new Atm128I2CMasterC() as Atm128I2CMasterC, new 
AMSenderC(AM_BED);

        BedC.Boot -> MainC;
        BedC.Leds -> LedsC;
        BedC.I2CResource -> Atm128I2CMasterC;
        BedC.I2CPacket1 -> Atm128I2CMasterC;
        BedC.I2CPacket2 -> Atm128I2CMasterC;
        BedC.Timer -> TimerMilliC;
        BedC.AMSend -> AMSenderC;
        BedC.RadioControl -> ActiveMessageC;
}
------------------------------------------------------------------------------

#include <I2C.h>
#include "Timer.h"
#include "bed.h"

module BedC @safe() {
        uses {
                interface Boot;
                interface Leds;
                interface Timer<TMilli>;
                //I2C 
                interface Resource as I2CResource;
                interface I2CPacket<TI2CBasicAddr> as I2CPacket1;
                interface I2CPacket<TI2CBasicAddr> as I2CPacket2;
                // RADIO COMMUNICATION
                interface AMSend;               
                interface SplitControl as RadioControl;
                
        }
}

implementation {

        message_t sendBuf;
        bool sendBusy = FALSE;
        bed_t local;
        uint8_t getDataCommand = 0x43;
        uint8_t chipnumber =1;
        uint8_t* readData;
        bool isReadyToSend = FALSE;
        uint8_t prevData[NREADINGS];
        
        
        event void Boot.booted() {
                local.id = TOS_NODE_ID;
                call RadioControl.start();
        }
        
        event void RadioControl.startDone(error_t error) {
                
                call Timer.startPeriodic(3000);
        }
        
        event void RadioControl.stopDone(error_t error) {
        }
        
        task void sendData() {
                if (!sendBusy && sizeof local <= call 
AMSend.maxPayloadLength()) {
                        memcpy(call AMSend.getPayload(&sendBuf, sizeof(local)), 
&local, sizeof local);
                        if (call AMSend.send(AM_BROADCAST_ADDR, &sendBuf, 
sizeof local) == SUCCESS) {
                                sendBusy = TRUE;
                                local.count++;
                        }
                }
        }
        
        event void Timer.fired() {
                if(isReadyToSend) {
                        isReadyToSend = FALSE;
                        post sendData();
                        
                } else { 
                
                call Leds.led1Toggle();  //green   
                call I2CResource.request();                     
                        
                }
        }
        
        event void I2CResource.granted() {      
                call I2CPacket1.write(I2C_START | I2C_STOP, 0x48, 1, 
(uint8_t*)(&getDataCommand));
        }
        
        async event void I2CPacket1.writeDone(error_t success, uint16_t addr, 
uint8_t length, uint8_t* data) {
                call I2CPacket1.read(I2C_START | I2C_STOP, 0x48, 4, 
(uint8_t*)(&readData));
                
        }
        
        async event void I2CPacket1.readDone(error_t success, uint16_t addr, 
uint8_t length, uint8_t* data) {
                
        if ( call I2CResource.isOwner() == TRUE) {
                if(success == SUCCESS) {
                        local.reading[0] = readData[0];
                        local.reading[1] = readData[1];
                        local.reading[2] = readData[2];
                        local.reading[3] = readData[3];
                }
                chipnumber++;
                call Leds.led2Toggle();  //yellow
                call I2CPacket2.write(I2C_START | I2C_STOP, 0x49, 1, 
(uint8_t*)(&getDataCommand));
                }
        }
        
        async event void I2CPacket2.writeDone(error_t success, uint16_t addr, 
uint8_t length, uint8_t* data) {
                call I2CPacket2.read(I2C_START | I2C_STOP, 0x49, 4, 
(uint8_t*)(&readData));
                
        }
        
        
                async event void I2CPacket2.readDone(error_t success, uint16_t 
addr, uint8_t length, uint8_t* data) {
                
                if ( call I2CResource.isOwner() == TRUE) {
                if(success == SUCCESS) {
                        local.reading[4] = readData[0];
                        local.reading[5] = readData[1];
                        local.reading[6] = readData[2];
                        local.reading[7] = readData[3];
                        
                        }
                        chipnumber=1;                   
                        call Leds.led0Toggle();                //red
                        call I2CResource.release();
                        isReadyToSend = TRUE;   
                }
                
                }
        
        
        event void AMSend.sendDone(message_t* msg, error_t error) {
                sendBusy = FALSE;
        }
        
}


---------------------------------------------------------------------------------------------

regards
BG
________________________________________
From: András Bíró [[email protected]]
Sent: Wednesday, February 29, 2012 4:41 AM
To: #BHARTI GOEL#
Cc: [email protected]
Subject: Re: [Tinyos-help] Reading two PCF8591 chips over I2C bus

Hi,

The address you provide to the i2cpacket will be automatically shift
left, and r/~w bit will be added based on the command (read or write).
So, your slave address is 1001 A2 A1 A0.

Andris

2012/2/28 #BHARTI GOEL# <[email protected]>:
> Hi,
>
> I do not understand what you mean. Yes, the address is 7 bit but the last r/w 
> bit is also sent with the address to make it 8 bit.  So 0x20 will be 0x40 for 
> write right?
>
> I am a little confused how we understand the address byte. According the 
> PCF8591 datasheet, the address byte format is :
>
> 1001 A2 A1 A0 R/W
>
> So this would make the address to be 90/91 if A2, A1, A0 = 000 or 92/93 if A2 
> A1 A0 = 001.
>
> However this does not work. For one chip it works for 48, 49.
>
> Do you understand this? Thanks
>
> BG
> ________________________________________
> From: András Bíró [[email protected]]
> Sent: Tuesday, February 28, 2012 1:34 AM
> To: #BHARTI GOEL#
> Cc: [email protected]
> Subject: Re: [Tinyos-help] Reading two PCF8591 chips over I2C bus
>
> Hi,
>
> I think that code shouldn't work with one sensor eighter. The
> I2CPacket interface only needs the 7 bit address, whithout the
> read/~write bit. So your address for the 0x40/0x41 chip is 0x20.
>
> Andris
>
> On Mon, Feb 27, 2012 at 6:10 PM, #BHARTI GOEL# <[email protected]> wrote:
>> Hi,
>>
>> I am trying to interface PCF8591 chips over I2C bus with an iris mote to get
>> pressure sensor reading. When I use one chip, my program is working. However
>> when I add one more chip , it stops after taking one reading. I am not sure
>> what the problem is. I am reading the 2 chips in sequence one after the
>> other. I am using Atm128I2CMasterC as the master device for both the chips.
>> Here is my code. Thanks a lot
>>
>> #include <I2C.h>
>> #include "Timer.h"
>> #include "bed.h"
>>
>> module BedC @safe() {
>>     uses {
>>         interface Boot;
>>         interface Leds;
>>         interface Timer<TMilli>;
>>         //I2C
>>         interface Resource as I2CResource;
>>         interface I2CPacket<TI2CBasicAddr> as I2CPacket1;
>>         interface I2CPacket<TI2CBasicAddr> as I2CPacket2;
>>         // RADIO COMMUNICATION
>>         interface AMSend;
>>         interface SplitControl as RadioControl;
>>
>>     }
>> }
>>
>> implementation {
>>
>>     message_t sendBuf;
>>     bool sendBusy = FALSE;
>>     bed_t local;
>>     uint8_t getDataCommand;
>>     uint8_t* readData;
>>     bool isReadyToSend = FALSE;
>>     uint8_t prevData[NREADINGS];
>>     //bool isDetection = FALSE;
>>
>>
>>     event void Boot.booted() {
>>         local.id = TOS_NODE_ID;
>>         call RadioControl.start();
>>     }
>>
>>     event void RadioControl.startDone(error_t error) {
>>
>>         call Timer.startPeriodic(3000);
>>     }
>>
>>     event void RadioControl.stopDone(error_t error) {
>>     }
>>
>>     task void sendData() {
>>         if (!sendBusy && sizeof local <= call AMSend.maxPayloadLength()) {
>>             memcpy(call AMSend.getPayload(&sendBuf, sizeof(local)), &local,
>> sizeof local);
>>             if (call AMSend.send(AM_BROADCAST_ADDR, &sendBuf, sizeof local)
>> == SUCCESS) {
>>                 sendBusy = TRUE;
>>                 local.count++;
>>             }
>>         }
>>     }
>>
>>     event void Timer.fired() {
>>         if(isReadyToSend) {
>>             call Leds.led0Toggle();                 //red
>>             post sendData();
>>             isReadyToSend = FALSE;
>>         } else {
>>             call Leds.led2Toggle();                 //yellow
>>             call I2CResource.request();
>>         }
>>     }
>>
>>     event void I2CResource.granted() {
>>         getDataCommand = 0x43;
>>         if(call I2CPacket1.write(I2C_START | I2C_STOP, 0x48, 1,
>> (uint8_t*)(&getDataCommand)) == SUCCESS) {
>>
>>         }
>>     }
>>
>>     async event void I2CPacket1.writeDone(error_t success, uint16_t addr,
>> uint8_t length, uint8_t* data) {
>>         if(call I2CPacket1.read(I2C_START | I2C_STOP, 0x49, 4,
>> (uint8_t*)(&readData)) == SUCCESS) {
>>
>>         }
>>     }
>>
>>     async event void I2CPacket1.readDone(error_t success, uint16_t addr,
>> uint8_t length, uint8_t* data) {
>>
>>         if(success == SUCCESS) {
>>             local.reading[0] = readData[0];
>>             local.reading[1] = readData[1];
>>             local.reading[2] = readData[2];
>>             local.reading[3] = readData[3];
>>             //call Leds.led1Toggle();  //green
>>
>>
>>         }
>>         if(call I2CPacket2.write(I2C_START | I2C_STOP, 0x40, 1,
>> (uint8_t*)(&getDataCommand)) == SUCCESS) {
>>
>>         }
>>     }
>>
>>     async event void I2CPacket2.writeDone(error_t success, uint16_t addr,
>> uint8_t length, uint8_t* data) {
>>         if(call I2CPacket2.read(I2C_START | I2C_STOP, 0x41, 4,
>> (uint8_t*)(&readData)) == SUCCESS) {
>>
>>         }
>>     }
>>
>>
>>         async event void I2CPacket2.readDone(error_t success, uint16_t addr,
>> uint8_t length, uint8_t* data) {
>>
>>         if(success == SUCCESS) {
>>             local.reading[4] = readData[0];
>>             local.reading[5] = readData[1];
>>             local.reading[6] = readData[2];
>>             local.reading[7] = readData[3];
>>             isReadyToSend = TRUE;
>>             call I2CResource.release();
>>
>>         }
>>
>>
>>         }
>>
>>
>>     event void AMSend.sendDone(message_t* msg, error_t error) {
>>         call Leds.led1Toggle();  //green
>>         sendBusy = FALSE;
>>     }
>>
>> }
>>
>> --------------------------------------------------------------////
>>
>> configuration BedAppC { }
>>
>> implementation
>> {
>>     components BedC, MainC, LedsC, ActiveMessageC,
>>     new TimerMilliC(), new Atm128I2CMasterC() as Atm128I2CMasterC, new
>> AMSenderC(AM_BED);
>>
>>     BedC.Boot -> MainC;
>>     BedC.Leds -> LedsC;
>>     BedC.I2CResource -> Atm128I2CMasterC;
>>     BedC.I2CPacket1 -> Atm128I2CMasterC;
>>     BedC.I2CPacket2 -> Atm128I2CMasterC;
>>     BedC.Timer -> TimerMilliC;
>>     BedC.AMSend -> AMSenderC;
>>     BedC.RadioControl -> ActiveMessageC;
>> }
>>
>>
>> regards
>> Bharti
>>
>>
>> _______________________________________________
>> Tinyos-help mailing list
>> [email protected]
>> https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
>
>



_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to