You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.3 KiB
82 lines
2.3 KiB
#include "port.h"
|
|
#include "systick.h"
|
|
|
|
static ADC_InitStruType x;
|
|
static GPIO_InitSettingType y;
|
|
static bool adccapture_error = false;
|
|
|
|
static uint16_t prv_adc_get_value() {
|
|
ErrorStatus status = ADC_SoftStart();
|
|
adccapture_error = false;
|
|
if (status != SUCCESS) {
|
|
adccapture_error = true;
|
|
return 0;
|
|
}
|
|
uint32_t inticket = get_sys_ticket();
|
|
|
|
while (ADC_GetConvStatus() != RESET) {
|
|
if (port_haspassedms(inticket) > 10) {
|
|
adccapture_error = true;
|
|
return 0;
|
|
}
|
|
}
|
|
uint16_t adcv = ADC_GetConvValue();
|
|
ADC_SoftStop();
|
|
return adcv ;
|
|
}
|
|
|
|
static void prv_adc_pb9_init() {
|
|
y.Signal = GPIO_Pin_Signal_Analog; //模拟
|
|
y.Dir = GPIO_Direction_Input; //输入
|
|
y.Func = GPIO_Reuse_Func0;
|
|
// x.CHS设置adc采集的通道每个引脚对应一个通道
|
|
GPIO_Init(GPIO_Pin_B9, &y);
|
|
|
|
//经过分频以后Tadclk=1/(PCLK/32)
|
|
x.CLKS = ADC_CLKS_PCLK;
|
|
x.CLKDIV = ADC_CLKDIV_1_32; /* ADC时钟源预分频 */
|
|
|
|
//采集到的模拟量*3.3/4096=现在的电压
|
|
x.VREF_SEL = ADC_VREF_SEL_0; /* 内部参考电压2.048v,仅设置内部参考电压为多少 */
|
|
x.VREFP = ADC_VREFP_VDD; /* 选择芯片的工作电压VDD,这个是设置adc具体是要哪个电压来做参考adc的参考电压为多少 */
|
|
x.VREFN = ADC_VREFN_VSS; /* 负向参考电压选择 */
|
|
x.CHS = ADC_CHS_AIN4;
|
|
// x.CHS = ADC_CHS_AIN8;
|
|
x.SMPS = ADC_SMPS_SOFT; /* AD采样模式为软件控制 */
|
|
//采样时间st*2+1(个Tadclk)=1.743us
|
|
x.ST = 0; /* AD硬件采样时间选择 */
|
|
x.BITSEL = ADC_BITSEL_8; /* AD分辨率12位 */
|
|
ADC_Init(&x);
|
|
prv_adc_get_value();
|
|
}
|
|
|
|
extern uint32_t getCNT();
|
|
extern uint32_t getTOP();
|
|
|
|
uint16_t capture(int timeoff) {
|
|
static bool inited = false;
|
|
if (inited == false) {
|
|
prv_adc_pb9_init();
|
|
inited = true;
|
|
}
|
|
|
|
uint16_t expectCNTBegin = getTOP() / 100.0 * timeoff;
|
|
uint16_t expectCNTEnd = getTOP() / 100.0 * (timeoff + 1);
|
|
// printf("%d %d , %d\n", timeoff, expectCNTBegin, expectCNTEnd);
|
|
|
|
int icount = 0;
|
|
while (true) {
|
|
volatile uint16_t countnow = T16Nx_GetCNT1(T16N0);
|
|
// printf("%d\n",countnow);
|
|
for (size_t i = 0; i < icount % 10; i++) {
|
|
}
|
|
icount++;
|
|
|
|
if (countnow >= expectCNTBegin && countnow < expectCNTEnd) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
uint16_t v = prv_adc_get_value();
|
|
return v;
|
|
}
|