STM32 USART1 USART2 UASRT3 UART4 UART5串口通信测试程序

时间:2021-05-24 16:46:01

STM32 USART1 USART2 UASRT3 UART4 UART5串口通信测试程序

(2014-02-11 20:09:19)STM32 USART1 USART2 UASRT3 UART4 UART5串口通信测试程序转载

分类: 单片机、嵌入系统
// 5个串口均可工作,已经把定时器中断、 串口中断和配置函数写在一个文件中   晶振  8Mhz 
#include "stm32f10x_lib.h"#include"stdio.h"
#define uchar unsigned char#define uint unsigned int#define ulong unsigned long
uint TimingDelay;
#define YES  1#define NO   0
#define  COM1_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_1) // GPIOC->BSRR=0xfd //&=0xfd  //USART1 发送使能#define  COM1_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_1) // GPIOC->BSRR=0x02 //|=0x02  //USART1 禁止发送
#define  COM2_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_2) // GPIOC->BSRR=0xfd //&=0xfd  //USART2 发送使能#define  COM2_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_2) 
#define  COM3_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_3) // GPIOC->BSRR=0xfd //&=0xfd  //USART3 发送使能#define  COM3_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_3) // GPIOC->BSRR=0x02 //|=0x02  //USART3 禁止发送
#define  COM4_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_4) // GPIOC->BSRR=0xfd //&=0xfd  //UART4 发送使能#define  COM4_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_4) // GPIOC->BSRR=0x02 //|=0x02  //UART4 禁止发送

//UART5 上行串口接收中断数据定义uchar ComSynFlag;          //=YES同步建立,UART0内部私有uchar ComReceiveCounter;   //接收字节计数,UART0内部私有uchar ComReceiveData[24];  //接收缓冲,UART0内部私有

GPIO_InitTypeDef GPIO_InitStructure;ErrorStatus HSEStartUpStatus;TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;TIM_OCInitTypeDef  TIM_OCInitStructure;
u16 Status; //定时器累加
void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void TIM2_Configuration(void);void UART5_Configuration(void);void SysTick_Configuration(void)  ;void Com3TxChar(vu8);

vu16 CCR1_Val = 0x0E10;  //360  50Hz  20msvu16 CCR2_Val = 0x0048;  //7200 10hz     100ms    vu16 CCR3_Val = 0x7530;  //30000 10Hzvu16 CCR4_Val = 0x1000; 
u16 capture1 = 0;u16 capture2 = 0;u16 capture3 = 0;u16 capture4 = 0;
int cnt1,cnt2,nt2=50;

//UART1 下行串口接收中断数据定义#define C1_LEN    300      //缓冲区长度uint  Com1RecCnt;          //COM1接收字节计数uchar Com1RecDat[C1_LEN];  //COM1接收缓冲uchar Com1AnsDat[24];uint  Com1AnsCnt;
uint  Com2RecCnt;          //COM2接收字节计数uchar Com2RecDat[C1_LEN];  //COM2接收缓冲uchar Com2AnsDat[24];uint  Com2AnsCnt; 
uint  Com3RecCnt;          //COM3接收字节计数uchar Com3RecDat[C1_LEN];  //COM3接收缓冲uchar Com3AnsDat[24];uint  Com3AnsCnt;
uint  Com4RecCnt;          //COM4接收字节计数uchar Com4RecDat[C1_LEN];  //COM4接收缓冲uchar Com4AnsDat[24];uint  Com4AnsCnt;

uint  Time20msCnt;   //20ms定时器uint  T20msCnt;      //20ms定时器uint  T1sCnt;        //1s定时器

uchar RunCnt;     //RUN LED闪光频率参数//=======================================================================
void RCC_Configuration(){  ErrorStatus HSEStartUpStatus; //定义外部高速晶振启动状态枚举变量  RCC_DeInit(); //复位RCC外部寄存器到默认值  RCC_HSEConfig(RCC_HSE_ON); //打开外部高速晶振  HSEStartUpStatus=RCC_WaitForHSEStartUp(); //等待外部高速时钟准备好
  if(HSEStartUpStatus==SUCCESS)  { //外部高速时钟已经准备好    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //开启FLASH预读缓冲功能,加速FLASH的读取。所有程序中必须的用法,位置:RCC初始化子函数里面,时钟起振之后    FLASH_SetLatency(FLASH_Latency_2); //FLASH时序延迟几个周期,等待总线同步操作。推荐按照单片机系统运行频率,0—24MHz时,取Latency=0;24—48MHz时,取Latency=1;48~72MHz时,取Latency=2。
    RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)==系统时钟/1    RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(高速)(PCLK2)==系统时钟/1     RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(低速)(PCLK1)==系统时钟/2//注:AHB主要负责外部存储器时钟。APB2负责AD,I/O,高级TIM,串口1。APB1负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM。

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //配置PLL时钟==(外部高速晶体时钟/1)* 9 ==72MHz    RCC_PLLCmd(ENABLE); //使能PLL时钟   while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET); //等待PLL时钟就绪   RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系统时钟==PLL时钟   while(RCC_GetSYSCLKSource()!=0x08); //等待系统时钟源的启动}
//==============以下为开启外设时钟的操作============================//// RCC_AHBPeriphClockCmd (ABP2设备1 | ABP2设备2 , ENABLE); //启动AHB设备// RCC_APB2PeriphClockCmd(ABP2设备1 | ABP2设备2 , ENABLE); //启动ABP2设备// RCC_APB1PeriphClockCmd(ABP2设备1 | ABP2设备2 , ENABLE); //启动ABP1设备
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1 , ENABLE); //打开APB2外设
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);   RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5 |RCC_APB1Periph_UART4 |RCC_APB1Periph_USART2 |RCC_APB1Periph_USART3 | RCC_APB1Periph_TIM2,ENABLE);//==============================================================
void SysTick_Configuration(void)         //延时函数初始化{   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);  NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);  SysTick_SetReload(72000);  SysTick_ITConfig(ENABLE);}//=================================void delay(u32 nTime)  SysTick_CounterCmd(SysTick_Counter_Enable);   TimingDelay = nTime;  while(TimingDelay != 0);   SysTick_CounterCmd(SysTick_Counter_Disable);   SysTick_CounterCmd(SysTick_Counter_Clear);}//==================================
void TimingDelay_Decrement(void){  if (TimingDelay != 0x00)  {     TimingDelay--;  }}
void Delay(vu32 nCount){  for(; nCount != 0; nCount--);}//============================================
void NVIC_Configuration(void ){  NVIC_InitTypeDef NVIC_InitStructure; //定义一个中断结构体
// NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //设置中断向量表的起始地址为0x08000000  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置NVIC优先级分组,方式。//注:一共16个优先级,分为抢占式和响应式。两种优先级所占的数量由此代码确定,NVIC_PriorityGroup_x可以是0、1、2、3、4,//分别代表抢占优先级有1、2、4、8、16个和响应优先级有16、8、4、2、1个。规定两种优先级的数量后,所有的中断级别必须在其中选择,//抢占级别高的会打断其他中断优先执行,而响应级别高的会在其他中断执行完优先执行。
  NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQChannel; //通道设置为串口5中断  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断  NVIC_Init(&NVIC_InitStructure); //初始化   //定时器2中断
   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);

   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);
   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);
   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);
   NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);void GPIO_Configuration(void){    GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体
   //--------将UART5 的TX 配置为复用推挽输出 AF_PP---------------------//    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12; //管脚位置定义,标号可以是NONE、ALL、0至15。    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //输出速度50MHz    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //推挽输出模式 Out_PP    GPIO_Init(GPIOC,&GPIO_InitStructure); //E组GPIO初始化
//--------将USART1,USART2 的TX 配置为复用推挽输出 AF_PP---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 |GPIO_Pin_2 | GPIO_Pin_0 | GPIO_Pin_6;   GPIO_Init(GPIOA, &GPIO_InitStructure);
//--------将USART3 的TX 配置为复用推挽输出 AF_PP---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;   GPIO_Init(GPIOB, &GPIO_InitStructure);
//--------将UART4 的TX 配置为复用推挽输出 AF_PP---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;   GPIO_Init(GPIOC, &GPIO_InitStructure);
//--------将UART5 的RX 配置为复用浮空输入 IN_FLOATING---------------------//   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; //管脚位置定义//输入模式下配置输出速度无意义//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz; //输出速度2MHz    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING    GPIO_Init(GPIOD,&GPIO_InitStructure); //C组GPIO初始化
//--------将USART1 USART2 的RX 配置为复用浮空输入 IN_FLOATING---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 |GPIO_Pin_3;   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING    GPIO_Init(GPIOA, &GPIO_InitStructure);
//--------将USART3 的RX 配置为复用浮空输入 IN_FLOATING---------------------//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING   GPIO_Init(GPIOB, &GPIO_InitStructure);
//--------将UART4 的RX 配置为复用浮空输入 IN_FLOATING---------------------//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING   GPIO_Init(GPIOC, &GPIO_InitStructure);}//==============================================================void TIM2_Configuration(void)  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//定时器初始化结构  //TIM_ICInitTypeDef TIM_ICInitStructure;         //通道输入初始化结构         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);  TIM_TimeBaseStructure.TIM_Period = 0xffff;            TIM_TimeBaseStructure.TIM_Prescaler = 499;          //时钟分频  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;     //时钟分割  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //基本初始化   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;  TIM_OC1Init(TIM2, &TIM_OCInitStructure);  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;  TIM_OC2Init(TIM2, &TIM_OCInitStructure);  TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);  //TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  //TIM_OCInitStructure.TIM_Pulse = 255;  //TIM_OC4Init(TIM2, &TIM_OCInitStructure);     TIM_Cmd(TIM2, ENABLE);
  TIM_ClearFlag(TIM2, TIM_FLAG_Update);                    TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update);        //清除TIM2等待中断更新中断标志位
   TIM_ITConfig(TIM2, TIM_IT_CC1 |TIM_IT_CC2 , ENABLE);}
//================================================   void Com5TxChar( vu8 i ) {  USART_SendData(UART5,i); //回发给PC    while(USART_GetFlagStatus(UART5, USART_FLAG_TXE)==RESET); }//---------------------    void Com5TxStr( vu8 *pt, vu8 length )  {       vu8 i;
    for( i=0;i
    {
       Com5TxChar(*pt++);
    }
   // delay(10);   
  }   //---------------------------- void Com1TxChar( vu8 i ) {   USART_SendData(USART1,(u8) i); //回发给PC     while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET)   {    }    //  return i;    void Com1TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com1TxChar(*pt++);  }  //   delay(10);    }    void Com2TxChar( vu8 i ) {  USART_SendData(USART2,(u8) i); //回发给PC   while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==RESET) { }  //  return i;  }     void Com2TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com2TxChar(*pt++);  }  //   delay(10);    }    void Com3TxChar( vu8 i ) {  USART_SendData(USART3,(u8) i); //回发给PC   while(USART_GetFlagStatus(USART3, USART_FLAG_TXE)==RESET) { }  //  return i;  }     void Com3TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com3TxChar(*pt++);  }  //   delay(10);    }    void Com4TxChar( vu8 i ) {  USART_SendData(UART4,(u8) i); //回发给PC    while(USART_GetFlagStatus(UART4, USART_FLAG_TXE)==RESET) { }  //  return i;  }     void Com4TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com4TxChar(*pt++);  }  //   delay(10);    }


    void TIM2_IRQHandler(void){      vu8 i;     if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)     {      TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); cnt1++; cnt2++; if(cnt1>=500) {//   polling=1;  cnt1=0; } if(cnt2>=nt2) //RUNLED,WDI    GPIO_WriteBit(GPIOA, GPIO_Pin_0, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0)));  if(Alert_flag==1)  {  GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_6)));   }  cnt2=0;       }
 Time20msCnt++;  // 秒级硬件延时if( Time20msCnt>=50 && T1sCnt ){Time20msCnt=0;T1sCnt--;}if(T20msCnt){  //20ms单位延时T20msCnt--;}
       capture1 = TIM_GetCapture1(TIM2); TIM_SetCompare1(TIM2, capture1 + CCR1_Val );     }
           if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)        {      TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
  capture2 = TIM_GetCapture2(TIM2); TIM_SetCompare2(TIM2, capture2 + CCR2_Val );           }//********串口COM5配置*****************************
void UART5_Configuration(void){   USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数
   USART_InitStructure.USART_BaudRate = 115200; //波特率115200   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; //打开Rx接收和Tx发送功能   //USART_Init(USART1, &USART_InitStructure);   USART_Init(UART5, &USART_InitStructure); //初始化   // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);   // USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
   USART_ITConfig(UART5,  USART_IT_RXNE, ENABLE); // 若接收数据寄存器满,则产生中断    //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    // USART_ITConfig(UART5, USART_IT_TXE, ENABLE);
    USART_Cmd(UART5, ENABLE); //启动串口     //USART_Cmd(USART1, ENABLE); 

//-----如下语句解决第1个字节无法正确发送出去的问题-----//     USART_ClearFlag(UART5, USART_FLAG_TC); // 清标志   //USART_ClearFlag(USART1, USART_FLAG_TC);}//************串口COM1~COM44配置*********************************
void USART_Configuration(void)
{   USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数
   USART_InitStructure.USART_BaudRate = 1200; //波特率1200   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; //打开Rx接收和Tx发送功能   USART_Init(USART1, &USART_InitStructure);   USART_Init(USART2, &USART_InitStructure);   USART_Init(USART3, &USART_InitStructure);   USART_Init(UART4 , &USART_InitStructure);                    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// 若接收数据寄存器满,则产生中断   USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);   USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);   USART_ITConfig(UART4 , USART_IT_RXNE, ENABLE);
   USART_Cmd(USART1, ENABLE); //启动串口   USART_Cmd(USART2, ENABLE);   USART_Cmd(USART3, ENABLE);   USART_Cmd(UART4, ENABLE);
  //-----如下语句解决第1个字节无法正确发送出去的问题-----//    USART_ClearFlag(USART1, USART_FLAG_TC);// 清标志   USART_ClearFlag(USART2, USART_FLAG_TC);   USART_ClearFlag(USART3, USART_FLAG_TC);   USART_ClearFlag(UART4 , USART_FLAG_TC);}//**********  串口中断函数  ********************void UART5_IRQHandler(void){   uchar x,y,z;   uint checksum1,checksum2;   if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) //若接收数据寄存器满  {    x=USART_ReceiveData(UART5);   }   Com5TxChar(x);}//------------------------------------------------
void USART1_IRQHandler(void){  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //若接收数据寄存器满   {      Com1RecDat[Com1RecCnt]=USART_ReceiveData(USART1);     }   Com1RecCnt++;  Com1RecCnt%=C1_LEN;
  Com1TxChar(USART_ReceiveData(USART1));}//----------------------------------------------
void USART2_IRQHandler(void){   if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //若接收数据寄存器满   {      Com2RecDat[Com2RecCnt]=USART_ReceiveData(USART2);     }    Com2RecCnt++;   Com2RecCnt%=C1_LEN;   Com2TxChar(USART_ReceiveData(USART2));}//-------------------------------------------------
void USART3_IRQHandler(void){  if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //若接收数据寄存器满  {     Com3RecDat[Com3RecCnt]=USART_ReceiveData(USART3);    }    Com3RecCnt++;   Com3RecCnt%=C1_LEN;   Com3TxChar(USART_ReceiveData(USART3));}//----------------------------------------------------
void UART4_IRQHandler(void){  if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) //若接收数据寄存器满  {     Com4RecDat[Com4RecCnt]=USART_ReceiveData(UART4);   }    Com4RecCnt++;   Com4RecCnt%=C1_LEN;   Com4TxChar(USART_ReceiveData(UART4));}//*****************主函数************************int main(void){ u8 ld[69]=0; u8 i;
 RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); UART5_Configuration(); USART_Configuration(); TIM2_Configuration(); SysTick_Configuration();
 for(i=0;i<69;i++) {  ld[i]=i; } Com1TxStr(ld,69); Com2TxStr(ld,69); Com3TxStr(ld,69); Com4TxStr(ld,69); Com5TxStr(ld,69); while(1)   {     }

STM32 USART1 USART2 UASRT3 UART4 UART5串口通信测试程序

(2014-02-11 20:09:19)STM32 USART1 USART2 UASRT3 UART4 UART5串口通信测试程序转载

分类: 单片机、嵌入系统
// 5个串口均可工作,已经把定时器中断、 串口中断和配置函数写在一个文件中   晶振  8Mhz 
#include "stm32f10x_lib.h"#include"stdio.h"
#define uchar unsigned char#define uint unsigned int#define ulong unsigned long
uint TimingDelay;
#define YES  1#define NO   0
#define  COM1_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_1) // GPIOC->BSRR=0xfd //&=0xfd  //USART1 发送使能#define  COM1_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_1) // GPIOC->BSRR=0x02 //|=0x02  //USART1 禁止发送
#define  COM2_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_2) // GPIOC->BSRR=0xfd //&=0xfd  //USART2 发送使能#define  COM2_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_2) 
#define  COM3_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_3) // GPIOC->BSRR=0xfd //&=0xfd  //USART3 发送使能#define  COM3_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_3) // GPIOC->BSRR=0x02 //|=0x02  //USART3 禁止发送
#define  COM4_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_4) // GPIOC->BSRR=0xfd //&=0xfd  //UART4 发送使能#define  COM4_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_4) // GPIOC->BSRR=0x02 //|=0x02  //UART4 禁止发送

//UART5 上行串口接收中断数据定义uchar ComSynFlag;          //=YES同步建立,UART0内部私有uchar ComReceiveCounter;   //接收字节计数,UART0内部私有uchar ComReceiveData[24];  //接收缓冲,UART0内部私有

GPIO_InitTypeDef GPIO_InitStructure;ErrorStatus HSEStartUpStatus;TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;TIM_OCInitTypeDef  TIM_OCInitStructure;
u16 Status; //定时器累加
void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void TIM2_Configuration(void);void UART5_Configuration(void);void SysTick_Configuration(void)  ;void Com3TxChar(vu8);

vu16 CCR1_Val = 0x0E10;  //360  50Hz  20msvu16 CCR2_Val = 0x0048;  //7200 10hz     100ms    vu16 CCR3_Val = 0x7530;  //30000 10Hzvu16 CCR4_Val = 0x1000; 
u16 capture1 = 0;u16 capture2 = 0;u16 capture3 = 0;u16 capture4 = 0;
int cnt1,cnt2,nt2=50;

//UART1 下行串口接收中断数据定义#define C1_LEN    300      //缓冲区长度uint  Com1RecCnt;          //COM1接收字节计数uchar Com1RecDat[C1_LEN];  //COM1接收缓冲uchar Com1AnsDat[24];uint  Com1AnsCnt;
uint  Com2RecCnt;          //COM2接收字节计数uchar Com2RecDat[C1_LEN];  //COM2接收缓冲uchar Com2AnsDat[24];uint  Com2AnsCnt; 
uint  Com3RecCnt;          //COM3接收字节计数uchar Com3RecDat[C1_LEN];  //COM3接收缓冲uchar Com3AnsDat[24];uint  Com3AnsCnt;
uint  Com4RecCnt;          //COM4接收字节计数uchar Com4RecDat[C1_LEN];  //COM4接收缓冲uchar Com4AnsDat[24];uint  Com4AnsCnt;

uint  Time20msCnt;   //20ms定时器uint  T20msCnt;      //20ms定时器uint  T1sCnt;        //1s定时器

uchar RunCnt;     //RUN LED闪光频率参数//=======================================================================
void RCC_Configuration(){  ErrorStatus HSEStartUpStatus; //定义外部高速晶振启动状态枚举变量  RCC_DeInit(); //复位RCC外部寄存器到默认值  RCC_HSEConfig(RCC_HSE_ON); //打开外部高速晶振  HSEStartUpStatus=RCC_WaitForHSEStartUp(); //等待外部高速时钟准备好
  if(HSEStartUpStatus==SUCCESS)  { //外部高速时钟已经准备好    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //开启FLASH预读缓冲功能,加速FLASH的读取。所有程序中必须的用法,位置:RCC初始化子函数里面,时钟起振之后    FLASH_SetLatency(FLASH_Latency_2); //FLASH时序延迟几个周期,等待总线同步操作。推荐按照单片机系统运行频率,0—24MHz时,取Latency=0;24—48MHz时,取Latency=1;48~72MHz时,取Latency=2。
    RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)==系统时钟/1    RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(高速)(PCLK2)==系统时钟/1     RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(低速)(PCLK1)==系统时钟/2//注:AHB主要负责外部存储器时钟。APB2负责AD,I/O,高级TIM,串口1。APB1负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM。

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //配置PLL时钟==(外部高速晶体时钟/1)* 9 ==72MHz    RCC_PLLCmd(ENABLE); //使能PLL时钟   while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET); //等待PLL时钟就绪   RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系统时钟==PLL时钟   while(RCC_GetSYSCLKSource()!=0x08); //等待系统时钟源的启动}
//==============以下为开启外设时钟的操作============================//// RCC_AHBPeriphClockCmd (ABP2设备1 | ABP2设备2 , ENABLE); //启动AHB设备// RCC_APB2PeriphClockCmd(ABP2设备1 | ABP2设备2 , ENABLE); //启动ABP2设备// RCC_APB1PeriphClockCmd(ABP2设备1 | ABP2设备2 , ENABLE); //启动ABP1设备
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1 , ENABLE); //打开APB2外设
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);   RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5 |RCC_APB1Periph_UART4 |RCC_APB1Periph_USART2 |RCC_APB1Periph_USART3 | RCC_APB1Periph_TIM2,ENABLE);//==============================================================
void SysTick_Configuration(void)         //延时函数初始化{   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);  NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);  SysTick_SetReload(72000);  SysTick_ITConfig(ENABLE);}//=================================void delay(u32 nTime)  SysTick_CounterCmd(SysTick_Counter_Enable);   TimingDelay = nTime;  while(TimingDelay != 0);   SysTick_CounterCmd(SysTick_Counter_Disable);   SysTick_CounterCmd(SysTick_Counter_Clear);}//==================================
void TimingDelay_Decrement(void){  if (TimingDelay != 0x00)  {     TimingDelay--;  }}
void Delay(vu32 nCount){  for(; nCount != 0; nCount--);}//============================================
void NVIC_Configuration(void ){  NVIC_InitTypeDef NVIC_InitStructure; //定义一个中断结构体
// NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //设置中断向量表的起始地址为0x08000000  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置NVIC优先级分组,方式。//注:一共16个优先级,分为抢占式和响应式。两种优先级所占的数量由此代码确定,NVIC_PriorityGroup_x可以是0、1、2、3、4,//分别代表抢占优先级有1、2、4、8、16个和响应优先级有16、8、4、2、1个。规定两种优先级的数量后,所有的中断级别必须在其中选择,//抢占级别高的会打断其他中断优先执行,而响应级别高的会在其他中断执行完优先执行。
  NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQChannel; //通道设置为串口5中断  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断  NVIC_Init(&NVIC_InitStructure); //初始化   //定时器2中断
   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);

   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);
   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);
   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);
   NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);void GPIO_Configuration(void){    GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体
   //--------将UART5 的TX 配置为复用推挽输出 AF_PP---------------------//    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12; //管脚位置定义,标号可以是NONE、ALL、0至15。    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //输出速度50MHz    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //推挽输出模式 Out_PP    GPIO_Init(GPIOC,&GPIO_InitStructure); //E组GPIO初始化
//--------将USART1,USART2 的TX 配置为复用推挽输出 AF_PP---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 |GPIO_Pin_2 | GPIO_Pin_0 | GPIO_Pin_6;   GPIO_Init(GPIOA, &GPIO_InitStructure);
//--------将USART3 的TX 配置为复用推挽输出 AF_PP---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;   GPIO_Init(GPIOB, &GPIO_InitStructure);
//--------将UART4 的TX 配置为复用推挽输出 AF_PP---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;   GPIO_Init(GPIOC, &GPIO_InitStructure);
//--------将UART5 的RX 配置为复用浮空输入 IN_FLOATING---------------------//   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; //管脚位置定义//输入模式下配置输出速度无意义//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz; //输出速度2MHz    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING    GPIO_Init(GPIOD,&GPIO_InitStructure); //C组GPIO初始化
//--------将USART1 USART2 的RX 配置为复用浮空输入 IN_FLOATING---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 |GPIO_Pin_3;   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING    GPIO_Init(GPIOA, &GPIO_InitStructure);
//--------将USART3 的RX 配置为复用浮空输入 IN_FLOATING---------------------//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING   GPIO_Init(GPIOB, &GPIO_InitStructure);
//--------将UART4 的RX 配置为复用浮空输入 IN_FLOATING---------------------//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING   GPIO_Init(GPIOC, &GPIO_InitStructure);}//==============================================================void TIM2_Configuration(void)  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//定时器初始化结构  //TIM_ICInitTypeDef TIM_ICInitStructure;         //通道输入初始化结构         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);  TIM_TimeBaseStructure.TIM_Period = 0xffff;            TIM_TimeBaseStructure.TIM_Prescaler = 499;          //时钟分频  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;     //时钟分割  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //基本初始化   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;  TIM_OC1Init(TIM2, &TIM_OCInitStructure);  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;  TIM_OC2Init(TIM2, &TIM_OCInitStructure);  TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);  //TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  //TIM_OCInitStructure.TIM_Pulse = 255;  //TIM_OC4Init(TIM2, &TIM_OCInitStructure);     TIM_Cmd(TIM2, ENABLE);
  TIM_ClearFlag(TIM2, TIM_FLAG_Update);                    TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update);        //清除TIM2等待中断更新中断标志位
   TIM_ITConfig(TIM2, TIM_IT_CC1 |TIM_IT_CC2 , ENABLE);}
//================================================   void Com5TxChar( vu8 i ) {  USART_SendData(UART5,i); //回发给PC    while(USART_GetFlagStatus(UART5, USART_FLAG_TXE)==RESET); }//---------------------    void Com5TxStr( vu8 *pt, vu8 length )  {       vu8 i;
    for( i=0;i
    {
       Com5TxChar(*pt++);
    }
   // delay(10);   
  }   //---------------------------- void Com1TxChar( vu8 i ) {   USART_SendData(USART1,(u8) i); //回发给PC     while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET)   {    }    //  return i;    void Com1TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com1TxChar(*pt++);  }  //   delay(10);    }    void Com2TxChar( vu8 i ) {  USART_SendData(USART2,(u8) i); //回发给PC   while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==RESET) { }  //  return i;  }     void Com2TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com2TxChar(*pt++);  }  //   delay(10);    }    void Com3TxChar( vu8 i ) {  USART_SendData(USART3,(u8) i); //回发给PC   while(USART_GetFlagStatus(USART3, USART_FLAG_TXE)==RESET) { }  //  return i;  }     void Com3TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com3TxChar(*pt++);  }  //   delay(10);    }    void Com4TxChar( vu8 i ) {  USART_SendData(UART4,(u8) i); //回发给PC    while(USART_GetFlagStatus(UART4, USART_FLAG_TXE)==RESET) { }  //  return i;  }     void Com4TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com4TxChar(*pt++);  }  //   delay(10);    }


    void TIM2_IRQHandler(void){      vu8 i;     if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)     {      TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); cnt1++; cnt2++; if(cnt1>=500) {//   polling=1;  cnt1=0; } if(cnt2>=nt2) //RUNLED,WDI    GPIO_WriteBit(GPIOA, GPIO_Pin_0, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0)));  if(Alert_flag==1)  {  GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_6)));   }  cnt2=0;       }
 Time20msCnt++;  // 秒级硬件延时if( Time20msCnt>=50 && T1sCnt ){Time20msCnt=0;T1sCnt--;}if(T20msCnt){  //20ms单位延时T20msCnt--;}
       capture1 = TIM_GetCapture1(TIM2); TIM_SetCompare1(TIM2, capture1 + CCR1_Val );     }
           if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)        {      TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
  capture2 = TIM_GetCapture2(TIM2); TIM_SetCompare2(TIM2, capture2 + CCR2_Val );           }//********串口COM5配置*****************************
void UART5_Configuration(void){   USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数
   USART_InitStructure.USART_BaudRate = 115200; //波特率115200   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; //打开Rx接收和Tx发送功能   //USART_Init(USART1, &USART_InitStructure);   USART_Init(UART5, &USART_InitStructure); //初始化   // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);   // USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
   USART_ITConfig(UART5,  USART_IT_RXNE, ENABLE); // 若接收数据寄存器满,则产生中断    //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    // USART_ITConfig(UART5, USART_IT_TXE, ENABLE);
    USART_Cmd(UART5, ENABLE); //启动串口     //USART_Cmd(USART1, ENABLE); 

//-----如下语句解决第1个字节无法正确发送出去的问题-----//     USART_ClearFlag(UART5, USART_FLAG_TC); // 清标志   //USART_ClearFlag(USART1, USART_FLAG_TC);}//************串口COM1~COM44配置*********************************
void USART_Configuration(void)
{   USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数
   USART_InitStructure.USART_BaudRate = 1200; //波特率1200   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; //打开Rx接收和Tx发送功能   USART_Init(USART1, &USART_InitStructure);   USART_Init(USART2, &USART_InitStructure);   USART_Init(USART3, &USART_InitStructure);   USART_Init(UART4 , &USART_InitStructure);                    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// 若接收数据寄存器满,则产生中断   USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);   USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);   USART_ITConfig(UART4 , USART_IT_RXNE, ENABLE);
   USART_Cmd(USART1, ENABLE); //启动串口   USART_Cmd(USART2, ENABLE);   USART_Cmd(USART3, ENABLE);   USART_Cmd(UART4, ENABLE);
  //-----如下语句解决第1个字节无法正确发送出去的问题-----//    USART_ClearFlag(USART1, USART_FLAG_TC);// 清标志   USART_ClearFlag(USART2, USART_FLAG_TC);   USART_ClearFlag(USART3, USART_FLAG_TC);   USART_ClearFlag(UART4 , USART_FLAG_TC);}//**********  串口中断函数  ********************void UART5_IRQHandler(void){   uchar x,y,z;   uint checksum1,checksum2;   if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) //若接收数据寄存器满  {    x=USART_ReceiveData(UART5);   }   Com5TxChar(x);}//------------------------------------------------
void USART1_IRQHandler(void){  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //若接收数据寄存器满   {      Com1RecDat[Com1RecCnt]=USART_ReceiveData(USART1);     }   Com1RecCnt++;  Com1RecCnt%=C1_LEN;
  Com1TxChar(USART_ReceiveData(USART1));}//----------------------------------------------
void USART2_IRQHandler(void){   if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //若接收数据寄存器满   {      Com2RecDat[Com2RecCnt]=USART_ReceiveData(USART2);     }    Com2RecCnt++;   Com2RecCnt%=C1_LEN;   Com2TxChar(USART_ReceiveData(USART2));}//-------------------------------------------------
void USART3_IRQHandler(void){  if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //若接收数据寄存器满  {     Com3RecDat[Com3RecCnt]=USART_ReceiveData(USART3);    }    Com3RecCnt++;   Com3RecCnt%=C1_LEN;   Com3TxChar(USART_ReceiveData(USART3));}//----------------------------------------------------
void UART4_IRQHandler(void){  if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) //若接收数据寄存器满  {     Com4RecDat[Com4RecCnt]=USART_ReceiveData(UART4);   }    Com4RecCnt++;   Com4RecCnt%=C1_LEN;   Com4TxChar(USART_ReceiveData(UART4));}//*****************主函数************************int main(void){ u8 ld[69]=0; u8 i;
 RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); UART5_Configuration(); USART_Configuration(); TIM2_Configuration(); SysTick_Configuration();
 for(i=0;i<69;i++) {  ld[i]=i; } Com1TxStr(ld,69); Com2TxStr(ld,69); Com3TxStr(ld,69); Com4TxStr(ld,69); Com5TxStr(ld,69); while(1)   {     }

STM32 USART1 USART2 UASRT3 UART4 UART5串口通信测试程序

(2014-02-11 20:09:19)
STM32 USART1 USART2 UASRT3 UART4 UART5串口通信测试程序转载

分类: 单片机、嵌入系统
// 5个串口均可工作,已经把定时器中断、 串口中断和配置函数写在一个文件中   晶振  8Mhz 
#include "stm32f10x_lib.h"#include"stdio.h"
#define uchar unsigned char#define uint unsigned int#define ulong unsigned long
uint TimingDelay;
#define YES  1#define NO   0
#define  COM1_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_1) // GPIOC->BSRR=0xfd //&=0xfd  //USART1 发送使能#define  COM1_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_1) // GPIOC->BSRR=0x02 //|=0x02  //USART1 禁止发送
#define  COM2_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_2) // GPIOC->BSRR=0xfd //&=0xfd  //USART2 发送使能#define  COM2_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_2) 
#define  COM3_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_3) // GPIOC->BSRR=0xfd //&=0xfd  //USART3 发送使能#define  COM3_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_3) // GPIOC->BSRR=0x02 //|=0x02  //USART3 禁止发送
#define  COM4_TX_EN  GPIO_ResetBits(GPIOC,GPIO_Pin_4) // GPIOC->BSRR=0xfd //&=0xfd  //UART4 发送使能#define  COM4_RX_EN  GPIO_SetBits(GPIOC,GPIO_Pin_4) // GPIOC->BSRR=0x02 //|=0x02  //UART4 禁止发送

//UART5 上行串口接收中断数据定义uchar ComSynFlag;          //=YES同步建立,UART0内部私有uchar ComReceiveCounter;   //接收字节计数,UART0内部私有uchar ComReceiveData[24];  //接收缓冲,UART0内部私有

GPIO_InitTypeDef GPIO_InitStructure;ErrorStatus HSEStartUpStatus;TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;TIM_OCInitTypeDef  TIM_OCInitStructure;
u16 Status; //定时器累加
void RCC_Configuration(void);void GPIO_Configuration(void);void NVIC_Configuration(void);void TIM2_Configuration(void);void UART5_Configuration(void);void SysTick_Configuration(void)  ;void Com3TxChar(vu8);

vu16 CCR1_Val = 0x0E10;  //360  50Hz  20msvu16 CCR2_Val = 0x0048;  //7200 10hz     100ms    vu16 CCR3_Val = 0x7530;  //30000 10Hzvu16 CCR4_Val = 0x1000; 
u16 capture1 = 0;u16 capture2 = 0;u16 capture3 = 0;u16 capture4 = 0;
int cnt1,cnt2,nt2=50;

//UART1 下行串口接收中断数据定义#define C1_LEN    300      //缓冲区长度uint  Com1RecCnt;          //COM1接收字节计数uchar Com1RecDat[C1_LEN];  //COM1接收缓冲uchar Com1AnsDat[24];uint  Com1AnsCnt;
uint  Com2RecCnt;          //COM2接收字节计数uchar Com2RecDat[C1_LEN];  //COM2接收缓冲uchar Com2AnsDat[24];uint  Com2AnsCnt; 
uint  Com3RecCnt;          //COM3接收字节计数uchar Com3RecDat[C1_LEN];  //COM3接收缓冲uchar Com3AnsDat[24];uint  Com3AnsCnt;
uint  Com4RecCnt;          //COM4接收字节计数uchar Com4RecDat[C1_LEN];  //COM4接收缓冲uchar Com4AnsDat[24];uint  Com4AnsCnt;

uint  Time20msCnt;   //20ms定时器uint  T20msCnt;      //20ms定时器uint  T1sCnt;        //1s定时器

uchar RunCnt;     //RUN LED闪光频率参数//=======================================================================
void RCC_Configuration(){  ErrorStatus HSEStartUpStatus; //定义外部高速晶振启动状态枚举变量  RCC_DeInit(); //复位RCC外部寄存器到默认值  RCC_HSEConfig(RCC_HSE_ON); //打开外部高速晶振  HSEStartUpStatus=RCC_WaitForHSEStartUp(); //等待外部高速时钟准备好
  if(HSEStartUpStatus==SUCCESS)  { //外部高速时钟已经准备好    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //开启FLASH预读缓冲功能,加速FLASH的读取。所有程序中必须的用法,位置:RCC初始化子函数里面,时钟起振之后    FLASH_SetLatency(FLASH_Latency_2); //FLASH时序延迟几个周期,等待总线同步操作。推荐按照单片机系统运行频率,0—24MHz时,取Latency=0;24—48MHz时,取Latency=1;48~72MHz时,取Latency=2。
    RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)==系统时钟/1    RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(高速)(PCLK2)==系统时钟/1     RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(低速)(PCLK1)==系统时钟/2//注:AHB主要负责外部存储器时钟。APB2负责AD,I/O,高级TIM,串口1。APB1负责DA,USB,SPI,I2C,CAN,串口2345,普通TIM。

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //配置PLL时钟==(外部高速晶体时钟/1)* 9 ==72MHz    RCC_PLLCmd(ENABLE); //使能PLL时钟   while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET); //等待PLL时钟就绪   RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //配置系统时钟==PLL时钟   while(RCC_GetSYSCLKSource()!=0x08); //等待系统时钟源的启动}
//==============以下为开启外设时钟的操作============================//// RCC_AHBPeriphClockCmd (ABP2设备1 | ABP2设备2 , ENABLE); //启动AHB设备// RCC_APB2PeriphClockCmd(ABP2设备1 | ABP2设备2 , ENABLE); //启动ABP2设备// RCC_APB1PeriphClockCmd(ABP2设备1 | ABP2设备2 , ENABLE); //启动ABP1设备
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1 , ENABLE); //打开APB2外设
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);   RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5 |RCC_APB1Periph_UART4 |RCC_APB1Periph_USART2 |RCC_APB1Periph_USART3 | RCC_APB1Periph_TIM2,ENABLE);//==============================================================
void SysTick_Configuration(void)         //延时函数初始化{   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);  NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);  SysTick_SetReload(72000);  SysTick_ITConfig(ENABLE);}//=================================void delay(u32 nTime)  SysTick_CounterCmd(SysTick_Counter_Enable);   TimingDelay = nTime;  while(TimingDelay != 0);   SysTick_CounterCmd(SysTick_Counter_Disable);   SysTick_CounterCmd(SysTick_Counter_Clear);}//==================================
void TimingDelay_Decrement(void){  if (TimingDelay != 0x00)  {     TimingDelay--;  }}
void Delay(vu32 nCount){  for(; nCount != 0; nCount--);}//============================================
void NVIC_Configuration(void ){  NVIC_InitTypeDef NVIC_InitStructure; //定义一个中断结构体
// NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //设置中断向量表的起始地址为0x08000000  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置NVIC优先级分组,方式。//注:一共16个优先级,分为抢占式和响应式。两种优先级所占的数量由此代码确定,NVIC_PriorityGroup_x可以是0、1、2、3、4,//分别代表抢占优先级有1、2、4、8、16个和响应优先级有16、8、4、2、1个。规定两种优先级的数量后,所有的中断级别必须在其中选择,//抢占级别高的会打断其他中断优先执行,而响应级别高的会在其他中断执行完优先执行。
  NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQChannel; //通道设置为串口5中断  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断  NVIC_Init(&NVIC_InitStructure); //初始化   //定时器2中断
   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);

   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);
   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);
   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);
   NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQChannel;   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   NVIC_Init(&NVIC_InitStructure);void GPIO_Configuration(void){    GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体
   //--------将UART5 的TX 配置为复用推挽输出 AF_PP---------------------//    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12; //管脚位置定义,标号可以是NONE、ALL、0至15。    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //输出速度50MHz    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //推挽输出模式 Out_PP    GPIO_Init(GPIOC,&GPIO_InitStructure); //E组GPIO初始化
//--------将USART1,USART2 的TX 配置为复用推挽输出 AF_PP---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 |GPIO_Pin_2 | GPIO_Pin_0 | GPIO_Pin_6;   GPIO_Init(GPIOA, &GPIO_InitStructure);
//--------将USART3 的TX 配置为复用推挽输出 AF_PP---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;   GPIO_Init(GPIOB, &GPIO_InitStructure);
//--------将UART4 的TX 配置为复用推挽输出 AF_PP---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;   GPIO_Init(GPIOC, &GPIO_InitStructure);
//--------将UART5 的RX 配置为复用浮空输入 IN_FLOATING---------------------//   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; //管脚位置定义//输入模式下配置输出速度无意义//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_2MHz; //输出速度2MHz    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING    GPIO_Init(GPIOD,&GPIO_InitStructure); //C组GPIO初始化
//--------将USART1 USART2 的RX 配置为复用浮空输入 IN_FLOATING---------------------//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 |GPIO_Pin_3;   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING    GPIO_Init(GPIOA, &GPIO_InitStructure);
//--------将USART3 的RX 配置为复用浮空输入 IN_FLOATING---------------------//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING   GPIO_Init(GPIOB, &GPIO_InitStructure);
//--------将UART4 的RX 配置为复用浮空输入 IN_FLOATING---------------------//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING   GPIO_Init(GPIOC, &GPIO_InitStructure);}//==============================================================void TIM2_Configuration(void)  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//定时器初始化结构  //TIM_ICInitTypeDef TIM_ICInitStructure;         //通道输入初始化结构         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);  TIM_TimeBaseStructure.TIM_Period = 0xffff;            TIM_TimeBaseStructure.TIM_Prescaler = 499;          //时钟分频  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;     //时钟分割  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //基本初始化   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;  TIM_OC1Init(TIM2, &TIM_OCInitStructure);  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;  TIM_OC2Init(TIM2, &TIM_OCInitStructure);  TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);  //TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  //TIM_OCInitStructure.TIM_Pulse = 255;  //TIM_OC4Init(TIM2, &TIM_OCInitStructure);     TIM_Cmd(TIM2, ENABLE);
  TIM_ClearFlag(TIM2, TIM_FLAG_Update);                    TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update);        //清除TIM2等待中断更新中断标志位
   TIM_ITConfig(TIM2, TIM_IT_CC1 |TIM_IT_CC2 , ENABLE);}
//================================================   void Com5TxChar( vu8 i ) {  USART_SendData(UART5,i); //回发给PC    while(USART_GetFlagStatus(UART5, USART_FLAG_TXE)==RESET); }//---------------------    void Com5TxStr( vu8 *pt, vu8 length )  {       vu8 i;
    for( i=0;i
    {
       Com5TxChar(*pt++);
    }
   // delay(10);   
  }   //---------------------------- void Com1TxChar( vu8 i ) {   USART_SendData(USART1,(u8) i); //回发给PC     while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET)   {    }    //  return i;    void Com1TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com1TxChar(*pt++);  }  //   delay(10);    }    void Com2TxChar( vu8 i ) {  USART_SendData(USART2,(u8) i); //回发给PC   while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==RESET) { }  //  return i;  }     void Com2TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com2TxChar(*pt++);  }  //   delay(10);    }    void Com3TxChar( vu8 i ) {  USART_SendData(USART3,(u8) i); //回发给PC   while(USART_GetFlagStatus(USART3, USART_FLAG_TXE)==RESET) { }  //  return i;  }     void Com3TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com3TxChar(*pt++);  }  //   delay(10);    }    void Com4TxChar( vu8 i ) {  USART_SendData(UART4,(u8) i); //回发给PC    while(USART_GetFlagStatus(UART4, USART_FLAG_TXE)==RESET) { }  //  return i;  }     void Com4TxStr( vu8 *pt, vu8 length )  {      vu8 i;  for(i=0;i Com4TxChar(*pt++);  }  //   delay(10);    }


    void TIM2_IRQHandler(void){      vu8 i;     if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)     {      TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); cnt1++; cnt2++; if(cnt1>=500) {//   polling=1;  cnt1=0; } if(cnt2>=nt2) //RUNLED,WDI    GPIO_WriteBit(GPIOA, GPIO_Pin_0, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0)));  if(Alert_flag==1)  {  GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_6)));   }  cnt2=0;       }
 Time20msCnt++;  // 秒级硬件延时if( Time20msCnt>=50 && T1sCnt ){Time20msCnt=0;T1sCnt--;}if(T20msCnt){  //20ms单位延时T20msCnt--;}
       capture1 = TIM_GetCapture1(TIM2); TIM_SetCompare1(TIM2, capture1 + CCR1_Val );     }
           if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)        {      TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
  capture2 = TIM_GetCapture2(TIM2); TIM_SetCompare2(TIM2, capture2 + CCR2_Val );           }//********串口COM5配置*****************************
void UART5_Configuration(void){   USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数
   USART_InitStructure.USART_BaudRate = 115200; //波特率115200   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; //打开Rx接收和Tx发送功能   //USART_Init(USART1, &USART_InitStructure);   USART_Init(UART5, &USART_InitStructure); //初始化   // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);   // USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
   USART_ITConfig(UART5,  USART_IT_RXNE, ENABLE); // 若接收数据寄存器满,则产生中断    //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    // USART_ITConfig(UART5, USART_IT_TXE, ENABLE);
    USART_Cmd(UART5, ENABLE); //启动串口     //USART_Cmd(USART1, ENABLE); 

//-----如下语句解决第1个字节无法正确发送出去的问题-----//     USART_ClearFlag(UART5, USART_FLAG_TC); // 清标志   //USART_ClearFlag(USART1, USART_FLAG_TC);}//************串口COM1~COM44配置*********************************
void USART_Configuration(void)
{   USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数
   USART_InitStructure.USART_BaudRate = 1200; //波特率1200   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; //打开Rx接收和Tx发送功能   USART_Init(USART1, &USART_InitStructure);   USART_Init(USART2, &USART_InitStructure);   USART_Init(USART3, &USART_InitStructure);   USART_Init(UART4 , &USART_InitStructure);                    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// 若接收数据寄存器满,则产生中断   USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);   USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);   USART_ITConfig(UART4 , USART_IT_RXNE, ENABLE);
   USART_Cmd(USART1, ENABLE); //启动串口   USART_Cmd(USART2, ENABLE);   USART_Cmd(USART3, ENABLE);   USART_Cmd(UART4, ENABLE);
  //-----如下语句解决第1个字节无法正确发送出去的问题-----//    USART_ClearFlag(USART1, USART_FLAG_TC);// 清标志   USART_ClearFlag(USART2, USART_FLAG_TC);   USART_ClearFlag(USART3, USART_FLAG_TC);   USART_ClearFlag(UART4 , USART_FLAG_TC);}//**********  串口中断函数  ********************void UART5_IRQHandler(void){   uchar x,y,z;   uint checksum1,checksum2;   if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) //若接收数据寄存器满  {    x=USART_ReceiveData(UART5);   }   Com5TxChar(x);}//------------------------------------------------
void USART1_IRQHandler(void){  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //若接收数据寄存器满   {      Com1RecDat[Com1RecCnt]=USART_ReceiveData(USART1);     }   Com1RecCnt++;  Com1RecCnt%=C1_LEN;
  Com1TxChar(USART_ReceiveData(USART1));}//----------------------------------------------
void USART2_IRQHandler(void){   if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //若接收数据寄存器满   {      Com2RecDat[Com2RecCnt]=USART_ReceiveData(USART2);     }    Com2RecCnt++;   Com2RecCnt%=C1_LEN;   Com2TxChar(USART_ReceiveData(USART2));}//-------------------------------------------------
void USART3_IRQHandler(void){  if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //若接收数据寄存器满  {     Com3RecDat[Com3RecCnt]=USART_ReceiveData(USART3);    }    Com3RecCnt++;   Com3RecCnt%=C1_LEN;   Com3TxChar(USART_ReceiveData(USART3));}//----------------------------------------------------
void UART4_IRQHandler(void){  if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) //若接收数据寄存器满  {     Com4RecDat[Com4RecCnt]=USART_ReceiveData(UART4);   }    Com4RecCnt++;   Com4RecCnt%=C1_LEN;   Com4TxChar(USART_ReceiveData(UART4));}//*****************主函数************************int main(void){ u8 ld[69]=0; u8 i;
 RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); UART5_Configuration(); USART_Configuration(); TIM2_Configuration(); SysTick_Configuration();
 for(i=0;i<69;i++) {  ld[i]=i; } Com1TxStr(ld,69); Com2TxStr(ld,69); Com3TxStr(ld,69); Com4TxStr(ld,69); Com5TxStr(ld,69); while(1)   {     }