欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接

程序员文章站 2022-06-08 19:39:03
...

文件下载地址: 点击打开链接

https://download.csdn.net/download/jkson10/10494553

除了之前按照原子的驱动写过一个版本,这是通过结构体的编写来实现串口收发

实现功能

初始化三个串口,调用两个,一个串口发送一个串口接收

串口数据接收除接收数据外还需给出帧有效数据字节长度;

串口可使用USART1USART2USART3,串口参数能*设置;

实现不定长数据帧的串口收发

编译环境:UV5    

设计思路及流程

将串口参数,初始化,中断,数组都写在结构体里面,各个模块分开,最后在主函数中进行数组设置,

变量,优先级设置,时钟使能。在循环中设置串口收发来实现功能。比在原子修改思路更加简洁。


1.建立SCOM.c SCOM.h文件

串口初始化结构:

stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接

 

 串口参数设置结构体:

 stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接

 stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接

中断函数结构:

 stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接 

stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接  

stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接

上述代码主要是判断数据位是否空,然后进行执行

 

主函数:

数组设置,变量,优先级设置,时钟使能

stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接


判断是否小于变量MIAO,小于则代表可以继续接收,然后进入循环。可以3com口任意调用两个:

stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接


Debug模式调试

 stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接


使用助手调试

 stm32f103双串口收发库函数(结构体)版本(调试通过)附上完整代码和下载链接

 

至于怎么debug 使用串口助手,请自行百度


完整代码:

SCOM.C

#include "SCOM.h"
#include "string.h"
#include "sys.h"

extern uint8_t OLED;
extern uint8_t MONE[TX_BUFFER_MAX_SIZE]; //ÉèÖûº³åÇø±äÁ¿//
extern uint8_t MIAO;
SCOM_TypeDef_Struct SCOM1_BASE,SCOM2_BASE,SCOM3_BASE;
////**************************************************´®¿Ú³õʼ»¯½á¹¹*****************************///////
SCOMHardware_TypeDef_Struct SCOM_Hardware[] = {

    { SCOM1_USART,SCOM1_USART_IRQn ,SCOM1_tx_GPIO, SCOM1_tx_GPIO_Pin,SCOM1_rx_GPIO,SCOM1_rx_GPIO_Pin,  
    SCOM1_rx_DMA_Channel, SCOM1_rx_DMA_Channel_IRQn,SCOM1_tx_DMA_Channel,SCOM1_tx_DMA_Channel_IRQn  },  

    { SCOM2_USART,SCOM2_USART_IRQn ,SCOM2_tx_GPIO, SCOM2_tx_GPIO_Pin,SCOM2_rx_GPIO,SCOM2_rx_GPIO_Pin,  
    SCOM2_rx_DMA_Channel, SCOM2_rx_DMA_Channel_IRQn,SCOM2_tx_DMA_Channel,SCOM2_tx_DMA_Channel_IRQn  },  

    { SCOM3_USART,SCOM3_USART_IRQn ,SCOM3_tx_GPIO, SCOM3_tx_GPIO_Pin,SCOM3_rx_GPIO,SCOM3_rx_GPIO_Pin,  
    SCOM3_rx_DMA_Channel, SCOM3_rx_DMA_Channel_IRQn,SCOM3_tx_DMA_Channel,SCOM3_tx_DMA_Channel_IRQn  },  
};

void SCOM_StructInit(SCOM_InitTypeDef_Struct  *SCOMx_InitStruct)
{
	SCOMx_InitStruct->enable_SCOM                               
				=SCOM_ENABLE_DEFAULT;
	SCOMx_InitStruct->usartInitParm.USART_BaudRate             
				=SCOM_BaudRate_DEFAULT;
	SCOMx_InitStruct->usartInitParm.USART_HardwareFlowControl   
				=SCOM_HardwareFlowControl_DEFAULT;
	SCOMx_InitStruct->usartInitParm.USART_WordLength            
				=SCOM_WordLength_DEFAULT;
	SCOMx_InitStruct->usartInitParm.USART_Parity                
				=SCOM_Parity_DEFAULT;
	SCOMx_InitStruct->usartInitParm.USART_StopBits              
				=SCOM_StopBits_DEFAULT;
	SCOMx_InitStruct->usartInitParm.USART_Mode                
				=SCOM_Mode_DEFAULT;
	SCOMx_InitStruct->SCOM_USART_PreemptionPriority            
				=SCOM_USART_PreemptionPriority_DEFAULT;
	SCOMx_InitStruct->SCOM_USART_SubPriority                    
				=SCOM_USART_SubPriority_DEFAULT;
}


//***********************************************´®¿Ú²ÎÊýÉèÖýṹ******************************///
ErrorStatus SCOM_Init(SCOM_TypeDef_Struct* SCOMx, SCOM_InitTypeDef_Struct* SCOMx_InitStruct)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	if	((SCOMx == NULL) || (SCOMx_InitStruct == NULL)) return ERROR;
	
	if(SCOMx==SCOM1)
		SCOM1_BASE.SCOM_Hardware=&SCOM_Hardware[0];
	else if(SCOMx==SCOM2)
		SCOM2_BASE.SCOM_Hardware=&SCOM_Hardware[1];
	else if(SCOMx==SCOM3)
		SCOM3_BASE.SCOM_Hardware=&SCOM_Hardware[2];
	else
		return ERROR;
	
	GPIO_InitStructure.GPIO_Pin = SCOMx->SCOM_Hardware->tx_GPIO_Pin;    //pin½ÅÑ¡Ôñ
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                  //50MHzËÙ¶È
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                    //¸´ÓÃÍÆÍìÊä³ö
	GPIO_Init(SCOMx->SCOM_Hardware->tx_GPIOx, &GPIO_InitStructure);     //°Ñ²ÎÊý´ø½øº¯ÊýÅäÖà 
		 
	GPIO_InitStructure.GPIO_Pin = SCOMx->SCOM_Hardware->rx_GPIO_Pin;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(SCOMx->SCOM_Hardware->rx_GPIOx, &GPIO_InitStructure);          //°Ñ²ÎÊý´ø½øº¯ÊýÅäÖà 
	
	USART_InitStructure.USART_BaudRate              
				=SCOMx_InitStruct->usartInitParm.USART_BaudRate;    //²¨ÌØÂÊ
	
	USART_InitStructure.USART_WordLength            
				=SCOMx_InitStruct->usartInitParm.USART_WordLength;  //Êý¾Ýλ
	
	USART_InitStructure.USART_StopBits              
				=SCOMx_InitStruct->usartInitParm.USART_StopBits;   //ֹͣλ
	
	USART_InitStructure.USART_Parity                
				=SCOMx_InitStruct->usartInitParm.USART_Parity;      //ÆæżУÑé
	
	USART_InitStructure.USART_HardwareFlowControl      //Ó²¼þÁ÷¿ØÖÆ
			  =SCOMx_InitStruct->usartInitParm.USART_HardwareFlowControl; 
	
	USART_InitStructure.USART_Mode                 
				=SCOMx_InitStruct->usartInitParm.USART_Mode;       //°ÑÉÏÃæÅäÖõIJÎÊý´ø½øº¯ÊýÀïÃæ³õʼ»¯´®¿Ú
	
	NVIC_InitStructure.NVIC_IRQChannel                    
				=SCOMx->SCOM_Hardware->USARTx_IRQn;                     //ÉèÖÃÓÅÏȼ¶
			
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority 
				=SCOMx_InitStruct->SCOM_USART_PreemptionPriority ;
			
	NVIC_InitStructure.NVIC_IRQChannelSubPriority         
				=SCOMx_InitStruct->SCOM_USART_SubPriority;
			
	NVIC_InitStructure.NVIC_IRQChannelCmd             
				=ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Init(SCOMx->SCOM_Hardware->USARTx, &USART_InitStructure);
	
	USART_Cmd(SCOMx->SCOM_Hardware->USARTx, ENABLE);                                //´ò¿ª
	
	USART_ITConfig(SCOMx->SCOM_Hardware->USARTx, USART_IT_RXNE, ENABLE);
	
	SCOMx->SCOM_Operating.enable_SCOM         =SCOMx_InitStruct->enable_SCOM;
	SCOMx->SCOM_Operating.rlen                =0;
	SCOMx->SCOM_Operating.USART_rxEndFlag     =RESET;
	
	return SUCCESS;
}



///***************************************´®¿ÚÖжÏÅжϽṹÌå************************************////

ErrorStatus SCOM_On(SCOM_TypeDef_Struct* SCOMx)
{
	if(SCOMx == NULL) return ERROR;
	USART_Cmd(SCOMx->SCOM_Hardware->USARTx, ENABLE);             //´ò¿ª´®¿Ú
	return SUCCESS;                                              //·µ»Ø
}

ErrorStatus SCOM_Off(SCOM_TypeDef_Struct* SCOMx)
{
	if(SCOMx == NULL) return ERROR;
	USART_Cmd(SCOMx->SCOM_Hardware->USARTx, DISABLE);            //
	return SUCCESS;
}

ErrorStatus SCOM_WriteData(SCOM_TypeDef_Struct *SCOMx,uint8_t *data) 
{
	uint8_t t;
	if(SCOMx==NULL) return ERROR;
	
	SCOMx->SCOM_Operating.wlen=MIAO;
	for(t=0;t<MIAO;t++)                                   //Èç¹ûСÓÚÊý×鳤¶È£¬Ã¿½ÓÊÕµ½Ò»¸ö×Ö½Úºó++
	{
		SCOMx->SCOM_Operating.wdata[t]=data[t];
	}
	
	if( (SCOMx->SCOM_Operating.wdata[0]==0x0f) && (SCOMx->SCOM_Operating.wdata[MIAO-1]==0x00) )
	{
		SCOMx->SCOM_Operating.USART_txStartFlag=SET;
	}                                                     //Êý¾ÝÖ¡ÕýÈ· 
	else
	{
		SCOMx->SCOM_Operating.USART_txStartFlag=RESET;
	}
	
	return SUCCESS;
}

uint8_t SCOM_Read_FrameByteNum(SCOM_TypeDef_Struct *SCOMx) //¶ÁÈ¡ÓÐЧÊý¾Ý³¤¶È
{
	uint8_t i;
	i=SCOMx->SCOM_Operating.Frame_ByteNum.FrameByteNum;
	return i;
}

ErrorStatus SCOM_USART_IRQHandler(SCOM_TypeDef_Struct* SCOMx)
{
	uint8_t i;
	if(SCOMx==NULL) return ERROR;
	if(USART_GetITStatus(SCOMx->SCOM_Hardware->USARTx, USART_IT_RXNE)!= RESET)
	{
		i=USART_ReceiveData(SCOMx->SCOM_Hardware->USARTx);
		if((SCOMx->SCOM_Operating.rlen==0)&&(i==0x0F))
		{
			SCOMx->SCOM_Operating.Frame_ByteNum.FrameByteFresh=SET;
		}
		if(SCOMx->SCOM_Operating.Frame_ByteNum.FrameByteFresh==SET)
		{
			SCOMx->SCOM_Operating.rdata[SCOMx->SCOM_Operating.rlen]=i;
			SCOMx->SCOM_Operating.rlen++;
			if( (SCOMx->SCOM_Operating.rlen==MIAO)&&(i==0x00))
			{
				SCOMx->SCOM_Operating.rxflag=SET;
				SCOMx->SCOM_Operating.Frame_ByteNum.FrameByteNum=SCOMx->SCOM_Operating.rlen-2;
				SCOMx->SCOM_Operating.rlen=0;
			}
			else if((SCOMx->SCOM_Operating.rlen==MIAO)&&(i!=0x00))
			{
				SCOMx->SCOM_Operating.rxflag=RESET;
				SCOMx->SCOM_Operating.Frame_ByteNum.FrameByteFresh=RESET;
				SCOMx->SCOM_Operating.rlen=0;
			}
		}
	}
	return SUCCESS;
}




SCOM.h

/**
  ******************************************************************************
  * @file scom.h
  * @author  W.DONG
  * @version V1.1.0
  * @date    15/April/2018
  * @brief   This file provides all the SCOM channel init functions.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, HANGZHOU NORMAL UNIVERSITY SHAOGUOJIAN SHALL NOT BE HELD LIABLE
  * FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS 
  * ARISING FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF
  * THE CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2014 HANGZHOU NORMAL UNIVERSITY SHAOGUOJIAN</center></h2>
  */

/* Define to prevent recursive inclusion -------------------------------------*/

#ifndef _SCOM_H
#define _SCOM_H	

/* Includes ------------------------------------------------------------------*/	 

#include "stm32f10x.h" 	
#include "stm32f10x_dma.h"
#include <string.h>

/** @addtogroup My_Driver
  * @{
  */
	
/** @defgroup An example of using SCOM functions
  * @{
  */
	
	/******************************************************************************************
	 // Define a variable of SCOM_InitTypeDef_Struct type
	 SCOM_InitTypeDef_Struct  SCOM_InitStruct;
	 // Define a variable of SCOM_READ_DATA_Struct type
	 SCOM_READ_DATA_Struct *pdata;
	 // Define a variable of SCOM_WRITE_DATA_Struct type
	 SCOM_WRITE_DATA_Struct *pdata;
	 
	 // Enable GPIOx which are related to SCOMx to APB2 or APB1 peripheral clock. 
	 // GPIOx   are defined at stm32f10x_scom.h and stated at stm32f10x_scom_hardwaretable.c,
   //	user must modify them for different Applications.
	 
	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx, ENABLE);
	 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx,ENABLE);
	 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
	 // Enable AFIO to APB2 peripheral clock.
	 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	 
 *-----------------------------Hardware_Table_Definition--------------------------------
 * SCOMx_USART;  //where x= 1,2,3
 * SCOMx_USART_IRQn;		
 * SCOMx_tx_GPIO;
 * SCOMx_tx_GPIO_Pin;	
 * SCOMx_rx_GPIO;
 * SCOMx_rx_GPIO_Pin;
 * SCOMx_rx_DMA_Channel;
 * SCOMx_rx_DMA_Channel_IRQn;	
 * SCOMx_tx_DMA_Channel;
 * SCOMx_tx_DMA_Channel_IRQn;
 *------------------------------------------------------------------------------------
 		SCOM_StructInit(&SCOM_InitStruct);
		
		// Set the value of SCOM_InitStruct. 
		
		// According to the value of SCOM_InitStruct, initialize SCOMx.
		SCOM_Init(SCOMx,&SCOM_InitStruct);
		
		
	
*******************************************************************************************/
/**
  * @}
  */ 
	
/** @addtogroup SCOM
  * @{
  */ 

/** @defgroup SCOM_Exported_Types 
  * @{
  */ 
#define RX_BUFFER_MAX_SIZE                       36
#define TX_BUFFER_MAX_SIZE                       36	




/** 
  * @brief    SCOM SCOM_READ_DATA structure definition
  */
	
typedef struct __SCOM_READ_DATA_Struct
{  
	 uint8_t      len;
   uint8_t      data[RX_BUFFER_MAX_SIZE];
}  SCOM_READ_DATA_Struct;


/** 
  * @brief    SCOM SCOM_WRITE_DATA structure definition
  */	

typedef struct __SCOM_WRITE_DATA_Struct
{  
	 uint8_t      len;
   uint8_t      data[TX_BUFFER_MAX_SIZE];
}  SCOM_WRITE_DATA_Struct;

/** 
  * @brief    SCOM SCOM_RX_Buffer structure definition
  */


typedef struct __SCOM_FrameByte_Struct
{  
	 FlagStatus      FrameByteFresh;
   uint8_t         FrameByteNum;   //ÓÐЧ֡³¤
} SCOM_FrameByte_Struct;

/** 
  * @brief  SCOM Register structure definition   
  */

typedef struct __SCOMRegister_TypeDef_Struct
{
		FunctionalState                enable_SCOM;
		FunctionalState                enable_DMA;
		USART_InitTypeDef              usartParm;		
		uint8_t                        rlen;
		uint8_t                        rdata[RX_BUFFER_MAX_SIZE];
    FlagStatus                     USART_rxEndFlag;						
		uint8_t                        wlen;
		uint8_t                        wdata[TX_BUFFER_MAX_SIZE];
		FlagStatus                     USART_txStartFlag;
    SCOM_FrameByte_Struct          Frame_ByteNum;	
	
		uint8_t                        rxflag;  
	
} SCOMRegister_TypeDef_Struct;

/** 
  * @brief  SCOM Hardware structure definition   
  */


typedef struct __SCOMHardware_TypeDef_Struct
{
	
		// USART Hardware
		USART_TypeDef*                USARTx;
		uint8_t                       USARTx_IRQn;		

		GPIO_TypeDef *                tx_GPIOx;
		uint16_t                      tx_GPIO_Pin;
			
		GPIO_TypeDef *                rx_GPIOx;
		uint16_t                      rx_GPIO_Pin;
		// DMA  Hardware 
		DMA_Channel_TypeDef *         rx_DMAy_Channelx;
		uint8_t                       rx_DMAy_Channelx_IRQn;	
		DMA_Channel_TypeDef *         tx_DMAy_Channelx;
		uint8_t                       tx_DMAy_Channelx_IRQn;

} SCOMHardware_TypeDef_Struct;


/** 
  * @brief  SCOM Type structure definition 
  */

typedef struct __SCOM_TypeDef_Struct
{
	
	 SCOMHardware_TypeDef_Struct *SCOM_Hardware;	
	 SCOMRegister_TypeDef_Struct SCOM_Operating;

}SCOM_TypeDef_Struct;

/** 
  * @brief  SCOM Init structure definition 
  */

typedef struct __SCOM_InitTypeDef_Struct
{

		FunctionalState             enable_SCOM;

		FunctionalState             enable_DMA;
			
		USART_InitTypeDef           usartInitParm;
			
		uint8_t	                    SCOM_USART_PreemptionPriority;
		uint8_t	                    SCOM_USART_SubPriority;
			
		uint8_t	                    SCOM_DMA_PreemptionPriority;
		uint8_t	                    SCOM_DMA_SubPriority;	
	
}SCOM_InitTypeDef_Struct;


/* ------------------------------- SCOM_nums_define --------------------------------- */
/* It is not permitted to modify SCOM_NUM_MAX greater than 3 */
#define SCOM_NUM_MAX                            3
/* It is not permitted to modify SCOM_NUM_USED greater than SCOM_NUM_MAX */
#define SCOM_NUM_USED                           3


#if(SCOM_NUM_MAX>3)
#error SCOM_NUM_MAX BIGGER THAN 3
#endif

#if(SCOM_NOW_NUM>SCOM_NUM_MAX)
#error SCOM_NUM_USED BIGGER THAN SCOM_NUM_USED
#endif

#if(SCOM_NUM_USED>=1)
extern SCOM_TypeDef_Struct SCOM1_BASE;
#define SCOM1                         (&SCOM1_BASE)      /* SCOM1 selected */
#define SCOM1_USART                   USART1 
#define SCOM1_USART_IRQn		          37  
#define SCOM1_tx_GPIO                 GPIOA
#define SCOM1_tx_GPIO_Pin	            GPIO_Pin_9
#define SCOM1_rx_GPIO                 GPIOA
#define SCOM1_rx_GPIO_Pin             GPIO_Pin_10
#define SCOM1_rx_DMA_Channel          DMA1_Channel5
#define SCOM1_rx_DMA_Channel_IRQn     15  	
#define SCOM1_tx_DMA_Channel          DMA1_Channel4
#define SCOM1_tx_DMA_Channel_IRQn     14          
#else
#define SCOM1                         NULL
#define SCOM1_USART                   NULL 
#define SCOM1_USART_IRQn		          NULL
#define SCOM1_tx_GPIO                 NULL
#define SCOM1_tx_GPIO_Pin	            NULL
#define SCOM1_rx_GPIO                 NULL
#define SCOM1_rx_GPIO_Pin             NULL
#define SCOM1_rx_DMA_Channel          NULL
#define SCOM1_rx_DMA_Channel_IRQn     NULL	
#define SCOM1_tx_DMA_Channel          NULL
#define SCOM1_tx_DMA_Channel_IRQn     NULL
#endif

#if(SCOM_NUM_USED>=2)
extern SCOM_TypeDef_Struct SCOM2_BASE;
#define SCOM2                        (&SCOM2_BASE)      /* SCOM2 selected */
#define SCOM2_USART                   USART2 
#define SCOM2_USART_IRQn		          38   
#define SCOM2_tx_GPIO                 GPIOA
#define SCOM2_tx_GPIO_Pin	            GPIO_Pin_2
#define SCOM2_rx_GPIO                 GPIOA
#define SCOM2_rx_GPIO_Pin             GPIO_Pin_3
#define SCOM2_rx_DMA_Channel          DMA1_Channel6
#define SCOM2_rx_DMA_Channel_IRQn     16  	
#define SCOM2_tx_DMA_Channel          DMA1_Channel7
#define SCOM2_tx_DMA_Channel_IRQn     17  
#else
#define SCOM2                         NULL
#define SCOM2_USART                   NULL 
#define SCOM2_USART_IRQn		          NULL
#define SCOM2_tx_GPIO                 NULL
#define SCOM2_tx_GPIO_Pin	            NULL
#define SCOM2_rx_GPIO                 NULL
#define SCOM2_rx_GPIO_Pin             NULL
#define SCOM2_rx_DMA_Channel          NULL
#define SCOM2_rx_DMA_Channel_IRQn     NULL	
#define SCOM2_tx_DMA_Channel          NULL
#define SCOM2_tx_DMA_Channel_IRQn     NULL
#endif

#if(SCOM_NUM_USED>=3)
extern SCOM_TypeDef_Struct SCOM3_BASE;
#define SCOM3                        (&SCOM3_BASE)      /* SCOM3 selected */
#define SCOM3_USART                   USART3 
#define SCOM3_USART_IRQn		          39  
#define SCOM3_tx_GPIO                 GPIOB
#define SCOM3_tx_GPIO_Pin	            GPIO_Pin_10
#define SCOM3_rx_GPIO                 GPIOB
#define SCOM3_rx_GPIO_Pin             GPIO_Pin_11
#define SCOM3_rx_DMA_Channel          DMA1_Channel3
#define SCOM3_rx_DMA_Channel_IRQn     13  	
#define SCOM3_tx_DMA_Channel          DMA1_Channel2
#define SCOM3_tx_DMA_Channel_IRQn     12  
#else
#define SCOM3                         NULL
#define SCOM3_USART                   NULL 
#define SCOM3_USART_IRQn		          NULL
#define SCOM3_tx_GPIO                 NULL
#define SCOM3_tx_GPIO_Pin	            NULL
#define SCOM3_rx_GPIO                 NULL
#define SCOM3_rx_GPIO_Pin             NULL
#define SCOM3_rx_DMA_Channel          NULL
#define SCOM3_rx_DMA_Channel_IRQn     NULL	
#define SCOM3_tx_DMA_Channel          NULL
#define SCOM3_tx_DMA_Channel_IRQn     NULL
#endif


#define SCOM_ENABLE_DEFAULT   ENABLE
#define SCOM_BaudRate_DEFAULT   9600
#define SCOM_WordLength_DEFAULT   USART_WordLength_8b
#define SCOM_StopBits_DEFAULT   USART_StopBits_1
#define SCOM_Parity_DEFAULT   USART_Parity_No
#define SCOM_HardwareFlowControl_DEFAULT   USART_HardwareFlowControl_None
#define SCOM_Mode_DEFAULT   USART_Mode_Rx|USART_Mode_Tx
#define SCOM_USART_PreemptionPriority_DEFAULT   3
#define SCOM_USART_SubPriority_DEFAULT   3
#define SCOM_DMA_Pre_DEFAULT   0
#define SCOM_DMA_Sub_DEFAULT   0

    
		
/**
  * @}
  */
	
/** @defgroup SCOM_Exported_Functions
  * @{
  */
	
/**
  * @brief  USART1 Interrupt handler.
  * @param  None.
  * @retval None.
  */
void SCOM_USART1_IRQHandler(void);

/**
  * @brief  USART2 Interrupt handler.
  * @param  None.
  * @retval None.
  */
void SCOM_USART2_IRQHandler(void);

/**
  * @brief  USART3 Interrupt handler.
  * @param  None.
  * @retval None.
  */
void SCOM_USART3_IRQHandler(void);

/**
  * @brief  DMA1_Channel2 interrupt SendData service routine
  * @param  None.
  * @retval None.
  */
//void SCOM_DMA1_Channel2_IRQHandler(void);

/**
  * @brief  DMA1_Channel3 interrupt SendData service routine
  * @param  None.
  * @retval None.
  */
//void SCOM_DMA1_Channel3_IRQHandler(void);

/**
  * @brief  DMA1_Channel4 interrupt SendData service routine
  * @param  None.
  * @retval None.
  */
//void SCOM_DMA1_Channel4_IRQHandler(void);

/**
  * @brief  DMA1_Channel5 interrupt SendData service routine
  * @param  None.
  * @retval None.
  */
//void SCOM_DMA1_Channel5_IRQHandler(void);

/**
  * @brief  DMA1_Channel6 interrupt SendData service routine
  * @param  None.
  * @retval None.
  */
//void SCOM_DMA1_Channel6_IRQHandler(void);

/**
  * @brief  DMA1_Channel7 interrupt SendData service routine
  * @param  None.
  * @retval None.
  */
//void SCOM_DMA1_Channel7_IRQHandler(void);	

/**
  * @brief  DMA2_Channel3 interrupt SendData service routine
  * @param  None.
  * @retval None.
  */
//void SCOM_DMA2_Channel3_IRQHandler(void);	
/**
  * @brief  DMA2_Channel5 interrupt SendData service routine
  * @param  None.
  * @retval None.
  */
//void SCOM_DMA2_Channel5_IRQHandler(void);		
/**
  * @brief  Fills each SCOM_InitStruct member with its default value.
  * @param SCOM_InitStruct : pointer to a SCOM_InitTypeDef
  *   structure which will be initialized.
  * @retval : None
  */

/**
  * @brief  Fills each SCOM_InitStruct member with its default value.
  * @param SCOM_InitStruct : pointer to a SCOM_InitTypeDef
  *   structure which will be initialized.
  * @retval : None
  */
void SCOM_StructInit(SCOM_InitTypeDef_Struct *SCOMx_InitStruct);

/**
  * @brief  Initializes the SCOMx  according to the specified
  *   parameters in the SCOM_InitStruct.
  * @param SCOMx: where x can be  1 to SCOM_NUM_USED  to select the SCOM.
  *        SCOM_NUM_USED is defined at stm32f10x_scom.h, can be 1 to SCOM_NUM_MAX, 
	*        and SCOM_NUM_MAX is also defined at stm32f10x_scom.h, can be 1 to 3.
  * @param SCOM_InitStruct : pointer to a SCOM_InitTypeDef_Struct
  *   structure which will be initialized.
  * @retval : ERROR or SUCCESS.
  */
ErrorStatus SCOM_Init(SCOM_TypeDef_Struct* SCOMx, SCOM_InitTypeDef_Struct* SCOMx_InitStruct);

/**
  * @brief  Enable the specified SCOM peripheral.
  * @param SCOMx: where x can be  1 to SCOM_NUM_USED  to select the SCOM.
  *        SCOM_NUM_USED is defined at stm32f10x_scom.h, can be 1 to SCOM_NUM_MAX, 
	*        and SCOM_NUM_MAX is also defined at stm32f10x_scom.h, can be 1 to 3.
  * @retval : ERROR or SUCCESS.
  */
ErrorStatus SCOM_On(SCOM_TypeDef_Struct* SCOMx);

/**
  * @brief  Disable the specified SCOM peripheral.
  * @param SCOMx: where x can be  1 to SCOM_NUM_USED  to select the SCOM.
  *        SCOM_NUM_USED is defined at stm32f10x_scom.h, can be 1 to SCOM_NUM_MAX, 
	*        and SCOM_NUM_MAX is also defined at stm32f10x_scom.h, can be 1 to 3.
  * @retval : ERROR or SUCCESS.
  */
ErrorStatus SCOM_Off(SCOM_TypeDef_Struct* SCOMx);

/**
  * @brief Read data from SCOMx to pdata of pointers..
  * @param SCOMx: where x can be  1 to SCOM_NUM_USED  to select the SCOM.
  *        SCOM_NUM_USED is defined at stm32f10x_scom.h, can be 1 to SCOM_NUM_MAX, 
	*        and SCOM_NUM_MAX is also defined at stm32f10x_scom.h, can be 1 to 3.
  * @param pdata: pointer to a SCOM_READ_DATA_Struct structure.
  * @retval : ERROR or SUCCESS.
  */
//ErrorStatus SCOM_ReadData(SCOM_TypeDef_Struct*SCOMx, SCOM_READ_DATA_Struct* pdata);
ErrorStatus SCOM_ReadData(SCOM_TypeDef_Struct*SCOMx);

//ErrorStatus SCOM_Read_FrameByteNum(SCOM_TypeDef_Struct *SCOMx, SCOM_FrameByte_Struct* FrameByteNum);
uint8_t SCOM_Read_FrameByteNum(SCOM_TypeDef_Struct *SCOMx);
/**
  * @brief Write data from the array of pointers  to SCOMx .
  * @param SCOMx: where x can be  1 to SCOM_NUM_USED  to select the SCOM.
  *        SCOM_NUM_USED is defined at stm32f10x_scom.h, can be 1 to SCOM_NUM_MAX, 
	*        and SCOM_NUM_MAX is also defined at stm32f10x_scom.h, can be 1 to 3.
  * @param pdata: pointer to a SCOM_WRITE_DATA_Struct structure.
  * @retval : ERROR or SUCCESS.
  */
//ErrorStatus SCOM_WriteData(SCOM_TypeDef_Struct* SCOMx, SCOM_WRITE_DATA_Struct* pdata);
ErrorStatus SCOM_WriteData(SCOM_TypeDef_Struct *SCOMx,uint8_t *data);

/**
  * @}
  */
/** @defgroup SCOM_Internaled_Functions
  * @{
  */

/**
  * @brief Scom interrupt service routine(Receive interrupt and send interrupt), 
	         should be called by some USART interrupt service function.
  * @param SCOMx: where x can be  1 to SCOM_NUM_USED  to select the SCOM.
  *        SCOM_NUM_USED is defined at stm32f10x_scom.h, can be 1 to SCOM_NUM_MAX, 
	*        and SCOM_NUM_MAX is also defined at stm32f10x_scom.h, can be 1 to 3.
  * @retval : ERROR or SUCCESS.
  */
ErrorStatus SCOM_USART_IRQHandler(SCOM_TypeDef_Struct* SCOMx);

/**
  * @brief DMA interrupt Receive datas Program, should be called by some DMA interrupt service function.
  * @param SCOMx: where x can be  1 to SCOM_NUM_USED  to select the SCOM.
  *        SCOM_NUM_USED is defined at stm32f10x_scom.h, can be 1 to SCOM_NUM_MAX, 
	*        and SCOM_NUM_MAX is also defined at stm32f10x_scom.h, can be 1 to 3.
  * @retval : ERROR or SUCCESS.
  */
//ErrorStatus SCOM_RX_DMA_IRQHandler(SCOM_TypeDef_Struct* SCOMx);

/**
  * @brief DMA interrupt send datas Program, should be called by some DMA interrupt service function.
  * @param SCOMx: where x can be  1 to SCOM_NUM_USED  to select the SCOM.
  *        SCOM_NUM_USED is defined at stm32f10x_scom.h, can be 1 to SCOM_NUM_MAX, 
	*        and SCOM_NUM_MAX is also defined at stm32f10x_scom.h, can be 1 to 3.
  * @retval : ERROR or SUCCESS.
  */
ErrorStatus	SCOM_TX_DMA_IRQHandler(SCOM_TypeDef_Struct *SCOMx);	

/**
  * @}
  */

#endif /*__STM32F10X_SCOM_H */
/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/********* (C) COPYRIGHT 2014 HANGZHOU NORMAL UNIVERSITY SHAOGUOJIAN *****END OF FILE****/
stm32f10x_scom_it.c
#include "SCOM.h"

void USART1_IRQHandler(void) 
{
	SCOM_USART1_IRQHandler();
}
void SCOM_USART1_IRQHandler(void) 
{
#if(SCOM1_USART_IRQn == 37)
	 SCOM_USART_IRQHandler(SCOM1);
#endif	
	
#if(SCOM2_USART_IRQn == 37)
	 SCOM_USART_IRQHandler(SCOM2);
#endif
	
#if(SCOM3_USART_IRQn == 37)
	SCOM_USART_IRQHandler(SCOM3);
#endif			
}



void USART2_IRQHandler(void) 
{
	SCOM_USART2_IRQHandler();
}
void SCOM_USART2_IRQHandler(void) 
{
#if(SCOM1_USART_IRQn == 38)	
	 SCOM_USART_IRQHandler(SCOM1);
#endif	
	
#if(SCOM2_USART_IRQn == 38)	
	 SCOM_USART_IRQHandler(SCOM2);
#endif
	
#if(SCOM3_USART_IRQn == 38)	
	SCOM_USART_IRQHandler(SCOM3);
#endif	
}

/**
  * @brief  USART3 interrupt service routine
  * @param  None.
  * @retval None.
  */
void USART3_IRQHandler(void) 
{
	SCOM_USART3_IRQHandler();
}
void SCOM_USART3_IRQHandler(void) 
{
#if(SCOM1_USART_IRQn == 39)	
	 SCOM_USART_IRQHandler(SCOM1);
#endif	
	
#if(SCOM2_USART_IRQn == 39)	
	 SCOM_USART_IRQHandler(SCOM2);
#endif
	
#if(SCOM3_USART_IRQn == 39)	
	SCOM_USART_IRQHandler(SCOM3);
#endif		
}

main.c

#include "SCOM.h"
#include "delay.h"

uint8_t MONE[] = {0x0f,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x23,0x13,0x00};
uint8_t MIAO = sizeof(MONE)/sizeof(MONE[0]);
uint8_t Data[128];
																																	
int main(void)
{
	uint8_t i,j,temp=1;
	volatile uint8_t length=0;
	SCOM_InitTypeDef_Struct   SCOMx_InitStruct;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3,ENABLE);
	delay_init();
	SCOM_StructInit(&SCOMx_InitStruct);
	SCOM_Init(SCOM1,&SCOMx_InitStruct);
	SCOM_Init(SCOM2,&SCOMx_InitStruct);
	SCOM_Init(SCOM3,&SCOMx_InitStruct);
	while(1)
	{
				for(i = 0;i < MIAO;i++)// ·¢ËÍÊý¾ÝµÄ³¤¶È
				{
					USART_SendData(SCOM1->SCOM_Hardware->USARTx,MONE[i]); //Ïò´®¿Ú·¢ËÍÊý¾Ý
					while(USART_GetFlagStatus(SCOM1->SCOM_Hardware->USARTx,USART_FLAG_TC)!=SET);
				}
				if(SCOM2->SCOM_Operating.rxflag == SET)//´®¿Ú½ÓÊÕµ½·ûºÏÖ¡µÄ±êÖ¾
				{
					length=SCOM_Read_FrameByteNum(SCOM2);//¶ÁÈ¡ÓÐЧ³¤¶È
			
			
					for(j=0;j<length+2;j++)              //¼õȥ֡ͷ֡βµÈÓÚÊý×鳤¶È
					{
						Data[j]=SCOM2->SCOM_Operating.rdata[j];//½ÓÊÕµ½µÄ·ûºÏÖ¡
					}
			
			
				}
	}	
}




















相关标签: stm32f103