# HG changeset patch # User kripperger@CL-SEC241-09.cedarville.edu # Date 2013-02-01 17:06:48 # Node ID f859ce69ebdbe0e2bdfe8192121776ba6e968f06 # Parent ec5ca12568cd7bfd2b017a4311d4e212423ce7b0 Master comms diff --git a/slave/slave/lib/i2c.h b/slave/slave/lib/i2c.h --- a/slave/slave/lib/i2c.h +++ b/slave/slave/lib/i2c.h @@ -60,6 +60,6 @@ void i2c_write(unsigned char addr, unsig unsigned char i2c_read(unsigned char addr, unsigned char reg); /////////////////added for humidity -unsigned char i2c_read16(unsigned char addr); +uint16_t i2c_read16(unsigned char addr); #endif /* I2C_H_ */ \ No newline at end of file diff --git a/slave/slave/lib/masterComm.c b/slave/slave/lib/masterComm.c --- a/slave/slave/lib/masterComm.c +++ b/slave/slave/lib/masterComm.c @@ -64,15 +64,47 @@ void masterComm_types() } +void masterComm_packetSend_unsigned(uint8_t id, uint32_t data) +{ + serial0_sendChar('['); + snprintf(buff2,64,"%u%u",id,data); + serial0_sendString(buff2); + serial0_sendChar(']'); + serial0_sendChar(masterComm_checksum(buff2)); +} + +void masterComm_packetSend_signed(uint8_t id, int32_t data) +{ + serial0_sendChar('['); + snprintf(buff2,64,"%u%d",id,data); + serial0_sendString(buff2); + serial0_sendChar(']'); + serial0_sendChar(masterComm_checksum(buff2)); +} + + + void masterComm_modules() { - // Send BoardTemperature (Common for all modules) + // Send Board Temperature (Common for all modules) + masterComm_packetSend_signed(0,sensors_getBoardTemp()); + + + // Send Heater Status (Common for all modules) serial0_sendChar('['); - snprintf(buff2,64,"0%u",sensors_getBoardTemp()); + snprintf(buff2,64,"1%u",/*Heater Status Function Here */2); serial0_sendString(buff2); serial0_sendChar(']'); serial0_sendChar(masterComm_checksum(buff2)); + // Send Battery Level (Common for all modules) + serial0_sendChar('['); + snprintf(buff2,64,"2%u",/*Heater Status Function Here */2); + serial0_sendString(buff2); + serial0_sendChar(']'); + serial0_sendChar(masterComm_checksum(buff2)); + + // Send module specific sensor readings switch(io_getModuleId()) { diff --git a/slave/slave/lib/masterComm.h b/slave/slave/lib/masterComm.h --- a/slave/slave/lib/masterComm.h +++ b/slave/slave/lib/masterComm.h @@ -10,11 +10,15 @@ char masterComm_checksum(const char* stringPtr); -void masterComm_types(); // Calculates the number of types the module has -void masterComm_modules(); // Sends sensor data depending on module +void masterComm_types(); // Calculates the number of types the module has + +void masterComm_packetSend_unsigned(uint8_t id, uint32_t data); //Creates the sensor specific packets and sends them +void masterComm_packetSend_signed(uint8_t id, int32_t data); + +void masterComm_modules(); // Sends sensor data depending on module void masterComm_checkParser(); // Runs parser and checks for data request -void masterComm_send(); // Sends data after being requested +void masterComm_send(); // Sends data after being requested diff --git a/slave/slave/lib/sensors.c b/slave/slave/lib/sensors.c --- a/slave/slave/lib/sensors.c +++ b/slave/slave/lib/sensors.c @@ -94,116 +94,116 @@ void sensors_setupPressure() md = i2c_read(PRESSURE_ADDR, 0xBE); md = md << 8; md = md | i2c_read(PRESSURE_ADDR, 0xBF); -} - -void sensors_readSpiTemp() -{ - // Select TEMP wait 100 microseconds then read four bytes - SELECT_TEMP; - _delay_us(100); - uint8_t one = send_spi(0xFF); - _delay_us(100); - uint8_t two = send_spi(0xFF); - _delay_us(100); - uint8_t three = send_spi(0xFF); - _delay_us(100); - uint8_t four = send_spi(0xFF); - DESELECT_TEMP; - - int16_t temperature = ((one<<4)|(two>>4)); // Shift and place into larger int. (Cuts off Decimal) - temperature = (temperature & (0x0800)) ? (temperature & 0xF000) : temperature; // Sign extend - - //int16_t temperature = ((one<<6)|(two>>2)); // Shift and place into larger int. (Includes Decimal) - //temperature = (temperature & (0x2000)) ? (temperature & 0xC000) : temperature; // Sign extend - - temperature = (two & 0x01) ? 0x00DE : temperature; // Error Condition. If error is detected output is set to 222 degrees (0x00DE) - - // Note: Temperature still needs to be scaled in order to be accurate (eg. boil water). Do this before implementing. - spiTemp = temperature; -} - -void sensors_readBoardTemp() -{ +} + +void sensors_readSpiTemp() +{ + // Select TEMP wait 100 microseconds then read four bytes + SELECT_TEMP; + _delay_us(100); + uint8_t one = send_spi(0xFF); + _delay_us(100); + uint8_t two = send_spi(0xFF); + _delay_us(100); + uint8_t three = send_spi(0xFF); + _delay_us(100); + uint8_t four = send_spi(0xFF); + DESELECT_TEMP; + + int16_t temperature = ((one<<4)|(two>>4)); // Shift and place into larger int. (Cuts off Decimal) + temperature = (temperature & (0x0800)) ? (temperature & 0xF000) : temperature; // Sign extend + + //int16_t temperature = ((one<<6)|(two>>2)); // Shift and place into larger int. (Includes Decimal) + //temperature = (temperature & (0x2000)) ? (temperature & 0xC000) : temperature; // Sign extend + + temperature = (two & 0x01) ? 0x00DE : temperature; // Error Condition. If error is detected output is set to 222 degrees (0x00DE) + + // Note: Temperature still needs to be scaled in order to be accurate (eg. boil water). Do this before implementing. + spiTemp = temperature; +} + +void sensors_readBoardTemp() +{ boardTemp = i2c_read(BOARDTEMP_ADDR, 0x00); // Read only the first byte of data (we don't need the resolution here) boardTemp = ((boardTemp*18)/10) + (32); // Converting Celsius to Fahrenheit - boardTemp = boardTemp - 3; // Linear offset -} - -void sensors_readPressure() -{ - i2c_write(0xEE, 0xF4, 0x2E); //write 0x2E (temp) into 0xF4 (control register), (write is 0xEE) - _delay_us(4500); //wait 4.5 ms - ut = i2c_read(PRESSURE_ADDR, 0xF6); - ut = ut << 8; - ut = ut | i2c_read(PRESSURE_ADDR, 0xF7); //ut = MSB<<8 + LSB - - i2c_write(0xEE, 0xF4, 0x34); //write 0x34 (pressure) into 0xF4 (control register), (write is 0xEE) - _delay_us(4500); //wait 4.5 ms - up = i2c_read(PRESSURE_ADDR, 0xF6); - up = up << 8; - up = up | i2c_read(PRESSURE_ADDR, 0xF7); //up = (MSB<<16 + LSB<<8 + XLSB(NOT USED)) >> (8-oss) - - //calculate true temperature - x1 = ((ut - ac6) * ac5) >> 15; - x2 = (mc << 11) / (x1 + md); - b5 = x1 + x2; - trueTemp = (b5 + 8) >> 4; - - //calculate b3 - b6 = b5 - 4000; - x1 = (b2 * (b6 * b6) >> 12) >> 11; - x2 = (ac2 * b6) >> 11; - x3 = x1 + x2; - b3 = ((ac1 * 4 + x3) + 2) / 4; - - //calculate b4 - x1 = (ac3 * b6) >> 16; - x2 = (b1 * ((b6 * b6) >> 12)) >> 16; - x3 = ((x1 + x2) + 2) >> 2; - b4 = (ac4 * (x3 + 32768)) >> 15; - - b7 = (up - b3) * 50000; - - if (b7 < 0x80000000) - { - pressure = (b7 << 1) / b4; - } - - else - { - pressure = (b7 / b4) << 1; - } - - x1 = (pressure >> 8) * (pressure >> 8); - x1 = (x1 * 3038) >> 16; - x2 = (-7357 * pressure) >> 16; - pressure += (x1 + x2 + 3791) >> 4; //This is the final value for our pressure -} - -void sensors_readHumid() -{ - humid = i2c_read16(HUMID_ADDR); - - //calculations to relative humidity: humid = (humid/((2^14) - 1))*100% >> is divide by power, << is multiply by power, 2^14-1 = 16383 - humid = (humid / 16383) * 100; -} - -void sensors_readLight() -{ - // FOR FIRST BYTE: - light = i2c_read(LIGHT_ADDR, 0x03); - // exponent = 8xE3 + 4xE2 + 2xE1 + E0 - // mantissa = 8xM7 + 4xM6 + 2xM5 + M4 - // light = 2^(exponent)*mantissa*0.72 - - // FOR BOTH BYTES: - // light = light << 4; - // light = light | (0x0F & i2c_read(LIGHT_ADDR, 0x04)); // This can be used to read in the 4 LSBs from the second register - // exponent = 8xE3 + 4xE2 + 2xE1 + E0 - // mantissa = 128xM7 + 64xM6 + 32xM5 + 16xM4 + 8xM3 + 4xM2 + 2xM1 + M0 - // light = 2^(exponent)*mantissa*0.045 -} - + boardTemp = boardTemp - 3; // Linear offset +} + +void sensors_readPressure() +{ + i2c_write(0xEE, 0xF4, 0x2E); //write 0x2E (temp) into 0xF4 (control register), (write is 0xEE) + _delay_us(4500); //wait 4.5 ms + ut = i2c_read(PRESSURE_ADDR, 0xF6); + ut = ut << 8; + ut = ut | i2c_read(PRESSURE_ADDR, 0xF7); //ut = MSB<<8 + LSB + + i2c_write(0xEE, 0xF4, 0x34); //write 0x34 (pressure) into 0xF4 (control register), (write is 0xEE) + _delay_us(4500); //wait 4.5 ms + up = i2c_read(PRESSURE_ADDR, 0xF6); + up = up << 8; + up = up | i2c_read(PRESSURE_ADDR, 0xF7); //up = (MSB<<16 + LSB<<8 + XLSB(NOT USED)) >> (8-oss) + + //calculate true temperature + x1 = ((ut - ac6) * ac5) >> 15; + x2 = (mc << 11) / (x1 + md); + b5 = x1 + x2; + trueTemp = (b5 + 8) >> 4; + + //calculate b3 + b6 = b5 - 4000; + x1 = (b2 * (b6 * b6) >> 12) >> 11; + x2 = (ac2 * b6) >> 11; + x3 = x1 + x2; + b3 = ((ac1 * 4 + x3) + 2) / 4; + + //calculate b4 + x1 = (ac3 * b6) >> 16; + x2 = (b1 * ((b6 * b6) >> 12)) >> 16; + x3 = ((x1 + x2) + 2) >> 2; + b4 = (ac4 * (x3 + 32768)) >> 15; + + b7 = (up - b3) * 50000; + + if (b7 < 0x80000000) + { + pressure = (b7 << 1) / b4; + } + + else + { + pressure = (b7 / b4) << 1; + } + + x1 = (pressure >> 8) * (pressure >> 8); + x1 = (x1 * 3038) >> 16; + x2 = (-7357 * pressure) >> 16; + pressure += (x1 + x2 + 3791) >> 4; //This is the final value for our pressure +} + +void sensors_readHumid() +{ + humid = i2c_read16(HUMID_ADDR); + + //calculations to relative humidity: humid = (humid/((2^14) - 1))*100% >> is divide by power, << is multiply by power, 2^14-1 = 16383 + humid = (humid / 16383) * 100; +} + +void sensors_readLight() +{ + // FOR FIRST BYTE: + light = i2c_read(LIGHT_ADDR, 0x03); + // exponent = 8xE3 + 4xE2 + 2xE1 + E0 + // mantissa = 8xM7 + 4xM6 + 2xM5 + M4 + // light = 2^(exponent)*mantissa*0.72 + + // FOR BOTH BYTES: + // light = light << 4; + // light = light | (0x0F & i2c_read(LIGHT_ADDR, 0x04)); // This can be used to read in the 4 LSBs from the second register + // exponent = 8xE3 + 4xE2 + 2xE1 + E0 + // mantissa = 128xM7 + 64xM6 + 32xM5 + 16xM4 + 8xM3 + 4xM2 + 2xM1 + M0 + // light = 2^(exponent)*mantissa*0.045 +} + int16_t sensors_getSpiTemp(void) // Gets spi temperature from variable { return spiTemp; @@ -219,7 +219,7 @@ int32_t sensors_getPressure(void) // Get return pressure; } -int sensors_getHumid(void) // Gets relative humidity from variable +uint16_t sensors_getHumid(void) // Gets relative humidity from variable { return humid; }