完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
串口
初始化 时钟 注意必须先开启时钟后再进行配置,否则无效 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 端口 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // GPIO_Init(GPIOA, &GPIO_InitStructure);// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);// 串口配置 Usart2_structure.USART_BaudRate=9600; Usart2_structure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; Usart2_structure.USART_Mode=USART_Mode_Rx; Usart2_structure.USART_Parity=USART_Parity_No; Usart2_structure.USART_StopBits=USART_StopBits_1; Usart2_structure.USART_WordLength=USART_WordLength_8b; USART_Init(USART2,&Usart2_structure); Usart2_structure.USART_BaudRate=9600; Usart2_structure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; Usart2_structure.USART_Mode=USART_Mode_Tx; Usart2_structure.USART_Parity=USART_Parity_No; Usart2_structure.USART_StopBits=USART_StopBits_1; Usart2_structure.USART_WordLength=USART_WordLength_8b; 中断配置 注意使能中断时不允许,用|连接符进行多项使能 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ; // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_ITConfig(USART2, USART_IT_TC, ENABLE); 使能 USART_Cmd(USART2,ENABLE); 中断函数 void USART2_IRQHandler(void) { if(USART_GetITStatus(USART2, USART_IT_TC)!=RESET) { ledctr=!ledctr; USART_ClearITPendingBit(USART2,USART_IT_TC); } if(USART_GetITStatus(USART2, USART_IT_RXNE)!=RESET) { ledctr=USART_ReceiveData(USART2); } } |
|
|
|
串口
初始化 时钟 注意必须先开启时钟后再进行配置,否则无效 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 端口 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // GPIO_Init(GPIOA, &GPIO_InitStructure);// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);// 串口配置 Usart2_structure.USART_BaudRate=9600; Usart2_structure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; Usart2_structure.USART_Mode=USART_Mode_Rx; Usart2_structure.USART_Parity=USART_Parity_No; Usart2_structure.USART_StopBits=USART_StopBits_1; Usart2_structure.USART_WordLength=USART_WordLength_8b; USART_Init(USART2,&Usart2_structure); Usart2_structure.USART_BaudRate=9600; Usart2_structure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; Usart2_structure.USART_Mode=USART_Mode_Tx; Usart2_structure.USART_Parity=USART_Parity_No; Usart2_structure.USART_StopBits=USART_StopBits_1; Usart2_structure.USART_WordLength=USART_WordLength_8b; 中断配置 注意使能中断时不允许,用|连接符进行多项使能 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ; // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_ITConfig(USART2, USART_IT_TC, ENABLE); 使能 USART_Cmd(USART2,ENABLE); 中断函数 void USART2_IRQHandler(void) { if(USART_GetITStatus(USART2, USART_IT_TC)!=RESET) { ledctr=!ledctr; USART_ClearITPendingBit(USART2,USART_IT_TC); } if(USART_GetITStatus(USART2, USART_IT_RXNE)!=RESET) { ledctr=USART_ReceiveData(USART2); } } |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1105 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1128 浏览 1 评论
558 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
420 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1011 浏览 2 评论
1604浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
270浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
283浏览 3评论
256浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
245浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-8-18 20:56 , Processed in 0.960359 second(s), Total 78, Slave 62 queries .
Powered by 电子发烧友网
© 2015 www.ws-dc.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号