A sample picture and pin diagram of LM35 |
In this example the system could only read the positive temperature value. Because I have some difficulty of working with negative voltage reading at this time.
LM35 connects to ADC0. There is no negative temperature reading here. |
Source code:
#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>
int main(void)
{ float voltage;
float temperature;
int _temperature;
char ssd[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,
0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
//PortC as output
DDRC=0xFF;
//PortD as output
DDRD=0xFF;
//PA0 or ADC0 as an analog input
DDRA=0;
//Turn on the ADC module
ADCSRA|=(1<<ADEN);
while (1)
{
//Start the conversion
ADCSRA|=(1<<ADSC);
//Wait for the completion
while((ADCSRA&(1<<ADSC))==1);
/*Convert ADC to voltage*/
voltage=(ADCL+(ADCH<<8))*5.0/1024;
/*Convert to temperature*/
temperature=voltage*100;
/*make dot point*/
temperature*=10;
_temperature=(int)temperature;
/*Displaying the Data*/
PORTD=0x00;
PORTC=ssd[_temperature/1000];
if(_temperature>=1000) PORTD=0x01;
_delay_ms(10);
PORTD=0x00;
PORTC=ssd[(_temperature%1000)/100];
PORTD=0x02;
_delay_ms(10);
PORTD=0x00;
PORTC=ssd[(_temperature%100)/10]|0x80;
PORTD=0x04;
_delay_ms(10);
PORTD=0x00;
PORTC=ssd[_temperature%10];
PORTD=0x08;
_delay_ms(10);
}
}
Click here to download its source file.
No comments:
Post a Comment