Microcontroller › AVR › avr adc reading to 7 segment › hi….udgisham give u one
November 16, 2011 at 6:10 am
#6841
Participant
hi….udgish
am give u one adc code..
but it is in atmega8.
in this project am sending AADC values to UART…..
i hope u undstd this code………
#define F_CPU 8000000UL
#include<avr/io.h>
#include<util/delay.h>
#include “lcd.h”
/*************************************************************
********** adc init ******
*************************************************************/
void InitADC()
{
ADMUX=(1<<REFS0);
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}
/************************************************
**** READING ADC BITS ***
************************************************/
uint16_t ReadADC(uint8_t ch)
{
ch=ch&0b00000111;
ADMUX =ch | (1<<REFS0);
ADCSRA|=(1<<ADSC);
while(!(ADCSRA & (1<<ADIF)));
ADCSRA|=(1<<ADIF);
return(ADC);
}
/****************************************************
***** UART*******************************************
****************************************************/
void uart_init()
{
// SETTING BIT PARITY (8BIT OR 9 BIT)
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); //8 BIT
//FOR TX AND RX
UCSRB=(1<<RXEN)|(1<<TXEN);// ENABLING TX,RX.
UBRRL=0x33; // baud rate(51 for 9600)
}
void tx_data(unsigned char c)
{
//SENDING DATA;
UDR=c;
while(!(UCSRA & (1<<TXC))); //set the TX flag
//clear the TX flag
UCSRA=(1<<TXC);
}
unsigned char rx_data()
{
// Wait for data to be received
while ( !(UCSRA & (1<<RXC)) ); // set the rx flag
UCSRA=(0<<RXC);// CLEAR THE rx flag
/* Get and return received data from buffer */
return UDR;
}
void Tx_String(unsigned char *str)
{
while(*str)
{
tx_data(*str);
str++;
}
}
/*******************************************************
**MAIN PROGRAM ** ** ** ** ** ** ** ** ** ** *
********************************************************/
void main()
{
int adc_result0;
uart_init();
InitADC();
InitLCD(LS_BLINK|LS_ULINE);
LCDWriteStringXY(0,0,”adc_test”);
while(1)
{
_delay_ms(1000);
ReadADC(0);
adc_result0=ReadADC(0);
LCDWriteIntXY(0,1,adc_result0,4);
tx_data(adc_result0);
}
}