/*****************************************************
Chip type : ATmega8
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega8.h>
#include <delay.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>
// Voltage Reference: Int., cap. on AREF
#define ADC_VREF_TYPE ((1<<REFS1) | (1<<REFS0) | (0<<ADLAR))
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
// Wait for the AD conversion to complete
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCW;
}
float delta,adc;
char str[16]; //allocate on lcd
float Tref=23;
int Vo;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
void main(void)
{
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;
// Alphanumeric LCD initialization// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:// RS - PORTD Bit 0// RD - PORTD Bit 1// EN - PORTD Bit 2
// D4 - PORTD Bit 4// D5 - PORTD Bit 5// D6 - PORTD Bit 6// D7 - PORTD Bit 7// Characters/line: 16
lcd_init(16);
lcd_clear();
while (1)
{
delay_ms(100);
Vo = read_adc(0);
lcd_gotoxy(0,1);
lcd_putsf("T=");
lcd_gotoxy(2,1);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2)); // temp in kelvin
T = T - 273.15;
// adc = (T * 9.0)/ 5.0 + 32.0; c to fehrenhit
ftoa(T, 0x02, str);
lcd_puts(str);
lcd_gotoxy(7,1);
lcd_putchar(0xdf); // degree
lcd_putsf("C");
lcd_gotoxy(1,0);
lcd_putsf("Hello");
delta=(adc-Tref);
if ( adc >=Tref && delta>=0.3)//
{
PORTB.1=0; //turn heater off
}
else if (adc <=Tref && delta<=-0.3)//
{
PORTB.1=1 ; //turn heater on
}
}
}