#include "xzbm.h"
//关于旋转编码器
//PC(3) PC(4) PC(0)
void XZBM_Init(void)
{
// RCC 和 GPIO 配置
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //应为输入信号会拉低电平 所以上拉输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
//分组
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource3);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource4);
//EXTI0 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;//EXTI2中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//EXTI3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;//EXTI2中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//EXTI4 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;//EXTI2中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
EXTI_InitStructure.EXTI_Line=EXTI_Line0|EXTI_Line3|EXTI_Line4 ;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
//按键
void EXTI0_IRQHandler(void)
{
if(EXTI_GetFlagStatus(EXTI_Line0)==SET)
{
LED1=!LED1;
delay_ms(10);
if(PCin(0)==0)
{
count=500;
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
LCD_ShowxNum(195,120,count,2,24,0);//显示数字
TIM_SetCompare4(TIM4,count); // 舵机180度
}
//当PG3先下降++
void EXTI3_IRQHandler(void)
{
LED1=!LED1;
if(EXTI_GetFlagStatus(EXTI_Line3)==SET)
{
delay_ms(1);
if(PCin(3)==0&&PCin(4)==1) //当3先下降 4后下降
{
if(PCin(3)==0&&PCin(4)==0)
{
count=count+10;//顺时针
//delay_ms(10);
}
else
{
count=count-10;//顺时针
}
}
while(!(PGin(3)==1&&PGin(4)==1)); //真时不停循环(不相等) 假时退出(都为1)
EXTI_ClearITPendingBit(EXTI_Line3);; //清除LINE3上的中断标志位
}
if(count>=2510)
count=500;
else if(count<=490)
count=2500;
LCD_ShowxNum(195,120,count,5,24,0);//显示数字
TIM_SetCompare4(TIM4,count); // 舵机180度
EXTI_ClearITPendingBit(EXTI_Line3);; //清除LINE3上的中断标志位
}
//当PG4先下降--
void EXTI4_IRQHandler(void)
{
LED1=!LED1;
if(EXTI_GetFlagStatus(EXTI_Line4)==SET)
{
delay_ms(1);
if(PCin(4)==0&&PCin(3)==1) //当4先下降 3后下降
{
if(PCin(3)==0&&PCin(4)==0)
{
count=count-10;//顺时针
//delay_ms(10);
}
else
{
count=count+10;//顺时针
}
}
while(!(PGin(3)==1&&PGin(4)==1)); //真时不停循环(不相等) 假时退出(都为1)
}
if(count>=2510)
count=500;
else if(count<=490)
count=2500;
LCD_ShowxNum(195,120,count,5,24,0);//显示数字
TIM_SetCompare4(TIM4,count); // 舵机180度
EXTI_ClearITPendingBit(EXTI_Line4);; //清除LINE3上的中断标志位
}