标题: stm32串口通信 [打印本页]

作者: wts123    时间: 2016-2-21 21:08
标题: stm32串口通信
前面对于IO口由于外面资料很多,只是写了一些自己的感想,仅此而已,没有别的意思。

       今天想了很久才写自己才开始写,写的不好,大家可以指出,我也好改进。

       对于单片机最基础了出了IO口,就是通信方式了,说的通信方式不得不想到串口通信。

       我自己是学习探索者stm32f4的,这里我就分享自己的感想和自己的问题。


       对于外设的使用首先必然是时钟的使能。

       RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//USART时钟使能
       RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);//IO时钟使能

       然后就是把IO复用

       GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);
       GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);


      接下来就是初始化,在这里我遇到了一个问题
           GPIO_InitTypeDef   GPIO_InitStructure;

           GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;//复用模式
           GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
           GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
           GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
           GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
           GPIO_Init(GPIOA,&GPIO_InitStructure);
    上面是探索者的程序
      可是这样写就不行
          GPIO_InitTypeDef   *GPIO_InitStructure;

           GPIO_InitStructure->GPIO_Mode=GPIO_Mode_AF;//复用模式
           GPIO_InitStructure->GPIO_OType=GPIO_OType_PP;
           GPIO_InitStructure->GPIO_Pin=GPIO_Pin_9;
           GPIO_InitStructure->GPIO_PuPd=GPIO_PuPd_UP;
           GPIO_InitStructure->GPIO_Speed=GPIO_Speed_100MHz;
           GPIO_Init(GPIOA,GPIO_InitStructure);
   个人c语言能力有限,如果有人知道,请指出


       然后就是串口相关的初始化
            设置波特率,奇偶校验,停止位等
        USART_InitStructure.USART_BaudRate=115200;
            USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
            USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
            USART_InitStructure.USART_Parity=USART_Parity_No;
            USART_InitStructure.USART_StopBits=USART_StopBits_1;
            USART_InitStructure.USART_WordLength=USART_WordLength_8b;
       
                  USART_Init(USART1,&USART_InitStructure);


        使用它必然要使能串口
          USART_Cmd(USART1,ENABLE);//使能串口

         这样串口的基本配置就完成了

        单片机嵌入式技术交流 511426545



             






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1