打印

STM32F4学习笔记之USART(使用固件库,查询方式)

[复制链接]
2907|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
feng_710|  楼主 | 2013-3-6 10:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 feng_710 于 2013-3-6 13:30 编辑

1.使能对应USART模块时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USARTx, ENABLE) for USART1 and USART6
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE) for USART2, USART3, UART4 or UART5.

2.使能对应GPIO的时钟
RCC_AHB1PeriphClockCmd() function. (The I/O can be TX, RX, CTS, or/and SCLK).
3.配置对应引脚的复用功能
        GPIO_PinAFConfig(GPIOx, GPIO_PinSourcex, GPIO_AF_USARTx); //引脚映射
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                    // 设置为推挽输出
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;                       //复用模式
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_x | GPIO_Pin_x;              //引脚选择
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_xxMHz;                 //速度选择
        GPIO_Init(GPIOx, &GPIO_InitStructure);                                        //写入配置信息
4.配置USART信息
          USART_InitStructure.USART_BaudRate = xxx;                        //波特率
          USART_InitStructure.USART_WordLength = USART_WordLength_xb;//字长
          USART_InitStructure.USART_StopBits = USART_StopBits_x;        //停止位
          USART_InitStructure.USART_Parity = USART_Parity_No;         //校验位,USART_Parity_Even,USART_Parity_Odd
          USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//硬件流控
          USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;    //工作模式
          USART_Init(USARTx, &USART_InitStructure);                                //写入配置信息
          USART_Cmd(USARTx, ENABLE);                                                    //使能串口模块
5.发送
          USART_SendData(USARTx, char xx);                                            //发送字符
                  while (USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);//等待发送完成
6.接收

        if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)    //查询是否接收到数据
            xxx=USART_ReceiveData(USARTx)                                            //读取接收到的数据
程序举例:

int main(void)
{
        GPIO_InitTypeDef  GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;

        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);   
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);     
        GPIO_StructInit(&GPIO_InitStructure);        
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;   
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;  
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
        GPIO_Init(GPIOA, &GPIO_InitStructure);   
         
          USART_InitStructure.USART_BaudRate = 115200;
          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);
        
        while (1)
        {
                if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
                {
                          USART_SendData(USART1, USART_ReceiveData(USART1));
                          while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
                }
        }
}


沙发
xia_318| | 2015-10-26 14:44 | 只看该作者
不错!

使用特权

评论回复
板凳
forgot| | 2015-10-26 15:17 | 只看该作者
现在全系列都可以使用cube了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

10

主题

51

帖子

3

粉丝