EmbeddedSystems/EmbeddedSystemsTHM/adc.c

74 lines
1.4 KiB
C
Raw Normal View History

2021-12-17 22:26:41 +01:00
/*
* adc.c
*
* Created: 09/12/2021 16:31:16
* Author: n0x
*/
#include "adc.h"
void
adc_init(void)
{
ADMUX = (1<<REFS1)|
(0<<REFS0)| /* Set Voltage reference to 2.56V */
(0<<ADLAR) /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */
;
ADCSRA = (1<<ADEN)| /* Enable ADC */
(0<<ADATE)| /* Disable the ADC auto trigger */
(1<<ADIE)| /* Enable the ADC interrupt */
(1<<ADPS2)|
(1<<ADPS1)|
(1<<ADPS0) /* Set ADC Prescaler to 128 */
;
}
volatile int done = 1;
/* ADC 1 */
uint16_t
adc_get_poti(void)
{
uint16_t adc;
ADCSRA &= ~(1<<ADIE); /* Disable interrupt */
ADMUX |= (1<<MUX0); /* Set ADMUX to access ADC channel 1 */
ADCSRA |= (1<<ADSC); /* Start the conversion */
done = 0; /* reset the done flag to false */
while(done == 0); /* Wait till conversion completes */
adc = ADC;
ADCSRA |= (1<<ADIE); /* Enable the interrupt again */
return adc;
}
/* ADC 0 */
uint16_t
adc_get_LM35(void)
{
uint16_t adc;
ADCSRA &= ~(1<<ADIE); /* Disable interrupt */
ADMUX &= (0<<MUX0); /* Set ADMUX to access ADC channel 0 */
ADCSRA |= (1<<ADSC); /* Start the conversion */
done = 0; /* reset the done flag to false */
while(done == 0); /* Wait till conversion completes */
adc = ADC;
ADCSRA |= (1<<ADIE); /* Enable the interrupt again */
return adc;
}
ISR(ADC_vect){
done = 1; /* set the done flag to true */
}