专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
STM32
中断时间过长时,怎么清中断
2019-07-20 12:27
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
4321
6
1534
进入一中断的时间太长,或者死在中断中,要如何设置,使其正常的退出中断或强制结束中断
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
6条回答
Eternal单人行
2019-07-20 21:57
jermy_z 发表于 2017-7-11 09:35
那是你逻辑上要改动了
请大神指点
[mw_shl_code=c,true]#include "wiegand.h"
#include "spi.h"
#include "w25qxx.h"
#include "rtc.h"
u32 WG_Data[4]={0x0,0x0,0x0,0x0};//[WG_DATA_BITS]={0};
static u8 u_DataBits[4] = {0,0,0,0};//当前数据位
static u8 u_OddCheck[4] = {2,2,2,2};
static u8 u_EvenCheck[4] = {2,2,2,2};
#ifdef USEING_INTERR //中断方式读取韦根协议
extern u8 flag_wiegand[4];
/***********************************************************
* 函数名:WiegandInit
* 功 能:外部中断引脚初始化
* 输 入:无
* 输 出:无
**********************************************************/
void WiegandInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(WIEGAND1_RCC_PORT | WIEGAND2_RCC_PORT | WIEGAND3_RCC_PORT | WIEGAND4_RCC_PORT,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//RCC_APB2Periph_AFIO
GPIO_InitStructure.GPIO_Pin = WIEGAND1_DATA0_GPIO | WIEGAND1_DATA1_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(WIEGAND1_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = WIEGAND2_DATA0_GPIO | WIEGAND2_DATA1_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(WIEGAND2_PORT, &GPIO_InitStructure);
#if DEBUG
GPIO_InitStructure.GPIO_Pin = WIEGAND3_DATA0_GPIO | WIEGAND3_DATA1_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(WIEGAND3_PORT, &GPIO_InitStructure);
#endif
GPIO_InitStructure.GPIO_Pin = WIEGAND4_DATA0_GPIO | WIEGAND4_DATA1_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(WIEGAND4_PORT, &GPIO_InitStructure);
//Relay_PIN_Init
GPIO_InitStructure.GPIO_Pin = Relay1_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Relay1_PORT, &GPIO_InitStructure);
//WIEGAND1
SYSCFG_EXTILineConfig(WIEGAND1_PortSource,EXTI_PinSource6);
EXTI_InitStructure.EXTI_Line = EXTI_Line6;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
SYSCFG_EXTILineConfig(WIEGAND1_PortSource,EXTI_PinSource7);
EXTI_InitStructure.EXTI_Line = EXTI_Line7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//中断优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //字优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断
NVIC_Init(&NVIC_InitStructure);
//WIEGAND2
SYSCFG_EXTILineConfig(WIEGAND2_PortSource,EXTI_PinSource11);
EXTI_InitStructure.EXTI_Line = EXTI_Line11;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
SYSCFG_EXTILineConfig(WIEGAND2_PortSource,EXTI_PinSource12);
EXTI_InitStructure.EXTI_Line = EXTI_Line12;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//中断优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //字优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断
NVIC_Init(&NVIC_InitStructure);
#if DEBUG //WIEGAND3
SYSCFG_EXTILineConfig(WIEGAND3_PortSource,EXTI_PinSource8);
EXTI_InitStructure.EXTI_Line = EXTI_Line8;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
SYSCFG_EXTILineConfig(WIEGAND3_PortSource,EXTI_PinSource14);
//GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line14;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//中断优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn ; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //字优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //字优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断
NVIC_Init(&NVIC_InitStructure);
#endif
//WIEGAND4
SYSCFG_EXTILineConfig(WIEGAND4_PortSource,EXTI_PinSource4);
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
SYSCFG_EXTILineConfig(WIEGAND4_PortSource,EXTI_PinSource5);
//GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//中断优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;//外部中断0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
NVIC_Init(&NVIC_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//外部中断0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
NVIC_Init(&NVIC_InitStructure);//配置
}
//Read_WG
/***********************************************************
* 函数名:EXTI15_10_IRQHandler
* 功 能
ATA数据线中断服务函数
* 输 入:无
* 输 出:无
**********************************************************/
void EXTI4_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4) != RESET && flag_wiegand[3]==0) //WIEGAND4_A
{ //Data0 -> 获得一位0
EXTI_ClearITPendingBit(EXTI_Line4);
if(u_DataBits[3] == 0)
{
u_EvenCheck[3]=0; //偶校验位
}
else if(u_DataBits[3] == (WG_DATA_BITS-1))
{
u_OddCheck[3]=0; //奇校验位
}
else
{
printf("0");
WG_Data[3]<<=1;
}
u_DataBits[3]++;
}
if(u_DataBits[3] >= WG_DATA_BITS)
{
flag_wiegand[3]=1;
return;
}
}
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line5) != RESET && flag_wiegand[3]==0) //WIEGAND4_B
{ //Data1 -> 获得一位1
EXTI_ClearITPendingBit(EXTI_Line5);
if(u_DataBits[3] == 0)
{
u_EvenCheck[3]=1; //偶校验位
}
else if(u_DataBits[3] == (WG_DATA_BITS-1))
{
u_OddCheck[3]=1; //奇校验位
}
else
{
printf("1");
WG_Data[3]<<=1;
WG_Data[3]|=1;
}
u_DataBits[3]++;
}
if(EXTI_GetITStatus(EXTI_Line6) != RESET && flag_wiegand[0]==0) //WIEGAND1_A
{ //Data0 -> 获得一位0
EXTI_ClearITPendingBit(EXTI_Line6);
if(u_DataBits[0] == 0)
{
u_EvenCheck[0]=0; //偶校验位
}
else if(u_DataBits[0] == (WG_DATA_BITS-1))
{
u_OddCheck[0]=0; //奇校验位
}
else
{
//printf("0");
WG_Data[0]<<=1;
}
u_DataBits[0]++;
}
if(EXTI_GetITStatus(EXTI_Line7) != RESET && flag_wiegand[0]==0) //WIEGAND1_B
{ //Data1 -> 获得一位1
EXTI_ClearITPendingBit(EXTI_Line7);
if(u_DataBits[0] == 0)
{
u_EvenCheck[0]=1; //偶校验位
}
else if(u_DataBits[0] == (WG_DATA_BITS-1))
{
u_OddCheck[0]=1; //奇校验位
}
else
{
//printf("1");
WG_Data[0]<<=1;
WG_Data[0]|=1;
}
u_DataBits[0]++;
}
if(EXTI_GetITStatus(EXTI_Line8) != RESET && flag_wiegand[2]==0) //WIEGAND3_A
{ //Data0 -> 获得一位0
EXTI_ClearITPendingBit(EXTI_Line8);
if(u_DataBits[2] == 0)
{
WG_Data[2]=0x0;
u_EvenCheck[2]=0; //偶校验位
}
else if(u_DataBits[2] == (WG_DATA_BITS-1))
{
u_OddCheck[2]=0; //奇校验位
}
else
{
//printf("0");
WG_Data[2]<<=1;
}
//[u_DataBits] = 1;
u_DataBits[2]++;
}
//设置标志位
if(u_DataBits[0] >= WG_DATA_BITS)
{
flag_wiegand[0]=1;
return ;
}
if(u_DataBits[2] >= WG_DATA_BITS)
{
flag_wiegand[2]=1;
return ;
}
if(u_DataBits[3] >= WG_DATA_BITS)
{
flag_wiegand[3]=1;
return ;
}
}
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line11) != RESET && flag_wiegand[1]==0) //WIEGAND2_A
{ //Data0 -> 获得一位0
EXTI_ClearITPendingBit(EXTI_Line11);
if(u_DataBits[1] == 0)
{
WG_Data[1]=0x0;
u_EvenCheck[1]=0; //偶校验位
}
else if(u_DataBits[1] == (WG_DATA_BITS-1))
{
u_OddCheck[1]=0; //奇校验位
}
else
{
//printf("0");
WG_Data[1]<<=1;
}
u_DataBits[1]++;
}
if(EXTI_GetITStatus(EXTI_Line12) != RESET && flag_wiegand[1]==0) //WIEGAND2_B
{ //Data1 -> 获得一位1
EXTI_ClearITPendingBit(EXTI_Line12);
if(u_DataBits[1] == 0)
{
WG_Data[1]=0x0;
u_EvenCheck[1]=1; //偶校验位
}
else if(u_DataBits[1] == (WG_DATA_BITS-1))
{
u_OddCheck[1]=1; //奇校验位
}
else
{
//printf("1");
WG_Data[1]<<=1;
WG_Data[1]|=1;
}
//[u_DataBits] = 1;
u_DataBits[1]++;
}
if(EXTI_GetITStatus(EXTI_Line14) != RESET && flag_wiegand[2]==0 )//WIEGAND3_B
{ //Data1 -> 获得一位1
EXTI_ClearITPendingBit(EXTI_Line14);
if(u_DataBits[2] == 0)
{
WG_Data[2]=0x0;
u_EvenCheck[2]=1; //偶校验位
}
else if(u_DataBits[2] == (WG_DATA_BITS-1))
{
u_OddCheck[2]=1; //奇校验位
}
else
{
//printf("1");
WG_Data[2]<<=1;
WG_Data[2]|=1;
}
//[u_DataBits] = 1;
u_DataBits[2]++;
}
if(u_DataBits[1] >= WG_DATA_BITS)
{
flag_wiegand[1]=1;
return ;
}
else if(u_DataBits[2] >= WG_DATA_BITS)
{
flag_wiegand[2]=1;
return ;
}
}
/***********************************************************
* 函数名
ataCheck
* 功 能:通过校验位,检验数据是否正确
* 输 入:第几个门禁
* 输 出:0->正确 1->数据错误 2->数据未接收完全
**********************************************************/
u8 DataCheck(u8 index)
{
u8 i;
u32 temp;
u8 oddcheck,evencheck;
u8 u_OddNums,u_EvenNums;
u_OddNums = u_EvenNums = 0;
if(u_DataBits[index] == WG_DATA_BITS)
{ //可以校验
for( i=0 ; i<((WG_DATA_BITS-2)/2) ; i++ )
{
temp=WG_Data[index];
if((temp>>i)&0x1)
u_OddNums++;
}
for( ; i<WG_DATA_BITS-2 ; i++ )
{
temp=WG_Data[index];
if((temp>>i)&0x1)
u_EvenNums++;
}
if(u_EvenNums % 2 == 0)
evencheck = 0; //偶数个1
else
evencheck = 1; //奇数个1
if(u_OddNums % 2 == 0)
oddcheck = 1; //偶数个1
else
oddcheck = 0; //奇数个1
/*
//校验位检测
printf("u_EvenCheck = %u ,evencheck = %u ,u_OddCheck = %u ,oddcheck = %u ",u_EvenCheck[index],evencheck,u_OddCheck[index],oddcheck);
return 0;
*/
if((u_EvenCheck[index] == evencheck) && (u_OddCheck[index] == oddcheck))
{ //正确
u_OddCheck[index]=u_EvenCheck[index]=2;
return 0;
}
else
{ //错误
u_OddCheck[index]=u_EvenCheck[index]=2;
return 1;
}
}
/*
else
return 2;
*/
}
/***********************************************************
* 函数名:IDDataPrintf
* 功 能:输出卡号信息
* 输 入:index(韦根标志)
* 输 出:WG_Data
**********************************************************/
void IDDataPrintf(u8 index)
{
if(DataCheck(index)==0)
{
printf("WIEGAND %d ID Card:%u ",index+1,WG_Data[index]);
/*
for( i=WG_DATA_BITS-2 ; i>=1 ; i--)
{
temp=WG_Data;
if((temp>>i-1)&0x1)
printf("1");
else
printf("0");
}
printf(" ");
*/
// 检索ID
/*
if(Flash_Strcmp(WG_Data[index]) == 0xffffffff)
{
LED1=0;//点亮LED1
LED0=1;//LED0灭
}
else
{
LED0=0;//点亮LED0
LED1=1;//LED1灭
}
*/
}/*
else
{
printf("WIEGAND Error ");
}*/
u_DataBits[index]=0;
return ;
// WG_Data=0x0;
/*
else
{
printf("Waiting Receive ");
}*/
}
#endif
[/mw_shl_code]
加载中...
查看其它6个回答
一周热门
更多
>
相关问题
STM32F4上I2C(在PROTEUS中模拟)调试不通的问题
6 个回答
芯片供应紧张,准备换个MCU,MM32L系列替换STM32L系列的怎么样?
7 个回答
STM32同时使用两个串口进行数据收发时数据丢包的问题
5 个回答
STM32F103串口通信死机问题
4 个回答
STM32WLE5CC连接SX1268在LoRa模式下能与 SX1278互通吗?
2 个回答
相关文章
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
×
关闭
采纳回答
向帮助了您的知道网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
STM32
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
×
付费偷看金额在0.1-10元之间
确定
×
关闭
您已邀请
0
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
请大神指点
[mw_shl_code=c,true]#include "wiegand.h"
#include "spi.h"
#include "w25qxx.h"
#include "rtc.h"
u32 WG_Data[4]={0x0,0x0,0x0,0x0};//[WG_DATA_BITS]={0};
static u8 u_DataBits[4] = {0,0,0,0};//当前数据位
static u8 u_OddCheck[4] = {2,2,2,2};
static u8 u_EvenCheck[4] = {2,2,2,2};
#ifdef USEING_INTERR //中断方式读取韦根协议
extern u8 flag_wiegand[4];
/***********************************************************
* 函数名:WiegandInit
* 功 能:外部中断引脚初始化
* 输 入:无
* 输 出:无
**********************************************************/
void WiegandInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(WIEGAND1_RCC_PORT | WIEGAND2_RCC_PORT | WIEGAND3_RCC_PORT | WIEGAND4_RCC_PORT,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//RCC_APB2Periph_AFIO
GPIO_InitStructure.GPIO_Pin = WIEGAND1_DATA0_GPIO | WIEGAND1_DATA1_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(WIEGAND1_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = WIEGAND2_DATA0_GPIO | WIEGAND2_DATA1_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(WIEGAND2_PORT, &GPIO_InitStructure);
#if DEBUG
GPIO_InitStructure.GPIO_Pin = WIEGAND3_DATA0_GPIO | WIEGAND3_DATA1_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(WIEGAND3_PORT, &GPIO_InitStructure);
#endif
GPIO_InitStructure.GPIO_Pin = WIEGAND4_DATA0_GPIO | WIEGAND4_DATA1_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //上拉
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(WIEGAND4_PORT, &GPIO_InitStructure);
//Relay_PIN_Init
GPIO_InitStructure.GPIO_Pin = Relay1_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Relay1_PORT, &GPIO_InitStructure);
//WIEGAND1
SYSCFG_EXTILineConfig(WIEGAND1_PortSource,EXTI_PinSource6);
EXTI_InitStructure.EXTI_Line = EXTI_Line6;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
SYSCFG_EXTILineConfig(WIEGAND1_PortSource,EXTI_PinSource7);
EXTI_InitStructure.EXTI_Line = EXTI_Line7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//中断优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //字优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断
NVIC_Init(&NVIC_InitStructure);
//WIEGAND2
SYSCFG_EXTILineConfig(WIEGAND2_PortSource,EXTI_PinSource11);
EXTI_InitStructure.EXTI_Line = EXTI_Line11;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
SYSCFG_EXTILineConfig(WIEGAND2_PortSource,EXTI_PinSource12);
EXTI_InitStructure.EXTI_Line = EXTI_Line12;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//中断优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //字优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断
NVIC_Init(&NVIC_InitStructure);
#if DEBUG //WIEGAND3
SYSCFG_EXTILineConfig(WIEGAND3_PortSource,EXTI_PinSource8);
EXTI_InitStructure.EXTI_Line = EXTI_Line8;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
SYSCFG_EXTILineConfig(WIEGAND3_PortSource,EXTI_PinSource14);
//GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line14;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//中断优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn ; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //字优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; //字优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断
NVIC_Init(&NVIC_InitStructure);
#endif
//WIEGAND4
SYSCFG_EXTILineConfig(WIEGAND4_PortSource,EXTI_PinSource4);
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
SYSCFG_EXTILineConfig(WIEGAND4_PortSource,EXTI_PinSource5);
//GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//中断优先级
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;//外部中断0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
NVIC_Init(&NVIC_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;//外部中断0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
NVIC_Init(&NVIC_InitStructure);//配置
}
//Read_WG
/***********************************************************
* 函数名:EXTI15_10_IRQHandler
* 功 能ATA数据线中断服务函数
* 输 入:无
* 输 出:无
**********************************************************/
void EXTI4_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4) != RESET && flag_wiegand[3]==0) //WIEGAND4_A
{ //Data0 -> 获得一位0
EXTI_ClearITPendingBit(EXTI_Line4);
if(u_DataBits[3] == 0)
{
u_EvenCheck[3]=0; //偶校验位
}
else if(u_DataBits[3] == (WG_DATA_BITS-1))
{
u_OddCheck[3]=0; //奇校验位
}
else
{
printf("0");
WG_Data[3]<<=1;
}
u_DataBits[3]++;
}
if(u_DataBits[3] >= WG_DATA_BITS)
{
flag_wiegand[3]=1;
return;
}
}
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line5) != RESET && flag_wiegand[3]==0) //WIEGAND4_B
{ //Data1 -> 获得一位1
EXTI_ClearITPendingBit(EXTI_Line5);
if(u_DataBits[3] == 0)
{
u_EvenCheck[3]=1; //偶校验位
}
else if(u_DataBits[3] == (WG_DATA_BITS-1))
{
u_OddCheck[3]=1; //奇校验位
}
else
{
printf("1");
WG_Data[3]<<=1;
WG_Data[3]|=1;
}
u_DataBits[3]++;
}
if(EXTI_GetITStatus(EXTI_Line6) != RESET && flag_wiegand[0]==0) //WIEGAND1_A
{ //Data0 -> 获得一位0
EXTI_ClearITPendingBit(EXTI_Line6);
if(u_DataBits[0] == 0)
{
u_EvenCheck[0]=0; //偶校验位
}
else if(u_DataBits[0] == (WG_DATA_BITS-1))
{
u_OddCheck[0]=0; //奇校验位
}
else
{
//printf("0");
WG_Data[0]<<=1;
}
u_DataBits[0]++;
}
if(EXTI_GetITStatus(EXTI_Line7) != RESET && flag_wiegand[0]==0) //WIEGAND1_B
{ //Data1 -> 获得一位1
EXTI_ClearITPendingBit(EXTI_Line7);
if(u_DataBits[0] == 0)
{
u_EvenCheck[0]=1; //偶校验位
}
else if(u_DataBits[0] == (WG_DATA_BITS-1))
{
u_OddCheck[0]=1; //奇校验位
}
else
{
//printf("1");
WG_Data[0]<<=1;
WG_Data[0]|=1;
}
u_DataBits[0]++;
}
if(EXTI_GetITStatus(EXTI_Line8) != RESET && flag_wiegand[2]==0) //WIEGAND3_A
{ //Data0 -> 获得一位0
EXTI_ClearITPendingBit(EXTI_Line8);
if(u_DataBits[2] == 0)
{
WG_Data[2]=0x0;
u_EvenCheck[2]=0; //偶校验位
}
else if(u_DataBits[2] == (WG_DATA_BITS-1))
{
u_OddCheck[2]=0; //奇校验位
}
else
{
//printf("0");
WG_Data[2]<<=1;
}
//[u_DataBits] = 1;
u_DataBits[2]++;
}
//设置标志位
if(u_DataBits[0] >= WG_DATA_BITS)
{
flag_wiegand[0]=1;
return ;
}
if(u_DataBits[2] >= WG_DATA_BITS)
{
flag_wiegand[2]=1;
return ;
}
if(u_DataBits[3] >= WG_DATA_BITS)
{
flag_wiegand[3]=1;
return ;
}
}
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line11) != RESET && flag_wiegand[1]==0) //WIEGAND2_A
{ //Data0 -> 获得一位0
EXTI_ClearITPendingBit(EXTI_Line11);
if(u_DataBits[1] == 0)
{
WG_Data[1]=0x0;
u_EvenCheck[1]=0; //偶校验位
}
else if(u_DataBits[1] == (WG_DATA_BITS-1))
{
u_OddCheck[1]=0; //奇校验位
}
else
{
//printf("0");
WG_Data[1]<<=1;
}
u_DataBits[1]++;
}
if(EXTI_GetITStatus(EXTI_Line12) != RESET && flag_wiegand[1]==0) //WIEGAND2_B
{ //Data1 -> 获得一位1
EXTI_ClearITPendingBit(EXTI_Line12);
if(u_DataBits[1] == 0)
{
WG_Data[1]=0x0;
u_EvenCheck[1]=1; //偶校验位
}
else if(u_DataBits[1] == (WG_DATA_BITS-1))
{
u_OddCheck[1]=1; //奇校验位
}
else
{
//printf("1");
WG_Data[1]<<=1;
WG_Data[1]|=1;
}
//[u_DataBits] = 1;
u_DataBits[1]++;
}
if(EXTI_GetITStatus(EXTI_Line14) != RESET && flag_wiegand[2]==0 )//WIEGAND3_B
{ //Data1 -> 获得一位1
EXTI_ClearITPendingBit(EXTI_Line14);
if(u_DataBits[2] == 0)
{
WG_Data[2]=0x0;
u_EvenCheck[2]=1; //偶校验位
}
else if(u_DataBits[2] == (WG_DATA_BITS-1))
{
u_OddCheck[2]=1; //奇校验位
}
else
{
//printf("1");
WG_Data[2]<<=1;
WG_Data[2]|=1;
}
//[u_DataBits] = 1;
u_DataBits[2]++;
}
if(u_DataBits[1] >= WG_DATA_BITS)
{
flag_wiegand[1]=1;
return ;
}
else if(u_DataBits[2] >= WG_DATA_BITS)
{
flag_wiegand[2]=1;
return ;
}
}
/***********************************************************
* 函数名ataCheck
* 功 能:通过校验位,检验数据是否正确
* 输 入:第几个门禁
* 输 出:0->正确 1->数据错误 2->数据未接收完全
**********************************************************/
u8 DataCheck(u8 index)
{
u8 i;
u32 temp;
u8 oddcheck,evencheck;
u8 u_OddNums,u_EvenNums;
u_OddNums = u_EvenNums = 0;
if(u_DataBits[index] == WG_DATA_BITS)
{ //可以校验
for( i=0 ; i<((WG_DATA_BITS-2)/2) ; i++ )
{
temp=WG_Data[index];
if((temp>>i)&0x1)
u_OddNums++;
}
for( ; i<WG_DATA_BITS-2 ; i++ )
{
temp=WG_Data[index];
if((temp>>i)&0x1)
u_EvenNums++;
}
if(u_EvenNums % 2 == 0)
evencheck = 0; //偶数个1
else
evencheck = 1; //奇数个1
if(u_OddNums % 2 == 0)
oddcheck = 1; //偶数个1
else
oddcheck = 0; //奇数个1
/*
//校验位检测
printf("u_EvenCheck = %u ,evencheck = %u ,u_OddCheck = %u ,oddcheck = %u ",u_EvenCheck[index],evencheck,u_OddCheck[index],oddcheck);
return 0;
*/
if((u_EvenCheck[index] == evencheck) && (u_OddCheck[index] == oddcheck))
{ //正确
u_OddCheck[index]=u_EvenCheck[index]=2;
return 0;
}
else
{ //错误
u_OddCheck[index]=u_EvenCheck[index]=2;
return 1;
}
}
/*
else
return 2;
*/
}
/***********************************************************
* 函数名:IDDataPrintf
* 功 能:输出卡号信息
* 输 入:index(韦根标志)
* 输 出:WG_Data
**********************************************************/
void IDDataPrintf(u8 index)
{
if(DataCheck(index)==0)
{
printf("WIEGAND %d ID Card:%u ",index+1,WG_Data[index]);
/*
for( i=WG_DATA_BITS-2 ; i>=1 ; i--)
{
temp=WG_Data;
if((temp>>i-1)&0x1)
printf("1");
else
printf("0");
}
printf(" ");
*/
// 检索ID
/*
if(Flash_Strcmp(WG_Data[index]) == 0xffffffff)
{
LED1=0;//点亮LED1
LED0=1;//LED0灭
}
else
{
LED0=0;//点亮LED0
LED1=1;//LED1灭
}
*/
}/*
else
{
printf("WIEGAND Error ");
}*/
u_DataBits[index]=0;
return ;
// WG_Data=0x0;
/*
else
{
printf("Waiting Receive ");
}*/
}
#endif
[/mw_shl_code]
一周热门 更多>