本帖最后由 安臣 于 2015-12-10 08:57 编辑
The 12-bit ADC is a successive approximation analog-to-digital converter. It has up to 19 multiplexed channels allowing it to measure signals from 16 external and 3 internal sources. A/D conversion of the various channels can be performed in single, continuous, scan or discontinuous mode. The result of the ADC is stored in a left-aligned or right-aligned 16-bit data register.
12位逐次逼近adc 19个通道 (包涵16个外部 和3个内部) 转换可以是 单次,连续,扫描 和断续模式 。结果可以左对齐或者右对齐存放。
关于校准Calibration (ADCAL)
Calibration software procedure:
1.Ensure that ADEN=0 确认 aden 等于0
2. Set ADCAL=1
3. Wait until ADCAL=0 等待校准完成
4. The calibration factor can be read from bits 6:0 of ADC_DR. 校准结果存放的位置。
库函数
- /**
- * @brief Perform an ADC automatic self-calibration
- * Calibration prerequisite: ADC must be disabled (execute this
- * function before HAL_ADC_Start() or after HAL_ADC_Stop() ).
- * @note Calibration factor can be read after calibration, using function
- * HAL_ADC_GetValue() (value on 7 bits: from DR[6;0]).
- * @param hadc: ADC handle
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_ADCEx_Calibration_Start(ADC_HandleTypeDef* hadc)
- {
复制代码 Channel selection (CHSEL, SCANDIR) 关于通道配置
- HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
复制代码
通道的宏定义
- /**
- * @brief Configure the channel number into channel selection register
- * @param _CHANNEL_: ADC Channel
- * @retval None
- */
- /* This function converts ADC channels from numbers (see defgroup ADC_channels)
- to bitfields, to get the equivalence of CMSIS channels:
- ADC_CHANNEL_0 ((uint32_t) ADC_CHSELR_CHSEL0)
- ADC_CHANNEL_1 ((uint32_t) ADC_CHSELR_CHSEL1)
- ADC_CHANNEL_2 ((uint32_t) ADC_CHSELR_CHSEL2)
- ADC_CHANNEL_3 ((uint32_t) ADC_CHSELR_CHSEL3)
- ADC_CHANNEL_4 ((uint32_t) ADC_CHSELR_CHSEL4)
- ADC_CHANNEL_5 ((uint32_t) ADC_CHSELR_CHSEL5)
- ADC_CHANNEL_6 ((uint32_t) ADC_CHSELR_CHSEL6)
- ADC_CHANNEL_7 ((uint32_t) ADC_CHSELR_CHSEL7)
- ADC_CHANNEL_8 ((uint32_t) ADC_CHSELR_CHSEL8)
- ADC_CHANNEL_9 ((uint32_t) ADC_CHSELR_CHSEL9)
- ADC_CHANNEL_10 ((uint32_t) ADC_CHSELR_CHSEL10)
- ADC_CHANNEL_11 ((uint32_t) ADC_CHSELR_CHSEL11)
- ADC_CHANNEL_12 ((uint32_t) ADC_CHSELR_CHSEL12)
- ADC_CHANNEL_13 ((uint32_t) ADC_CHSELR_CHSEL13)
- ADC_CHANNEL_14 ((uint32_t) ADC_CHSELR_CHSEL14)
- ADC_CHANNEL_15 ((uint32_t) ADC_CHSELR_CHSEL15)
- ADC_CHANNEL_16 ((uint32_t) ADC_CHSELR_CHSEL16)
- ADC_CHANNEL_17 ((uint32_t) ADC_CHSELR_CHSEL17)
- ADC_CHANNEL_18 ((uint32_t) ADC_CHSELR_CHSEL18)
- */
- #define ADC_CHSELR_CHANNEL(_CHANNEL_) \
- ( 1U << (_CHANNEL_))
复制代码
关于转换模式
Single conversion mode (CONT=0) 单次转换
Conversion is started by either: 启动方式有两种
• Setting the ADSTART bit in the ADC_CR register 置位ADSTART 启动
• Hardware trigger event 硬件启动
Inside the sequence, after each conversion is complete: 结束每次转换
• The converted data are stored in the 16-bit ADC_DR register 数据在ADC_DR 寄存器
• The EOC (end of conversion) flag is set EOC 中断 读取寄存器数据
• An interrupt is generated if the EOCIE bit is set
After the sequence of conversions is complete: 序列转换完成
• The EOSEQ (end of sequence) flag is set eos中断 准备开始接受下一次数据的状态。
• An interrupt is generated if the EOSEQIE bit is set
Continuous conversion mode (CONT=1) 连续转换
automatically re-starts and continuously performs the same sequence of conversions. This mode is selected when CONT=1 in the ADC_CFGR1 register. Conversion is started by either:
• Setting the ADSTART bit in the ADC_CR register
• Hardware trigger event
Inside the sequence, after each conversion is complete:
• The converted data are stored in the 16-bit ADC_DR register
• The EOC (end of conversion) flag is set
• An interrupt is generated if the EOCIE bit is set
After the sequence of conversions is complete:
• The EOSEQ (end of sequence) flag is set
• An interrupt is generated if the EOSEQIE bit is set
一次序列转换结束后,ADC 立即重新转换相同的序列通道。
Discontinuous mode (DISCEN)
在这个模式(DISCEN=1) 下,需要硬件或软件的触发事件去启动定义在一个序列中的每次转换。
相反,DISCEN=0 时, 一个硬件或软件的触发事件就可以启动定义在一个序列中的所有转换。
例如:
● DISCEN=1, 需要转换的通道为: 0, 3, 7, 10
- 1st 触发:通道 0 被转换且一个 EOC事件产生
- 2nd 触发:通道 3 被转换且一个 EOC事件产生
- 3rd 触发:通道 7 被转换且一个 EOC事件产生
- 4th 触发:通道 10 被转换且产生EOC和 EOS事件
- 5th 触发:通道 0 被转换且一个 EOC事件产生
- 6th 触发:通道 3 被转换且一个 EOC事件产生
- ...
● DISCEN=0, 需要转换的通道为: 0, 3, 7, 10
- 1st 触发:整个完整的序列转换:依次为通道 0, 3, 7和 10。
每次转换产生一个EOC 事件,到最后一通道还产生一个EOS 事件。
- 任何触发事件都会重新开始完整的序列转换。
The EXTSEL[2:0] control bits are used to select which of 8 possible events can trigger conversions. (关于外部触发)
- void MX_ADC_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig;
- /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
- */
- hadc.Instance = ADC1;
- hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC;
- hadc.Init.Resolution = ADC_RESOLUTION12b;
- hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
- hadc.Init.EOCSelection = EOC_SINGLE_CONV;
- hadc.Init.LowPowerAutoWait = DISABLE;
- hadc.Init.LowPowerAutoPowerOff = DISABLE;
- hadc.Init.ContinuousConvMode = DISABLE;
- hadc.Init.DiscontinuousConvMode = ENABLE;
- hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T2_TRGO;
- hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
- hadc.Init.DMAContinuousRequests = DISABLE;
- hadc.Init.Overrun = OVR_DATA_PRESERVED;
- HAL_ADC_Init(&hadc);
- /**Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_0;
- sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
- sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- /**Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_1;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- /**Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_2;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- /**Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_3;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- /**Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_4;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- /**Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_5;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- /**Configure for the selected ADC regular channel to be converted.
- */
- sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
- HAL_ADC_ConfigChannel(&hadc, &sConfig);
- }
复制代码
|
也来这边逛逛啊?
很好的总结,收藏学习!!
最近的书进度慢了 也没有笔记
是吗? 这个是艺术品哦!
感谢支持,共同进步
大神驾到,感谢支持
哈哈。 有机会我会努力的
又见大神