#include "stm32f4xx.h"
#include "stdio.h"
#define LED1_ON GPIO_SetBits(GPIOD, GPIO_Pin_15);
#define LED1_OFF GPIO_ResetBits(GPIOD, GPIO_Pin_15);
#define LED2_ON GPIO_SetBits(GPIOC, GPIO_Pin_6);
#define LED2_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_6);
#define LED3_ON GPIO_SetBits(GPIOC, GPIO_Pin_7);
#define LED3_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_7);
#define EN_USART2_TX GPIO_SetBits(GPIOD,GPIO_Pin_7);
#define EN_USART2_RX GPIO_ResetBits(GPIOD,GPIO_Pin_7);
u8 USART_RX_BUFF[200]; //接收缓冲,最大USART_REC_LEN个字节.
u16 USART_RX_STAA=0; //接收状态标记
void USART2_IRQHandler(void);
//发送数据
int fputc(int ch,FILE*f)
{
USART_SendData(USART2,(unsigned char)ch);
while(!(USART2->SR&USART_FLAG_TXE));
return(ch);
}
//接收数据
int GetKey(void)
{
while(!(USART2->SR & USART_FLAG_RXNE));
return((int)(USART2->DR&0x1FF));
}
void delay_ms(u16 time)
{
u32 i=0;
while(time--)
{
i=36000;
while(i--);
}
}
void RCC_Configuration(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC |RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_PinAFConfig(GPIOD,GPIO_PinSource6,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource5,GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //CS_485
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //管脚2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //复用推挽输出
GPIO_Init(GPIOD, &GPIO_InitStructure); //TX初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //管脚3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//浮空输入
GPIO_Init(GPIOD, &GPIO_InitStructure); //RX初始化
}
void InitUSART2 ( void )
{
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd ( RCC_APB1Periph_USART2, ENABLE );
USART_InitStructure.USART_BaudRate = 9600u;/*设置波特率为9600*/
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; /*发送和接收*/
USART_Init ( USART2, &USART_InitStructure );
USART_Cmd ( USART2, ENABLE );
USART_ITConfig ( USART2, USART_IT_TXE, DISABLE );
USART_ITConfig ( USART2, USART_IT_RXNE, ENABLE );
}
void InitNVIC ( void )
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig ( NVIC_PriorityGroup_0 );
NVIC_InitStructure.NVIC_IRQChannel = ( uint8_t ) USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init ( &NVIC_InitStructure );
}
void USART2_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
USART_ClearFlag(USART2,USART_FLAG_RXNE);
Res =USART_ReceiveData(USART2); //读取接收到的数据
if((USART_RX_STAA&0x8000)==0)//接收未完成
{
if(USART_RX_STAA&0x4000)//接收到了0x0d
{
if(Res!=0x0a)USART_RX_STAA=0;//接收错误,重新开始
else USART_RX_STAA|=0x8000; //接收完成了
}
else //还没收到0X0D
{
if(Res==0x0d)USART_RX_STAA|=0x4000;
else
{
USART_RX_BUFF[USART_RX_STAA&0X3FFF]=Res ;
USART_RX_STAA++;
if(USART_RX_STAA>200)
{
USART_RX_STAA=0;//接收数据错误,重新开始接收
}
}
}
}
}
}
int main()
{
u8 len;
u8 t;
RCC_Configuration();
GPIO_Configuration();
InitUSART2 ();
InitNVIC ();
EN_USART2_TX;
printf("
您发送的消息为:
");
EN_USART2_RX;
while(1)
{
if(USART_RX_STAA&0x8000)
{
EN_USART2_TX;
len=USART_RX_STAA&0x3fff;//得到此次接收到的数据长度
for(t=0;t<len;t++)
{
USART2->DR=USART_RX_BUFF[t];
while((USART2->SR&0X40)==0);//等待发送结束
}
delay_ms(1000);
EN_USART2_RX;
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>