The book connects to the text and starts to toss about the DMA transmission of the ADC. Because everyone is talking about DMA, even the st example uses DMA.
The data collected by the ADC is stored in a fixed register. When multiple channels are sampled in the conventional sampling method, DMA can be used to avoid data loss. When the DMA function of the ADC is enabled, a DMA request is sent when each channel is converted. The DMA mode cannot completely avoid data loss. To avoid data loss, you must enable the overrun mode at the same time as the DMA mode. when the data is lost, the data conversion is stopped. We only need to check if there is any Ovr time to solve the problem caused by the loss of sample data. For example, channel dislocation or something.
In reference manual of ipvf4, we can find that the DMA ing of adc1 is on dma1, ch0, and stream0.
[Experiment 1. Single-channel data acquisition through DMA]
Configure the DMA initialization settings of adc1 as follows:
// DMA initialization
frequency = 4;
dma_initstructure.dma_channel = dma_channel_0;
frequency = frequency;
frequency = dma_1_omode_disable;
Destination = (uint32_t) & adcvalue1; // target data bit
Destination = dma_memoryburst_single;
Destination = dma_memorydatasize_halfword;
Destination = dma_memoryinc_disable;
dma_initstructure.dma_mode = dma_mode_circular;
Signature = adc1_base + 0x4c; // ADC-> Dr address
Signature = dma_peripheralburst_single;
Signature = signature;
queue = queue;
queue = dma_priority_high;
dma_init (dma2_stream0, & dma_initstructure);
dma_cmd (dma2_stream0, enable);
Enable DMA transmission in the ADC register. One of the two functions is to set the DDS bit of Cr2 so that DMA transmission is enabled every time the ADC data is updated;
The other is to set the DMA bit of the ADC Cr2 to enable the DMA transmission of the ADC.
Use the following two functions:
Adc_dmarequestafterlasttransfercmd (adc1, enable); // enable DMA transmission when the source data changes
Adc_dmacmd (adc1, enable); // enables DMA transmission of ADC
Finally, read the sample value of the ADC in the adcvalue. We can see that the Dr register of the ADC is not read using the adc_getconversionvalue function. The sample value of the ADC can still be output:
While (1)
{
For (I = 0; I <10000; I ++)
{
Sum + = adcvalue1;
If (I = 9999)
{
Avgvota = sum/10000;
Sum = 0;
Printf ("AVG vota is: % d \ r \ n", AVG vota * 3300/0 xfff );
}
}
}
[Experiment 2. Four Channels of data are collected in DMA mode]
At the same time, the adc_nbrofconversion in adc_initstructyre must be changed before sampling the two data types. Then, use adc_regularchannelconfig to add channel 0 to the scan channel sequence.
From one road to four road, a total of changed linesCode, Add three lines of code:
Adc_initstructyre.adc_nbrofconversion = 2;
Adc_regularchannelconfig (adc1, adc_channel_0, 1, adc_sampletime_144cycles );
Adc_regularchannelconfig (adc1, adc_channel_1, 2, adc_sampletime_144cycles );
Adc_regularchannelconfig (adc1, adc_channel_2, 3, adc_sampletime_144cycles );
Adc_regularchannelconfig (adc1, adc_channel_3, 4, adc_sampletime_144cycles );
During the experiment, the inputs of pa0, pa1, pa2, and pa3 are grounded or connected to a 3.3 V power supply. On the computer side, the following two data changes are displayed: 0 and 3300, indicating that the data has been sampled.
[Note]
During this experiment, I encountered an episode.
I wrote this when initializing the Pa Port:
Gpio_initstructure.gpio_pin = gpio_pinsource0 | gpio_pinsource1 | gpio_pinsource2 | gpio_pinsoure3;
This problem causes the gpio initialization to fail. Yes, the ADC does not sample the value of the corresponding PIN. I have been looking for problems with the configuration of DMA and ADC, but I can't find these problems by chance.
Gpio_pinsource0 and gpio_pin_0 are different. Gpio_pin_0 should be used for Pin initialization. View the macro definitions in the database. The two values are different.
Gpio_pinsource0 indicates the pin number, while gpio_pin_0 indicates the position in the gpio register. Be clear
After the change, everything will be normal. We can sample the four-way input data perfectly.
Next, we will experiment with other ADC working modes.
Technorati flag: 127f4, ADC