Professional Documents
Culture Documents
ADC & UART Functions
ADC & UART Functions
ADC & UART Functions
com
CC Dharmani
ADC_ENABLE
ADCSRA |= (1<<ADEN)
ADC_DISABLE
ADCSRA &= 0x7F
ADC_START_CONVERSION
ADCSRA |= (1<<ADSC)
TX_NEWLINE
{transmitByte(0x0d); transmitByte(0x0a);}
void ADC_init(void);
int ADC_read(void);
float ADC_calculateTemp(int);
unsigned char* updateTempDisplay(float);
unsigned char* temporary(void);
void ADC_transmitValue(void);
ww.dharmanitech.com
CC Dharmani
//***************************************************************
// Initialize the ADC
//***************************************************************
void ADC_init(void)
{
ADCSRA = 0x00; //disable adc
ADMUX = 0x40; //select adc input 0
ADCSRA = 0x86;
}
//******************************************************************
// Do a Analog to Digital Conversion
//******************************************************************
int ADC_read(void)
{
char i;
int ADC_temp, ADCH_temp;
int ADC_var = 0;
ADC_ENABLE;
ADC_START_CONVERSION;
ADCSRA|=(1<<ADIF);
for(i=0;i<8;i++)
{
ADC_START_CONVERSION;
while(!(ADCSRA & 0x10));
ADCSRA|=(1<<ADIF);
ADC_temp = ADCL;
// read out ADCL register
ADCH_temp = ADCH;
// read out ADCH register
ADC_temp +=(ADCH_temp << 8);
ADC_var += ADC_temp;
// accumulate result (8 samples) for
//later averaging
}
ADC_var = ADC_var >> 3;
ADC_DISABLE;
return ADC_var;
}
2 of 5
ww.dharmanitech.com
CC Dharmani
//*******************************************************************
//To calculate Voltage
//*******************************************************************
float ADC_calculateValue(int inputValue)
{
float actualValue;
actualValue=(inputValue * Vref/1024.0);
//calculates the voltage present
return actualValue;
}
//******************************************************************
//To update the valueDisplay string based on the latest voltage read
//******************************************************************
unsigned char* updateDisplay(float actualValue)
{
int temp;
unsigned char c;
temp=(int)(actualValue*100.0);
ww.dharmanitech.com
CC Dharmani
value1 = ADC_calculateValue(value);
//following three functions are part of the UART routines, sending the
//voltage values to serial port. UART must be initialized before
//calling them
transmitString_F("
Channel ");
transmitByte(i | 0x30);
transmitString(updateDisplay(value1));
TX_NEWLINE;
}
}
//**************************************************************
//******** FUNCTIONS FOR SERIAL COMMUNICATION USING UART *******
//**************************************************************
//***************************************************
//Function to initialize UART
//***************************************************
//UART0 initialize
// desired baud rate: 19200
// actual: baud rate:19231 (0.2%)(at 16MHz crystal)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x33; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x98;
}
//**************************************************
//Function to receive a single byte
//*************************************************
unsigned char receiveByte( void )
{
unsigned char data, status;
while(!(UCSRA & (1<<RXC)));
status = UCSRA;
data = UDR;
return(data);
}
4 of 5
ww.dharmanitech.com
CC Dharmani
//***************************************************
//Function to transmit a single byte
//***************************************************
void transmitByte( unsigned char data )
{
while ( !(UCSRA & (1<<UDRE)) )
;
//Wait for empty transmit buffer
UDR = data;
//Start transmition
}
//***************************************************
//Function to transmit a string stored in Flash
//***************************************************
void transmitString_F(const unsigned char* string)
{
while (*string)
transmitByte(*string++);
}
//***************************************************
//Function to transmit a string from RAM
//***************************************************
void transmitString(unsigned char* string)
{
while (*string)
transmitByte(*string++);
}
//********************
END
********************
5 of 5