用ucos做了一个串口收发的程序,但是能发不能收,怀疑是中断配置组的问题,但是不知道怎么弄,大家一起交流交流吧,代码如下:
void USART2_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(USART2);//(USART2->DR);
if(USART2_RX_STA<10)
{
USART2_RX_BUF[USART2_RX_STA]=Res;
USART2_RX_STA++;
}
}
}
void Reveice_data(void)
{
u8 rlen=10;
u8 i;
// delay_ms(10);
if(rlen==USART2_RX_STA)
{
for(i=0;i<rlen;i++)
{
buf[i]=USART2_RX_BUF[i];
}
USART2_RX_STA=0;
}
}
配置:应该没问题
void usart2_init(u32 bound)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // GPIOAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //′®¿ú2ê±Öóê1Äü
USART_DeInit(USART2); //¸′λ′®¿ú2
//USART3_TX PA2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART3_RX PA3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = bound;//2¨ìØÂêò»°ãéèÖÃÎa9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
//éèÖÃÖD¶ÏóÅÏ輶
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
USART_Init(USART2, &USART_InitStructure); //3õê¼»ˉ′®¿ú
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//¿aÆôÖD¶Ï
USART_Cmd(USART2, ENABLE); //ê1Äü′®¿ú
}
以下是.h文件
#ifndef __USART2_H
#define __USART2_H
#include "sys.h"
#define USART2_REC_LEN 10 //¶¨òå×î′ó½óêÕ×Ö½úêy 20
void usart2_init(u32 bound);
void Reveice_data(void);
void Send_data(void);
extern u8 USART2_RX_STA;
extern u8 USART2_RX_BUF[USART2_REC_LEN];
#endif
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>