ADC introduction
Preface
ADC, or Analog-to-Digital Converter, simply converts analog signals into digital signals. Here are some basic terms related to ADC:
- Resolution: The length of the data that is read out, such as 8 bits meaning a maximum value of 255, or a range of [0, 255]. 12 bits mean a maximum value of 4096, or a range of [0, 4096].
- Channel: The ADC input pin. Typically, one ADC controller controls multiple channels. If multiple channels are needed, each channel must be scanned separately.
ADC conversion modes
- Single conversion, only one channel is converted at a time.
- Continuous conversion, immediately after a channel is converted, the next channel’s conversion is automatically performed.
- Scan mode, once enabled, automatically reads multiple channels continuously.
ADC three mode
- Polling
- Interrupt mode
- DMA(Direct Memory Access)
PS:Note: There are usually positive and negative reference voltages in ADC chips. Positive reference voltage is usually connected to VCC and negative reference voltage is usually connected to GND.
cubemx Setting
PS:The difference between Single-ended and Differential lies in the difference in the reference point for comparison.
Sample code
uint16_t dong_get_adc(){
//開啟ADC1
HAL_ADC_Start(&hadc3);
//等待ADC轉換完成,超時為100ms
HAL_ADC_PollForConversion(&hadc3,100);
//判斷ADC是否轉換成功
if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc3),HAL_ADC_STATE_REG_EOC)){
//讀取值
return HAL_ADC_GetValue(&hadc3);
}
return 0;
}
Basically, using ADC is not difficult and can be achieved by following the example. For more advanced techniques, please refer to other detailed articles by friends. Analog products are generally less commonly used in control and are mainly used in LEDs