Skip to content

Instantly share code, notes, and snippets.

@prasant1010
Created August 24, 2016 06:44
Show Gist options
  • Save prasant1010/d1f6ee928a39e549d1007a8d4aae0bae to your computer and use it in GitHub Desktop.
Save prasant1010/d1f6ee928a39e549d1007a8d4aae0bae to your computer and use it in GitHub Desktop.
#ifndef F_CPU
# define F_CPU 1000000UL
#endif
#include<avr/io.h>
#include<util/delay.h>
void digital_value(int);
void ADC_initialization(void);
unsigned int ADC_read(unsigned int );
int main(void)
{
unsigned int value;
DDRC=0xff;
ADC_initialization(); // Initialization of ADC
while(1)
{
value=ADC_read(0);      // channel 0;
digital_value(value);
}
}
void ADC_initialization(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 int x)
{
ADMUX|=x; //selecting cannel
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 digital_value(int value)
{
if(value==0)
{
PORTC=0x00;
}
if ((value>0) &  (value < 100))
{
PORTC=0x01;
}
{
if ((value >=100 )& (value < 200))
PORTC =0x02;
}
if ((value >=200) & (value <300))
{
PORTC=0x03;
}
if ((value>=300) & (value < 400) )
{
PORTC =0x04;
}
if( (value >=400) & (value <500))
{
PORTC=0x05;
}
if (( value >=500) &( value <600) )
{
PORTC =0x06;
}
if ( (value >=600) & (value <700) )
{
PORTC =0x07;
}
if ( (value >=700) & (value <800) )
{
PORTC =0x08;
}
if ( (value >=800) & (value <900) )
{
PORTC =0x09;
}
if ( (value >=900) & (value <1024) )
{
PORTC =0x00;
_delay_ms(250);
PORTC=0X08;
_delay_ms(250);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment