| Electronics > Beginners |
| Is possible to use multiple HAL_GPIO_Write in #define sentence? [SOLVED] |
| (1/2) > >> |
| igtaba:
Hello guys, again here I am with some newbiee questions. I'm trying to tiding up a bit of code, and I want to simplify the code by defining actions so I can call them and erase some repetition. I commented the next bit of code so you could see if what I'm trying to do is correct or not. I'm using KEIL uVision 5 and running the code in a STM32F103C8 --- Code: --- // I'm pretty sure that this is correct, is a define just to know which pin is connected to the stteper motor #define StepperA GPIO_PIN_9 #define StepperB GPIO_PIN_8 #define StepperC GPIO_PIN_7 #define StepperD GPIO_PIN_6 #define StepperE GPIO_PIN_5 // This is what I'm NOT sure if is wright, I have seen this done is some libraries in the past, but never exactly like this, so can you tell me is this is ok? #define StepperInit (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_SET)) #define StepperP1 (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_SET)) #define StepperP2 (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_RESET)) #define StepperP3 (HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperB, GPIO_PIN_SET),HAL_GPIO_WritePin(GPIOB, StepperC, GPIO_PIN_RESET),HAL_GPIO_WritePin(GPIOB, StepperD, GPIO_PIN_RESET)) // Again, of this I'm pretty sure that works #define StepperE_H (HAL_GPIO_WritePin(GPIOB, StepperE, GPIO_PIN_SET)) #define StepperE_L (HAL_GPIO_WritePin(GPIOB, StepperE, GPIO_PIN_RESET)) // Then in the rest of the code, I would use it like this for example: //Init Stepper Motor StepperE_H; // Enable HIGH StepperInit; // Init A,B,C,D HAL_Delay(1); StepperE_L; // Enable LOW --- End code --- So, please can you help me to know if the definition of multiple GPIO set and reset lines can be done in a single line of a #define ? |
| ataradov:
In its simplest form #define is a simple substitution. So what you are doing here will work. But it is not the best code practice. Just define the functions with those writes and call them instead. |
| KaneTW:
Why not just make a function? You can even inline it if you're worried about the overhead. Macros are a terrible idea for that kind of thing. |
| igtaba:
I'm due for a project presentation tomorrow in uni, but it's the first time that I'm programming Cortex and using Keil uVision 5. It's been a buggy ride and sometimes the functions do not work properly and even that example that I put before didn't work. So I just stated everything every time I need it and works like a charm, maybe after the presentation I'm gonna sit relaxed to make the code more efficient, but for now works. I actually want to keep learning Cortex but programming in Microsoft's Visual since it's what I always had used. But for this case the professor insisted that we use ARM IDE, so after this I'm ditching it Sorry for my bad english, and again thanks for the answers!! |
| ataradov:
Ok, I can pretty much guarantee that it is not Keil's fault. Keil has its bugs, but they are not something a beginner will ever experience. Also what do you mean by "Microsoft's Visual"? Where are you going to get that for Cotrex-M devices? |
| Navigation |
| Message Index |
| Next page |