STM32串口库函数版例程

来源:互联网 发布:卡卡软件下载 编辑:程序博客网 时间:2024/04/30 10:40
定义:
TXD1----- PA9-US1-TX
RXD1----- PA10-US1-RX

速率:115200,n,8,1 

/* Includes ------------------------------------------------------------------*/#include "stm32f10x.h"#include "platform_config.h"#include "stm32f10x_usart.h"#include "misc.h"#include "stdarg.h"/* Private variables ---------------------------------------------------------*/USART_InitTypeDef USART_InitStructure;uint8_t TxBuffer1[] = "USART Interrupt Example: This is USART1 DEMO";  uint8_t RxBuffer1[],rec_f,tx_flag;__IO uint8_t TxCounter1 = 0x00;__IO uint8_t RxCounter1 = 0x00; uint32_t Rec_Len;/* Private function prototypes -----------------------------------------------*/void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void Delay(__IO uint32_t nCount);void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...);char *itoa(int value, char *string, int radix);void USART_Config(USART_TypeDef* USARTx);GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStruct;USART_ClockInitTypeDef USART_ClockInitStruct;/***************************************************************************** 名    称:void ili9325_DrawPicture(u16 StartX,u16 StartY, u8 Dir,u8 *pic)* 功    能:在指定座标范围显示一副图片* 入口参数:StartX     行起始座标*           StartY     列起始座标*Dir   图像显示方向       *           pic        图片头指针* 出口参数:无* 说    明:图片取模格式为水平扫描,16位颜色模式  取模软件img2LCD* 调用方法:ili9325_DrawPicture(0,0,0,(u16*)demo);****************************************************************************/void USART_Config(USART_TypeDef* USARTx){  USART_InitStructure.USART_BaudRate = 115200;//速率115200bps  USART_InitStructure.USART_WordLength = USART_WordLength_8b;//数据位8位  USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位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;//收发模式  /* Configure USART1 */  USART_Init(USARTx, &USART_InitStructure);//配置串口参数函数     /* Enable USART1 Receive and Transmit interrupts */  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);                    //使能接收中断  USART_ITConfig(USART1, USART_IT_TXE, ENABLE);//使能发送缓冲空中断     /* Enable the USART1 */  USART_Cmd(USART1, ENABLE);}/***************************************************************************** 名    称:int main(void)* 功    能:主函数* 入口参数:无* 出口参数:无* 说    明:* 调用方法:无 ****************************************************************************/int main(void){   uint8_t a=0;  /* System Clocks Configuration */  RCC_Configuration();  //系统时钟设置         /*嵌套向量中断控制器       说明了USART1抢占优先级级别0(最多1位) ,和子优先级级别0(最多7位) */   NVIC_Configuration();  //中断源配置  /*对控制LED指示灯的IO口进行了初始化,将端口配置为推挽上拉输出,口线速度为50Mhz。PA9,PA10端口复用为串口1的TX,RX。  在配置某个口线时,首先应对它所在的端口的时钟进行使能。否则无法配置成功,由于用到了端口B, 因此要对这个端口的时钟  进行使能,同时由于用到复用IO口功能用于配置串口。因此还要使能AFIO(复用功能IO)时钟。*/  GPIO_Configuration();  //端口初始化  USART_Config(USART1);  //串口1初始化    USART_OUT(USART1,"****测试串口 *******\r\n");      //向串口1发送开机字符。   USART_OUT(USART1,"***************************************************\r\n");   USART_OUT(USART1,"\r\n");   USART_OUT(USART1,"\r\n");       while (1)  {if(rec_f==1){  //判断是否收到一帧有效数据rec_f=0;USART_OUT(USART1,"\r\n您发送的信息为: \r\n");    USART_OUT(USART1,&TxBuffer1[0]);if(a==0) {GPIO_SetBits(GPIOB, GPIO_Pin_5); a=1;}          //LED1  V6(V3板) V2(MINI板) 明暗闪烁                else {GPIO_ResetBits(GPIOB, GPIO_Pin_5);a=0;  }}  }}/***************************************************************************** 名    称:void Delay(__IO uint32_t nCount)* 功    能:延时函数* 入口参数:无* 出口参数:无* 说    明:* 调用方法:无 ****************************************************************************/void Delay(__IO uint32_t nCount){  for(; nCount != 0; nCount--);}/***************************************************************************** 名    称:void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...)* 功    能:格式化串口输出函数* 入口参数:USARTx:  指定串口Data:   发送数组...:     不定参数* 出口参数:无* 说    明:格式化串口输出函数        "\r"回车符   USART_OUT(USART1, "abcdefg\r")   "\n"换行符   USART_OUT(USART1, "abcdefg\r\n")"%s"字符串   USART_OUT(USART1, "字符串是:%s","abcdefg")"%d"十进制   USART_OUT(USART1, "a=%d",10)* 调用方法:无 ****************************************************************************/void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...){ const char *s;    int d;    char buf[16];    va_list ap;    va_start(ap, Data);while(*Data!=0){                          //判断是否到达字符串结束符if(*Data==0x5c){  //'\'switch (*++Data){case 'r':          //回车符USART_SendData(USARTx, 0x0d);   Data++;break;case 'n':          //换行符USART_SendData(USARTx, 0x0a);Data++;break;default:Data++;    break;} }else if(*Data=='%'){  //switch (*++Data){case 's':  //字符串                s = va_arg(ap, const char *);                for ( ; *s; s++) {                    USART_SendData(USARTx,*s);while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);                }Data++;                break;            case 'd':  //十进制                d = va_arg(ap, int);                itoa(d, buf, 10);                for (s = buf; *s; s++) {                    USART_SendData(USARTx,*s);while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);                }Data++;                break;default:Data++;    break;} }else USART_SendData(USARTx, *Data++);while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);}}/******************************************************整形数据转字符串函数        char *itoa(int value, char *string, int radix)radix=10 标示是10进制非十进制,转换结果为0;      例:d=-379;执行itoa(d, buf, 10); 后buf="-379"     **********************************************************/char *itoa(int value, char *string, int radix){    int     i, d;    int     flag = 0;    char    *ptr = string;    /* This implementation only works for decimal numbers. */    if (radix != 10)    {        *ptr = 0;        return string;    }    if (!value)    {        *ptr++ = 0x30;        *ptr = 0;        return string;    }    /* if this is a negative value insert the minus sign. */    if (value < 0)    {        *ptr++ = '-';        /* Make the value positive. */        value *= -1;    }    for (i = 10000; i > 0; i /= 10)    {        d = value / i;        if (d || flag)        {            *ptr++ = (char)(d + 0x30);            value -= (d * i);            flag = 1;        }    }    /* Null terminate the string. */    *ptr = 0;    return string;} /* NCL_Itoa *//***************************************************************************** 名    称:void RCC_Configuration(void)* 功    能:系统时钟配置为72MHZ, 外设时钟配置* 入口参数:无* 出口参数:无* 说    明:* 调用方法:无 ****************************************************************************/ void RCC_Configuration(void){   SystemInit();    RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 |RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO  , ENABLE);  }/***************************************************************************** 名    称:void GPIO_Configuration(void)* 功    能:通用IO口配置* 入口参数:无* 出口参数:无* 说    明:* 调用方法:****************************************************************************/  void GPIO_Configuration(void){  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;     //LED1控制--PB5  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  GPIO_Init(GPIOB, &GPIO_InitStructure);   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;          //USART1 TX  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     //复用推挽输出  GPIO_Init(GPIOA, &GPIO_InitStructure);     //A端口   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;          //USART1 RX  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    //复用开漏输入  GPIO_Init(GPIOA, &GPIO_InitStructure);          //A端口 }/***************************************************************************** 名    称:void NVIC_Configuration(void)* 功    能:中断源配置* 入口参数:无* 出口参数:无* 说    明:* 调用方法:无 ****************************************************************************/void NVIC_Configuration(void){  /*  结构声明*/  NVIC_InitTypeDef NVIC_InitStructure;  /* Configure the NVIC Preemption Priority Bits */    /* Configure one bit for preemption priority */  /* 优先级组 说明了抢占优先级所用的位数,和子优先级所用的位数   在这里是1, 7 */      NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);      NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;     //设置串口1中断  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;     //抢占优先级 0  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//子优先级为0  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能  NVIC_Init(&NVIC_InitStructure);}
/******************************************************************************//*            STM32F10x Peripherals Interrupt Handlers                        *//******************************************************************************//**  * @brief  This function handles USART1 global interrupt request.  * @param  None  * @retval : None  */void USART1_IRQHandler(void)      //串口1 中断服务程序{  unsigned int i;  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)   //判断读寄存器是否非空  {        RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1);   //将读寄存器的数据缓存到接收缓冲区里    if(RxBuffer1[RxCounter1-2]==0x0d&&RxBuffer1[RxCounter1-1]==0x0a)     //判断结束标志是否是0x0d 0x0a    {  for(i=0; i< RxCounter1; i++) TxBuffer1[i]= RxBuffer1[i];      //将接收缓冲器的数据转到发送缓冲区,准备转发  rec_f=1; //接收成功标志  TxBuffer1[RxCounter1]=0;                                     //发送缓冲区结束符      RxCounter1=0;      }  }    if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)                   //这段是为了避免STM32 USART 第一个字节发不出去的BUG   {      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);     //禁止发缓冲器空中断,   }  }