switch_test/User/Safetybarrier.c

222 lines
8.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "basic.h"
void safe_gpio_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //配置输入模式为浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //配置输出频率为100MHZ
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
/*对应安全栅1~4 */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/*对应安全栅5~11 */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOG, &GPIO_InitStructure);
/*对应安全栅12~18 */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*对应安全栅19,20 */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void Gpio_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
/*I2C F0,F1 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//模拟输入
GPIO_InitStructure.GPIO_OType= GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//上拉
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化
/*ENB,Vout_EN F2,F11 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/*BOOT1 B2 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*Beeper,LED G0,G1 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure);
/*限位 E11,E12,E13,E14 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //配置输入模式为浮空输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //配置输出频率为100MHZ
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //配置输入模式为浮空输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //配置输出频率为100MHZ
GPIO_Init(GPIOE, &GPIO_InitStructure);
/*PUL,DIR,ENA,PUL2 D12,D13,D14,D15 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //输出模式
//GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/*DO C6 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //输入模式
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_2);
GPIO_ResetBits(GPIOD,GPIO_Pin_14 | GPIO_Pin_13);
GPIO_ResetBits(GPIOE,GPIO_Pin_15);
GPIO_SetBits(GPIOF,GPIO_Pin_11);
GPIO_ResetBits(GPIOG,GPIO_Pin_0 | GPIO_Pin_1);
GPIO_SetBits(GPIOD,GPIO_Pin_14);
GPIO_ResetBits(GPIOF,GPIO_Pin_2);
GPIO_ResetBits(GPIOE,GPIO_Pin_14 | GPIO_Pin_13);
}
u8 state[20] = {0};
char s = 0;
//获取20个安全栅状态
void get_state()
{
state[0] = GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_4 );
state[1] = GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_5 );
state[2] = GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_6 );
state[3] = GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_7 );
state[4] = GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_9 );
state[5] = GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_10);
state[6] = GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_11);
state[7] = GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_12);
state[8] = GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_13);
state[9] = GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_14);
state[10] = GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_15);
state[11] = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3 );
state[12] = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4 );
state[13] = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5 );
state[14] = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6 );
state[15] = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7 );
state[16] = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8 );
state[17] = GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9 );
state[18] = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_0 );
state[19] = GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_1 );
//s = GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_6 );
}
uint16_t N = 0;
int MOT_Fre = 0;
uint32_t count = 0;
uint32_t count_array[20];
uint32_t count_sum = 0;
void EXTI_Key_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* 配置NVIC为优先级组1 */
/* 提示 NVIC_PriorityGroupConfig() 在整个工程只需要调用一次来配置优先级分组*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* 配置中断源按键1 */
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
/* 配置抢占优先级1 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
/* 配置子优先级1 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
/* 使能中断通道 */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*开启按键GPIO口的时钟*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
/* 使能 SYSCFG 时钟 使用GPIO外部中断时必须使能SYSCFG时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* 选择IO输入的引脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;;
/* 设置引脚为输入模式 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
/* 设置引脚不上拉也不下拉 */
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
/* 使用上面的结构体初始化按键 */
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* 连接 EXTI 中断源 到key1引脚 */
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC,EXTI_PinSource6);
/* 选择 EXTI 中断源 */
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);
}
/*********************************************END OF FILE**********************/
void EXTI_CTRL(char flag)
{
EXTI_InitTypeDef EXTI_InitStructure;
if(flag == 0)
EXTI_InitStructure.EXTI_LineCmd = DISABLE;
else
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}