求助:帮忙看下我的串口程序

2019-07-14 17:29发布

#include "STM32f30x.h"
#define uint unsigned int
               
int main()
{
   
          GPIO_InitTypeDef GPIO_InitStructure;
          USART_InitTypeDef USART_InitStructure;
                RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//enable clk
          RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
                GPIO_DeInit(GPIOA);
          USART_DeInit(USART1);
        
          GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        
          GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_9;//TX
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
                GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        
          GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//LED
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        
                USART_InitStructure.USART_BaudRate = 115200;//UART
                USART_InitStructure.USART_WordLength = USART_WordLength_8b;
                USART_InitStructure.USART_StopBits = USART_StopBits_1;
                USART_InitStructure.USART_Parity = USART_Parity_No ;
                USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
                USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        

          GPIO_Init(GPIOA, &GPIO_InitStructure);
                USART_Init(USART1, &USART_InitStructure);
               
                USART_Cmd(USART1, ENABLE);//EN UART
               

                        
                USART_SendData(USART1, 0X55);
               
                while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==1)
                {
               
                GPIO_SetBits(GPIOA, GPIO_Pin_5);
                }
        
        
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
18条回答
LANxs
1楼-- · 2019-07-16 08:57
wq644921241 发表于 2018-9-24 11:01
不知道你用的是哪个库,里面官网应该提供有实例,你可以参考下

我改好了  帅哥,谢谢了
LANxs
2楼-- · 2019-07-16 10:28
结贴

#include "stm32f30x.h"
#define uint unsigned int

        uint i,j,k;

void Delay(i)
                {
                for(j=0;j<=i;j++)
                        ;
                }


int main()
{
   
          GPIO_InitTypeDef GPIO_InitStructure;
          USART_InitTypeDef USART_InitStructure;
                RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//enable clk
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
                GPIO_DeInit(GPIOA);
          USART_DeInit(USART1);
       
          GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
       
          GPIO_Init(GPIOA, &GPIO_InitStructure);
          GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
       
          GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_9;//TX
                GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
                GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
       
          GPIO_Init(GPIOA, &GPIO_InitStructure);
          GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
       
          GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//LED
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
               
                GPIO_Init(GPIOA, &GPIO_InitStructure);
       
                USART_InitStructure.USART_BaudRate = 115200;//UART
                USART_InitStructure.USART_WordLength = USART_WordLength_8b;
                USART_InitStructure.USART_StopBits = USART_StopBits_1;
                USART_InitStructure.USART_Parity = USART_Parity_No ;
                USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
                USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
          
                USART_Init(USART1, &USART_InitStructure);
               
                USART_Cmd(USART1, ENABLE);//EN UART
                USART_ClearFlag(USART1,USART_FLAG_TC);
                       
               
                while(1)
                {
                USART_SendData(USART1, 0X55);
                while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==1)
                {
               
                GPIO_SetBits(GPIOA, GPIO_Pin_5);
                Delay(500);
                       
                GPIO_ResetBits(GPIOA, GPIO_Pin_5);
                        Delay(500);
                }
               
        }
       
}
www030
3楼-- · 2019-07-16 13:10
 精彩回答 2  元偷偷看……
LANxs
4楼-- · 2019-07-16 14:17
www030 发表于 2018-9-24 11:35
恭喜楼主!

同喜 同喜
wenminglang
5楼-- · 2019-07-16 16:14
1)USART配置;
2)对应的IO配置;
3)USART异常清除;
4)USART打开运行;
骑猪去天山
6楼-- · 2019-07-16 18:18
不会用库爱莫能肋

一周热门 更多>