#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "buttondebounce.h"
unsigned char n,p;
unsigned int Red=0,Yellow=0,Blue=0;
void DisplayDigits(unsigned int n)
{
unsigned char x[]={~0x3f,~0x06,~0x5b,~0x4f,~0x66,~0x6d,~0x7d,~0x07,~0x7f,~0x67};
unsigned char B,C,D;
unsigned int A;
A=n/100; //Quotient 251/100==> 2 <==
B=n%100; //Remainder 251%100==> 51 X
C=B/10; //Quotient 51/10==> 5 <==
D=B%10; //Remainder 51%10==> 1 <==
PORTD=1<<PD0;
PORTB=x[A];
_delay_ms(8);
PORTD=1<<PD1;
PORTB=x[C];
_delay_ms(8);
PORTD=1<<PD2;
PORTB=x[D];
_delay_ms(8);
}
unsigned int read_adc(unsigned char adc_input)
{
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1);
// clear the channel bits and set the new channel
ADMUX = (ADMUX & 0xF8) | adc_input;
// Delay needed for the stabilization of the ADC input voltage
_delay_us(10);
// Start the AD conversion
ADCSRA |= 0x40;
ADMUX |= 0x40;
// Wait for the AD conversion to complete
while (ADCSRA & (1 << ADSC))
// return result
return ADCW;
}
int main( void )
{
Timer_Init();
DDRD = 0xf7; //0b11110111
PORTD = 0xff; //0b11111111
DDRB = 0xFF;
PORTB = 0x00;
DDRC = 0x00;
PORTC = 0x00;
n = 0;
p = 0;
while(1)
{
n = get_key_press( 8 );
if(n==8)
{
p++;
}
switch(p)
{
case 0:
PORTD = 1<<PD4;
Red=read_adc(0);
DisplayDigits(Red);
break;
case 1:
PORTD = 0<<PD4 | 1<<PD5;
Yellow=read_adc(1);
DisplayDigits(Yellow);
break;
case 2:
PORTD = 0<<PD4 | 0<<PD5 | 1<<PD6;
Blue=read_adc(2);
DisplayDigits(Blue);
break;
default:
p=0;
break;
}
}
}