/*
Stepper motor state machine - pc test code
*/
#include <stdio.h>
#include "binary.h"
#include "itob.h"
#define setForward(m) (((m)->State) &= 0x03)
#define setReverse(m) (((m)->State) |= 0x04)
typedef struct stepper4ph_t {
unsigned State :4;
} stepper4ph ;
unsigned char stateTable[] = {
(3)+(b0101<<4),
(0)+(b0110<<4),
(1)+(b1010<<4),
(2)+(b1001<<4),
(5)+(b0001<<4),
(6)+(b0010<<4),
(7)+(b0100<<4),
(4)+(b1000<<4)
};
unsigned char stepmotor (stepper4ph * motor) ;
int main(void) {
stepper4ph motor;
int i;
motor.State = 0;
setForward(&motor);
for (i = 0;i < 30; i++) {
if (i > 12) setReverse(&motor);
printf("%s\n", itob4
[stepmotor
(&motor
)]); }
return 0;
}
unsigned char stepmotor (stepper4ph * motor) {
unsigned char index;
index = stateTable[motor->State];
motor->State = (index & 0x0F); // calc next state
return (index & 0xF0) >> 4; // calc motor bits
}
LyoKICBTdGVwcGVyIG1vdG9yIHN0YXRlIG1hY2hpbmUgLSBwYyB0ZXN0IGNvZGUKKi8KCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSAiYmluYXJ5LmgiCiNpbmNsdWRlICJpdG9iLmgiCgojZGVmaW5lIHNldEZvcndhcmQobSkgKCgobSktPlN0YXRlKSAmPSAweDAzKQojZGVmaW5lIHNldFJldmVyc2UobSkgKCgobSktPlN0YXRlKSB8PSAweDA0KQoKdHlwZWRlZiBzdHJ1Y3Qgc3RlcHBlcjRwaF90IHsgICAgCiAgICAgIHVuc2lnbmVkIFN0YXRlICAgICAgOjQ7ICAgICAgCn0gc3RlcHBlcjRwaCA7CgoKCnVuc2lnbmVkIGNoYXIgc3RhdGVUYWJsZVtdID0geyAgCiAgICAoMykrKGIwMTAxPDw0KSwgICAgICAgICAgICAgICAgICAgIAogICAgKDApKyhiMDExMDw8NCksCiAgICAoMSkrKGIxMDEwPDw0KSwKICAgICgyKSsoYjEwMDE8PDQpLAogICAgKDUpKyhiMDAwMTw8NCksICAgIC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