STM32——串口通信(中断)

/***********************************************/
#include “stdio.h”
#include “sting.h”
#include “stm32f10x.h”
#define LED_ON ResetInputDataBit (GPIOC , GPIO_Pin_8)
#define LED_OFF SetInputDataBit (GPIOC , GPIO_Pin_8)
#define USARTMY USART2
#define MaxLength 10
u8 RXBuffer[Max];
u8 TXBuffer[Max];
u8 RXCount = 0;
u8 TXCount = 0;
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
// 使能GPIOB时钟(LED在BP5引脚),使能AFIO时钟(定时器3通道2需要重映射到BP5引脚)
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz; //设置最大输出速度
GPIO_Init(GPIOB,&GPIO_InitStrue); //GPIO端口初始化设置

}

void Delay_ms(u16 time)

{
u16 i = 0;
while(time–)
{
i=12000;
while(i–);
}
}
void USARTx_GPIO_Configuration(USART_TypeDef * USARTx)
{
if (USARTx == USART1)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA,ENABLE);
// Configure USART1_Tx as alternate push-pull
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);

// Configure USART1_Rx as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);  
}
if (USARTx == USART2)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA,ENABLE);
// Configure USART1_Tx as alternate push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

// Configure USART1_Rx as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);  
}
if (USARTx == USART3)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART3 | RCC_APB2Periph_GPIOB,ENABLE);
// Configure USART1_Tx as alternate push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

// Configure USART1_Rx as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);  
}
if

}
void NVIC_Configuration (USART_TypeDef * USARTx)
{
NVIC_InitTypeDef * NVIC_InitStructure;

if (USARTx == USART1)
{
  NVIC_InitStructure.NVIC_IRQChannl = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannlPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannlSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannlCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//接收中断使能
}
if (USARTx == USART2)
{
  NVIC_InitStructure.NVIC_IRQChannl = USART2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannlPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannlSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannlCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//接收中断使能
}
if (USARTx == USART3)
{
  NVIC_InitStructure.NVIC_IRQChannl = USART3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannlPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannlSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannlCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//接收中断使能
}

}

//然后是配置串口参数

void USARTx_Configuration(USART_TypeDef * USARTx)
{

USART_InitTypeDef USART_InitStructure;

USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USARTx, &USART_InitStructure);

USART_Cmd(USARTx, ENABLE);
}

//发送一个字符

void SendChar(USART_TypeDef * USARTx , u8 data)
{

USART_SendData(USARTx, (u8) data);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)

{
}
}
//发送一个字符串

void SendString(u8* buf , u8 len)
{
for(u8 i=0;i<len;i++)
{
SendChar(USARTMY,buf++);
}
}
//接收字符串
u8 ReceiveByte(USART_TypeDef * USARTx)
{
while(USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == RESET);
return (USART_ReceiveData(USARTx));
}
//返回接收标志
//1 = ON, 2 = OFF , 其他 = 无效
//USARTx x = 1,2,3
u8 ReceiveFlag (SART_TypeDef * USARTx)
{
while (1)
{
RXBuffer[RXCount++] = ReceiveByte(USARTx);
if(strstr((char )RXBuffer , “ON”) != NULL) //判断str1中是否包含子串str2
{
RXCount = 0;
return 1;
}
else if (strstr((char )RXBuffer , “OFF”) != NULL)
{
RXCount = 0;
return 2;
}
else
{
if (RXCount > 3)
RXCount = 0;
return 0;
}
}
void EmptyRXBuffer(u8 Length)
{
u8 i;
for (i=0;i<Length;i++)
RXBuffer[i] = 0;
}
void EmptyTXBuffer(u8 Length)
{
u8 i;
for (i=0;i<Length;i++)
TXBuffer[i] = 0;
}
/
*****************************************************/
int main(void)
{
u8 Length;
GPIO_Config();
USARTx_Configuration(USARTMY);
USARTx_GPIO_Configuration(USARTMY);
NVIC_Configuration (USARTMY);
while (1)
{
TXBuffer[Max] = “ON”; //OFF
Length = strlen(TXBuffer) - 1;
SendString(TXBuffer,Length);
EmptyTXBuffer(Length);
Length = 0;
switch (RxOK)
{
case 1:
if (GPIO_ReadInputDataBit (GPIOC,GPIO_Pin_8) == RESET)
{
RxOK = 0;
TXBuffer[Max] = (u8) “ON”;
Length = strlen(TXBuffer) - 1;
SendString(TXBuffer,Length);
}
else
LED_ON;break;
case 2:
if (GPIO_ReadInputDataBit (GPIOC,GPIO_Pin_8) == SET)
{
RxOK = 0;
TXBuffer[Max] = (u8) “OFF”;
Length = strlen(TXBuffer) - 1;
SendString(TXBuffer,Length);
}
else
LED_OFF;break;
case 0: break;
}
EmptyRXBuffer(Max);
}
/stm32f10x.h
/
extern u8 RxBuffer[];
extern vu8 RxCount;
extern vu8 RxHeader;
extern vu8 RxOK;
extern vu8 RxLength;
void USART1_IRQHandler(void);
void USART2_IRQHandler(void);
void USART3_IRQHandler(void);
/stm32f10x.c
/

void USART1_IRQHandler(void)
{
if (USART1_GetITStatus(USART1,USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit (USART1,USART_IT_RXNE);

	   RxBuffer[RxCount++] = USART_ReceiveData(USART1);
	   RxCount & = 0XFF;
   }
 	if(strstr((char *)RXBuffer , "1") != NULL)
	{
        RxCount = 0;
		RxOK = 1;
	}
	else 	if(strstr((char *)RXBuffer , "2") != NULL)
		{
        RxCount = 0;
		RxOK = 2;
	}
	else
			{
        RxCount = 0;
		RxOK = 0;
	}
	if (USART_GetFlagStatus (USART1,USART_FLAG_ORE) == SET) //空闲中断
	{
		USART_ClearFlag (USART1,USART_FLAG_ORE);//先读SD
		USART_ReceiveData(USART1); //在读DR
	}

}
void USART2_IRQHandler(void)
{
if (USART3_GetITStatus(USART2,USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit (USART2,USART_IT_RXNE);

	   RxBuffer[RxCount++] = USART_ReceiveData(USART2);
	   RxCount & = 0XFF;
   }
 	if(strstr((char *)RXBuffer , "1") != NULL)
	{
        RxCount = 0;
		RxOK = 1;
	}
	else 	if(strstr((char *)RXBuffer , "2") != NULL)
		{
        RxCount = 0;
		RxOK = 2;
	}
	else
			{
        RxCount = 0;
		RxOK = 0;
	}
	if (USART_GetFlagStatus (USART2,USART_FLAG_ORE) == SET) //空闲中断
	{
		USART_ClearFlag (USART2,USART_FLAG_ORE);//先读SD
		USART_ReceiveData(USART2); //在读DR
	}

}
void USART3_IRQHandler(void)
{
if (USART3_GetITStatus(USART3,USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit (USART3,USART_IT_RXNE);

	   RxBuffer[RxCount++] = USART_ReceiveData(USART3);
	   RxCount & = 0XFF;
   }
 	if(strstr((char *)RXBuffer , "1") != NULL)
	{
        RxCount = 0;
		RxOK = 1;
	}
	else 	if(strstr((char *)RXBuffer , "2") != NULL)
		{
        RxCount = 0;
		RxOK = 2;
	}
	else
			{
        RxCount = 0;
		RxOK = 0;
	}
	if (USART_GetFlagStatus (USART3,USART_FLAG_ORE) == SET) //空闲中断
	{
		USART_ClearFlag (USART3,USART_FLAG_ORE);//先读SD
		USART_ReceiveData(USART3); //在读DR
	}

}

猜你喜欢

转载自blog.csdn.net/News53231323/article/details/113506745