|

楼主 |
发表于 2016-6-9 13:11:48
|
显示全部楼层
xisogou 发表于 2016-6-9 08:30 
你好,能请教下,那个旋转编码器的程序怎么写的吗?
#define Rate_U PINB.0
#define Rate_I PINC.1
unsigned char Code_SW_V(void)
{
static unsigned char Aold,Bold; //定义了两个变量用来储蓄上一次调用此方法是编码开关两引脚的电平
static unsigned char stat; //定义了一个变量用来储蓄以前是否出现了两个引脚都为高电平的状态
unsigned char tmp = 1;
if(PINC.2&&PINC.3)
stat = 1; //
if(stat) //如果stat为1执行下面的步骤
{
if(PINC.2==0&&PINC.3==0) //如果当前编码开关的两个引脚都为底电平执行下面的步骤
{
if(Bold) //为高说明编码开关在向加大的方向转
{
stat = 0;
tmp++; //
}
if(Aold) //为高说明编码开关在向减小的方向转
{
stat = 0;
tmp--;
}
}
}
Aold = PINC.2;
Bold = PINC.3;
return tmp; //设返回值
}
///////////////////////////////////////////////////////////////////////////
unsigned char Code_SW_A(void)
{
static unsigned char Aold,Bold; //定义了两个变量用来储蓄上一次调用此方法是编码开关两引脚的电平
static unsigned char stat; //定义了一个变量用来储蓄以前是否出现了两个引脚都为高电平的状态
unsigned char tmp = 1;
if(PINC.4&&PINC.5)
stat = 1; //
if(stat) //如果stat为1执行下面的步骤
{
if(PINC.4==0&&PINC.5==0) //如果当前编码开关的两个引脚都为底电平执行下面的步骤
{
if(Bold) //为高说明编码开关在向加大的方向转
{
stat = 0;
tmp++; //
}
if(Aold) //为高说明编码开关在向减小的方向转
{
stat = 0;
tmp--;
}
}
}
Aold = PINC.4;
Bold = PINC.5;
return tmp; //设返回值
} |
|