|
网上找了2种关于旋钮编码开关的代码,测试了下没有调试成功,现发上来请大家帮忙看看问题在哪里!
#include
#define CodingsWitch_A P2IN&BIT1
#define CodingsWitch_B P2IN&BIT2
//DCO 16MHZ
typedef unsigned int u16;
typedef unsigned char u8;
u16 x;
u8 cnt;
u8 RIGHT,LEFT;
u8 Last_BMB_status;
u8 Current_BMB_status;
void mdelay(u16 times)
{
u16 i = times, j;
while(i > 0)
{
i--;
for(j = 4000; j > 0; j--) {}
}
}
void udelay(u16 times)
{
u16 i = times * 4;
while(i > 0)
{
i--;
}
}
u8 yy_pwm(u8 cnt)
{
P1DIR |= BIT0;
P1OUT |= BIT0;
udelay(100);
P1OUT &=~BIT0;//resetpluse
udelay(100);
P1DIR |= BIT2;
while(cnt--)
{
P1OUT |= BIT2;
udelay(100);
P1OUT &=~ BIT2;
udelay(100);
}
return 1;
}
/***************编码开关1**************************/
void CodingsWitchPolling()
{
static u8 Aold,Bold; //定义了两个变量用来储蓄上一次调用此方法是编码开关两引脚的电平
static u8 st;
if(CodingsWitch_A && CodingsWitch_B) //CodingsWitch_A=P2.1 CodingsWitch_B=P2.2
st = 1;
if(st)
{
if(CodingsWitch_A==0&&CodingsWitch_B==0) //如果当前编码开关的两个引脚都为底电平执行下面的步骤
{
if(Bold) //为高说明编码开关在向加大的方向转
{
st = 0;
RIGHT=1;
}
if(Aold) //为高说明编码开关在向减小的方向转
{
st = 0;
LEFT=1;
}
}
}
Aold = CodingsWitch_A; //储存这次的状态,以备下次调用时比较
Bold = CodingsWitch_B; //储存
}
/*************************/
/*********编码开关2***********
void CodingsWitchPolling()
{
static u8 st; //定义了一个变量用来储蓄以前是否出现了两个引脚都为高电平状态
P2DIR &=~ (BIT1+BIT2);
Last_BMB_status=P2IN&BIT2;
while(!P2IN&BIT1) //p2.1-----A脚 A脚为低电平时
{
st=1;
Current_BMB_status=P2IN&BIT2;
P1DIR |= BIT6;
}
if(st)
{
st=0;
if((Last_BMB_status==0)&&(Current_BMB_status==1)) //如果B脚电平由0变为1则顺时针旋转
{
RIGHT=1;
P1OUT &=~ BIT6; //熄灭led
}
if((Last_BMB_status==1)&&(Current_BMB_status==0))//如果B脚电平由高变低则逆时针旋转
{
LEFT=1;
P1OUT |= BIT6; //点亮led
}
}
}
*****************************/
void main(void)
{
WDTCTL = WDTPW +WDTHOLD; // Stop Watchdog Timer
if (CALBC1_16MHZ ==0xFF || CALDCO_16MHZ == 0xFF)
{
while(1); // If calibration constants erased
// do not load, trap CPU!!
}
//1Mhz
BCSCTL1 = CALBC1_16MHZ; // Set range
DCOCTL = CALDCO_16MHZ; // Set DCO step + modulation */
/* //8Mhz
BCSCTL1 = CALBC1_8MHZ; // Set range
DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation */
/* //12Mhz
BCSCTL1 = CALBC1_12MHZ; // Set range
DCOCTL = CALDCO_12MHZ; // Set DCO step + modulation*/
/* //16Mhz
BCSCTL1 = CALBC1_16MHZ; // Set range
DCOCTL = CALDCO_16MHZ; // Set DCO step + modulation*/
while(1)
{
CodingsWitchPolling();
if(RIGHT)
{
RIGHT=0;
// cnt=1;
yy_pwm(1);
}
if(LEFT)
{
LEFT=0;
// cnt=2;
yy_pwm(2);
}
}
}
|
|