Question

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?

Was it helpful?

Solution

If I understand your question correctly, I think you almost have it. You just need PORTB to correspond with a numeric value which corresponds to pin , which is the amount you need to shift by. So you could add something like this:

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

This assumes the mapping of the pins to the shift value. You may have to adjust this order as necessary. Of course adding comments helps out enormously with this sort of thing too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top