文件下载地址:点击打开连接数组
实现功能:函数
初始化三个串口,调用两个,一个串口发送一个串口接收ui
串口数据接收除接收数据外还需给出帧有效数据字节长度;spa
串口可以使用USART1、USART2、USART3,串口参数能自由设置;.net
实现不定长数据帧的串口收发debug
设计思路及流程:设计
将中断串口参数设置初始化,中断函数的处理写在USART.C 文件里面,再使用主函数文件进行调用。3d
1. 创建usart.c usart.h 文件调试
3个串口的串口参数能自由设置,结构体初始化code
Usart1:
Usart2:
\
Usart3:
中断函数编写:
定义中断函数数组
串口一中断函数;
if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)//
接收中断,每接收一个字节(8位二进制数据),中断一次,每次中断(即每接收一个数据)都执行一次下列程序
串口二中断函数;
串口三中断函数;
字符串处理函数:
Usaert.h
设置最大接收字节数,使能串口
2.主函数实现:
Main.c
包含头文件,定义数组
定义变量,延时,串口初始化
While 循环内 设置数组长度length每次接收到一个字节的数据后usart1_in++,
2. 实际功能实现任意两个串口收发
引脚设置: PA9 USART1_TX
PA10 USART1_RX
发送串口修改选择箭头所示的串口名,能够修改成USART2,USART3
到目前为止这种方法编写的程序只能进入debug模式进行演示,还必须得分步调试,其它串口才能收到,如图所示
话很少说,直接上代码
串口二接收状况
串口三接收状况
USART.C
#include "sys.h" #include "usart.h" #include <stdio.h> #include <stdlib.h> #if SYSTEM_SUPPORT_UCOS #include "includes.h" #endif #if 1 #pragma import(__use_no_semihosting) //±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý struct __FILE { int handle; }; FILE __stdout; //±ÜÃâʹÓðëÖ÷»úģʽ _sys_exit(int x) { x = x; } //Öض¨Òåfputcº¯Êý int fputc1(int ch, FILE *f) { while((USART1->SR&0X40)==0);//Ñ»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï USART1->DR = (u8) ch; return ch; } int fputc2(int ch, FILE *f) { while((USART2->SR&0X40)==0);//Ñ»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï USART2->DR = (u8) ch; return ch; } int fputc3(int ch, FILE *f) { while((USART3->SR&0X40)==0);//Ñ»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï USART3->DR = (u8) ch; return ch; } #endif #if EN_USARTx_RX //Èç¹ûʹÄÜÁ˽ÓÊÕ //´®¿Ú1ÖжϷþÎñ³ÌÐò u8 USART_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú. //½ÓÊÕ״̬ //bit15£¬ ½ÓÊÕÍê³É±êÖ¾ //bit14£¬ ½ÓÊÕµ½0x0d //bit13~0£¬ ½ÓÊÕµ½µÄÓÐЧ×Ö½ÚÊýÄ¿ u16 USART_RX_STA=0; //½ÓÊÕ״̬±ê¼Ç void usart1_init(u32 bound) { //GPIO¶Ë¿ÚÉèÖà GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);//ʹÄÜUSART1£¬GPIOAʱÖÓÒÔ¼°¸´Óù¦ÄÜʱÖÓ //USART1_TX PA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö GPIO_Init(GPIOA, &GPIO_InitStructure); //USART1_RX PA.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë GPIO_Init(GPIOA, &GPIO_InitStructure); //Usart1 NVIC ÅäÖà NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//ÇÀÕ¼ÓÅÏȼ¶1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×ÓÓÅÏȼ¶0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷ //USART ³õʼ»¯ÉèÖà USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ USART_InitStructure.USART_StopBits = USART_StopBits_2;//Ò»¸öֹͣλ 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_ITConfig(USART1, USART_IT_TXE, ENABLE);//ʹÄÜÖÐ¶Ï USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿ªÆôÖÐ¶Ï USART_Cmd(USART1, ENABLE); //ʹÄÜ´®¿Ú } void usart2_init(u32 bound) { //GPIO¶Ë¿ÚÉèÖà GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB2Periph_AFIO, ENABLE); //ʹÄÜUSART2£¬GPIOAʱÖÓÒÔ¼°¸´Óù¦ÄÜʱÖÓ //USART2_TX PA.2 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö GPIO_Init(GPIOA, &GPIO_InitStructure); //USART2_RX PA.3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);//¸¡¿ÕÊäÈë //Usart2 NVIC ÅäÖà NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//ÇÀÕ¼ÓÅÏȼ¶1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×ÓÓÅÏȼ¶0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷ //USART ³õʼ»¯ÉèÖà USART_InitStructure.USART_BaudRate =115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ USART_InitStructure.USART_StopBits = USART_StopBits_2;//Ò»¸öֹͣλ 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(USART2, &USART_InitStructure); //³õʼ»¯´®¿Ú // USART_ITConfig(USART2, USART_IT_TXE, ENABLE);//ʹÄÜÖÐ¶Ï USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//¿ªÆôÖÐ¶Ï USART_Cmd(USART2, ENABLE); //ʹÄÜ´®¿Ú } void usart3_init(u32 bound) { //GPIO¶Ë¿ÚÉèÖà GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);//ʹÄÜUSART1£¬GPIOAʱÖÓÒÔ¼°¸´Óù¦ÄÜʱÖÓ //USART3 _TX PA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö GPIO_Init(GPIOA, &GPIO_InitStructure); //USART1_RX PA.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë GPIO_Init(GPIOA, &GPIO_InitStructure); //Usart3 NVIC ÅäÖà NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//ÇÀÕ¼ÓÅÏȼ¶1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×ÓÓÅÏȼ¶0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷ //USART ³õʼ»¯ÉèÖà USART_InitStructure.USART_BaudRate = 115200;//Ò»°ãÉèÖÃΪ115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ USART_InitStructure.USART_StopBits = USART_StopBits_2;//Ò»¸öֹͣλ 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_ITConfig(USART3, USART_IT_TXE, ENABLE);//ʹÄÜÖÐ¶Ï USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//¿ªÆôÖÐ¶Ï USART_Cmd(USART3, ENABLE); //ʹÄÜ´®¿Ú } /* ***************************************ÖжϺ¯Êý ********************************************************/ #define TxBufferSize1 (countof(TxBuffer1)-1) #define TxBufferSize2 (countof(TxBuffer2)-1) #define TxBufferSize3 (countof(TxBuffer3)-1) #define RxBufferSize1 (countof(TxBuffer1)-1) #define RxBufferSize2 (countof(TxBuffer2)-1) #define RxBufferSize3 (countof(TxBuffer3)-1) #define countof(a) (sizeof(a)/sizeof(*(a))) //±íʾÊý×éaÖеÄÔªËظöÊý char TxBuffer1[27]=""; char TxBuffer2[27]=""; char TxBuffer3[27]=""; char RxBuffer1[RxBufferSize1]; char RxBuffer2[RxBufferSize2]; char RxBuffer3[RxBufferSize3]; __IO uint8_t TxCounter1 = 0; __IO uint8_t TxCounter2 = 0; __IO uint8_t TxCounter3 = 0; __IO uint8_t RxCounter1 = 0; __IO uint8_t RxCounter2 = 0; __IO uint8_t RxCounter3 = 0; uint8_t NbrOfDataToTransfer1 = TxBufferSize1; uint8_t NbrOfDataToTransfer2 = TxBufferSize2; uint8_t NbrOfDataToTransfer3 = TxBufferSize3; uint8_t NbrOfDataToRead1 = RxBufferSize1; uint8_t NbrOfDataToRead2 = RxBufferSize2; uint8_t NbrOfDataToRead3 = RxBufferSize3; /****************************************************************´®¿Ú1ÖжϷþÎñ³ÌÐò*/ void USART1_IRQHandler(void) { unsigned int i; /*½ÓÊÕÖжÏ*/ if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET) { RxBuffer1[RxCounter1++] = USART_ReceiveData(USART1); if(RxCounter1 == NbrOfDataToRead1) //½ÓÊÕÊý¾Ý´ïµ½ÐèÒª£¬Ôò½«Êý×鸴ÖƵ½·¢ËÍÊý×飬²¢ÖñêÖ¾ { for(i=0; i< RxCounter1; i++) TxBuffer1[i] = RxBuffer1[i]; RxCounter1=0; TxCounter1=0; USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //´ò¿ª·¢ËÍÖÐ¶Ï } } /*USART1·¢ËÍÖжÏ*/ // if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // { // USART_SendData(USART1, TxBuffer1[TxCounter1++]); // if(TxCounter1 == NbrOfDataToTransfer1)//·¢ËÍÊý¾ÝÍê³É // { // USART_ITConfig(USART1, USART_IT_TXE, DISABLE); //¹Ø±Õ·¢ËÍÖÐ¶Ï // } // } } /*************************************************************************************´®¿Ú2ÖжϷþÎñ³ÌÐò*/ void USART2_IRQHandler(void) { unsigned int j; /*½ÓÊÕÖжÏ*/ if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) { RxBuffer2[RxCounter2++] = USART_ReceiveData(USART2); if(RxCounter2 == NbrOfDataToRead2) //½ÓÊÕÊý¾Ý´ïµ½ÐèÒª£¬Ôò½«Êý×鸴ÖƵ½·¢ËÍÊý×飬²¢ÖñêÖ¾ { for(j=0; j< RxCounter2; j++) TxBuffer2[j] = RxBuffer2[j]; RxCounter2=0; TxCounter2=0; USART_ITConfig(USART2, USART_IT_TXE, ENABLE); //´ò¿ª·¢ËÍÖÐ¶Ï } } /*USART2·¢ËÍÖжÏ*/ // if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET) // { // USART_SendData(USART2, TxBuffer2[TxCounter2++]); // if(TxCounter2 == NbrOfDataToTransfer2)//·¢ËÍÊý¾ÝÍê³É // { // USART_ITConfig(USART2, USART_IT_TXE, DISABLE); //¹Ø±Õ·¢ËÍÖÐ¶Ï // } // } } /****************************************************************´®¿Ú3ÖжϷþÎñ³ÌÐò*/ void USART3_IRQHandler(void) { unsigned int i; /*½ÓÊÕÖжÏ*/ if(USART_GetITStatus(USART3,USART_IT_RXNE) != RESET) { RxBuffer3[RxCounter1++] = USART_ReceiveData(USART1); if(RxCounter3 == NbrOfDataToRead1) //½ÓÊÕÊý¾Ý´ïµ½ÐèÒª£¬Ôò½«Êý×鸴ÖƵ½·¢ËÍÊý×飬²¢ÖñêÖ¾ { for(i=0; i< RxCounter3; i++) TxBuffer3[i] = RxBuffer3[i]; RxCounter3=0; TxCounter3=0; USART_ITConfig(USART3, USART_IT_TXE, ENABLE); //´ò¿ª·¢ËÍÖÐ¶Ï } } /*USART1·¢ËÍÖжÏ*/ // // if(USART_GetITStatus(USART3, USART_IT_TXE) != RESET) // { // USART_SendData(USART3, TxBuffer3[TxCounter3++]); // if(TxCounter3 == NbrOfDataToTransfer3)//·¢ËÍÊý¾ÝÍê³É // { // USART_ITConfig(USART3, USART_IT_TXE, DISABLE); //¹Ø±Õ·¢ËÍÖÐ¶Ï // } // } } ///**********************************************************************///// char u0[]=""; char u1[]=""; char u2[]=""; char u3[]=""; char u4[]=""; char u5[]=""; char u6[]=""; int num0=0; int num1=0; int num2=0; int num3=0; int num4=0; int num5=0; int num6=0; void str_deal() // ×Ö·û´®´¦Àíº¯Êý { strncpy(u0,RxBuffer1+6,2); num0=u0[0]+u0[1]*256; strncpy(u1,RxBuffer1+8,2); num1=u1[0]+u1[1]*256; num1=65536-num1; strncpy(u2,RxBuffer1+10,2); num2=u2[0]+u2[1]*256; strncpy(u3,RxBuffer1+12,2); num3=u3[0]+u3[1]*256; strncpy(u4,RxBuffer1+14,2); num4=u4[0]+u4[1]*256; strncpy(u5,RxBuffer1+16,1); num5=u5[0]; strncpy(u6,RxBuffer1+17,1); num6=u6[0]; } #endifUSAER.h
#ifndef __USART_H #define __USART_H #include "stdio.h" #include "sys.h" #define USART_REC_LEN 200 //¶¨Òå×î´ó½ÓÊÕ×Ö½ÚÊý 200 #define EN_USARTx_RX 1 //ʹÄÜ£¨1£©/½ûÖ¹£¨0£©´®¿Ú1½ÓÊÕ extern u8 USART_RX_BUF[USART_REC_LEN]; //½ÓÊÕ»º³å,×î´óUSART_REC_LEN¸ö×Ö½Ú.Ä©×Ö½ÚΪ»»Ðзû extern u16 USART_RX_STA; //½ÓÊÕ״̬±ê¼Ç extern int num0; extern int num1; extern int num2; extern int num3; extern int num4; extern int num5; extern int num6; extern int num7; void usart1_init(u32 bound); void usart2_init(u32 bound); void usart3_init(u32 bound); void USART1_IRQHandler(void); void USART2_IRQHandler(void); void USART3_IRQHandler(void); void str_deal(); #endif
main.c
#include "delay.h" #include "sys.h" #include "usart.h" #include <stdio.h> #include <stdlib.h> uint8_t TxBuffer[]={0x0f,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10, 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x00}; uint8_t length=sizeof(TxBuffer)/sizeof(TxBuffer[0]); int main(void) { u8 a,flag=1; u8 i=0x00; u8 j=0x00; u8 k=0x00; u8 l=0x00; extern int num0; extern int num1; extern int num2; extern int num3; extern int num4; extern int num5; extern int num6; extern int num7; delay_init(); //ÑÓʱº¯Êý³õʼ»¯ usart1_init(115200); //´®¿Ú1³õʼ»¯Îª115200 usart2_init(115200); NVIC_Configuration(); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶ while(1) { // str_deal(); if(flag==1) { for(i=0;i<length;i++) { USART_SendData(USART1, TxBuffer[i]);//´®¿ÚÒ»·¢ËÍ //delay_ms(20); } flag=0; } } }