热度 19
2014-1-11 08:14
1184 次阅读|
0 个评论
sbit Cor_A = P2^0; // 正交编码器入口 sbit Cor_B = P2^1; sbit Cor_Z = P2^2; unsigned int xdata Coder_Value; char bdata Bit1_Value _at_ 0x2F; sbit Temp_Bit = Bit1_Value^0; void CP00_Serve(void); // A void CP01_Serve(void); void CP10_Serve(void); // B void CP11_Serve(void); void Comparator_Init() { int i = 0; SFRPAGE = CPT0_PAGE; CPT0MD = 0x30; CPT0CN = 0x8F; for (i = 0; i 600; i++); CPT0CN = ~0x30; SFRPAGE = CPT1_PAGE; CPT1MD = 0x30; CPT1CN = 0x8F; for (i = 0; i 600; i++); CPT1CN = ~0x30; } void Interrupts_Init() { // IE |= 0x17; // IP |= 0x17; EIE1 |= 0xF0; EIP1 |= 0xF0; } void main (void) { Comparator_Init(); Interrupts_Init(); while(1) { Temp_Value = Coder_Value; } return; } /************ 正交编码器入口 *** A ****/ void CP00_Serve(void) interrupt 10 {CPT0CN = ~0x10;// 下降沿中断 if(Cor_B == 1){Coder_Value ++; Temp_Bit = 1;} if(Cor_B == 0){Coder_Value --; Temp_Bit = 0;} pid_A.ActualSpeed = Coder_Value; pid_B.ActualSpeed = Coder_Value; return; } /******************************* A ****/ void CP01_Serve(void) interrupt 11 {CPT0CN = ~0x20;// 上升沿中断 if(Cor_B == 0){Coder_Value ++; Temp_Bit = 1;} if(Cor_B == 1){Coder_Value --; Temp_Bit = 0;} pid_A.ActualSpeed = Coder_Value; pid_B.ActualSpeed = Coder_Value; if(0x07 (P2 0x07)){Pow_ON = 0; if(Coder_Value 4000)Coder_Value = 1; if(Coder_Value 1)Coder_Value = 4000; if(Cor_Z == 1){Pow_ON = 1 ; Coder_Value = 2000;} } return; } /******************************* B ****/ void CP10_Serve(void) interrupt 12 {CPT1CN = ~0x10;// 下降沿中断 if(Cor_A == 0){Coder_Value ++; Temp_Bit = 1;} if(Cor_A == 1){Coder_Value --; Temp_Bit = 0;} pid_A.ActualSpeed = Coder_Value; pid_B.ActualSpeed = Coder_Value; return; } /******************************* B ****/ void CP11_Serve(void) interrupt 13 {CPT1CN = ~0x20;// 上升沿中断 if(Cor_A == 1){Coder_Value ++; Temp_Bit = 1;} if(Cor_A == 0){Coder_Value --; Temp_Bit = 0;} pid_A.ActualSpeed = Coder_Value; pid_B.ActualSpeed = Coder_Value; if(0x07 (P2 0x07)){Pow_ON = 0; if(Coder_Value 4000)Coder_Value = 1; if(Coder_Value 1)Coder_Value = 4000; if(Cor_Z == 1){Pow_ON = 1 ; Coder_Value = 2000;} } return; } /*************** END ********************/