Rath-HAL - ADC 读取模拟数值
2021-07-28 本文已影响0人
T_K_233
器材
-
1x Tequila Nano + RA_LINK 调试器
-
1x 电位器
-
3x 杜邦线
电路连接
Tequila Nano | 电位器 |
---|---|
PA0 | Sig (信号) |
3V3 | 3V3 |
GND | GND |
原理
代码
/**
* @file main.c
* @version 1.0
* @date 2021-07-27
*
*/
#include "Rath_HAL.h"
static void AG_RCU_init(void);
static void AG_GPIO_init(void);
static void AG_ADC0_init(void);
static void AG_USART0_init(void);
int main(void) {
AG_RCU_init();
AG_GPIO_init();
AG_ADC0_init();
AG_USART0_init();
while (1) {
uint16_t adc_channel_0_data = 0;
uint16_t adc_channel_1_data = 0;
ADC_ChannelInitTypeDef ADC0_channel_init;
ADC0_channel_init.channel = ADC_CHANNEL_0;
ADC0_channel_init.rank = ADC_RANK_0;
ADC0_channel_init.sample_time = ADC_SAMPLETIME_1POINT5;
HAL_ADC_initChannel(ADC0, &ADC0_channel_init);
HAL_ADC_startConversion(ADC0, 0);
adc_channel_0_data = HAL_ADC_read(ADC0);
ADC0_channel_init.channel = ADC_CHANNEL_1;
ADC0_channel_init.rank = ADC_RANK_0;
ADC0_channel_init.sample_time = ADC_SAMPLETIME_1POINT5;
HAL_ADC_initChannel(ADC0, &ADC0_channel_init);
HAL_ADC_startConversion(ADC0, 0);
adc_channel_1_data = HAL_ADC_read(ADC0);
printf("ADC channel 0: %u\tchannel 1: %u\n", adc_channel_0_data, adc_channel_1_data);
HAL_delay(20);
}
}
static void AG_RCU_init(void) {
HAL_RCU_initDefaultSystemClock();
HAL_RCU_resetPeriphClock(RCU_GPIOA);
HAL_RCU_resetPeriphClock(RCU_ADC0);
HAL_RCU_resetPeriphClock(RCU_USART0);
HAL_RCU_setADCPrescaler(RCU_ADCPRESCALER_DIV_8);
HAL_RCU_enablePeriphClock(RCU_GPIOA);
HAL_RCU_enablePeriphClock(RCU_ADC0);
HAL_RCU_enablePeriphClock(RCU_USART0);
}
static void AG_GPIO_init(void) {
GPIO_InitTypeDef GPIO_init;
GPIO_init.pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_init.mode = GPIO_MODE_ANALOG;
GPIO_init.pull = GPIO_PULL_NONE;
GPIO_init.speed = GPIO_SPEED_NONE;
HAL_GPIO_init(GPIOA, &GPIO_init);
GPIO_init.pin = GPIO_PIN_9;
GPIO_init.mode = GPIO_MODE_AF_PP;
GPIO_init.pull = GPIO_PULL_NONE;
GPIO_init.speed = GPIO_SPEED_50MHZ;
HAL_GPIO_init(GPIOA, &GPIO_init);
GPIO_init.pin = GPIO_PIN_10;
GPIO_init.mode = GPIO_MODE_INPUT;
GPIO_init.pull = GPIO_PULL_NONE;
GPIO_init.speed = GPIO_SPEED_NONE;
HAL_GPIO_init(GPIOA, &GPIO_init);
}
static void AG_ADC0_init(void) {
HAL_ADC_disable(ADC0);
ADC_InitTypeDef ADC0_init;
ADC0_init.mode = ADC_MODE_FREE;
ADC0_init.data_align = ADC_DATAALIGN_RIGHT;
ADC0_init.scan_mode = DISABLE;
ADC0_init.continuous_mode = DISABLE;
ADC0_init.discontinuous_mode = DISABLE;
ADC0_init.trigger_source = ADC_TRIGGERSOURCE_REGULAR_SOFTWARE;
ADC0_init.conversion_length = 1;
ADC0_init.inserted_discontinuous_mode = DISABLE;
ADC0_init.inserted_trigger_source = ADC_TRIGGERSOURCE_INSERTED_SOFTWARE;
ADC0_init.inserted_conversion_length = 0;
ADC0_init.auto_insertion_mode = DISABLE;
HAL_ADC_init(ADC0, &ADC0_init);
ADC_ChannelInitTypeDef ADC0_channel_init;
ADC0_channel_init.channel = ADC_CHANNEL_0;
ADC0_channel_init.rank = ADC_RANK_0;
ADC0_channel_init.sample_time = ADC_SAMPLETIME_1POINT5;
ADC0_channel_init.insersion_offset = 0;
HAL_ADC_initChannel(ADC0, &ADC0_channel_init);
HAL_ADC_enable(ADC0);
}
static void AG_USART0_init(void) {
HAL_USART_disable(USART0);
USART_InitTypeDef USART0_init;
USART0_init.mode = USART_MODE_TX_RX;
USART0_init.baudrate = 115200UL;
USART0_init.word_length = USART_WORDLENGTH_8BIT;
USART0_init.stop_bits = USART_STOPBITS_1BIT;
USART0_init.parity = USART_PARITY_NONE;
USART0_init.hw_control = USART_HWCONTROL_NONE;
HAL_USART_init(USART0, &USART0_init);
HAL_USART_enable(USART0);
}