大家好!小弟用正点原子的例程稍加改动了一下想用串口发命令控制单片机的IO口,用下面的程序能实现。但接收缓冲区不能清零只能覆盖,例如:先发“asddfer",后发”zxc"缓冲区的数据是"zxcdfer"。请各路大神指点一下,小弟在此表示万分感激!代码如下:
#include "sys.h"
#include "usart.h"
#include "string.h"
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
_sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);
USART1->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART1_RX
char USART_RX_BUF[USART_REC_LEN];
u16 USART_RX_STA=0;
void My_USART2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//ê1ÄüUSART1ê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=115200;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART2,&USART_InitStructure);
USART_Cmd(USART2 ,ENABLE);
#if EN_USART1_RX
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
#endif
void USART2_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART2);//(USART1->DR);
if((USART_RX_STA&0x8000)==0)
{
if(USART_RX_STA&0x4000)
{
if(Res!=0x0a)USART_RX_STA=0;
else USART_RX_STA|=0x8000;
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;
}
}
}
}
}
#endif
u8 shibie()
{
// u8 len=50;
//u8 t;
char str[]={"B1ON"};
char abc[]={"B1OFF"};
char bcd[]={"B2ON"};
char cda[]={"B2OFF"};
const char *a ,*b,*c,*d,*e;
a=USART_RX_BUF;
// clear();
b=str;
c=abc;
d=bcd;
e=cda;
//len=USART_RX_STA&0x3fff;
// for(t=0;t<len;t++)
// {
//
// USART_RX_BUF[t]=0;
//
// }
//
if(!strcmp(a,b)) return 1;
else if(!strcmp(a,c )) return 2;
else if(!strcmp(a,d )) return 3;
else if(!strcmp(a,e )) return 4;
return 9;
}
// void clear()
// {
// u8 t;
// u8 len=50;
// for(t=0;t<len;t++)
// {
//
// USART_RX_BUF[t]=0;
//
// }
//
// }
这段是usrat.文件。
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "beep.h"
#include "key.h"
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
My_USART2_Init();
JDQ_Init();
while(1)
{
u8 val;
if(USART_RX_STA&0x8000)
{
USART_RX_STA=0;
}
val=shibie();
switch(val)
{
case 1:
{
GPIO_ResetBits(GPIOF,GPIO_Pin_1 );
}
break;
case 2:
{
GPIO_SetBits(GPIOF,GPIO_Pin_1 );
case 3:
{
GPIO_ResetBits(GPIOF,GPIO_Pin_3 );
}
break;
case 4:
{
GPIO_SetBits(GPIOF,GPIO_Pin_3);
}
break;
}
}
}
我本来想用clear 函数把串口缓冲区清空但不行,用了它之后一次都收不到。。。恳请各位大神帮忙。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
别纠结了。
什么意思??不太懂。请明示。
一周热门 更多>