找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2603|回复: 0
打印 上一主题 下一主题
收起左侧

关于stm32f4串口USART置程序

[复制链接]
跳转到指定楼层
楼主
ID:473192 发表于 2019-1-26 19:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "usart3.h"        

//////////使用printf需要以下代码
struct __FILE
{
        int handle;
};

FILE __stdout;      
//定义_sys_exit()以避免使用半主机模式   
_sys_exit(int x)
{
        x = x;
}
//重定义fputc函数
//重定向c库函数printf到USART3
int fputc(int ch, FILE *f)
{
                /* 发送一个字节数据到USART3 */
                USART_SendData(USART3, (uint8_t) ch);
               
                /* 等待发送完毕 */
                while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);               
        
                return (ch);
}


void uart3_init(void){
   //GPIO端口设置
  GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //使能GPIOb时钟
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//使能USART3时钟

        //串口3对应引脚复用映射
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);
        
        //USART3端口配置
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;        //速度50MHz
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
        GPIO_Init(GPIOB,&GPIO_InitStructure); //初始化PB

   //USART3 初始化设置
        USART_InitStructure.USART_BaudRate = 4800;//波特率设置
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
        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(USART3, &USART_InitStructure); //初始化串口3
        
  USART_Cmd(USART3, ENABLE);  //使能串口3

//               
//        USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启相关中断

//        //Usart3 NVIC 配置
//  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//串口3中断通道
//        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=4;//抢占优先级3
//        NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;                //子优先级3
//        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ通道使能
//        NVIC_Init(&NVIC_InitStructure);        //根据指定的参数初始化VIC寄存器、

}

//u8 Res3;//全局变量
//void USART3_IRQHandler(void)                        //串口3中断服务程序
//{
//        
//        if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)  //接收中断
//        {
//                Res3=USART_ReceiveData(USART3);
//                USART_ClearFlag(USART3,USART_IT_RXNE); //清除标志位
//        }
//        
//}

。h文件
#ifndef __USART3_H
#define __USART3_H        
#include "sys.h"
#include <stdio.h>

extern u8 Res3;

void uart3_init(void);

#endif



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表