#define F_CPU (12000000UL)
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <avr/interrupt.h>
void ADC_init(void);
unsigned int ADC_read(unsigned char);
int user_D_temp(void);
void seven_seg(unsigned int x);
int main(void)
{
DDRB.2 = 1;// is used for output resistor
DDRB.0 = 0; // temperature increase switch
DDRB.1 = 0; // temperature decrease switch
DDRC = 0xFF;
PORTC = 0x00; // 7 segment 1st and 2nd digit
DDRD=0xFF;
PORTD=0x00; // 7 segment 3rd digit
ADC_init();
unsigned int ADC_data;
unsigned int i,temp_D;
float t_duty,t1,t2,ADC_data1;
while(1)
{
lcd_clear();
ADC_data = ADC_read(0);
ADC_data1=ADC_data/4;
temp_D=user_D_temp();
t_duty = (1-(((ADC_data1)-temp_D)/2));
PORTB.2=1;
t1=10*t_duty;
for(i=0;i<t1;i++)
_delay_ms(1000);
PORTB.2=0;
t2=10*(1-t_duty);
for(i=0;i<t2;i++)
_delay_ms(1000);
//TODO:: Please write your application code
}
}
void ADC_init(void) // Initialization of ADC
{
ADMUX =(1<<REFS0); // internal 2.56v with external capacitor at AREF
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
// Enable ADC and set Prescaler division factor as 128`
}
unsigned int ADC_read(unsigned char ch)
{
ch= ch & 0b00000111; // channel must be b/w 0 to 7
ADMUX |= ch; // selecting channel
ADCSRA|=(1<<ADSC); // start conversion
while(!(ADCSRA & (1<<ADIF))); // waiting for ADIF, conversion complete
ADCSRA|=(1<<ADIF); // clearing of ADIF, it is done by writing 1 to it
return (ADC);
}
int user_D_temp(void)
{
unsigned int temp=0;
if(PORTB.0==1)
{
temp++;
seven_seg(temp);
}
else if(PORTB.1==1)
{
temp--;
seven_seg(temp);
}
else
temp=temp;
return(temp);
}
void seven_seg(unsigned int x)
{
unsigned int y,z;
y=x/100; //1st digit on lower nibble
PORTC &=(0x0F)&y;
y=x%100;
z=y/10; //2nd digit on higher nibble
PORTC |=(0xF0)|(z<<4);
z=y%10; //3rd digit on lower nibble
PORTD &=z;
}