是这样的 我有一个超声波模块核心板子是STC15W的,输出信息格式也是串口输出,数据是一秒50个数据,数据格式是0xA5后面加两个字节的信息
我需要将这个信息在我的32板子上接受,并且显示在OLED上,OLED程序已经没有问题能够显示字符了,但是发现串口接受不到信息,我是用中断去接收的
想了很久不知道问题出在哪里,希望大家帮忙看看!万分感谢,初学32
****************************************
void USART1_IRQHandler(void) //串口1中断服务程序
{
int flag=0;
u16 distance;
uint8_t dat[3];
uint8_t num;
USART_ClearFlag(USART1,USART_FLAG_TC);
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=Bit_RESET)//if data register has data
{
dat[num]=USART_ReceiveData(USART1);
if(dat[0]!=0xa5)
{
num = 0;
}
else
{
num++;
}
if(num==3)
{
num = 0;
distance = dat[1]<<8 | dat[2];//意思是想要消除前面的A5,读出后面的数据
flag = 1;
}
}
}
*****************************************************
//串口的初始化
void uart1_init()
{
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); //ê1ÄüUSART1£¬GPIOAê±ÖóòÔ¼°¸′óÃ1|Äüê±Öó
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
//USART 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = 9600;//ò»°ãéèÖÃÎ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; //êÕ·¢Ä£ê½
USART_Init(USART1, &USART_InitStructure); //3õê¼»ˉ′®¿ú
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿aÆôÖD¶Ï
USART_Cmd(USART1, ENABLE); //ê1Äü′®¿ú
}
*****************************
int main(void)
{
// u8 t;
u8 flag = 0;
char a[]="distance";
delay_init();
uart1_init();
NVIC_Configuration(); //éèÖÃNVICÖD¶Ï·Ö×é2:2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
OLED_Init(); //3õê¼»ˉOLED
OLED_Clear();
while(1)
{
//USART1_IRQHandler();
OLED_ShowString(32,0,a);
//if(flag)
//{
OLED_ShowNum(4,2,distance,8,16);
//}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
u16 distance;
uint8_t dat[3];
uint8_t num;
全是局部变量啊,要么全局变量,要么static静态变量
一周热门 更多>