数据手册上看.STM32F030R8T6 的PA2 PA3端口 是可以使用usart2的.
代码如下
- GPIO_InitTypeDef GPIO_InitStruct;
- USART_InitTypeDef USART_InitStruct;
- NVIC_InitTypeDef NVIC_InitStructure;
- //ʱÖÓÅäÖÃ
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- //gpioÅäÖÃ
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
-
- /* Configure USART Tx as alternate function push-pull */
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- /* Configure USART Rx as alternate function push-pull */
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
-
-
-
- //´®¿ÚģʽÅäÖÃ
- USART_InitStruct.USART_BaudRate =115200;
- USART_InitStruct.USART_WordLength = USART_WordLength_8b;
- USART_InitStruct.USART_StopBits = USART_StopBits_1;
- USART_InitStruct.USART_Parity = USART_Parity_No ;
- USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART2, &USART_InitStruct);
-
- USART_Cmd(USART2, ENABLE);
- USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
-
- NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- // NVIC_Config(USART2_IRQn);
复制代码 发送使用的 USART_SendData(USART2, data); 不是printf;
可以发送也能接受进入中断.但是数据都不对.
我给电脑发送0XAA 接受的是F9 发送0x01 接受的是E0
电脑给芯片发送0xdd 接受的是FB?
何解啊...哪里的问题?
|
应该是系统时钟频率有误引起的,检查晶振和倍频配置,下帖供楼主参考:
https://www.stmcu.org.cn/module/forum/thread-614091-1-1.html
评分
查看全部评分
谢谢大佬,,这么说来可能是 库函数是8MHZ*6=48MHZ 我用的是12MHZ*4=48MHZ 可能只修改了系统时钟而没修改APB2的时钟吧.
请问大佬 这个APB2的修改是哪个啊?
谢谢大佬 解决了
#if !defined (HSE_VALUE)
#define HSE_VALUE ((uint32_t)12000000) /*!< Value of the External oscillator in Hz*/
#endif /* HSE_VALUE */
把这个 从8000000改成12000000就ok了 么么哒~
很好