STM32F103 DAC和ADC不会同时运行

时间:2016-03-09 11:35:32

标签: c arm stm32 adc digital-analog-converter

我设置了一个线程,可以不断读取ADC1和ADC3上的18个ADC并将它们填充到阵列中。

void TSK_AdcSensor(void const *argument){

ADC1_Configuration();
ADC3_Configuration();

//Clear the ADC value registers
for (i=0; i<MAX_ADC1_CHANNELS; i++){
    adc1Sensor[i] = 0;
}  

for (i=0; i<MAX_ADC3_CHANNELS; i++){
    adc3Sensor[i] = 0;
}

while(1){
    //Start ADC1 software conversion
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);

    //Start ADC3 software conversion
    ADC_SoftwareStartConvCmd(ADC3, ENABLE);

    //wait for DMA to finish:
    result1 = osSignalWait(ADC1FINISHED, osWaitForever);
    result2 = osSignalWait(ADC3FINISHED, osWaitForever);


    for(i=0; i<MAX_ADC1_CHANNELS; i++){
        adc1Sensor[i] += ADC1_SensorValues[i];
    }

    for(j=0; j<MAX_ADC3_CHANNELS; j++){
        adc3Sensor[j] += ADC3_SensorValues[j];
    }


    ADCcounter++;


    if(ADCcounter>=100){
        for(i=0; i<MAX_ADC1_CHANNELS; i++){
            adcSensor[i]=(float)adc1Sensor[i]/ADCcounter;
            adc1Sensor[i]=0;
            //printf("\n\radc1[%i]=%.1f\n\r",i,adcSensor[i]);
        }
        for(j=0; j<MAX_ADC3_CHANNELS; j++){
            adcSensor[j+MAX_ADC1_CHANNELS]=(float)adc3Sensor[j]/ADCcounter;
            adc3Sensor[j]=0;
            //printf("\n\radc3[%i]=%.1f\n\r",j,adcSensor[j+MAX_ADC1_CHANNELS]);
        }
        ADCcounter=0;
    }

    osDelay(5);

}//end while(1)
}

我还有一个功能,可以在收到usart所需的设置时设置两个输出DAC通道之一

void RegSetDAC(u8 reg_select, u16 reg_value)
{  
DAC_Configuration();

if(reg_select == 1)
{
    if(reg_value > 1250)
    printf("\n\r *E*0** regulator 1 out of range\n\r");

    else
    {                    
        dac_value1=0;  
        dac_value1=EPC1(reg_value);
        osDelay(100);
        // Set DAC Channel2 DHR12Rregister
        DAC_SetChannel2Data(DAC_Align_12b_R,  dac_value1);
        // Start DAC Channel2 conversion by software
        DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE); 
        printf("*E*%u*%u**\t\n\r  " ,reg_select,reg_value);
    }
}

else if (reg_select == 2)
{  
    if(reg_value > 100)
        printf("\n\r *E*0** regulator 2 out of range\n\r");

    else
    {
            dac_value2=0;
        dac_value2=EPC2(reg_value);
        osDelay(100);

        // Set DAC Channel1 DHR12R register
        DAC_SetChannel1Data(DAC_Align_12b_R, dac_value2);
        // Start DAC Channel1 conversion by software
            DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);
            printf("*E*%u*%u**\t\n\r  " ,reg_select,reg_value);
        }
    }  
//      printf("*%f*%f**\t\n\r  " ,dac_value1,dac_value2);
}

dac功能完美运行,直到我启用线程TSK_AdcSensor 然后dac输出将给出无意义的值。

通过消除过程,我发现了评论:

// ADC1_Configuration();
// ADC3_Configuration();

//Start ADC1software conversion
//  ADC_SoftwareStartConvCmd(ADC1,ENABLE);

//Start ADC3 softwareconversion
//  ADC_SoftwareStartConvCmd(ADC3, ENABLE);

让dac正常工作。但我不明白为什么?

设置DAC通道后printf显示正确的值,但输出端的电压读数不对应。

任何人都有任何想法?

DAC配置

  void DAC_Configuration(void)
{
    DAC_InitTypeDef DAC_InitStructure;


    dac_GPIO();
    dac_RCC();

    //DAC channel1 Configuration (Digital Regulator 2)
    DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software;
    DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
    DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
    DAC_Init(DAC_Channel_1, &DAC_InitStructure); 

    //DAC channel2 Configuration (Digital Regulator 1)
    DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software;
    DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
    DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
    DAC_Init(DAC_Channel_2, &DAC_InitStructure); 

//  // Enable DAC Channel1 reg 2
    DAC_Cmd(DAC_Channel_1, ENABLE);
    // Enable DAC Channel2 reg 1
    DAC_Cmd(DAC_Channel_2, ENABLE); 

  // Set default analog output as zero */
  DAC_SetChannel2Data(DAC_Align_12b_R, 0x0000);  // PA5 
    //  // Set default analog output as zero */
  DAC_SetChannel1Data(DAC_Align_12b_R, 0x0000);  // PA4     
}

0 个答案:

没有答案