#include <stdint.h>
#define __ATmega2560__
#include <avr/io.h>
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/interrupt.h>
#include <util/delay.h>
#include "usart.h"
void ADC_configura(void)
{
ADCSRA = 0x87; // ADEN=1 ADSC=0 ADATE=0 ADIE=0 ADIF=0 ADPS[2:0]=111
ADCSRB = 0x00; // bit[7]=reservado=0 ACME=0 bits[5:4]=reservado=0 MUX[5]=0 ADTS[2:0]=000
ADMUX = 1; // tensão referencial 1,1V
}
uint16_t ADC_medida(void)
{
ADCSRA |= (1 << ADSC); // fazendo ADSC=1 em ADCSRA, iniciamos uma nova conversão
while (ADCSRA & (1 << ADSC)) // enquanto ADSC=1, a conversão está em andamento
;
return ADC; // retorna o valor do canal convertido
}
void transmite_ADC_tensao(uint16_t leitura, float vref)
{
}
int main(void)
{
uint16_t leitura;
ADC_configura(); // configuramos o conversor AD
USART0_configura(); // configuramos a USART0
while (1) //
{
leitura = ADC_medida();// realizamos uma leitura do conversor
transmite_ADC(leitura);// transmitimos pela UART0 o valor hexadecimal de ADC
_delay_ms(2000);// esperamos 200 ms
}
return 0;
}