示例#1
0
int checkTemperature(int channel){
    //TODO: comment code
    double temp = readTempSensor(channel);
    if(temp < TEMP_MIN || temp > TEMP_MAX){
        setRelay(RELAY_OFF);
        return RED_STATUS;
    }
    if(temp <= TEMP_YELLOW_MIN || temp >= TEMP_YELLOW_MAX){
        return OVERTEMP_STATUS;
    }
    else{
        return GREEN_STATUS;
    }
}
示例#2
0
void main(void)
{
	unsigned char address;
	unsigned char byte1, byte2, slope, counter;
	int temp, old_temp;
	int diff, last_diff;

	// zero our variables to save iteration values
	old_temp = 0;
	last_diff = 0;

	// Set internal oscillator to 4Mhz
	OSCCON = 0x6C;

	// Set PORT C (TRISC<3:4> bits) as digital outputs 
	TRISC = 0x18;
	LATC = 0x18;

	// Set Port B for digital output (LEDs)
	TRISB = 0x00;
	LATB = 0x3F;

	address = 0x07; // don't use this right now!
	setTempSensorAddress(address);

	// According to the datasheet, with a 4Mhz clock we
	// need SSPADD set to 0x28 to get a 100KHz clock for
	// the i2c master.  However, this is clearly wrong
	// when you look at the i2c clock with the logic tool.
	// The i2c frequency is about 23 KHz.  An errata sheet
	// for the processor says there is an error in the data
	// sheet.  SSPADD = 0x0A appears to get 83KHz.
	SSPADD = 0x0A;

	// Init PIC I2C hardware as Master
	OpenI2C (MASTER, SLEW_OFF);
	
	configStartTempSensor();

	while(1) {

		// Flash top red LED to indicate that the program is running
		LATBbits.LATB5 = ~(LATBbits.LATB5 & 1);

		// read temp sensor, get bytes for more accurate result
		readTempSensor(&byte1, &byte2, &slope, &counter);

		// check to see if we have a read error. If not, check to 
		// see if the temperature has changed.  If so, do something
		// with the red LEDs.
		if(byte1!=0xFF) {
			temp = (int) byte1; 
			temp = temp << 1;
			byte2 = byte2 >> 7;
			temp = temp + (int) byte2;
			diff = temp - old_temp;
			if(diff<0) diff = -diff;
			if((diff+last_diff)>=3) {
				LATB = 0x3F;
			} else if ((diff+last_diff)==2) {
				LATB = 0x1F;
			} else if ((diff+last_diff)==1) {
				LATB = 0x0F;
			} else {
				LATB = 0x07;
			}

			// save temp as old_temp to compute difference
			old_temp = temp;
			last_diff = diff;
		} else {