#include "inc/delay.h"
#include "inc/hc595.h"
#define uchar unsigned char
#define uint unsigned int
sbit PIN_ROTARY_A = P2^0 //引脚1接口
sbit PIN_ROTARY_B = P2^1 //引脚2接口
sbit PIN_ROTARY_C = P2^2 //按下的接口
sbit PIN_ROTARY_D = P2^3 //按下的接口
//uchar code table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}
/* 存储待发送的数据 */
/* 1->8的段码表 */
code unsigned char ucDis_Segmentcode[16] = {0x3f,0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}
/* 位码表 */
code unsigned char ucDis_Bitcode[8] = {0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F}
uchar dis_XS[8] = {0x3f,0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d}
uchar count = 0
uchar flag
uchar Last_BMB_status
uchar Current_BMB_status
void display()
{
uchar i
dis_XS[0] = ucDis_Segmentcode[count/100]//百位
dis_XS[1] = ucDis_Segmentcode[count%100/10]//十位
dis_XS[2] = ucDis_Segmentcode[count%10]//个位
if(i <2)
{
i++
}
else
{
i = 0
}
// for (i = 0i <3i++ )
{
Send_Data(dis_XS[i], ucDis_Bitcode[i])
//PIN_ROTARY_D = 0
// Delay1ms(1)
}
}
//************************************************
void main()
{
TMOD=0x01//定时器0,工作方式1
TH0=0xD8
TL0=0xF0//给定时器装上初值,10ms中断一次
ET0=1//打开定时器中断
EA =1//打开总中断
TR0=1//启动定时器0
while(1)
{
//display()
Last_BMB_status=PIN_ROTARY_B
while(!PIN_ROTARY_A) //BMA为低电平时
{
Current_BMB_status = PIN_ROTARY_B
flag = 1//标志位置为1说明编码开关被旋转了
}
if(flag == 1)
{
flag = 0//时刻要注意这一点!给标志位清零
if((Last_BMB_status == 0)&&(Current_BMB_status == 1)) //BMB上升沿表示正转
{
count++
if(count == 255)
{
count = 0
}
}
if((Last_BMB_status == 1)&&(Current_BMB_status == 0)) //BMB下降沿表示反转
{
count--
if(count == 0)
{
count = 255
}
}
}
}
}
//***********************************************
void timer0() interrupt 1//定时器0的中断服务程序
{
TH0=0xF8 // TH0=0xD8
TL0=0xF0//再次装入初值
display()//每隔10ms显示一次
if(!PIN_ROTARY_C) //按下旋转编码开关则计数清零
{
count = 0
}
PIN_ROTARY_D = !PIN_ROTARY_D
}
配置一个1ms定时器,并设置为自动清零模式,配置好后记得打开定时器中断。首先要开启定时器TIM3,我们使用这个 HAL_TIM_IC_Start_IT(htim, Channel);启动定时器。使用定时器回调函数
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
在里面编写上面的程序,因为我们将B相接在PA7引脚,所以我们使用switch case语句进行判断引脚电平,如果单片机检测到A相为高电平就会进入这个中断判断B相电平,低电平为反转,高电平极为正转(程序中的cnt为计数作用)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)