The receive routine
The receive routine is based on two states: either looking for a device or sampling data from a found device.
There is also a timeout in case a sensors is not transmitting.
void read_port(unsigned char item){
unsigned char p = 0xFF;
long delay;
unsigned char a;
char b=0;
unsigned char d = 0 ;
unsigned char search = 0x17;
int e;
int t = 0;
BCSCTL1 |= RSEL3 | RSEL2 | RSEL1 | RSEL0; // increase speed
P1DIR = 0xF4; // Set P1.2 to output direction
P1OUT = 0xF8; // set p1.2 low to leave snooze mode set p1.3 high to mark sample point
TACTL = 0x1E4; // start timer ACLK /8 continuous mode
for( e = 0; e < 5000 ; e++); // wait to awake from snoze
hit = 0; // reset hit marker
while(t < 1500 & hit == 0){
t = TAR;
d = 0;
p = 0x17;
while(p != 0){ // wait for startbit
p = P1IN;
p &= 0x02;
}
delay = 15;
while(delay > 1) // half bit extra delay at start
delay--;
for(a = 8; a > 0 ; a--){
delay = 33;
while(delay > 1)
delay--;
d = d >> 1;
P1OUT = 0xF8; // set sample pin
p = P1IN;
p &= 0x02;
p = p << 6; // LSB first
d |= p;
} // one byte ready
//send_char(d);
//BCSCTL1 |= RSEL3 | RSEL2 | RSEL1 | RSEL0; // increase speed
if((search == 0x17) & (d == (0x41 + item ))){
search = 0x16;
}
else if(search == 0x16){
buffer[b++] = d;
if( b > size[item])
hit = 0x17;
} // end of char
delay = 15;
while(delay > 1) // half bit extra delay at end
delay--;
} // end of timeout
BCSCTL1 = RSEL2 | RSEL1 | RSEL0; // reduce speed
P1OUT = 0xF4; // set P1.2 high to enter snooze mode
}