EmbeddedSystems/EmbeddedSystemsTHM/adc.c

69 lines
1.3 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"
#define myADC ((ADC_t*)(0x78))
2021-12-17 22:26:41 +01:00
void
adc_init(void)
{
myADC->uiADLAR = 0; /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */
myADC->uiREFS = 0; /* Turn off reference Voltage */
2021-12-17 22:26:41 +01:00
2022-01-20 17:22:50 +01:00
myADC->uiADPS = 7; /* Set ADC Prescaler to 128 */
myADC->uiADIE = 1; /* Enable the ADC interrupt */
myADC->uiADATE = 0; /* Disable the ADC auto trigger */
myADC->uiADEN = 1; /* Enable ADC */
2021-12-17 22:26:41 +01:00
}
volatile int done = 1;
/* ADC 1 */
uint16_t
2021-12-17 22:26:41 +01:00
adc_get_poti(void)
{
uint16_t adc;
myADC->uiMUX = 1; /* Set ADMUX to access ADC channel 1 */
myADC->uiADSC = 1; /* Start the conversion */
2021-12-17 22:26:41 +01:00
done = 0; /* reset the done flag to false */
while(done == 0); /* Wait till conversion completes */
adc = myADC->uiADC;
2021-12-17 22:26:41 +01:00
return adc;
}
/* ADC 0 */
uint16_t
adc_get_LM35(void)
{
uint16_t adc;
//myADC->uiADIE = 0; /* Disable interrupt */
myADC->uiMUX = 0; /* Set ADMUX to access ADC channel 1 */
myADC->uiADSC = 1; /* Start the conversion */
2021-12-17 22:26:41 +01:00
done = 0; /* reset the done flag to false */
while(done == 0); /* Wait till conversion completes */
adc = myADC->uiADC;
2021-12-17 22:26:41 +01:00
//myADC->uiADIE = 1; /* Enable the interrupt again */
2021-12-17 22:26:41 +01:00
return adc;
}
ISR(ADC_vect){
done = 1; /* set the done flag to true */
}