コード例 #1
0
ファイル: twi.c プロジェクト: rasmusto/ece473
uint16_t rd_temp()
{
    /*
    TWCR = TWCR_START;           //send start condition

    TWDR = LM73_READ;            //send device addr, read bit set
    TWCR = TWCR_RACK;                 //receive data byte, return ACK

    while(!(TWCR & (1<<TWINT))){}     //wait for data byte to come in
    if(TW_STATUS != TW_MR_DATA_ACK){return(1);}  //byte 1 read failure  
    int lm73_temp_high = TWDR;            //store temp high byte 

    TWCR = TWCR_RNACK;                //recv temp low byte, return NACK 
    while(!(TWCR & (1<<TWINT))){}     //wait for data byte to come in 
    if(TW_STATUS != TW_MR_DATA_NACK){return(2);} //byte 2 read failure
    int lm73_temp_low = TWDR;             //store temp low byte 
    TWCR = TWCR_STOP;                 //conclude transaction
    return(lm73_temp_low + (lm73_temp_high<<8));                        //return success value
    */
    TWCR = TWCR_START; //send start condition
	while (!(TWCR & (1<<TWINT))); //wait for start condition to transmit
	if((TWSR & 0xF8) != TW_START){
		string2lcd("ERROR1"); //check start status
	}
	TWDR = LM73_READ; //send device addr, read bit set
	TWCR = (1<<TWINT)|(1<<TWEN);
	while (!(TWCR & (1<<TWINT)));
	if((TWSR & 0xf8) != TW_MR_SLA_ACK){
		string2lcd("ERROR2");
	}
	TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);//enable TWI and ACK
	while(!(TWCR & (1<<TWINT)));
	uint8_t indoor1= TWDR;//assign byte read to variable
	if((TWSR & 0xF8) != TW_MR_DATA_ACK){
	}
	TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);//enable TWI and ACK
	while(!(TWCR & (1<<TWINT)));
	uint8_t indoor2 = TWDR;//assign second byte read to variable
	if((TWSR & 0xF8) != TW_MR_DATA_ACK){
	}
	TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);//stop transmission
    return ((((indoor1 << 8) + indoor2) >> 5));
}   
コード例 #2
0
ファイル: mega48_lab5.c プロジェクト: Lars139/ECE473
int main ()
{     
   uint16_t lm73_temp;  //a place to assemble the temperature from the lm73

   spi_init();   //initalize SPI 
   lcd_init();   //initalize LCD (lcd_functions.h)
   init_twi();   //initalize TWI (twi_master.h)  

   lm73_resolution_config();

   //set LM73 mode for reading temperature by loading pointer register
   //this is done outside of the normal interrupt mode of operation 
   //load lm73_wr_buf[0] with temperature pointer address
   lm73_wr_buf[0] = LM73_PTR_TEMP; 
   //start the TWI write process (twi_start_wr())
   twi_start_wr(LM73_ADDRESS, lm73_wr_buf, 1); 

   //enable interrupts to allow start_wr to finish
   sei();             

   clear_display();   //clean up the display

   while(1){          //main while loop
      _delay_ms(100);  //tenth second wait
      clear_display(); //wipe the display

      //read temperature data from LM73 (2 bytes)  (twi_start_rd())
      twi_start_rd(LM73_ADDRESS, lm73_rd_buf, 2); 
      _delay_ms(2);    //wait for it to finish
      //now assemble the two bytes read back into one 16-bit value
      lm73_temp = lm73_rd_buf[0]; //save high temperature byte into lm73_temp
      lm73_temp <<= 8; //shift it into upper byte 
      lm73_temp |= lm73_rd_buf[1]; //"OR" in the low temp byte to lm73_temp 
      itoa(lm73_temp, lcd_string_array, 2); //convert to string in array with itoa() from avr-libc                           
      string2lcd(lcd_string_array); //send the string to LCD (lcd_functions)
   } //while
} //main