本帖最后由 dirty123 于 2025-8-31 00:05 编辑
本篇讲述使用开发板电阻采样。
一.硬件准备
查阅芯片手册PA0作为ADC采集口,接上可调电阻变位器。选用电位器10K可调范围。
二.代码
- int main(void)
- {
- /* ADC convert to voltage */
- float voltage = 0;
- USART_Config_T USART_ConfigStruct;
- /* USART config */
- USART_ConfigStructInit(&USART_ConfigStruct);
- USART_ConfigStruct.baudRate = 115200;
- USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
- USART_ConfigStruct.mode = USART_MODE_TX;
- USART_ConfigStruct.parity = USART_PARITY_NONE;
- USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
- USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
- BOARD_COM_Config(COM1, &USART_ConfigStruct);
- /* ADC1 initialization */
- ADC_Init();
- /* Infinite loop */
- while (1)
- {
- if (DMA_ReadStatusFlag(DMA1_FLAG_TC1) == SET)
- {
- voltage = (double)DMA_ADCConvertedValue / 4095 * 3.3;
- printf("\r\n");
- printf("ADC register data = 0x%04X \r\n", DMA_ADCConvertedValue);
- printf("voltage = %.03f V \r\n", voltage);
- Delay(5000);
- DMA_ClearStatusFlag(DMA1_FLAG_TC1);
- }
- }
- }
三.调试
编译烧录后,扭动旋钮调节电阻,可看到随着阻值变化adc读值与电压变化。
|