47 lines
1.3 KiB
C
47 lines
1.3 KiB
C
#include "adc.h"
|
|
#include "derivative.h"
|
|
|
|
int16_t adc_buf[ADC_M];
|
|
uint16_t pADC = 0;
|
|
uint16_t cADC = 0;
|
|
uint8_t fADC = 0;
|
|
|
|
void adc0_Init() {
|
|
// Enable ADC0 and PORTE clock
|
|
SIM->SCGC6 |= SIM_SCGC6_ADC0_MASK;
|
|
SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK;
|
|
|
|
// USE AD0 (ADC0_SE4a)
|
|
PORTE->PCR[21] |= PORT_PCR_MUX(0x0);
|
|
|
|
ADC0->CFG1 = ADC_CFG1_ADLSMP(0x01) // Sample time 0 = Short, 1 = Long
|
|
| ADC_CFG1_MODE(0x01); // Conversion mode 00 = 8-bit, 01 = 12-bit, 10 = 10-bit, 11 = 16-bit
|
|
ADC0->SC3 = ADC_SC3_ADCO(0x01) // Continuous conversion enable
|
|
| ADC_SC3_AVGE(0x01) // Hardware average
|
|
| ADC_SC3_AVGS(0x01); // Hardware average 00 = 4 samples, 01 = 8, 10 = 16, 11 = 32
|
|
ADC0->SC1[0] = ADC_SC1_ADCH(0x04); // Input channel select, AD4a is selected as input
|
|
}
|
|
|
|
int16_t adc0_TR_DATA() {
|
|
while (!(ADC0->SC1[0] & ADC_SC1_COCO_MASK)) asm("nop");
|
|
int16_t ADC_Result = ADC0->R[0];
|
|
ADC0->SC1[0] &= ~ADC_SC1_COCO_MASK;
|
|
return ADC_Result;
|
|
}
|
|
|
|
static uint16_t lastPos = 0;
|
|
void adc0_read_buf(int16_t *data, uint16_t len) {
|
|
if (cADC > ADC_M) {
|
|
cADC = ADC_M;
|
|
lastPos = pADC;
|
|
}
|
|
while (len) {
|
|
if (cADC) {
|
|
*data++ = adc_buf[lastPos];
|
|
lastPos = (lastPos + 1) % ADC_M;
|
|
cADC--;
|
|
len--;
|
|
}
|
|
}
|
|
}
|