We are facing d problem with our ADC code. The code is used to switch ON the PORTC leds on detecting certain voltage level into channel 1 of ADC(PORTA), but when we run the program on hardware and test it, The LED glows when the analog input is switched off, rather than switch on. (It does not glow on switching on the ADC supply but glows when we switch off the supply)
Below is our code
#include "atmega32.h"
void set_adc()
{
ADMUX = 0x40;
ADCSRA = 0x82;
}
float get_value(unsigned int channel)
{
int a,b;
float fa;
ADMUX = channel;
ADCSRA |= 0x40; //start
while(!(ADCSRA & 0x10));
ADCSRA |= 0x10;
a = ADCL;
b = ADCH;
a = a | (b<<8);
//fa =(float) (a*5) /1023;
return a;
}
float get_value(unsigned int);
void main()
{
//unsigned int i;
int d;
DDRA = 0x00;
PORTA = 0x00;
PINA = 0x00;
DDRC = 0xff ;
PORTC = 0x00;
PINC = 0x00;
set_adc();
//PORTC = get_value();
//PINC = get_value();
d = get_value(1);
while (d<512);
PORTC = 0xFF;
PINC = 0x00;
DDRA = 0xff;
//PORTA = 0x00;
//PINA = 0x00;
//ADCSRA = 0x00;
//set_adc();
//d=get_value(2);
}
Thanks in advance
Below is our code
#include "atmega32.h"
void set_adc()
{
ADMUX = 0x40;
ADCSRA = 0x82;
}
float get_value(unsigned int channel)
{
int a,b;
float fa;
ADMUX = channel;
ADCSRA |= 0x40; //start
while(!(ADCSRA & 0x10));
ADCSRA |= 0x10;
a = ADCL;
b = ADCH;
a = a | (b<<8);
//fa =(float) (a*5) /1023;
return a;
}
float get_value(unsigned int);
void main()
{
//unsigned int i;
int d;
DDRA = 0x00;
PORTA = 0x00;
PINA = 0x00;
DDRC = 0xff ;
PORTC = 0x00;
PINC = 0x00;
set_adc();
//PORTC = get_value();
//PINC = get_value();
d = get_value(1);
while (d<512);
PORTC = 0xFF;
PINC = 0x00;
DDRA = 0xff;
//PORTA = 0x00;
//PINA = 0x00;
//ADCSRA = 0x00;
//set_adc();
//d=get_value(2);
}
Thanks in advance