Exemple #1
0
int main(void)
{
    init_lcd();
    writecommand(0x01);
    updateScreen();

    DDRD |= (1 << PD2);//output for green LED
    DDRD |= (1 << PD3);//output for red LED

    //button pullup resistors:
    PORTB |= (1 << PB3) | (1 << PB4);

    //rotary encoder initializations:
    initialize_rotary();
    sei();

    PORTC |= (1 << PC4) | (1 << PC5);//i2c pullup resistors

    //enable line
    PORTC |= (1 << PC3);
    DDRC |= (1 << PC3); //set as output

    //DS1631
    ds1631_init();
    ds1631_conv();
    unsigned char temp[2];
    //serial communication
    init_serial();

    while (1) {
        PORTC &= ~(1 << PC3);//set enable to 0
        ds1631_temp(temp);
        int tempCelsius = temp[0];
        //convert from celsius to fahrenheit:
        if(temp[1] == 0){
            tempF = (tempCelsius*9)/5 + 32;
        }
        else{
            tempF = tempCelsius * 10 + 5;
            tempF = (tempF*9)/5 + 320;
            tempF /= 10;
        }

        if(oldTempF != tempF){//if temperature changes, update + transmit data
            //update temp
            updateTop();
            char temp[5];
            snprintf(temp, 5, "%d", tempF);
            transmitData(temp, &tempF);
        }

        oldTempF = tempF;//update old temp

        if((PINB & (1 << PB3)) == 0){//if high button is pressed
            buttonState = 1;//high
        }
        if((PINB & (1 << PB4)) == 0){//if low button is pressed
            buttonState = 0;//low
        }

        if(bufferValidFlag == 1){//valid data received
            //convert to int stored inside remoteVal
            int hundreds = (receivedDataBuffer[1]-0x30)*100;
            int tens = (receivedDataBuffer[2]-0x30)*10;
            int ones = receivedDataBuffer[3]-0x30;
            remoteVal = hundreds + tens + ones;
            if(receivedDataBuffer[0] == '-'){
                remoteVal = 0 - remoteVal;
            }
            updateTop();
            bufferValidFlag = 0;//reset
        }

        if(change == 1){
            updateScreen();
            change = 0;//reset
        }

        updateLED();
    }
    return 0;   /* never reached */
}
int main() {
	int oldl = 1000, oldh = 1000, oldt = 1000;//, oldr = 1000;
	unsigned char out[4];
	unsigned char rec[5];
	unsigned char temp[2];
	moduleinit();

	while (1) {
		//receive and convert temperature
		ds1631_temp(temp);
		float c = temp[0];
		if (temp[1]) {
			c += 0.5;
		}
		float f = (9*c)/5;
		int x = f +32;

		//unsigned char rmt = rx_char();

		if (low != oldl) {
			moveto(0x45);	
			stringout("   "); //clear previous number
			moveto(0x45);
			sprintf(out, "%2d", low);
			stringout(out);
			oldl = low;
		}
		if (high != oldh) {
			moveto(0x4d);
			stringout("   ");	//clear previous number
			moveto(0x4d);
			sprintf(out, "%2d", high);
			stringout(out);
			oldh = high;
		}
		if (x != oldt) {
			moveto(5);
			stringout("   ");
			moveto(5);
			itoa(x, out, 10);
			stringout(out);
			oldt = x;
			//send temperature
			if (x > 0) {
				tx_char('+');	
			}
			else if (x < 0) {
				tx_char('-');
			}
			//if 3 digits
			if (out[2] == 0) {
				tx_char('0');
				_delay_ms(5);
				tx_char(out[0]);
				_delay_ms(5);
				tx_char(out[1]);
			}
			//if 2 digits
			else {
				tx_char(out[0]);
				_delay_ms(5);
				tx_char(out[1]);
				_delay_ms(5);
				tx_char(out[2]);
			}
		}

		//receive temperature
		if (UCSR0A & (1 << RXC0)) {
			// character has been received
			if (rx_char() == '+') {
				rec[0] = rx_char();	//100's digit
				if (rec[0] == '0') {		//if 100's digit is 0
					rec[0] = rx_char();
					rec[1] = rx_char();
					rec[2] = 0;
				}
				else {		//else reset rec[0] to 10's digit
					rec[1] = rx_char();
					rec[2] = rx_char();
					rec[3] = 0;
				}
			}
			
			else if (rx_char() == '-') {
				rec[0] = '-';
				rec[1] = rx_char();
				if (rec[1] == '0') {		//if 100's digit is 0
					rec[1] = rx_char();
					rec[2] = rx_char();
					rec[3] = 0;
				}
				else {		//else reset rec[0] to 10's digit
					rec[2] = rx_char();
					rec[3] = rx_char();
					rec[4] = 0;
				}
			}
			moveto(13);
			stringout("   ");
			moveto(13);
			stringout(rec);
		}

		if (x < low) {
			PORTD |= (1 << PD3);
		}
		else {
			PORTD &= ~(1 << PD3);
		}
		if (x > high) {
			PORTD |= (1 << PD2);
		}
		else {
			PORTD &= ~(1 << PD2);
		}
		moveto(0x50);	//move cursor off of screen
	}
	return 0;
}