pvvx
Активный участник сообщества
Основной кусок с ADC участвовавший в тесте:
Код:
/*
* adc.c test
*
* Created on: 19.11.2019
* Author: pvvx
*/
#include "../../proj/tl_common.h"
#include "../../proj/drivers/adc_8266.h"
#define ADC_AUTO_MODE_EN 1
void init_adc(void) {
/**set adc clock and enable adc clock**/
adc_SetClkFreq(ADC_CLK_4M);
WriteAnalogReg(0x88,0x0f);// select 192M clk output
WriteAnalogReg(0x05,0x60);// power on pll
WriteAnalogReg(0x06,0xfe);// power on sar
/**select the input channel**/
adc_AnaChSet(ADC_CHN_D5);
/**set the adc's mode**/
adc_AnaModeSet(SINGLEEND);
/**set the reference voltage**/
adc_RefVoltageSet(ADC_REF_VOL_1V3); // ADC_REF_VOL_1V3, ADC_REF_VOL_AVDD
/**set resolution**/
adc_ResSet(ADC_SAMPLING_RES_14BIT);
/**set sample cycle**/
adc_SampleTimeSet(ADC_SAMPLING_CYCLE_24);
#if ADC_AUTO_MODE_EN
reg_adc_period_chn0 = 1000; // CLK CPU / 1000 = 48 kHz
// enable auto mode
reg_adc_ctrl = FLD_ADC_CHNM_AUTO_EN | MASK_VAL(FLD_ADC_DONE_SIGNAL, ADC_DONE_SIGNAL_FALLING); // = 0x88
#else
// enable manual mode
reg_adc_ctrl = MASK_VAL(FLD_ADC_AUD_MODE, 2); // = 0x20
#endif
}
unsigned int cmd_adc(unsigned short *odata, unsigned char *idata) {
unsigned int i;
unsigned char smp_cycle = idata[1] & 7;
unsigned int smp_count = idata[2] | (idata[3] << 8);
if(smp_count > (USB_CDC_MAX_RX_BLK_SIZE - 4)/2)
smp_count = (USB_CDC_MAX_RX_BLK_SIZE - 4)/2;
odata[0] = 0x5a | (smp_cycle << 8);
odata[1] = (unsigned short)smp_count;
adc_SampleTimeSet(smp_cycle);
for(i=0; i < smp_count; i++){
#if ADC_AUTO_MODE_EN
while(!(reg_adc_chn0_input & 1));
while(reg_adc_chn0_input & 1);
odata[i+2] = reg_adc_dat_byp_outp & 0x3FFF;// read data
#else
reg_adc_chn1_outp = FLD_ADC_CHN_MANU_START; // Set a run signal,start to run adc
sleep_us(5); // wait for data
odata[i+2] = reg_adc_dat_byp_outp & 0x3FFF;// read data
#endif
}
return ((smp_count + 2)<<1);
}