//逐行注释例程主函数:
//```c
/*!
* @file main.c
*
* @brief Main program
*
* @version V1.0.0
*
* @date 2024-03-20
*
* @attention
*
* Copyright (C) 2024 Geehy Semiconductor
*
* You may not use this file except in compliance with the
* GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
*
* The program is only for reference, which is distributed in the hope
* that it will be useful and instructional for customers to develop
* their software. Unless required by applicable law or agreed to in
* writing, the program is distributed on an "AS IS" BASIS, WITHOUT
* ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
* See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
* and limitations under the License.
*/
/* Includes */
#include "user_config.h"
#include "g32a1xxx_adc.h"
#include "g32a1xxx_pins.h"
/** @addtogroup G32A1465_Examples
@{
*/
/** @addtogroup ADC_HardwareCompare
@{
*/
/** @defgroup ADC_HardwareCompare_Macros Macros
@{
*/
/* ADC instance */
#define ADC_INSTANCE (0U)
/**@} end of group ADC_HardwareCompare_Macros*/
/** @defgroup ADC_HardwareCompare_Variables Variables
@{
*/
static __IO uint32_t g_timingDelay; // 定义一个静态变量用于延迟计数
float g_adcMaxValue = 4096; // ADC最大值
float g_adcDifference = 3.3f; // ADC差值,单位为伏特
bool g_compareFlag; // 比较标志位
/**@} end of group ADC_HardwareCompare_Variables*/
/** @defgroup ADC_HardwareCompare_Functions Functions
@{
*/
/*!
* @brief Start SysTick
*
* @param None
*
* @retval None
*/
void SysTick_Init(void)
{
/* Update System Core Clock info */
SystemCoreClockUpdate(); // 更新系统核心时钟信息
/* SystemFrequency / 1000 = 1ms */
if (SysTick_Config(SystemCoreClock / 1000)) // 如果配置SysTick失败
{
/* Capture error */
while (1); // 捕获错误并进入死循环
}
}
/*!
* @brief Precise Delay
*
* @param nTime in milliseconds
*
* @retval None
*/
void SysTick_Delay_ms(__IO uint32_t nTime)
{
g_timingDelay = nTime; // 设置延迟时间
while(g_timingDelay != 0); // 等待延迟时间结束
}
/*!
* @brief Decrements the TimingDelay
*
* @param None
*
* @retval None
*/
void TimingDelay_Decrement(void)
{
if(g_timingDelay != 0) // 如果延迟时间不为0
{
g_timingDelay--; // 延迟时间减1
}
}
/*!
* @brief ADC software conversion initialization configuration
*
* @param None
*
* @retval None
*/
void ADC_init(void)
{
/* Reset adc module */
ADC_Reset(ADC_INSTANCE); // 复位ADC模块
/* Initialize ADC based on configuration values */
ADC_ConfigConverter(ADC_INSTANCE,&adcConvCfg0); // 根据配置值初始化ADC转换器
/* Verify ADC */
ADC_AutoCalibration(ADC_INSTANCE); // 验证ADC校准
/* Enable hardware comparison function */
ADC_ConfigHwCompare(0,&adcCompareCfg0); // 启用硬件比较功能
}
/*!
* @brief Read ADC value
*
* @param None
*
* @retval ADC value
*/
float ADC_readValue(void)
{
uint16_t partAdcValue; // 部分ADC值变量
/* Trigger using software */
ADC_ConfigChan(ADC_INSTANCE, 0u, &adcChanCfg0); // 使用软件触发ADC通道配置
/* waiting for conversion to complete */
ADC_WaitConvDone(ADC_INSTANCE); // 等待转换完成
/* Obtain conversion flag bits */
g_compareFlag = ADC_ReadConvCompleteFlag(ADC_INSTANCE,0); // 获取转换完成标志位
/* Obtain conversion results */
ADC_ReadChanResult(ADC_INSTANCE, 0u, &partAdcValue); // 获取转换结果
return (float)partAdcValue; // 返回转换结果的浮点值
}
/*!
* @brief Main function
*
* @param None
*
* @retval None
*/
int main(void)
{
float adcValue; // 定义一个浮点变量用于存储ADC值
/* Initialize clock */
CLOCK_SYS_Init(&g_clockConfig); // 初始化时钟系统
/* LED init */
LED_Init(); // 初始化LED灯
/* UART init */
COM_Init(); // 初始化串口通信
/* ADC init */
ADC_init(); // 初始化ADC模块
SysTick_Init(); // 初始化SysTick定时器
while (1) // 主循环
{
SysTick_Delay_ms(250); // 延迟250毫秒
/* Calculated voltage value */
adcValue = ADC_readValue() / g_adcMaxValue * g_adcDifference; // 计算电压值
/* serial output ADC conversion value ,Compare flag bits */
printf("ADC conversion voltage value = %fV Compare flag bits = %d\r\n",adcValue,g_compareFlag); // 输出ADC转换值和比较标志位到串口
if(g_compareFlag == 0) // 如果比较标志位为0
{
LED_On(LED_RED); // 点亮红色LED灯
LED_Off(LED_GREEN); // 关闭绿色LED灯
}
else // 如果比较标志位为1
{
LED_On(LED_GREEN); // 点亮绿色LED灯
LED_Off(LED_RED); // 关闭红色LED灯
}
}
}
/**@} end of group ADC_HardwareCompare_Functions */
/**@} end of group ADC_HardwareCompare */
/**@} end of group Examples */
//```
详细解释:- SysTick_Delay_ms(250): 使用SysTick定时器实现一个250毫秒的延迟。
- ADC_readValue(): 读取ADC的值,返回一个16位的数值。
- *adcValue = ADC_readValue() / g_adcMaxValue g_adcDifference: 将ADC值转换为实际电压值。假设最大ADC值为4096(12位ADC),差值为3.3V。
- printf: 通过串口输出计算得到的电压值和比较标志位。
- LED控制: 根据比较标志位的值来控制LED灯的状态。如果比较标志位为0,则点亮红色LED并熄灭绿色LED;否则,点亮绿色LED并熄灭红色LED。
这个程序主要用于演示如何读取ADC值并通过简单的逻辑控制LED灯的状态,适用于一些基本的嵌入式应用,如传感器数据读取和状态指示等。
烧录接线,JTAG参照:
烧录效果:
XCOM显示:
|