Toll-Free Customer Support 24/7

ADC Interfacing with ATMEGA Code

# define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
//#include <iom16.h>

//#define LCD_PORT PORTB
#define RS PC0 // connect register select pin to PC0
#define EN PC1 // connect enable pin to PC1
#define LTHRES 500 //setting the threshold valve
#define RTHRES 500

#include <stdlib.h>

 

void CMD_WRT(unsigned char val)
{

PORTB=val; // initializing PORTB as input and passing valve onto it
PORTC = PORTC & (~(1<<RS));//make RS pin low
_delay_ms(1);
PORTC = PORTC | ((1<<EN));// make EN pin high
_delay_ms(1);
PORTC = PORTC & (~(1<<EN));// make EN pin low

}


void DATA_WRT(unsigned char ch)
{

PORTB = ch; //initializing PORTB as input and passing CMD onto it
PORTC = PORTC | ((1<<RS)); // make RS pin high
_delay_ms(1);
PORTC = PORTC | ((1<<EN)); //make EN pin high
_delay_ms(1);
PORTC = PORTC & (~(1<<EN));// make EN pin low
}
void LCD_WRT( char *string)
{
while(*string)
DATA_WRT(*string++);

}




// initialize adc
void adc_init()
{
// AREF = AVcc
ADMUX = (1<<REFS0); //initialize admux

// ADC Enable and prescaler of 128
// 16000000/128 = 125000
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
} //ADEN means ADC enabled

// read adc value
int adc_read(char ch)
{
// select the corresponding channel 0~7
// ANDing with '7' will always keep the value
// of 'ch' between 0 and 7
ch &= 0b00000111; // AND operation with 7
ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing

// start single conversion
// write '1' to ADSC
ADCSRA |= (1<<ADSC);

// wait for conversion to complete
// ADSC becomes '0' again
// till then, run loop continuously
while(ADCSRA & (1<<ADSC));

return (ADC);
}


int main(void)
{
uint16_t adc_result0;//, adc_result1;
char int_buffer[10]; //creating array of 10
unsigned char CMD[]={0x38,0x01,0x0f,0x06,0x80},TEMP1,i;
DDRB=0XFF;//set port b as output
DDRC = 0xFF;//(1<<RS)|(1<<EN);
_delay_ms(10);

for(i=0;i<5;i++)
{
TEMP1=CMD[i]; //for each one cycle each command will be placed in that cmd array
CMD_WRT(TEMP1);
}
adc_init();
while(1)
{
//adc_result0 = adc_read(0); // read adc value at PA0
adc_result0 = adc_read(1);

itoa(adc_result0, int_buffer,10);

CMD_WRT(0X01); //clear display
CMD_WRT(0X80); // cursor on first line
LCD_WRT(" --RDL--"); //display RDL
CMD_WRT(0XC0); //cursor on next line
LCD_WRT(int_buffer);

_delay_ms(1000);

//TODO:: Please write your application code
}
return 0;
}

Back to top