本帖最后由 eew_cT3H5d 于 2024-8-8 08:13 编辑
本部分采样滑动变阻器模块的电压,将电压数值打印到电脑上,显示当前采样的电压
配置ADC使能
添加配置ADC模块
配置相关adc参数
配置相关通道
添加函数
adc.h文件
- #ifndef _ADC_H
- #define _ADC_H
-
- #include "hal_data.h"
-
- void ADC_Init(void);
- double Read_ADC_Voltage_Value(void);
-
- #endif
-
adc.c文件
- #include <ADC/adc.h>
-
-
- volatile bool scan_complete_flag = false;
-
- void adc_callback(adc_callback_args_t * p_args)
- {
- FSP_PARAMETER_NOT_USED(p_args);
- scan_complete_flag = true;
- }
-
- void ADC_Init(void)
- {
- fsp_err_t err;
- err = R_ADC_Open(&g_adc0_ctrl, &g_adc0_cfg);
- err = R_ADC_ScanCfg(&g_adc0_ctrl, &g_adc0_channel_cfg);
- assert(FSP_SUCCESS == err);
- }
-
-
- double Read_ADC_Voltage_Value(void)
- {
- uint16_t adc_data;
- double a0;
-
- (void)R_ADC_ScanStart(&g_adc0_ctrl);
- while (!scan_complete_flag)
- {
- ;
- }
- scan_complete_flag = false;
-
-
- R_ADC_Read(&g_adc0_ctrl, ADC_CHANNEL_0, &adc_data);
-
- a0 = (double)(adc_data*3.3/4095);
-
- return a0;
- }
-
hal_entry.c函数
- /*
- * Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-
-
-
-
-
- fsp_err_t err;
-
- void R_BSP_WarmStart(bsp_warm_start_event_t event);
-
- //extern bsp_leds_t g_bsp_leds;
-
- /*******************************************************************************************************************//**
- * @brief Blinky example application
- *
- * Blinks all leds at a rate of 1 second using the software delay function provided by the BSP.
- *
- **********************************************************************************************************************/
- void hal_entry (void)
- {
-
- Debug_UART3_Init();
- ADC_Init();
- while (1)
- {
- printf("HELLO eeworld !\r\n");
- printf("a0 = %f\r\n", Read_ADC_Voltage_Value());
- R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_SECONDS); //延时1秒
- }
- }
-
- /*******************************************************************************************************************//**
- * This function is called at various points during the startup process. This implementation uses the event that is
- * called right before main() to set up the pins.
- *
- * @param[in] event Where at in the start up process the code is currently at
- **********************************************************************************************************************/
- void R_BSP_WarmStart (bsp_warm_start_event_t event)
- {
- if (BSP_WARM_START_RESET == event)
- {
-
-
- /* Enable reading from data flash. */
- R_FACI_LP->DFLCTL = 1U;
-
- /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
- * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
-
- }
-
- if (BSP_WARM_START_POST_C == event)
- {
- /* C runtime environment and system clocks are setup. */
-
- /* Configure pins. */
- R_IOPORT_Open(&IOPORT_CFG_CTRL, &IOPORT_CFG_NAME);
- }
- }
-
编译下载程序