完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
刚学单片机, 弄了2天没有搞定,希望大家帮帮忙。谢谢!
void USART_Config_USART1() { GPIO_InitTypeDef GPIO_InitStructure9; GPIO_InitTypeDef GPIO_InitStructure10; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; // Enable GPIO clock RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // Enable USART clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Configure USART Tx as alternate function GPIO_InitStructure9.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure9.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure9.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure9.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure9.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure9); // Configure USART Rx as alternate function GPIO_InitStructure10.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure10.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure10.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure10); // Config AF pin GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); // Initialize USART 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); // Enable USART interrupt USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // Enable USART USART_Cmd(USART1, ENABLE); // Initialize USART interrupt on NVIC NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void USART1_IRQHANDLER(void) { if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET) { USART_GetFlagStatus(USART1, USART_FLAG_TC); // Read one byte from the receive data register USART_SendData(USART1, USART_ReceiveData(USART1)); while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) { } } } |
|
相关推荐
19个回答
|
|
不懂帮顶
|
|
|
|
复用端口,时钟没有打开
|
|
|
|
|
|
|
|
帮顶。。。。。。。。。。
|
|
|
|
谢谢大家先,f429没有复用时钟的概念。
|
|
|
|
就是不行,我实在找不到原因:
void USART_Config_USART1() { GPIO_InitTypeDef GPIO_InitStructure9; GPIO_InitTypeDef GPIO_InitStructure10; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_ClearITPendingBit(USART1, USART_IT_RXNE); USART_ClearFlag(USART1, USART_FLAG_RXNE); // Enable GPIO clock RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // Enable USART clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Configure USART Tx as alternate function GPIO_InitStructure9.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure9.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure9.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure9.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure9.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure9); // Configure USART Rx as alternate function GPIO_InitStructure10.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure10.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure10.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure10); // Config AF pin GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); // Initialize USART 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); // Enable USART interrupt USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // Enable USART USART_Cmd(USART1, ENABLE); #if defined(INNO_DEBUG) USART1_SendDataStrings("USART1 CMD enabled! n"); #endif // Initialize USART interrupt on NVIC NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0xF; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #if defined(INNO_DEBUG) USART1_SendDataStrings("USART1 NVIC enabled! n"); #endif } void USART_Config() { USART_Config_USART1(); } NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* USART configuration */ USART_Config(); #if defined(INNO_DEBUG) USART1_SendDataStrings("USART configuration finished! n"); #endif |
|
|
|
|
|
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
// Enable USART clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); 容易出错的地方,看看参考手册 |
|
|
|
#define RCC_APB2Periph_USART1 ((uint32_t)0x00000010)
#define RCC_AHB1Periph_GPIOA ((uint32_t)0x00000001) 应该也没有问题 |
|
|
|
帮顶.................
|
|
|
|
没有复用模式就用模拟模式试试
|
|
|
|
不懂,帮顶
|
|
|
|
看看帮顶!
|
|
|
|
发现了一点问题,但问题最终没有解决。看似一进中断就死机
// Enable USART interrupt USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // Enable USART USART_Cmd(USART1, ENABLE); #if defined(INNO_DEBUG) USART1_SendDataStrings("USART1 CMD enabled! n"); #endif // Initialize USART interrupt on NVIC NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0xF; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); 问题: 使能中断不能在使能NVIC 表格之前 |
|
|
|
回12楼: 不是没有复用模式,是没有复用时钟
|
|
|
|
|
|
|
|
优先级不对吧,你改成0试试。这我得看一下M4内核手册。
|
|
|
|
谢谢大家,同样的程序,因泡桐样例程序之后,工作正常了:
// NVIC configration NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); // USART configuation USART_Config(); void USART_Config_USART1() { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; // Enable GPIO clock RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // Enable USART clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // Configure USART Tx as alternate function GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure USART Rx as alternate function GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); // Config AF pin GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); // Initialize USART 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); // Enable USART USART_Cmd(USART1, ENABLE); #if defined(INNO_DEBUG) USART1_SendDataStrings("USART1 enabled! n"); #endif // Initialize USART interrupt on NVIC NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0xF; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #if defined(INNO_DEBUG) USART1_SendDataStrings("USART1 NVIC enabled! n"); #endif // Enable USART interrupt USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); #if defined(INNO_DEBUG) USART1_SendDataStrings("USART1 IT_RXNE enabled! n"); #endif //USART_ITConfig(USART1, USART_IT_TXE, ENABLE); #if defined(INNO_DEBUG) USART1_SendDataStrings("USART1 IT_TXE enabled! n"); #endif } void USART_Config() { USART_Config_USART1(); #if defined(INNO_DEBUG) USART1_SendDataStrings("USART1 configuration finished! n"); #endif #if defined(INNO_DEBUG) USART1_SendDataStrings("USART all configuration finished! n"); #endif } it.c void USART1_IRQHandler(void) { uint16_t receiveData = 0; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { receiveData = USART_ReceiveData(USART1); USART1_SendDataCharacter(receiveData); } } USART_GetITStatus && USART_ReceiveData USART_GetFlagStatus && USART_ReceiveData 软件流清标志位都可 |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1252 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1184 浏览 3 评论
2263 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1351 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1774 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-28 11:25 , Processed in 1.011281 second(s), Total 109, Slave 93 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号