【Renesas RA_FSP】Potentiometer voltage acquisition


1. Hardware Design

The ADC potentiometer circuit diagram of Wildfire Qiming 6M5 development board is shown in the figure.
insert image description here
It can be seen that the potentiometer on the development board is connected to the P000 pin, and the P000 pin can be connected to the ADC peripheral inside the MCU, so as to collect the analog signal input by the potentiometer.
insert image description here

2. Software Design

1. File structure

ADC
├─ ......
└─ src
   ├─ led
   │  ├─ bsp_led.c
   │  └─ bsp_led.h
   ├─ debug_uart
   │  ├─ bsp_debug_uart.c
   │  └─ bsp_debug_uart.h
   ├─ adc
   │  ├─ bsp_adc.c
   │  └─ bsp_adc.h
   └─ hal_entry.c

2. FSP configuration

Open the FSP configuration interface of the project for configuration.

First click "Stacks" -> "Pins" -> "Peripherals" -> "ADC0" to configure the pin corresponding to channel AN000 as P000. As shown below.
insert image description here
Then click “Stacks” -> “New Stack” -> “Analog” -> “ADC (r_adc)” to configure the ADC module. As shown below.
insert image description here
ADC attribute configuration:
insert image description here
insert image description here
After the configuration is completed, you can press the shortcut key "Ctrl + S" to save, and finally click the "Generate Project Content" button in the upper right corner to let the software automatically generate the configuration code.

3. ADC initialization function

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);
}
  • R_ADC_Open() sets the operating mode, trigger source, interrupt priority and configuration for the entire peripheral. If interrupts are enabled, this function will register a callback function pointer to notify the user when the scan is complete.

  • R_ADC_ScanCfg() configures ADC scan parameters. Channel specific settings are set in this function.

4. ADC interrupt callback function

//ADC转换完成标志位
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;
}

Register the callback function and priority on the FSP configuration page, and then you can use the interrupt callback function from the ADC.

Tips: Use the interrupt callback function of the ADC to judge whether the ADC conversion is complete. A Boolean data scan_complete_flag needs to be defined as the flag bit for ADC reading completion. When no conversion is completed, the value of scan_complete_flag is always false, and when a single ADC triggers an interrupt, the value of scan_complete_flag is changed to true.

5. If interrupts are not enabled

If interrupts are not enabled, the R_ADC_StatusGet() API can be used to poll the ADC to determine when the scan is complete. The read API function is used to access the converted ADC result. This applies to normal scans and calibration scans for MCUs that support calibration.

6. ADC read conversion result function

ADC reading idea, call R_ADC_ScanStart here to trigger the corresponding adc channel conversion, when the ADC conversion is completed, the scan_complete_flag flag will become true. Use R_ADC_Read() or R_ADC_Read32() to read the converted value after judging that the flag bit becomes true.

/* 进行ADC采集,读取ADC数据并转换结果 */
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; //重新清除标志位

   /* 读取通道0数据 */
   R_ADC_Read(&g_adc0_ctrl, ADC_CHANNEL_0, &adc_data);
   /* ADC原始数据转换为电压值(ADC参考电压为3.3V) */
   a0 = (double)(adc_data*3.3/4095);

   return a0;
}
  • R_ADC_ScanStart() starts a software scan or enables a hardware trigger for the scan, depending on how the trigger is configured in the R_ADC_Open call. This function allows the trigger signal to reach the ADC unit if the unit is configured for ELC or external hardware triggering. This function has no control over the generation of the trigger itself. If the unit is configured for software triggering, this function initiates a software triggered scan.

  • R_ADC_Read() reads the conversion result from a single channel or sensor register, and the returned data is uint16_t type.

  • R_ADC_Read32() reads the conversion result from a single channel or sensor register, and the returned data is of type uint32_t.

7. hal_entry entry function

void hal_entry(void)
{
    
    
   LED_Init();         // LED 初始化
   Debug_UART4_Init(); // SCI4 UART 调试串口初始化

   /* ADC 初始化 */
   ADC_Init();

   printf("这是一个读取电位器ADC电压转换值的例程\r\n");
   printf("打开串口助手查看ADC转换结果,旋钮电位器,可以看到ADC值在一定范围之内发生变化\r\n");
   printf("开始读取ADC转换值:\r\n");


   while(1)
   {
    
    
      printf("a0 = %f\r\n", Read_ADC_Voltage_Value());
      R_BSP_SoftwareDelay(500, BSP_DELAY_UNITS_MILLISECONDS); //大概0.5秒钟读取一次
      LED1_TOGGLE;
   }


#if BSP_TZ_SECURE_BUILD
   /* Enter non-secure code */
   R_BSP_NonSecureEnter();
#endif
}

Guess you like

Origin blog.csdn.net/Dustinthewine/article/details/131152801