GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;// 初始化GPIO口RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;GPIO_Init(GPIOA, &GPIO_InitStructure);// 初始化串口RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);USART_InitStructure.USART_BaudRate = 115200;USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_InitStructure.USART_StopBits = USART_StopBits_1;USART_InitStructure.USART_Parity = USART_Parity_No;USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode = USART_Mode_Tx;USART_Init(USART1, &USART_InitStructure);USART_Cmd(USART1, ENABLE);ADC_InitTypeDef ADC_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;NVIC_InitTypeDef NVIC_InitStructure;// 初始化ADC模块RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;ADC_InitStructure.ADC_ScanConvMode = DISABLE;ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;ADC_InitStructure.ADC_NbrOfChannel = 1;ADC_Init(ADC1, &ADC_InitStructure);ADC_Cmd(ADC1, ENABLE);// 初始化定时器RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);TIM_TimeBaseStructure.TIM_Period = 72000000 / 1000 - 1; // 计数器自动重装值TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 分频系数TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 时钟分割TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 计数器向上计数TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);// 配置定时器触发ADC采样TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);ADC_ExternalTrigConvCmd(ADC1, ENABLE);// 初始化定时器中断TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);void TIM3_IRQHandler(void) { if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_Update); ADC_SoftwareStartConvCmd(ADC1, ENABLE); while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); uint16_t adcValue = ADC_GetConversionValue(ADC1); USART_SendData(USART1, adcValue >> 8); while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USART1, adcValue & 0xff); while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); }}

时间: 2023-08-29 14:08:12 浏览: 131
这段代码是 STM32F103C8T6 开发板使用 ADC 和 USART 模块进行模拟量采集并通过串口传输的代码。 1. 初始化 GPIO 口和串口 2. 初始化 ADC 模块 3. 初始化定时器,配置定时器触发 ADC 采样和定时器中断 4. 中断服务函数中,通过 ADC 获取模拟量数值,然后通过 USART 将数值通过串口传输出去 代码注释如下: ``` GPIO_InitTypeDef GPIO_InitStructure; // 定义 GPIO 初始化结构体 USART_InitTypeDef USART_InitStructure; // 定义 USART 初始化结构体 ADC_InitTypeDef ADC_InitStructure; // 定义 ADC 初始化结构体 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // 定义定时器初始化结构体 NVIC_InitTypeDef NVIC_InitStructure; // 定义中断初始化结构体 // 初始化 GPIO 口 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能 GPIOA 时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // 初始化 GPIOA 的第 0 位 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; // GPIOA 的第 0 位设置为模拟输入模式 GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化 GPIOA // 初始化串口 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 使能 USART1 时钟 USART_InitStructure.USART_BaudRate = 115200; // 设置波特率为 115200 USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 每个数据帧 8 位 USART_InitStructure.USART_StopBits = USART_StopBits_1; // 一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No; // 无奇偶校验 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 无硬件流控制 USART_InitStructure.USART_Mode = USART_Mode_Tx; // USART1 发送模式 USART_Init(USART1, &USART_InitStructure); // 初始化 USART1 USART_Cmd(USART1, ENABLE); // 使能 USART1 // 初始化 ADC 模块 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // 使能 ADC1 时钟 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; // 独立模式 ADC_InitStructure.ADC_ScanConvMode = DISABLE; // 禁止扫描模式 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // 连续转换模式 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; // 选择定时器 3 触发采样 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; // 数据右对齐 ADC_InitStructure.ADC_NbrOfChannel = 1; // 采样通道数为 1 ADC_Init(ADC1, &ADC_InitStructure); // 初始化 ADC1 ADC_Cmd(ADC1, ENABLE); // 使能 ADC1 // 初始化定时器 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // 使能 TIM3 时钟 TIM_TimeBaseStructure.TIM_Period = 72000000 / 1000 - 1; // 设置计数器自动重装值,即定时器周期为 1ms TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 设置分频系数,即定时器时钟为 1MHz TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 设置时钟分割 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 计数器向上计数 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // 初始化定时器 TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update); // 配置定时器触发 ADC 采样 ADC_ExternalTrigConvCmd(ADC1, ENABLE); // 使能 ADC 外部触发转换 // 初始化定时器中断 TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); // 使能定时器更新中断 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; // 设置定时器 3 的中断向量 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 中断抢占优先级为 0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 中断响应优先级为 0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 使能中断 NVIC_Init(&NVIC_InitStructure); // 初始化中断向量表 // 定时器中断服务函数 void TIM3_IRQHandler(void) { if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) { // 判断是否为定时器更新中断 TIM_ClearITPendingBit(TIM3, TIM_IT_Update); // 清除定时器更新中断标志位 ADC_SoftwareStartConvCmd(ADC1, ENABLE); // 开始 ADC 转换 while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); // 等待 ADC 转换完成 uint16_t adcValue = ADC_GetConversionValue(ADC1); // 获取 ADC 转换结果 USART_SendData(USART1, adcValue >> 8); // 发送高 8 位 while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // 等待数据发送完成 USART_SendData(USART1, adcValue & 0xff); // 发送低 8 位 while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // 等待数据发送完成 } } ```

相关推荐

void uart_init(u32 bound) { //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟 //USART1_TX GPIOA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9 //USART1_RX GPIOA.10初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10 //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器 //USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;//串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART1, &USART_InitStructure); //初始化串口1 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断 USART_Cmd(USART1, ENABLE); //使能串口1 }

请帮我解释一下下面这串代码”#include "sys.h" #include "delay.h" #include "usart.h" int main(void) { GPIO_SetBits(GPIOC, GPIO_Pin_13); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//复用 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStruct); //这行代码的作用是将初始化结构体 GPIO_InitStructure 中配置的参数应用到 GPIOA 引脚上,以完成引脚的初始化设置。 GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); USART_InitTypeDef USART_InitStructure; //USART 是一种常见的串行通信接口,用于实现与外部设备或其他单片机之间的数据传输。 NVIC_InitTypeDef NVIC_InitStructure; USART_InitStructure.USART_BaudRate=9600; USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_Cmd(USART2,ENABLE); USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//用于配置 USART 模块的中断功能。 USART_Init(USART2, &USART_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; NVIC_InitStructure.NVIC_IRQChannelSubPriority=2; NVIC_Init(&NVIC_InitStructure); USART_Cmd(USART2,ENABLE); while(1) { } } void USART2_IRQHandler(void) { char Res; if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET) //接收中断 { Res =USART_ReceiveData(USART2); //读取接收到的数据 if(Res=='a') { GPIO_ResetBits(GPIOC, GPIO_Pin_13); } if(Res=='b') { GPIO_SetBits(GPIOC, GPIO_Pin_13); } } USART_ClearITPendingBit(USART2,USART_IT_RXNE); } “

void USART2_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* config USART2 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);//¿ªÆôGPIOAʱÖÓ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//¿ªÆôUSART2ʱÖÓ /* USART2 GPIO config */ /* Configure USART2 Tx (PA.02) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//¸´ÓÃÊä³ö GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART2 Rx (PA.03) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART2 mode config */ USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ô³¤8 USART_InitStructure.USART_StopBits = USART_StopBits_1;//Ò»¸öֹͣλ USART_InitStructure.USART_Parity = USART_Parity_No ; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); USART_Cmd(USART2, ENABLE); /*Enable usart2 receive interrupt*/ USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //ÇÀÕ¼ÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //ÏìÓ¦ÓÅÏȼ¶ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }

void myUSART_Init() { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); USART_InitTypeDef USART_InitStuctyre; USART_InitStuctyre.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStuctyre.USART_Parity = USART_Parity_No; USART_InitStuctyre.USART_StopBits = USART_StopBits_1; USART_InitStuctyre.USART_BaudRate = 9600; USART_InitStuctyre.USART_WordLength = USART_WordLength_8b; USART_InitStuctyre.USART_HardwareFlowControl =USART_HardwareFlowControl_None; USART_Init(USART1,&USART_InitStuctyre); USART_Cmd(USART1,ENABLE); USART_ITConfig(USART1,USART_IT_TXE,ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_Init(&NVIC_InitStructure); } void myUSARTsend_Byte(uint16_t Byte) { USART_SendData(USART1,Byte); while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET); } void myUSARTsend_Array(uint8_t *Array,uint16_t Length) { uint16_t i; for(i=0;i<=Length;i++) { myUSARTsend_Byte(Array[i]); } }

int main(void) { char message[100]={0}; gpio_Init(); USART1_Init(); while(1) { // ??????? while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // ???? while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET) { message[i++] = USART_ReceiveData(USART1); } // ???????? if(strstr(message, "LIGHT ON") != NULL) { GPIO_SetBits(GPIOC, GPIO_Pin_13); } else if(strstr(message, "LIGHT OFF") != NULL) { GPIO_ResetBits(GPIOC, GPIO_Pin_13); } else if(strstr(message, "TEMPERATURE") != NULL) { // ?????? float temperature = 0; // TODO: ?????? // ?????? char str[50]; sprintf(str, "Temperature: %.2f", temperature); USART1_SendString((uint8_t*) str); } // ??1? delay(1000); }} void gpio_Init(void){ GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure);} void USART1_Init(void) { USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE);} void USART1_SendByte(uint8_t byte) { while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendD解释每句代码的意思,在每句代码后面写出注释

#include "stm32f10x.h" #include "oled.h" #include "USART.h" void USART1_IRQHandler(void) { if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { uint8_t data = USART_ReceiveData(USART1); if (usart_rx_len < USART_RX_BUF_SIZE) { usart_rx_buf[usart_rx_len++] = data; } } } void USART1_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_Cmd(USART1, ENABLE); }

#include "stm32f10x.h"#include "stdio.h"#define RX_BUFFER_SIZE 9uint8_t rx_buffer[RX_BUFFER_SIZE];uint8_t rx_index = 0;void USART1_Init(void){ GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; // 打开USART1和GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); // 配置USART1的GPIO引脚 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置USART1的通信参数 USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); // 打开USART1 USART_Cmd(USART1, ENABLE);}void USART1_IRQHandler(void){ if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET) { uint8_t data = USART_ReceiveData(USART1); if (rx_index < RX_BUFFER_SIZE) { rx_buffer[rx_index++] = data; } if (rx_index == RX_BUFFER_SIZE) { USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); } }}int main(void){ USART1_Init(); while (1) { // 发送查询指令 USART_SendData(USART1, 0xFF); USART_SendData(USART1, 0x01); USART_SendData(USART1, 0x86); USART_SendData(USART1, 0x00); USART_SendData(USART1, 0x00); USART_SendData(USART1, 0x00); USART_SendData(USART1, 0x00); USART_SendData(USART1, 0x00); USART_SendData(USART1, 0x79); // 等待数据接收完成 rx_index = 0; USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); while (rx_index < RX_BUFFER_SIZE); // 计算甲醛浓度 uint16_t ch2o_raw = (rx_buffer[2] << 8) | rx_buffer[3]; float ch2o_conc = ch2o_raw / 1000.0; // 显示甲醛浓度 printf("CH2O Concentration: %.3f mg/m3\r\n", ch2o_conc); // 等待一段时间后再进行下一次检测 delay_ms(1000); }}void delay_ms(uint32_t ms){ uint32_t i, j; for (i = 0; i < ms; i++) for (j = 0; j < 2000; j++);}代码中串口是不是错了,应该是usart3吧

最新推荐

recommend-type

关于__Federico Milano 的电力系统分析工具箱.zip

1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
recommend-type

mlab-upenn 研究小组的心脏模型模拟.zip

1.版本:matlab2014/2019a/2021a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

:YOLO目标检测算法的挑战与机遇:数据质量、计算资源与算法优化,探索未来发展方向

![:YOLO目标检测算法的挑战与机遇:数据质量、计算资源与算法优化,探索未来发展方向](https://img-blog.csdnimg.cn/7e3d12895feb4651b9748135c91e0f1a.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5rKJ6YaJ77yM5LqO6aOO5Lit,size_20,color_FFFFFF,t_70,g_se,x_16) # 1. YOLO目标检测算法简介 YOLO(You Only Look Once)是一种
recommend-type

def ObjFun(x,y,beta): # 目标函数 """ Logistic regression loss function :param beta: model parameter vector :param x: feature matrix :param y: label vector :return: loss value """ n = x.shape[0] p = x.shape[1] pred = 1 / (1 + np.exp(-np.dot(x, beta))) pred = np.clip(pred, 1e-15, 1 - 1e-15) # 将预测值限制在一个很小的区间内 ObjVal = -np.sum(y * np.log(pred) + (1 - y) * np.log(1 - pred)) / n return ObjVal

好的,根据上面的目标函数,我们可以推导出其对应的梯度向量函数,如下所示: def gradient(x, y, beta): """ Compute gradient of the logistic regression loss function :param beta: model parameter vector :param x: feature matrix :param y: label vector :return: gradient vector """ n = x.shape[0] pred = 1 /
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

:YOLO目标检测算法的最佳实践:模型训练、超参数调优与部署优化,打造高性能目标检测系统

![:YOLO目标检测算法的最佳实践:模型训练、超参数调优与部署优化,打造高性能目标检测系统](https://img-blog.csdnimg.cn/20201024153508415.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1NNRjA1MDQ=,size_16,color_FFFFFF,t_70) # 1. YOLO目标检测算法概述 **1.1 YOLO算法简介** YOLO(You Only Look Once)是一种
recommend-type

pecl-memcache-php7 下载

你可以通过以下步骤来下载 pecl-memcache-php7: 1. 打开终端或命令行工具。 2. 输入以下命令:`git clone https://github.com/websupport-sk/pecl-memcache.git` 3. 进入下载的目录:`cd pecl-memcache` 4. 切换到 php7 分支:`git checkout php7` 5. 构建和安装扩展:`phpize && ./configure && make && sudo make install` 注意:在执行第5步之前,你需要确保已经安装了 PHP 和相应的开发工具。