#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <math.h>
#include <util/delay.h>
#define SEVEN_SEGMENT_PORT PORTC
#define SEVEN_SEGMENT_DDR DDRC
/*global variable declaration
*/
unsigned int values[12]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xC6,0x83};
//unsigned int values[11]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xC6};
unsigned int var1,num1,num2,num3,num4,var2,key1,key2;
uint16_t t;
uint16_t volt,scale,offset,final,ac;
// static uint8_t show_digit;
/*function declaration
*/
void ADC_init(void);
//void init_Ex2(void);
unsigned int ADC_read(unsigned char);
void init_Ex1(void);
int button1_is_pressed();
int button2_is_pressed();
/*main programm starts here*/
int main()
{
init_Ex1();
// init_Ex2();
sei();//Enable Global Interrupts
DDRB=0xff;
PORTB=0xff;
SEVEN_SEGMENT_DDR=0xff; //Turn off all segments
SEVEN_SEGMENT_PORT=0XFF;
DDRA=0x00;
PORTA=(1<<PA3)|(1<<PA4);
// DDRA=0xff;
// PORTA=(1<<PA3);
ADC_init();
key1=0;
key2=0;
while(1)
{
// ADC_init();
t=ADC_read(1);
//volt=(((5.000)*t)/1023)*1000;
// scale=t*.01955;
// final=((scale/2)-1);
if(button1_is_pressed()) {key1=1;}
if(key1==1)
{
scale=(((uint32_t)t)*((uint32_t)5000))/1023;
scale=((uint32_t)scale * 100)/75;
//key1=0;
}
if(button2_is_pressed()) {key2=1;}
if(key2==1)
{
scale=(((uint32_t)t)*((uint32_t)5000))/1023;
scale=((uint32_t)scale *150)/75;
// key2=0;
}
// }
}
}
void ADC_init(void) // Initialization of ADC
{
ADMUX=(1<<REFS0); // AVcc 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);
}
void init_Ex1(void)
{
TCCR0=0x02; // Prescaler = FCPU/1024
TIMSK|=(1<<TOIE0); //Enable Overflow Interrupt Enable
TCNT0=0; //Initialize Counter
// sei();//Enable Global Interrupts
// TIFR=0x01;
}
void init_Ex2(void)
{
TIMSK|=(1<<TOIE1); // enabled global and timer overflow interrupt;
TCCR1A= 0x00; // normal operation page 148 (mode0);
TCNT1=0x0000; // 16bit counter register
TCCR1B = 0x03; // start timer/ set clock
//sei();//Enable Global Interrupts
}
int button1_is_pressed()
{
if(!(PINA&(1<<PINA3)))
{
_delay_ms(250);
if(!(PINA&(1<<PINA3)))
return 1;
}
return 0;
}
int button2_is_pressed()
{
if(!(PINA&(1<<PINA4)))
{
_delay_ms(250);
if(!(PINC&(1<<PINA4)))
return 1;
}
return 0;
}
ISR(TIMER0_OVF_vect) //ISR rutine for sevensegment
{
static uint8_t show_digit;
var1=scale;
if (show_digit==0)
{
// write code to show digit 1
num1=scale%10; // this is the first digit
PORTB=(0<<PB0)|(0<<PB1)|(0<<PB2)|(0<<PB3)|(1<<PB4); // Turning ON Units place of display
SEVEN_SEGMENT_PORT=values[num1]; // Writing corresponding number on Units place
// _delay_us(450);//rt delay
}
else if (show_digit==1)
{
// write code to show digit 2
num2=(scale % 1000)/100;
PORTB=(0<<PB0)|(0<<PB1)|(0<<PB2)|(1<<PB3)|(0<<PB4); // Turning ON Tens place and Turning OFF Units place of seconds
SEVEN_SEGMENT_PORT=values[num2]+0x80; // Writing corresponding number on Tens place
// _delay_us(450);// Short delay
}
else if (show_digit==2)
{
// write code to show digit 3
num3=(scale % 10000)/1000;
PORTB=(0<<PB0)|(0<<PB1)|(1<<PB2)|(0<<PB3)|(0<<PB4); // Turning ON Units place of minutes // Turning ON Units place of minutes
SEVEN_SEGMENT_PORT=values[num3]; // Writing corresponding number on Units place of minutes
// _delay_us(450);
}
else if (show_digit==3)//&&(!(PINC&(1<<PINC1))))
{
PORTB=(0<<PB0)|(1<<PB1)|(0<<PB2)|(0<<PB3)|(0<<PB4); // Turning ON Units place of minutes
// Turning ON Tens place and Turning OFF Units place of minutes
num4=scale /10000;
SEVEN_SEGMENT_PORT=values[num4]; // Writing corresponding number on Tens place of minutes
}
else if (show_digit==4)
{
PORTB=(1<<PB0)|(0<<PB1)|(0<<PB2)|(0<<PB3)|(0<<PB4);
SEVEN_SEGMENT_PORT=values[0];
if(key1==1)
{
PORTB=(1<<PB0)|(0<<PB1)|(0<<PB2)|(0<<PB3)|(0<<PB4);
SEVEN_SEGMENT_PORT=values[10];
}
else if(key2==1)
{
PORTB=(1<<PB0)|(0<<PB1)|(0<<PB2)|(0<<PB3)|(0<<PB4);
SEVEN_SEGMENT_PORT=values[11];
}
}
if(++show_digit==5) {show_digit=0;}
}
/*
ISR(TIMER2_OVF_vect) {
//if(!(PINC&(1<<PINC1))&&(PINC&(1<<PINC2)))
if(button1_is_pressed())
{
PORTB=(1<<PB0);
SEVEN_SEGMENT_PORT=values[10];
// _delay_us(150);
}
else if(button2_is_pressed())
{
PORTB=(1<<PB0);
SEVEN_SEGMENT_PORT=values[11];
// _delay_us(150);
}
}*/