串口发送代码讲解

新建bsp_usart.c和bsp_usart.h,添加到工程,魔术棒添加头文件所在的文件夹.

上位机下载

bsp_usart.c

#include "bsp_usart.h"

void USART_Config(void)
 {
     GPIO_InitTypeDef GPIO_InitStructure;
     USART_InitTypeDef USART_InitStructure;

    // 打开串口GPIO的时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
     
     // 打开串口外设的时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    // 将USART Tx的GPIO配置为推挽复用模式
    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);

  // 将USART Rx的GPIO配置为浮空输入模式
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
     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);        
 }

/* 发送一个字节 8 位*/
 /*STM32 USART只能一次发送8位*/
void Usart_SendByte(USART_TypeDef* pUSARTx, uint8_t data)
 {
     USART_SendData(pUSARTx, data);
     while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET );
 }

/* 发送两个字节的数据 * 16位*/
 /*分两次发送*/
void Usart_SendHalfWord(USART_TypeDef* pUSARTx, uint16_t data)
 {
     uint8_t temp_h,temp_l;/*高低8位*/
     
     temp_h = (data&0xff00) >> 8 ;
     temp_l = data&0xff;
     /*先发送高8位*/
     USART_SendData(pUSARTx, temp_h);
     while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET );
     /*在发送低8位*/
     USART_SendData(pUSARTx, temp_l);
     while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET );
 }

/* 发送8位数据的数组 */
void Usart_SendArray(USART_TypeDef* pUSARTx, uint8_t *array,uint8_t num)
 {
     uint8_t i;
     for( i=0; i<num; i++ )
   {
         Usart_SendByte(pUSARTx, array[i]);
     }
     //发送8位是判读,TXE,大于8位判断TC
     while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET );
 }

/* 发送字符串 */
void Usart_SendStr(USART_TypeDef* pUSARTx, uint8_t *str)
 {
     uint8_t i=0;
     do
   {
         Usart_SendByte(pUSARTx, *(str+i));
         i++;
     }while(*(str+i) != '\0');
     while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET );
 }

///重定向c库函数printf到串口,重定向后可使用printf函数
int fputc(int ch, FILE *f)
 {
         /* 发送一个字节数据到串口 */
         USART_SendData(USART1, (uint8_t) ch);
         
         /* 等待发送完毕 */
         while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);        
     
         return (ch);
 }


 

bsp_usart.h

#ifndef __BSP_USART_H
 #define __BSP_USART_H

#include "stm32f10x.h"
 #include <stdio.h>

void USART_Config(void);
 void Usart_SendByte(USART_TypeDef* pUSARTx, uint8_t data);
 void Usart_SendHalfWord(USART_TypeDef* pUSARTx, uint16_t data);
 void Usart_SendArray(USART_TypeDef* pUSARTx, uint8_t *array,uint8_t num);
 void Usart_SendStr(USART_TypeDef* pUSARTx, uint8_t *str);



#endif 

 

main.c

#include "stm32f10x.h"   // 相当于51单片机中的  #include <reg51.h>
#include "bsp_led.h"
#include "bsp_exit.h"
#include "bsp_usart.h"



int main(void)
{
    // 来到这里的时候,系统的时钟已经被配置成72M。

//USART和上位机交互
    uint8_t a[10]={100,2,3,4,5,6,7,8,9,10};//声明必须在前面
    USART_Config();
    
    //默认发送的是字符,当不是字符时候,会自动将其转换成相对于的ASCII值 100对应d 
    //当然你也可以这样 'A' 当然你也可以在上位机上选择16进制
    //Usart_SendByte(USART1, 100);
    //Usart_SendByte(USART1,'A');
    
    //Usart_SendHalfWord(USART1, 0xff56);
    //printf( "串口printf函数测试\n" );
    
    //Usart_SendStr(USART1, "欢迎使用秉火STM32F103开发板 \n");
    
    
    Usart_SendArray(USART1, a,10);
    
    
    
    
    
  while(1){
        
    }
}

 

posted @ 2018-04-29 23:33  wenshinlee  阅读(1309)  评论(0编辑  收藏  举报