EmbeddedSystems/EmbeddedSystemsTHM/adc.c

106 lines
2.0 KiB
C

/*
* adc.c
*
* Created: 09/12/2021 16:31:16
* Author: n0x
*/
#include "adc.h"
typedef struct ADC_t ADC_t;
struct ADC_t{
/* ADC Data */
uint16_t uiADC :16;
/* ADCSRA */
uint8_t uiADPS0 :1;
uint8_t uiADPS1 :1;
uint8_t uiADPS2 :1;
uint8_t uiADIE :1;
uint8_t uiADIF :1;
uint8_t uiADATE :1;
uint8_t uiADSC :1;
uint8_t uiADEN :1;
/* ADCSRB */
uint8_t uiADTS0 :1;
uint8_t uiADTS1 :1;
uint8_t uiADTS2 :1;
uint8_t nui2 :3;
uint8_t uiACME :1;
uint8_t nui1 :1;
/* ADMUX */
uint8_t uiMUX :5;
uint8_t uiADLAR :1;
uint8_t uiREFS0 :1;
uint8_t uiREFS1 :1;
};
#define myADC ((ADC_t*)(0x78))
void
adc_init(void)
{
myADC->uiADLAR = 0;
myADC->uiREFS0 = 0; /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */
myADC->uiREFS1 = 0; /* Set Voltage reference to 2.56V */
myADC->uiADPS0 = 1; /* Set ADC Prescaler to 128 */
myADC->uiADPS1 = 1;
myADC->uiADPS2 = 1;
myADC->uiADIE = 1; /* Enable the ADC interrupt */
myADC->uiADATE = 0; /* Disable the ADC auto trigger */
myADC->uiADEN = 1; /* Enable ADC */
}
volatile int done = 1;
/* ADC 1 */
uint16_t
adc_get_poti(void)
{
uint16_t adc;
//myADC->uiADIE = 0; /* Disable interrupt */
myADC->uiMUX = 1; /* Set ADMUX to access ADC channel 1 */
myADC->uiADSC = 1; /* Start the conversion */
done = 0; /* reset the done flag to false */
while(done == 0); /* Wait till conversion completes */
adc = myADC->uiADC;
//myADC->uiADIE = 1; /* Enable the interrupt again */
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 */
done = 0; /* reset the done flag to false */
while(done == 0); /* Wait till conversion completes */
adc = myADC->uiADC;
//myADC->uiADIE = 1; /* Enable the interrupt again */
return adc;
}
ISR(ADC_vect){
done = 1; /* set the done flag to true */
}