|
@ -1,5 +1,9 @@ |
|
|
#include "adc.h" |
|
|
#include "adc.h" |
|
|
static adc_t s_adc[] = {0}; |
|
|
|
|
|
|
|
|
static adc_t s_adc[] = { |
|
|
|
|
|
{.port = "A", .pin = 2}, |
|
|
|
|
|
{.port = "B", .pin = 8}, |
|
|
|
|
|
{.port = "B", .pin = 9}, |
|
|
|
|
|
}; |
|
|
/*********************************************************************************************************************** |
|
|
/*********************************************************************************************************************** |
|
|
* =====================================================adc====================================================== |
|
|
* =====================================================adc====================================================== |
|
|
** |
|
|
** |
|
@ -14,15 +18,18 @@ void ADCInit(uint8_t *port, uint8_t pin) { |
|
|
y.Dir = GPIO_Direction_Input; //输入 |
|
|
y.Dir = GPIO_Direction_Input; //输入 |
|
|
y.Func = GPIO_Reuse_Func0; |
|
|
y.Func = GPIO_Reuse_Func0; |
|
|
// GPIO_Init(GPIO_Pin_B9, &y); |
|
|
// GPIO_Init(GPIO_Pin_B9, &y); |
|
|
if (pin == 4) { |
|
|
|
|
|
GPIO_Init(GPIO_Pin_A4, &y); |
|
|
|
|
|
|
|
|
if (pin == 2) { |
|
|
|
|
|
GPIO_Init(GPIO_Pin_A2, &y); |
|
|
x.CHS = ADC_CHS_AIN8; |
|
|
x.CHS = ADC_CHS_AIN8; |
|
|
|
|
|
printf("A4\r\n"); |
|
|
} else if (pin == 8) { |
|
|
} else if (pin == 8) { |
|
|
GPIO_Init(GPIO_Pin_B8, &y); |
|
|
GPIO_Init(GPIO_Pin_B8, &y); |
|
|
x.CHS = ADC_CHS_AIN3; |
|
|
x.CHS = ADC_CHS_AIN3; |
|
|
|
|
|
printf("B8\r\n"); |
|
|
} else if (pin == 9) { |
|
|
} else if (pin == 9) { |
|
|
GPIO_Init(GPIO_Pin_B9, &y); |
|
|
GPIO_Init(GPIO_Pin_B9, &y); |
|
|
x.CHS = ADC_CHS_AIN4; |
|
|
x.CHS = ADC_CHS_AIN4; |
|
|
|
|
|
printf("B9\r\n"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//经过分频以后Tadclk=1/(PCLK/4)约等于0.083us |
|
|
//经过分频以后Tadclk=1/(PCLK/4)约等于0.083us |
|
@ -43,24 +50,26 @@ void ADCInit(uint8_t *port, uint8_t pin) { |
|
|
ADC_Init(&x); |
|
|
ADC_Init(&x); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
uint16_t get_adc_value(void) { |
|
|
|
|
|
|
|
|
|
|
|
uint16_t adc_value; |
|
|
|
|
|
ADC_SoftStart(); |
|
|
|
|
|
while (ADC_GetConvStatus() == SET) |
|
|
|
|
|
; //正在转换 |
|
|
|
|
|
adc_value = ADC_GetConvValue(); |
|
|
|
|
|
ADC_SoftStop(); |
|
|
|
|
|
return adc_value; |
|
|
|
|
|
|
|
|
void get_adc_value(void) { |
|
|
|
|
|
uint16_t adc_value = 0; |
|
|
|
|
|
for (int i = 0; i < 3; i++) { |
|
|
|
|
|
ADCInit(s_adc[i].port, s_adc[i].pin); |
|
|
|
|
|
ADC_SoftStart(); |
|
|
|
|
|
while (ADC_GetConvStatus() == SET) |
|
|
|
|
|
; //正在转换 |
|
|
|
|
|
s_adc[i].adc_just_now_gather_val = ADC_GetConvValue(); |
|
|
|
|
|
ADC_SoftStop(); |
|
|
|
|
|
record_adc_gather_value(s_adc[i].adc_just_now_gather_val); |
|
|
|
|
|
//printf("%d\r\n",s_adc[i].adc_just_now_gather_val); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void adc_loop_gather(void) { |
|
|
void adc_loop_gather(void) { |
|
|
static uint32_t adc_ticket = 0; |
|
|
static uint32_t adc_ticket = 0; |
|
|
uint16_t adc_value = 0; |
|
|
|
|
|
|
|
|
|
|
|
if (port_haspassedms(adc_ticket) > 500) { |
|
|
if (port_haspassedms(adc_ticket) > 500) { |
|
|
adc_ticket = get_sys_ticket(); |
|
|
adc_ticket = get_sys_ticket(); |
|
|
adc_value = get_adc_value(); |
|
|
|
|
|
record_adc_gather_value(adc_value); |
|
|
|
|
|
|
|
|
get_adc_value(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|