eew_xQfw2M 发表于 2022-4-25 23:04

[AT-START-F425测评] ADC测电压

<p><strong>ADC</strong><br />
分辨率12 位,采样率最高 2MSPS,最多18 个通道,0~15为外部输入</p>

<p>转换时间:在最大时钟28MHz下,最快0.5 &mu;s</p>

<p>ADC输入范围:VSSA &le; VIN &le; VDDA</p>

<p>转换优先级:抢占通道优先级高于普通通道,即使普通通道正在转换中也会被抢占,在抢占通道转换完成后,再重新进行转换(是怕普通通道转换时间太长,耽误抢占通道的时效吗?)</p>

<p>内部参考电压:典型值 1.2V</p>

<p>&nbsp;</p>

<p><strong>操作流程</strong>: 先校准,以消除电容误差,再触发转换</p>

<p>&nbsp;</p>

<p><strong>触发源</strong>:</p>

<p><strong>转换时间</strong>:</p>

<p>用ADC_SPT1与ADC_SPT2的CSPTx 配置各个通道(ADC_INx)的采样周期&nbsp;</p>

<p>一次转换所需的时间(ADCCLK 的周期) = 采样时间+ 12.5</p>

<p>CSPTx 选择1.5周期,一次转换需要1.5+12.5=14个ADCCLK 周期。</p>

<p>&nbsp;</p>

<p><strong>转换模式:</strong></p>

<p><strong>序列模式:</strong></p>

<p></p>

<p><strong>抢占自动转换模式:</strong></p>

<p> &nbsp;</p>

<p><strong>反复模式:</strong></p>

<p>&nbsp;</p>

<p><strong>分割模式:</strong></p>

<p> &nbsp;</p>

<p><strong>数据获取:</strong></p>

<p>普通通道转换数据可通过CPU 或DMA 读取普通数据寄存器(ADC_ODT)获得。 多通道连续转换时,貌似只能通过DMA来读取数据?</p>

<p>抢占通道数据只可通过CPU读取抢占数据寄存器读取抢占数据寄存器xx((ADC_PDTxADC_PDTx)获得。</p>

<p>&nbsp;</p>

<p><strong>ADC channel 与 pin脚对应关系</strong></p>

<table style="border-collapse: collapse; width: 288px; height: 20px;" width="286">
        <colgroup>
                <col style="width:64pt" width="85" />
                <col style="width:37pt" width="49" />
                <col style="width:11pt" width="14" />
                <col style="width:62pt" width="82" />
                <col style="width:42pt" width="56" />
        </colgroup>
        <tbody>
                <tr>
                        <td class="xl65" style="border-bottom:1px solid black; height:18px; width:85px; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:1px solid black; border-right:1px solid black; border-left:1px solid black"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN0</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; width:49px; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:1px solid black; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PA0</span></span></span></span></span></span></td>
                        <td class="xl66" rowspan="8" style="border-bottom:.7px solid black; width:14px; text-align:center; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:1px solid black; border-right:1px solid black; border-left:1px solid black"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体"> </span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; width:82px; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:1px solid black; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN8</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; width:56px; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:1px solid black; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PB0</span></span></span></span></span></span></td>
                </tr>
                <tr>
                        <td class="xl65" style="border-bottom:1px solid black; height:18px; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN1</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PA1</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN9</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PB1</span></span></span></span></span></span></td>
                </tr>
                <tr>
                        <td class="xl65" style="border-bottom:1px solid black; height:18px; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN2</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PA2</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN10</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PC0</span></span></span></span></span></span></td>
                </tr>
                <tr>
                        <td class="xl65" style="border-bottom:1px solid black; height:18px; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN3</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PA3</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN11</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PC1</span></span></span></span></span></span></td>
                </tr>
                <tr>
                        <td class="xl65" style="border-bottom:1px solid black; height:18px; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN4</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PA4</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN12</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PC2</span></span></span></span></span></span></td>
                </tr>
                <tr>
                        <td class="xl65" style="border-bottom:1px solid black; height:18px; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN5</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PA5</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN13</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PC3</span></span></span></span></span></span></td>
                </tr>
                <tr>
                        <td class="xl65" style="border-bottom:1px solid black; height:18px; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN6</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PA6</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN14</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PC4</span></span></span></span></span></span></td>
                </tr>
                <tr>
                        <td class="xl65" style="border-bottom:1px solid black; height:18px; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:1px solid black"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN7</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PA7</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">ADC1_IN15</span></span></span></span></span></span></td>
                        <td class="xl65" style="border-bottom:1px solid black; padding-top:1px; padding-right:1px; padding-left:1px; vertical-align:bottom; white-space:nowrap; border-top:none; border-right:1px solid black; border-left:none"><span style="font-size:15px"><span style="color:black"><span style="font-weight:400"><span style="font-style:normal"><span style="text-decoration:none"><span style="font-family:宋体">PC5</span></span></span></span></span></span></td>
                </tr>
        </tbody>
</table>

<p>&nbsp;</p>

<p>------------------分割线---------------------</p>

<p>以下为2个普通通道的转换代码 ,通过DMA来获取ADC的转换数据</p>

<pre>
<code>__IO uint16_t adc1_ordinary_valuetab = {0};
__IO uint16_t dma_trans_complete_flag = 0;

void DMA1_Channel1_IRQHandler(void)
{
if(dma_flag_get(DMA1_FDT1_FLAG) != RESET)
{
    dma_flag_clear(DMA1_FDT1_FLAG);
    dma_trans_complete_flag = 1;
}
}


static void dma_config(void)
{
dma_init_type dma_init_struct;
crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE);
nvic_irq_enable(DMA1_Channel1_IRQn, 0, 0);
dma_reset(DMA1_CHANNEL1);

dma_flexible_config(DMA1, FLEX_CHANNEL1, DMA_FLEXIBLE_ADC1);

dma_default_para_init(&amp;dma_init_struct);
dma_init_struct.buffer_size = 2;
dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY;
dma_init_struct.memory_base_addr = (uint32_t)adc1_ordinary_valuetab;
dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_HALFWORD;
dma_init_struct.memory_inc_enable = TRUE;
dma_init_struct.peripheral_base_addr = (uint32_t)&amp;(ADC1-&gt;odt);
dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_HALFWORD;
dma_init_struct.peripheral_inc_enable = FALSE;
dma_init_struct.priority = DMA_PRIORITY_HIGH;
dma_init_struct.loop_mode_enable = FALSE;
dma_init(DMA1_CHANNEL1, &amp;dma_init_struct);

dma_interrupt_enable(DMA1_CHANNEL1, DMA_FDT_INT, TRUE);
dma_channel_enable(DMA1_CHANNEL1, TRUE);
}

static void gpio_config(void)
{
gpio_init_type gpio_initstructure;
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);

gpio_default_para_init(&amp;gpio_initstructure);
gpio_initstructure.gpio_mode = GPIO_MODE_ANALOG;
gpio_initstructure.gpio_pins = GPIO_PINS_4|GPIO_PINS_5;
gpio_init(GPIOA, &amp;gpio_initstructure);
}

void adc_config_ch(void)
{
    adc_base_config_type adc_base_struct;
    crm_periph_clock_enable(CRM_ADC1_PERIPH_CLOCK, TRUE);
    crm_adc_clock_div_set(CRM_ADC_DIV_6);

    gpio_config();
    dma_config();
    adc_base_default_para_init(&amp;adc_base_struct);
    adc_base_struct.sequence_mode = TRUE;
    adc_base_struct.repeat_mode = FALSE;
    adc_base_struct.data_align = ADC_RIGHT_ALIGNMENT;
    adc_base_struct.ordinary_channel_length = 2;
    adc_base_config(ADC1, &amp;adc_base_struct);
    adc_ordinary_channel_set(ADC1, ADC_CHANNEL_4, 1, ADC_SAMPLETIME_239_5);
    adc_ordinary_channel_set(ADC1, ADC_CHANNEL_5, 2, ADC_SAMPLETIME_239_5);
    adc_ordinary_conversion_trigger_set(ADC1, ADC12_ORDINARY_TRIG_SOFTWARE, TRUE);
   
    adc_dma_mode_enable(ADC1, TRUE);
    adc_enable(ADC1, TRUE);
    adc_calibration_init(ADC1);
    while(adc_calibration_init_status_get(ADC1));
    adc_calibration_start(ADC1);
    while(adc_calibration_status_get(ADC1));
}

void adc_channel_sample_dma()
{
    dma_trans_complete_flag = 0;
    dma_config();

    adc_ordinary_software_trigger_enable(ADC1, TRUE);
    while(dma_trans_complete_flag == 0);
}

void get_voltage()
{
    float v = 0.0f;
    uint16_t adc_val = 0;

    adc_channel_sample_dma();
   
    adc_val = adc1_ordinary_valuetab;
    v = (float)adc_val*3.3f/4095.0f;
    LCD_printf_line(6," %.3fV",v);
   
    adc_val = adc1_ordinary_valuetab;
    v = (float)adc_val*3.3f/4095.0f;
    LCD_printf_line(7," %.3fV",v);
}


void adc_test()
{
    adc_config_ch();
    get_voltage();
}</code></pre>

<p>&nbsp;</p>

<p>----------------------------------------------</p>

<p>最后</p>

<p>多通道转换时,一个通道转换完成后马上把数据拿出来,想通过CPU去把数据拿出来要怎么做呢?</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<table style="border-collapse:collapse; width:157px" width="157">
        <colgroup>
                <col style="width:64pt" width="85" />
        </colgroup>
</table>

lugl4313820 发表于 2022-4-26 08:58

可以用轮询,或者dma中断、或者用缓存。

freebsder 发表于 2022-4-26 16:36

<p>中断吧,到了马上处理,不然可能会丢数据。</p>
页: [1]
查看完整版本: [AT-START-F425测评] ADC测电压