请问有没有人用过STM32F030C8T6的串口?

2019-08-08 13:22发布

我写了个程序测试STM32F030的串口,但是用串口调试助手看不到发送过来的数据。用示波器测TX引脚也没波形,请问有谁知道这是什么原因吗?谢谢!

[mw_shl_code=c,true]/** ****************************************************************************** * @file Project/STM32F0xx_StdPeriph_Templates/main.c * @author MCD Application Team * @version V1.5.0 * @date 05-December-2014 * @brief Main program body ****************************************************************************** * @attention * * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2> * * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); * You may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.st.com/software_license_agreement_liberty_v2 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /** @addtogroup STM32F0xx_StdPeriph_Templates * @{ */ /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ /** * @brief GPIO Config program. * @param None * @retval None */ void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; //开启PA、PB时钟 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB,ENABLE); //串口收发引脚配置 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); //RS485控制引脚配置 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); //RS485发送 GPIO_SetBits(GPIOB,GPIO_Pin_12); // //RS485接收 // GPIO_ResetBits(GPIOB,GPIO_Pin_12); //RS485指示灯引脚配置 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_12; GPIO_Init(GPIOA,&GPIO_InitStructure); //点亮RS485通信标志灯 GPIO_SetBits(GPIOA,GPIO_Pin_11|GPIO_Pin_12); } /** * @brief Simple Delay program. * @param None * @retval None */ void Delay(__IO uint32_t count) { for(;count>0;count--); } /** * @brief USART1 Config program. * @param None * @retval None */ void USART1_Config(void) { USART_InitTypeDef USART_InitStructure; //打开USART1时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //PA.2和PA3复用为USART1的收发引脚 GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1); //IO配置 GPIO_Config(); //串口参数初始化 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); //开启串口 USART_Cmd(USART1,ENABLE); } /** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* Add your application code here */ USART1_Config(); /* Infinite loop */ while (1) { //发送数据 USART_SendData(USART1,0x30); //等待发送完成 while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET); //闪烁RS485通信标志灯 GPIO_SetBits(GPIOA,GPIO_Pin_11|GPIO_Pin_12); Delay(0x9ffff); GPIO_ResetBits(GPIOA,GPIO_Pin_11|GPIO_Pin_12); Delay(0x9ffff); } } /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ [/mw_shl_code]


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
11条回答
正点原子
1楼-- · 2019-08-09 12:45
我建议你GPIO_Config(); 
放到:   
 GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
 GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);

这两行代码之前。

设置IO的时候,必须先使能其时钟,你明显GPIO的时钟还没开启,就在设置AFIO了。
正点原子
2楼-- · 2019-08-09 17:29
另外,论坛不保证你所有问题都有满意答案,你去任何其他论坛,都是无法给你所有问题都做解答。

凡事,不要总想着为什么别人不帮你,多想想别人凭什么要帮你。

这世界就是这么残酷。
andychen92
3楼-- · 2019-08-09 21:30
回复【8楼】正点原子:
---------------------------------
谢谢原子哥,误会了,以为已经把我的问题设置成已解决了,不好意思。你说的那个问题我发现了,但还是不行。我并不是希望一定有人能帮我解决,只是想人多力量大,自己没解决就拿出来讨论讨论,看看能不能得到什么启发。毕竟我还没出学校,还真有点天真,想得没那么多。我还是去问那边公司的人吧
孚令
4楼-- · 2019-08-10 02:30
andychen92 发表于 2015-8-19 08:54
回复【8楼】正点原子:
---------------------------------
谢谢原子哥,误会了,以为已经把我的问题设置成 ...

问题解决了吗?我也遇到了这个问题,配置没问题但就是不能用,不知道什么问题
孚令
5楼-- · 2019-08-10 04:44
 精彩回答 2  元偷偷看……

一周热门 更多>