#include "stm32f10x.h" // Device header
#include "OLED.h"
#include "mokuaicount.h"
extern int num;
/***************************************************/初始化旋钮模块
#include "xuanniu.h" // Device header
#include "stm32f10x.h"
void Xuanniu_init(uint16_t GPIO_Pin)//需要指定端口Pin进行模式设置
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);//使能GPIOA的时钟
GPIO_InitTypeDef Xuanniu_InitStruct;
Xuanniu_InitStruct.GPIO_Mode=GPIO_Mode_IPU;//设置为 上拉输入 模式
Xuanniu_InitStruct.GPIO_Pin=GPIO_Pin; //外界参数设置Pin口
Xuanniu_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; //输出速度为50MHz,在输入模式下该设置没用
GPIO_Init( GPIOA,&Xuanniu_InitStruct); //以上三个都要传入该函数初始化
}
/***************************************************/配置中断
//**该模块由中断函数实现,可接入任意输入模式的模块实现计数
//**这里引入Key实现数据的减法,LightR实现加法
#include "stm32f10x.h" // Device header
#include "mokuaicount.h"
#include "Delay.h"
#include "xuanniu.h"
int num;
void Count_interrupt_init(void)
{
//1、2、**步骤一二在模块内已经实现
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//AFIO时钟需要使能
Xuanniu_init(GPIO_Pin_0|GPIO_Pin_1);
//3**配置AFIO,选择输入引脚
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);//Xuanniu旋钮
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);
//4**配置EXTI寄存器,选择触发方式,这里配置EXTI和GPIO一样需要定义一个结构体
EXTI_InitTypeDef EXTI_InitStruct;
EXTI_InitStruct.EXTI_Line=EXTI_Line0|EXTI_Line1;
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStruct);
//5**配置NVIC,选择合适的中断优先级 --------每一个外设要单独配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//要先分组,整个系统NVIC只分配一种模式
//**配置Xuanniu A端
NVIC_InitTypeDef NVIC_InitStruct_Xuanniu;
NVIC_InitStruct_Xuanniu.NVIC_IRQChannel=EXTI0_IRQn;
NVIC_InitStruct_Xuanniu.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct_Xuanniu.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct_Xuanniu.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct_Xuanniu);
//**配置Xuanniu C端
NVIC_InitStruct_Xuanniu.NVIC_IRQChannel=EXTI1_IRQn;
NVIC_InitStruct_Xuanniu.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct_Xuanniu.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct_Xuanniu.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct_Xuanniu);
//****所有配置完成
}
//中断位置随便放,也不需要声明
void EXTI0_IRQHandler()//正转触发
{
if(EXTI_GetITStatus(EXTI_Line0)==SET)
{
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0)
{
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)==1)
{
num++;
}
}
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
void EXTI1_IRQHandler()
{
if(EXTI_GetITStatus(EXTI_Line1)==SET)
{
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)==0)
{
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==1)
{
num--;
}
}
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
/***************************************************/主函数
int main(void)
{
OLED_Init();
Count_interrupt_init();
OLED_ShowString(1,1,"Hello,my honey");
OLED_ShowString(2,1,"Hello WangFang");
OLED_ShowString(3,1,"Hello Tomorrow");
while(1)
{
OLED_ShowSignedNum(4,1,num,5);
}
}
/*********************阿布君***********************/
/****************编码不易,谢谢关注*****************/
/****************QQ:2062808868********************/
|