示波器看了是标准的相位差为90°的a b
现在可以检测到左旋反转的 左旋立即右旋能检测到一次右旋 直接右旋无反应
代码如下 求指点
void Rotate_encode_initial(void)
{
BMA_pre_status[0]=PORTC_IN&0x01; //pc0
BMB_pre_status[0]=PORTC_IN&0x02;
BMA_pre_status[1]=PORTC_IN&0x04;
BMB_pre_status[1]=PORTC_IN&0x08; //pc3
//BMA_pre_status[2]=PORTC_IN&0x10;
//BMB_pre_status[2]=PORTC_IN&0x20;
//BMA_pre_status[3]=PORTC_IN&0x40;
//BMB_pre_status[3]=PORTC_IN&0x80; //pc7
/* BMA_pre_status[4]=PORTE_IN&0x01;
BMB_pre_status[4]=PORTE_IN&0x02;
BMA_pre_status[5]=PORTE_IN&0x04;
BMB_pre_status[5]=PORTE_IN&0x08;
BMA_pre_status[6]=PORTE_IN&0x10;
BMB_pre_status[6]=PORTE_IN&0x20;
BMA_pre_status[7]=PORTE_IN&0x40;
BMB_pre_status[7]=PORTE_IN&0x80;
BMA_pre_status[8]=PORTF_IN&0x01;
BMB_pre_status[8]=PORTF_IN&0x02;
BMA_pre_status[9]=PORTF_IN&0x04;
BMB_pre_status[9]=PORTF_IN&0x08;
BMA_pre_status[10]=PORTF_IN&0x10;
BMB_pre_status[10]=PORTF_IN&0x20;
BMA_pre_status[11]=PORTF_IN&0x40;
BMB_pre_status[11]=PORTF_IN&0x80; */
}
/*PORTC 外部中断处理*/
ISR(PORTC_INT1_vect) //(B 相) pc 1 3 5 7 配置为上升沿触发中断
{
PORTC_INT_Disable;
if(BMB_pre_status[0]!=(PORTC_IN&0x02)) // c.1口被拉高
{
delay_us(1000); //消抖
if(BMB_pre_status[0]!=(PORTC_IN&0x02)) //
{
if((PORTC_IN&0x01)==0x00)
BMB_pre_status[0]=0x02;
}
}
if(BMB_pre_status[1]!=(PORTC_IN&0x08))
{
delay_us(1000);
if(BMB_pre_status[1]!=(PORTC_IN&0x08))
{
if((PORTC_IN&0x04)==0x00)
BMB_pre_status[1]=0x08;
}
}
/*if(BMB_pre_status[2]!=(PORTC_IN&0x20))
{
delay_us(1000);
if(BMB_pre_status[2]!=(PORTC_IN&0x20))
{
if((PORTC_IN&0x10)==0x00)
BMB_pre_status[2]=0x20;
}
}
if(BMB_pre_status[3]!=(PORTC_IN&0x80))
{
delay_us(1000);
if(BMB_pre_status[3]!=(PORTC_IN&0x80))
{
if((PORTC_IN&0x40)==0x00)
BMB_pre_status[3]=0x80;
}
}*/
PORTC_INT_Enable;
}
ISR(PORTC_INT0_vect) // A 相 pc 0 2 4 6 配置为双沿触发中断
{
PORTC_INT_Disable;
if(BMA_pre_status[0]!=(PORTC_IN&0x01)) // c.0口 为高
{
delay_us(1000);
if(BMA_pre_status[0]!=(PORTC_IN&0x01)) //
{
BMA_current_status[0]=PORTC_IN&0x01; //
BMB_current_status[0]=PORTC_IN&0x02;
Encode_temp=(BMB_pre_status[0]<<2)|(BMA_pre_status[0]<<2)|BMB_current_status[0]|BMA_current_status[0];
Encode_temp=Encode_temp&0x0f;
BMA_pre_status[0]=BMA_current_status[0];
BMB_pre_status[0]=BMB_current_status[0];
switch(Encode_temp)
{
case 0x03: usart_putchar(USART_SERIAL_EXAMPLE, Pos_rotation_Encode1); // menu 正转
BMB_pre_status[0]=0x00; //
break;
case 0x0e: usart_putchar(USART_SERIAL_EXAMPLE,Pos_rotation_Encode1);
BMB_pre_status[0]=0x00; //?
break;
case 0x0b: usart_putchar(USART_SERIAL_EXAMPLE,Neg_rotation_Encode1); //反转
BMB_pre_status[0]=0x00; //
break;
case 0x0c: usart_putchar(USART_SERIAL_EXAMPLE,Neg_rotation_Encode1);
BMB_pre_status[0]=0x00; //
break;
default: break;
}
Encode_temp=0;
}
}
if(BMA_pre_status[1]!=(PORTC_IN&0x04))
{
delay_us(1000);
if(BMA_pre_status[1]!=(PORTC_IN&0x04))
{
BMA_current_status[1]=PORTC_IN&0x04;
BMB_current_status[1]=PORTC_IN&0x08;
Encode_temp=BMB_pre_status[1]|BMA_pre_status[1]|(BMB_current_status[1]>>2)|(BMA_current_status[1]>>2);
Encode_temp=Encode_temp&0x0f;
BMA_pre_status[1]=BMA_current_status[1];
BMB_pre_status[1]=BMB_current_status[1];
switch(Encode_temp)
{
case 0x03: usart_putchar(USART_SERIAL_EXAMPLE,Pos_rotation_Encode2);
BMB_pre_status[1]=0x00; //
break;
case 0x0e: usart_putchar(USART_SERIAL_EXAMPLE,Pos_rotation_Encode2);
BMB_pre_status[1]=0x00;
break;
case 0x0b: usart_putchar(USART_SERIAL_EXAMPLE,Neg_rotation_Encode2);
BMB_pre_status[1]=0x00; //
break;
case 0x0c: usart_putchar(USART_SERIAL_EXAMPLE,Neg_rotation_Encode2);
BMB_pre_status[1]=0x00; //
break;
default: break;
}
Encode_temp=0;
}
} |