int main(void) { /*初始化USART 配置模式为 115200 8-N-1,中断接收*/ Debug_USART_Config(); Rheostat_Init(); while (1) { ADC_ConvertedValueLocal[0] =(float) ADC_ConvertedValue[0]/4096*(float)3.3; ADC_ConvertedValueLocal[1] =(float) ADC_ConvertedValue[1]/4096*(float)3.3; ADC_ConvertedValueLocal[2] =(float) ADC_ConvertedValue[2]/4096*(float)3.3; ADC_ConvertedValueLocal[3] =(float) ADC_ConvertedValue[3]/4096*(float)3.3; ADC_ConvertedValueLocal[4] =(float) ADC_ConvertedValue[4]/4096*(float)3.3; ADC_ConvertedValueLocal[5] =(float) ADC_ConvertedValue[5]/4096*(float)3.3; ADC_ConvertedValueLocal[6] =(float) ADC_ConvertedValue[6]/4096*(float)3.3; ADC_ConvertedValueLocal[7] =(float) ADC_ConvertedValue[7]/4096*(float)3.3; //printf("\r\n CH1_PA1 value = %f V \r\n",ADC_ConvertedValueLocal[0]); //printf("\r\n CH2_PA2 value = %f V \r\n",ADC_ConvertedValueLocal[1]); //printf("\r\n CH3_PA3 value = %f V \r\n",ADC_ConvertedValueLocal[2]); //printf("\r\n CH4_PA4 value = %f V \r\n",ADC_ConvertedValueLocal[3]); //printf("\r\n CH5_PA5 value = %f V \r\n",ADC_ConvertedValueLocal[4]); //printf("\r\n CH6_PA6 value = %f V \r\n",ADC_ConvertedValueLocal[5]); //printf("\r\n CH1_PA1 value = %f V \r\n",ADC_ConvertedValueLocal[6]); //printf("\r\n CH1_PA1 value = %f V \r\n",ADC_ConvertedValueLocal[7]); printf("\r\n %f %f %f \r\n",ADC_ConvertedValueLocal[7],ADC_ConvertedValueLocal[2],ADC_ConvertedValueLocal[4]); //printf("\r\n\r\n"); Delay(0xafffff);
时间: 2023-06-19 19:08:21 浏览: 192
这段代码是一段嵌入式系统的C语言代码,主要包括USART和ADC的初始化以及数据的读取和输出。其中,USART是一种串行通信协议,用于与外部设备进行通信,而ADC是一种模数转换器,用于将模拟信号转换为数字信号。这段代码的主要作用是读取ADC转换后的电压值,并通过USART输出到外部设备上。具体来说,通过调用`ADC_ConvertedValue`数组中的元素,将其除以4096并乘以3.3,得到实际电压值,然后通过`printf`函数输出到外部设备上。其中,`Delay`函数是用于延时的函数。
相关问题
#include "stm32f4xx.h" #include "./usart/bsp_debug_usart.h" #include "./adc/bsp_adc.h" // ADC转换的电压值通过MDA方式传到SRAM extern __IO uint16_t ADC_ConvertedValue[RHEOSTAT_NOFCHANEL]; // 局部变量,用于保存转换计算后的电压值 float ADC_ConvertedValueLocal[RHEOSTAT_NOFCHANEL]={0}; static void Delay(__IO uint32_t nCount) //简单的延时函数 { for(; nCount != 0; nCount--); } /** * @brief 主函数 * @param 无 * @retval 无 */ int main(void) { /*初始化USART 配置模式为 115200 8-N-1,中断接收*/ Debug_USART_Config(); Rheostat_Init(); while (1) { ADC_ConvertedValueLocal[0] =(float) ADC_ConvertedValue[0]/4096*(float)3.3; ADC_ConvertedValueLocal[1] =(float) ADC_ConvertedValue[1]/4096*(float)3.3; ADC_ConvertedValueLocal[2] =(float) ADC_ConvertedValue[2]/4096*(float)3.3; ADC_ConvertedValueLocal[3] =(float) ADC_ConvertedValue[3]/4096*(float)3.3; ADC_ConvertedValueLocal[4] =(float) ADC_ConvertedValue[4]/4096*(float)3.3; ADC_ConvertedValueLocal[5] =(float) ADC_ConvertedValue[5]/4096*(float)3.3; ADC_ConvertedValueLocal[6] =(float) ADC_ConvertedValue[6]/4096*(float)3.3; ADC_ConvertedValueLocal[7] =(float) ADC_ConvertedValue[7]/4096*(float)3.3; //printf("\r\n CH1_PA1 value = %f V \r\n",ADC_ConvertedValueLocal[0]); //printf("\r\n CH2_PA2 value = %f V \r\n",ADC_ConvertedValueLocal[1]); //printf("\r\n CH3_PA3 value = %f V \r\n",ADC_ConvertedValueLocal[2]); //printf("\r\n CH4_PA4 value = %f V \r\n",ADC_ConvertedValueLocal[3]); //printf("\r\n CH5_PA5 value = %f V \r\n",ADC_ConvertedValueLocal[4]); //printf("\r\n CH6_PA6 value = %f V \r\n",ADC_ConvertedValueLocal[5]); //printf("\r\n CH1_PA1 value = %f V \r\n",ADC_ConvertedValueLocal[6]); //printf("\r\n CH1_PA1 value = %f V \r\n",ADC_ConvertedValueLocal[7]); printf("\r\n %f %f %f \r\n",ADC_ConvertedValueLocal[7],ADC_ConvertedValueLocal[2],ADC_ConvertedValueLocal[4]); //printf("\r\n\r\n"); Delay(0xafffff); } } /*********************************************END OF FILE**********************/
这是一个基于STM32F4的ADC采集电压值并传输到串口的程序。程序中通过调用ADC转换后的电压值,并计算出实际电压值,然后通过串口输出显示。其中还包含一个简单的延时函数用来控制输出频率。需要注意的是,程序中使用了外部定义的ADC_ConvertedValue数组,该数组实际上是在另外一个文件中定义的,这里只是通过extern关键字引用该数组。
基于HAL库,串口2接收中断接收字符通过串口1打印字符串
下面是基于HAL库的代码实现:
```c
#include "main.h"
#include "stm32f1xx_hal.h"
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART2_UART_Init(void);
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
/* Infinite loop */
while (1)
{
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
/**
* @brief This function handles USART1 global interrupt.
* @retval None
*/
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
uint8_t data;
if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET)
{
data = (uint8_t)(huart1.Instance->DR & (uint8_t)0x00FF);
HAL_UART_Transmit(&huart2, &data, 1, HAL_MAX_DELAY);
}
/* USER CODE END USART1_IRQn 1 */
}
/**
* @brief This function handles USART2 global interrupt.
* @retval None
*/
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */
/* USER CODE END USART2_IRQn 1 */
}
```
在 `main.c` 中,我们先初始化了两个串口 `huart1` 和 `huart2`,并且开启了它们的中断。在中断处理函数 `USART1_IRQHandler` 中,我们检查是否有数据到达串口1的接收缓冲区,如果有,就取出数据并发送到串口2中。这样,串口2就可以接收到串口1发送的数据了。
阅读全文