![]() |
/* 兩個ULN2003驅(qū)動兩個5線4相步進(jìn)電機(jī) */ /* 步進(jìn)電機(jī)四相端口*/ #define pMotor_A_A_Init() GPIO_Init(GPIOC,GPIO_PIN_7,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_A_B_Init() GPIO_Init(GPIOC,GPIO_PIN_6,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_A_C_Init() GPIO_Init(GPIOC,GPIO_PIN_5,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_A_D_Init() GPIO_Init(GPIOC,GPIO_PIN_3,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_B_A_Init() GPIO_Init(GPIOD,GPIO_PIN_0,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_B_B_Init() GPIO_Init(GPIOD,GPIO_PIN_2,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_B_C_Init() GPIO_Init(GPIOD,GPIO_PIN_3,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_B_D_Init() GPIO_Init(GPIOD,GPIO_PIN_4,GPIO_MODE_OUT_PP_LOW_FAST) /* A AB B BC C CD D DA */ uint8_t Beat_A_Code[8] = {0x80,0xC0,0x40,0x60,0x20,0x28,0x08,0x88}; //步進(jìn)電機(jī)A四相八拍驅(qū)動 /* A AB B BC C CD D DA */ uint8_t Beat_B_Code[8] = {0x01,0x05,0x04,0x0C,0x08,0x18,0x10,0x11}; //步進(jìn)電機(jī)B四相八拍驅(qū)動 int16_t motor_A_Beats = 0; int16_t motor_B_Beats = 0; uint8_t index_A = 0; uint8_t index_B = 0; #pragma vector=0x19 __interrupt void TIM4_UPD_OVF_IRQHandler(void) //定時中斷 1ms { /* 步進(jìn)電機(jī)A轉(zhuǎn)動控制流量值大于4L */ if(motor_A_Beats != 0) { if(motor_A_Beats > 0) //正轉(zhuǎn) { index_A++; index_A &= 0x07; motor_A_Beats--; } else //反轉(zhuǎn) { index_A--; index_A &= 0x07; motor_A_Beats++; } temp0 = GPIOC->ODR; temp0 &= 0x17; temp0 |= Beat_A_Code[index_A]; GPIOC->ODR = temp0; } else { GPIOC->ODR &= 0x17; } /* 步進(jìn)電機(jī)B轉(zhuǎn)動控制流量值小于等于4L */ if(motor_B_Beats != 0) { if(motor_B_Beats > 0) //正轉(zhuǎn) { index_B++; index_B &= 0x07; motor_B_Beats--; } else //反轉(zhuǎn) { index_B--; index_B &= 0x07; motor_B_Beats++; } temp2 = GPIOD->ODR; temp2 &= 0xE2; temp2 |= Beat_B_Code[index_B]; GPIOD->ODR = temp2; } else { GPIOD->ODR &= 0xE2; } TIM4_ClearITPendingBit(TIM4_IT_UPDATE); } |
/* 兩個ULN2003驅(qū)動兩個5線4相步進(jìn)電機(jī) */ /* 步進(jìn)電機(jī)四相端口*/ #define pMotor_A_A_Init() GPIO_Init(GPIOC,GPIO_PIN_7,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_A_B_Init() GPIO_Init(GPIOC,GPIO_PIN_6,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_A_C_Init() GPIO_Init(GPIOC,GPIO_PIN_5,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_A_D_Init() GPIO_Init(GPIOC,GPIO_PIN_3,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_B_A_Init() GPIO_Init(GPIOD,GPIO_PIN_0,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_B_B_Init() GPIO_Init(GPIOD,GPIO_PIN_2,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_B_C_Init() GPIO_Init(GPIOD,GPIO_PIN_3,GPIO_MODE_OUT_PP_LOW_FAST) #define pMotor_B_D_Init() GPIO_Init(GPIOD,GPIO_PIN_4,GPIO_MODE_OUT_PP_LOW_FAST) /* A AB B BC C CD D DA */ uint8_t Beat_A_Code[8] = {0x80,0xC0,0x40,0x60,0x20,0x28,0x08,0x88}; //步進(jìn)電機(jī)A四相八拍驅(qū)動 /* A AB B BC C CD D DA */ uint8_t Beat_B_Code[8] = {0x01,0x05,0x04,0x0C,0x08,0x18,0x10,0x11}; //步進(jìn)電機(jī)B四相八拍驅(qū)動 int16_t motor_A_Beats = 0; int16_t motor_B_Beats = 0; uint8_t index_A = 0; uint8_t index_B = 0; #pragma vector=0x19 __interrupt void TIM4_UPD_OVF_IRQHandler(void) //定時中斷 1ms { /* 步進(jìn)電機(jī)A轉(zhuǎn)動控制流量值大于4L */ if(motor_A_Beats != 0) { if(motor_A_Beats > 0) //正轉(zhuǎn) { index_A++; index_A &= 0x07; motor_A_Beats--; } else //反轉(zhuǎn) { index_A--; index_A &= 0x07; motor_A_Beats++; } temp0 = GPIOC->ODR; temp0 &= 0x17; temp0 |= Beat_A_Code[index_A]; GPIOC->ODR = temp0; } else { GPIOC->ODR &= 0x17; } /* 步進(jìn)電機(jī)B轉(zhuǎn)動控制流量值小于等于4L */ if(motor_B_Beats != 0) { if(motor_B_Beats > 0) //正轉(zhuǎn) { index_B++; index_B &= 0x07; motor_B_Beats--; } else //反轉(zhuǎn) { index_B--; index_B &= 0x07; motor_B_Beats++; } temp2 = GPIOD->ODR; temp2 &= 0xE2; temp2 |= Beat_B_Code[index_B]; GPIOD->ODR = temp2; } else { GPIOD->ODR &= 0xE2; } TIM4_ClearITPendingBit(TIM4_IT_UPDATE); } |
我的這一個是用uln2003驅(qū)動電路的
|
Powered by 單片機(jī)教程網(wǎng)