diyi,你什么电机?第二,步进电机你是几线几向的?你这都没说清楚让人咋写?
其实思想很简单,比如你是用PWM写的,就是控制脉冲占空比。
思想都说了,你要代码?
给你个例子你看看就行了,估计直接用是不可能的,接口不同。
// 左轮驱动
void __leftMotorContr (void)
{
static int8 cStep = 0; /* ±£Žæµç»úµ±Ç°Î»Öà */
switch (__GmLeft.cDir) {
case __MOTORGOAHEAD: /* ÏòÇ°²œœø */
cStep = (cStep + 1) % 8;
break;
case __MOTORGOBACK: /* Ïòºó²œœø */
cStep = (cStep + 7) % 8;
break;
default:
break;
}
switch (cStep) {
case 0: /* A2B2 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2);
break;
case 1: /* B2 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLB1 | __PHLB2);
break;
case 2: /* A1B2 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA2 | __PHLB1 | __PHLB2);
break;
case 3: /* A1 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA2);
break;
case 4: /* A1B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA2 | __PHLB2);
break;
case 5: /* B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLB2);
break;
case 6: /* A2B1 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA1 | __PHLA2 | __PHLB2);
break;
case 7: /* A2 */
GPIOPinWrite(GPIO_PORTD_BASE,
__PHLA1 | __PHLA2 | __PHLB1 | __PHLB2,
__PHLA1 | __PHLA2);
break;
default:
break;
}
}
// 右轮驱动同理