1

I have the following code :

#define phase_1 0b00011000
#define phase_2 0b00101000
#define phase_3 0b00001010
#define phase_4 0b00001100

Which actually corresponds to the pins that need to be set to high for a given phase of a stepper motor. It works perfectly fine, but it is not really clear which bits correspond to which wire.

What I am trying to acheive is something like that

#define Yellow PORTB5
#define Orange PORTB4
#define Enable PORTB3
#define Blue PORTB2
#define White PORTB1

and then having something like that to set my bit in a way which is really easy to understand

#define phase_1 (1 << Yellow) | (1 << Enable)
#define phase_2 (1 << Blue) | (1 << Enable)

etc.

Is there a way to make define in a fashion similar to that so that when I look at my code in some time I will exactly know where each wire goes and what it means?

4

1 回答 1

2

如果我正确理解了你的问题,我想你几乎有它。您只需要 PORTB 与对应于 pin 的数值相对应,这是您需要移动的数量。所以你可以添加这样的东西:

#define PORTB5 5
#define PORTB4 4
#define PORTB3 3
#define PORTB2 2
#define PORTB1 1

这假设引脚映射到移位值。您可能需要根据需要调整此顺序。当然,添加评论对这类事情也有很大帮助。

于 2013-10-11T04:05:14.667 回答