Friday, 16 September 2016

ADC Read

ADC Read:

Code:



#include <avr/io.h>
#define F_CPU 8000000ul
#include<util/delay.h>

char result[4];

void serial_init()
{
UCSRB=0x18; // 1<<TXEN | (1<<RXEN)
UCSRC=0x86;  // 8 bit select
UBRRL=0x33;  // 51   for 9600 boud rate at 8 MHz
}

void send(char item)
{
UDR=item;
while(!(UCSRA & (1<<UDRE)));
}

void txstring(char *str)
{
while(*str)
{
send(*str);
str++;
}
}

void adc_init()
{
ADMUX=0xC0;  // channel 0 and internal reference select
ADCSRA=0x87;   // adc enable, selects frequency
}

int main(void)
{
adc_init();
serial_init();
    while(1)
    {
      ADCSRA|=1<<ADSC;
 while(!(ADCSRA & (1<<ADIF)));
 sprintf(result,"%d",ADC);
 txstring(result);
 txstring("\r\n");
 _delay_ms(1000);
 ADCSRA|=1<<ADIF;
    }
}


Simulation:





0 comments:

Post a Comment

if you have any doubt please let me know