经过各位大神的指点,现在STM32F030已经可以收发数据,但是发现一个新问题,每发送两次,只能接收到一次数据,第1,3,5,7。。。。。次可以接收到数据,第2,4,6,8。。。。。。次不能接收到数据,不知道是什么原因,再次请教大神,小弟在此跪谢啦!
代码如下,(附件中随代码):
主函数:
#include "stm32f0xx.h"
#include "led.h"
#include "exit.h"
#include "uart.h"
unsigned int i0=1; //串口文件里的接收递增变量,做成全局变量
unsigned int tmp; //串口文件里的字节接收变量,做成全局变量
unsigned int CommandFromPc[7]; //串口文件里的接收数组,做成全局变量
unsigned int ADD; //串口文件里的和校验值,做成全局变量
unsigned int keyflag=0;
unsigned int downswitchflag=0;
unsigned int upwaterflag=0;
unsigned int RXD_OK_flag=0;
unsigned int AnswerData[7];
int main(void)
{
SystemInit();
LED_Init();
LED_Init1();
USART_Configuration();
AnswerData[1]=0XED; //**μ±Ç°½úμãoÅ****//
AnswerData[2]=0X27; //**é趨½Ç¶èÖμ****//
AnswerData[3]=0X10; //**é趨½Ç¶èÖμ****//
AnswerData[4]=0XEA; //**é趨½Ç¶èÖμ****//
//EXIT_KEY_Init();
while(1)
{
if(RXD_OK_flag==1)
{
RXD_OK_flag=0;
USART_SendData(USART1,AnswerData[1]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
USART_SendData(USART1,AnswerData[2]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
USART_SendData(USART1,AnswerData[3]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
USART_SendData(USART1,AnswerData[4]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
GPIO_ResetBits(GPIOA, GPIO_Pin_3);
GPIO_SetBits(GPIOA, GPIO_Pin_4);
}
}
}
串口文件如下:
#include "uart.h"
#include <stdio.h>
extern unsigned int i0;
extern unsigned int tmp;
extern unsigned int CommandFromPc[7];
extern unsigned int ADD;
extern unsigned int RXD_OK_flag;
extern unsigned int AnswerData[7];
void USART_Configuration(void)//′®¿ú3õê¼»ˉoˉêy
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
/*
* USART1_TX -> PA9 , USART1_RX -> PA10
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;//éèÖÃ′®¿ú2¨ìØÂê
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//éèÖÃêy¾Yλ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//éèÖÃí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//éèÖÃD§Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//éèÖÃá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//éèÖÃ1¤×÷Ä£ê½
USART_Init(USART1, &USART_InitStructure); //ÅäÖÃèë½á11ìå
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void) //??1??????
{
unsigned int m;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //????(?????????0x0d 0x0a??)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
tmp=USART_ReceiveData(USART1);//(USART1->DR); //????????
switch(i0)
{
case 0x01 : //???
if(tmp==0xED)
{ //**?????****//
CommandFromPc[i0]=tmp; //**????******//
i0++;
}
else
{ i0=1; } //**??????**//
break ;
case 0x02 :
if(tmp==0x27)
{ //**?????****//
CommandFromPc[i0]=tmp; //**????******//
i0++;
}
else
{ i0=1; }
break ;
case 0x03 :
for(m=1;m<3;m++)
{ ADD=ADD+CommandFromPc[m]; }
ADD=ADD&0XFF;
if(tmp==ADD)
{ //**?????****//
CommandFromPc[i0]=tmp; //**????******//
i0++;
}
else
{ i0=1;
ADD=0;
}
break;
case 0x04 :
if(tmp==0xEA)
{
CommandFromPc[i0]=tmp; //**??????**//
RXD_OK_flag=1; //**??????**//
i0=1;
}
else
{
i0=1;
ADD=0;
}
break ;
default:
i0=1;
ADD=0;
}
}
}
接收中断的意思是接收到十六进制的0XED 0X27 0X14 0XEA时(其中0X14为0XED+0X27的和校验值),认为正确接收,主函数再做应答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
if(tmp==0xEA)
{
CommandFromPc[i0]=tmp;
RXD_OK_flag=1;
i0=1;
ADD=0; //加上这一句就OK了
}
一周热门 更多>