mirror of https://github.com/ARMmbed/mbed-os.git
Modify some processing of AnalgIn.
Modifies are as below. - Change the A/D conversion time to run fast. - Change the macro value of ANALOGIN_MEDIAN_FILTER 1 -> 0pull/875/head
parent
83712b1f83
commit
44b4fb4b41
|
|
@ -22,20 +22,18 @@
|
|||
#include "adc_iodefine.h"
|
||||
#include "cpg_iodefine.h"
|
||||
|
||||
#define ANALOGIN_MEDIAN_FILTER 1
|
||||
|
||||
#define ADC_12BIT_RANGE 0xFFF
|
||||
#define ANALOGIN_MEDIAN_FILTER 0
|
||||
|
||||
static const PinMap PinMap_ADC[] = {
|
||||
{P1_8, AN0, 1},
|
||||
{P1_9, AN1, 1},
|
||||
{P1_10, AN2, 1},
|
||||
{P1_11, AN3, 1},
|
||||
{P1_12, AN3, 1},
|
||||
{P1_12, AN3, 1},
|
||||
{P1_13, AN5, 1},
|
||||
{P1_14, AN5, 1},
|
||||
{P1_14, AN5, 1},
|
||||
{P1_15, AN7, 1},
|
||||
{NC, NC, 0}
|
||||
{NC, NC, 0}
|
||||
};
|
||||
|
||||
static volatile uint16_t *ADCDR[] = {
|
||||
|
|
@ -49,45 +47,41 @@ static volatile uint16_t *ADCDR[] = {
|
|||
&ADCADDRH,
|
||||
};
|
||||
|
||||
#define ADC_RANGE ADC_12BIT_RANGE
|
||||
|
||||
void analogin_init(analogin_t *obj, PinName pin) {
|
||||
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
||||
MBED_ASSERT(obj->adc != (ADCName)NC);
|
||||
|
||||
|
||||
CPGSTBCR3 &= ~(1 << 1);
|
||||
CPGSTBCR6 &= ~(1 << 7);
|
||||
|
||||
// 000_0 000_1 00_00 0_xxx
|
||||
// 15: ADFlag 14: IntEn 13: start, [12:9] Triger..0
|
||||
// [8:6] CLK 100 :: 12-bit 1054tclk
|
||||
// [5:3] scanmode 000 :: single mode
|
||||
// [2:0] channel select
|
||||
ADCADCSR = 0x01c0 ;
|
||||
|
||||
for (int i = 0; i< sizeof(PinMap_ADC)/sizeof(PinMap); i++) {
|
||||
pinmap_pinout(PinMap_ADC[i].pin, PinMap_ADC);
|
||||
}
|
||||
// 15: ADF 14: ADIE 13: ADST, [12:9] TRGS..0
|
||||
// [8:6] CKS 000 :: 256tclk
|
||||
// [5:3] MDS 000 :: single mode
|
||||
// [2:0] CH 000 :: AN0
|
||||
ADCADCSR = 0x0000;
|
||||
|
||||
//pinmap_pinout(pin, PinMap_ADC);
|
||||
pinmap_pinout(pin, PinMap_ADC);
|
||||
}
|
||||
|
||||
static inline uint32_t adc_read(analogin_t *obj) {
|
||||
volatile uint16_t data;
|
||||
|
||||
// Select the appropriate channel and start conversion
|
||||
|
||||
ADCADCSR &= 0xfff8;
|
||||
ADCADCSR |= (1 << 13 | (obj->adc&0x7));
|
||||
|
||||
// Repeatedly get the sample data until DONE bit
|
||||
#define nothing
|
||||
while ((ADCADCSR & (1 << 15)) == 0 || (ADCADCSR & (1<<13)) != 0) nothing;
|
||||
|
||||
ADCADCSR |= (1 << 13 | (obj->adc & 0x7));
|
||||
|
||||
// Wait end of conversion
|
||||
do {
|
||||
data = ADCADCSR;
|
||||
} while (((data & (1 << 15)) == 0) || ((data & (1 << 13)) != 0));
|
||||
|
||||
// clear flag
|
||||
ADCADCSR &= ~(1 << 15);
|
||||
|
||||
return ((*(ADCDR[obj->adc]))>>4) & ADC_RANGE; // 12 bit
|
||||
|
||||
return ((*(ADCDR[obj->adc])) >> 4) & 0x0FFF; // 12 bits range
|
||||
}
|
||||
|
||||
#if ANALOGIN_MEDIAN_FILTER
|
||||
static inline void order(uint32_t *a, uint32_t *b) {
|
||||
if (*a > *b) {
|
||||
uint32_t t = *a;
|
||||
|
|
@ -95,6 +89,7 @@ static inline void order(uint32_t *a, uint32_t *b) {
|
|||
*b = t;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline uint32_t adc_read_u32(analogin_t *obj) {
|
||||
uint32_t value;
|
||||
|
|
@ -114,12 +109,12 @@ static inline uint32_t adc_read_u32(analogin_t *obj) {
|
|||
|
||||
uint16_t analogin_read_u16(analogin_t *obj) {
|
||||
uint32_t value = adc_read_u32(obj);
|
||||
|
||||
return value;
|
||||
//(value << 4) | ((value >> 8) & 0x000F); // 12 bit
|
||||
|
||||
return (value << 4) | ((value >> 8) & 0x000F); // 12-bit to 16-bit conversion
|
||||
}
|
||||
|
||||
float analogin_read(analogin_t *obj) {
|
||||
uint32_t value = adc_read_u32(obj);
|
||||
return (float)value * (1.0f / (float)ADC_RANGE);
|
||||
|
||||
return (float)value * (1.0f / (float)0x0FFF); // 12 bits range
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue