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.
|
|
#include "adc.h"
static adc_t s_adc[] = {0}; /***********************************************************************************************************************
* =====================================================adc====================================================== ** ***********************************************************************************************************************/ // uint16_t meanvalue;
adc_t adc = {0}; void ADCInit(uint8_t *port, uint8_t pin) { // GPIO_PIN(A, 28);
ADC_InitStruType x; GPIO_InitSettingType y; y.Signal = GPIO_Pin_Signal_Analog; //�
y.Dir = GPIO_Direction_Input; //����
y.Func = GPIO_Reuse_Func0; // GPIO_Init(GPIO_Pin_B9, &y);
if (pin == 4) { GPIO_Init(GPIO_Pin_A4, &y); x.CHS = ADC_CHS_AIN8; } else if (pin == 8) { GPIO_Init(GPIO_Pin_B8, &y); x.CHS = ADC_CHS_AIN3; } else if (pin == 9) { GPIO_Init(GPIO_Pin_B9, &y); x.CHS = ADC_CHS_AIN4; }
//������Ƶ�Ժ�Tadclk=1/(PCLK/4)Լ����0.083us
x.CLKS = ADC_CLKS_PCLK; x.CLKDIV = ADC_CLKDIV_1_4; /* ADCʱ��ԴԤ��Ƶ */
//�ɼ�����ģ����*3.3/4096=���ڵĵ�ѹ
x.VREF_SEL = ADC_VREF_SEL_0; /* �ڲ��ο���ѹ2.048v,�����ڲ��ο���ѹΪ���� */ x.VREFP = ADC_VREFP_VDD; /* ѡ��оƬ�Ĺ�����ѹVDD��adc�IJο���ѹΪ���� */ 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 = 10; /* AD����ʱ��ѡ�� */ x.BITSEL = ADC_BITSEL_12; /* AD�ֱ���12λ */ 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 adc_loop_gather(void) { static uint32_t adc_ticket = 0; uint16_t adc_value = 0; if (port_haspassedms(adc_ticket) > 500) { adc_ticket = get_sys_ticket(); adc_value = get_adc_value(); record_adc_gather_value(adc_value); } }
void record_adc_gather_value(uint16_t adc_value) { /**
* @brief ��¼adc����ͨ���ɼ��������� * */ for (int i = 0; i < 3; i++) { s_adc[i].adc_value_buff[s_adc[i].adc_value_count++] = adc_value; if (s_adc[i].adc_value_count == 5) { s_adc[i].past_count_reached_five = true; s_adc[i].adc_value_count = 0; } if (s_adc[i].past_count_reached_five == true) { beg_average_value(s_adc[i].adc_value_buff); } } }
void beg_average_value(uint16_t *adc_buff) { uint16_t adc_average_value = 0; uint32_t adc_sum_value = 0; for (int i = 0; i < 5; i++) { adc_sum_value += *adc_buff; adc_buff++; } adc_average_value = adc_sum_value / 5; if (adc_average_value <= 16 || adc.abnormal_state == true) { adc.abnormal_state = true; process_voltage_abnormal(); // printf("short out%d\r\n", adc_average_value); //��·
} else { //����adc_value=1700����ѹ=1700/4096=1.369
printf("adc_value_t%d\r\n", adc_average_value); } }
|