OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2744|回复: 0

STM32F103串口试验

[复制链接]

23

主题

52

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
218
金钱
218
注册时间
2015-1-19
在线时间
28 小时
发表于 2016-5-28 18:56:01 | 显示全部楼层 |阅读模式
      简介:用串口1打印“hello STM32!”;
      [mw_shl_code=c,true]#ifndef __UART_H
#define __UART_H
#include "stm32f10x.h"

void Uart_Config(u32 BaudRate);
void UART_SendData(char byte);
void UART_SendStr(char * str);
#endif
[/mw_shl_code]
[mw_shl_code=c,true]/*****************************
* @file:uart.h
* @brief:config uart & Send Byte
* @author:zx
* @date: 2016/5/28
* @version: V0.1
* @attention: UART1 only
*****************************/
#include "uart.h"

/**
* 功能:初始化串口1波特率 8位数据 一位停止位 无奇偶校验
* 参数:波特率预设值
* 返回值:None
*/
void Uart_Config(u32 BaudRate)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
     
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
/*********************GPIO Config***************************/   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;           //发送管脚
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     //复用推挽
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
    GPIO_Init(GPIOA,&GPIO_InitStructure);
   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;          //接收管脚
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;         //浮空输入
    GPIO_Init(GPIOA,&GPIO_InitStructure);
   
/********************UART Config*****************************/
    USART_InitStructure.USART_BaudRate = BaudRate;
    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);                                       //使能串口外设
}

/**
* 功能:串口发送单个字节
* 参数:字节内容
* 返回值:None
*/
void UART_SendData(char byte)
{
    USART_SendData(USART1, byte);
    while(!USART_GetFlagStatus(USART1,USART_FLAG_TC));
}

/**
* 功能:串口发送字符串
* 参数:字符串指针
* 返回值:None
*/
void UART_SendStr(char * str)
{
     while(*str)
     {
         UART_SendData(*str++);
     }
}
[/mw_shl_code]

[mw_shl_code=c,true]#include "Delay.h"
#include "uart.h"

int main(void)
{
   s8 times = -6;
   SysTick_Init();
   Uart_Config(9600);
   for(;;)
   {
      UART_SendStr("hello STM32!\r\n");
      Delay_ms(800);
   }      
}
[/mw_shl_code]

QQ截图20160528185544.png
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

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

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-9 19:21

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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