#include<stdio.h>
#include<string.h>
#include<avr/io.h>
#include<util/delay.h>
#include "uart.h"
void ADC_Init();
int read_adc();
unsigned int ADC_calculating();
unsigned int array[10];
unsigned int ADC_VALUE;
unsigned int value,temp;
void main()
{
_delay_ms(50);
UART_Init(25);
UART_TransmitString("Testing the room temperature:\r\n");
DDRB = 0x01;
PORTB= 0x01;
DDRB = 0x00;
PORTB= 0x00;
ADC_Init();
while(1)
{
if(bit_is_clear(PINB,0))
{
ADC_calculating();
while(bit_is_clear(PINB,0));
}
}
}
void ADC_Init()
{
ADCSRA = 0x00;
ADMUX = 0x40;
ADCSRA = 0x86;
}
int read_adc()
{
//select the ADC channel
ADMUX |= 0x01;
//START THE ADC CONVERATION
ADCSRA |= (1<<ADSC);
//WAIT FOR CONVERATION TO COMPLETE
while(!(ADCSRA) & (1<< ADIF));
//CLEAR ADIF BY WRITING ONE TO IT
ADCSRA |= 0x01;
return(ADC);
}
unsigned int ADC_calculating()
{
ADC_VALUE = read_adc();
itoa(ADC_VALUE, array, 10);
UART_TransmitString("ADC:");
UART_TransmitString(array);
UART_TransmitString("\r\n");
value = ADC_VALUE * 4.8875;
itoa(value, array, 10);
UART_TransmitString("Ref:");
UART_TransmitString(array);
UART_TransmitString("\r\n");
temp = value / 10;
itoa(temp, array, 10);
UART_TransmitString("temp:");
UART_TransmitString(array);
UART_TransmitString("\r\n");
UART_TransmitString("\n");
}