最近买了一个AS608指纹识别模块 ,看了一下原子哥的源代码, 原子哥的串口2通信 用的是定时检测一帧数据 ,有点看不懂 , 然后在网上查了下资料, 用 串口空闲中断可以解决接受一帧不定长数据, 所以我就用了这种方法, 开发板是探索者的, 但是现在无法正常通信 , 有哪位大神帮忙看下,如果解决了,发个50红包给你, 说到做到
串口相关代码如下
#include "myusart.h"
void Usart2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
//USRT2对应引脚复用映射
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; -
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//USART2初始化配置
USART_InitStructure.USART_BaudRate = 56700;
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);
//使能USART2
USART_Cmd(USART2,ENABLE);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); // 开启接收非空中断
USART_ITConfig(USART2,USART_IT_IDLE,ENABLE); // 开启接受空闲中断
//USART1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; // 串口2中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0 ; // 响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能
NVIC_Init(&NVIC_InitStructure);
}
extern uint8_t aRxBuffer[100];
extern uint8_t RxCounter ;
extern uint8_t ReceiveState;
void USART2_IRQHandler(void)
{
u8 clear = clear;
if(USART_GetFlagStatus(USART2,USART_IT_RXNE) != RESET) //如果接受到一个字节
{
aRxBuffer[RxCounter++]= USART_ReceiveData(USART2);
}
else if(USART_GetFlagStatus(USART2,USART_IT_IDLE) != RESET) //如果接收到一帧数据
{
clear = USART2->SR ; // 读SR寄存器
clear = USART2->DR; // 读DR寄存器 (先读SR再DR,就是为了清除IDLE中断标志位)
ReceiveState = 1; // 标记接受到一帧数据
}
}
/***************************************************************************************************************************************
#include "as608.h"
#include "stm32f4xx.h"
#include "string.h"
#include "delay.h"
//发送一个字节
static void MYUSART2_SendData(u8 data)
{
USART_SendData(USART2,data);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET);
}
//发送一个包头
static void SendHead(void)
{
MYUSART2_SendData(0xEF);
MYUSART2_SendData(0x01);
}
//读取一帧数据
void MYUSART2_GetFrameData(void)
{
u8 i;
if(ReceiveState == 1)
{
ReceiveState = 0; // 见标志位清零
for(i = 0; i < RxCounter; i++)
ACK_data = aRxBuffer;
RxCounter = 0; //
}
}
//与AS608 模块握手函数
u8 PS_HandShake(u32 *PS_Addr)
{
int i, j;
SendHead();
SendAddr();
MYUSART2_SendData(0X01);
MYUSART2_SendData(0X00);
MYUSART2_SendData(0X00);
delay_ms(200);
MYUSART2_GetFrameData();
if(ACK_data[0] == 0xEF && ACK_data[1] == 0x01 && ACK_data[6] == 0x07)
{
*PS_Addr=(ACK_data[2]<<24) + (ACK_data[3]<<16)+(ACK_data[4]<<8) + (ACK_data[5]);
return 1; //握手成功返回1
}
else
return 0;
}
//****************************************************************************
#include "myusart.h"
#include "delay.h"
#include "as608.h"
#include "led.h"
volatile uint8_t aRxBuffer[100] = {0x00}; // 接受缓存数据
volatile uint8_t RxCounter = 0;
volatile uint8_t ReceiveState = 0 ; //接收一帧数据标志位
int main(void)
{
uint8_t i;
delay_init(168);
Usart_Init(115200);
Usart2_Init();
LED_Init();
delay_ms(200);
while(1)
{
if(PS_HandShake(&AS608Addr))
{
printf("握手成功
");
}
else
{
printf("握手失败
");
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>