Is possible to use multiple HAL_GPIO_Write in #define sentence? [SOLVED] - Page 1
Forum section: electronics / beginners

author: igtaba
author_rank: Newbie
timestamp: December 04, 2018, 03:53:03 pm
content: 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 STM32F103C8Code:// I'm pretty sure that this is correct, is a define just to know which pin is connected to the stteper motor#define StepperAGPIO_PIN_9#define StepperBGPIO_PIN_8#define StepperCGPIO_PIN_7#define StepperDGPIO_PIN_6#define StepperEGPIO_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 MotorStepperE_H;// Enable HIGHStepperInit;// Init A,B,C,DHAL_Delay(1);StepperE_L;// Enable LOWSo, 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 ?

author: ataradov
author_rank: Super Contributor
timestamp: December 04, 2018, 04:16:59 pm
content: 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.

author: KaneTW
author_rank: Frequent Contributor
timestamp: December 04, 2018, 04:26:45 pm
content: 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.

author: igtaba
author_rank: Newbie
timestamp: December 04, 2018, 05:46:43 pm
content: 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 itSorry for my bad english, and again thanks for the answers!!

author: ataradov
author_rank: Super Contributor
timestamp: December 04, 2018, 05:52:26 pm
content: 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?

author: igtaba
author_rank: Newbie
timestamp: December 13, 2018, 02:34:07 am
content: I have always programmed in Microsoft Visual Studio, and you can use it to program a cortex device, there is a few tools to do it, but its like using Eclipse or others IDE with GNU tools

author: langwadt
author_rank: Super Contributor
timestamp: December 13, 2018, 02:50:53 am
content: Quote from: igtaba on December 04, 2018, 03:53:03 pm#defines are simple text substitution so just separate things with ; like you normally would and put { } around the whole things sosomething likeif(...) StepperE_Hdoesn't fool you

author: westfw
author_rank: Super Contributor
timestamp: December 13, 2018, 03:06:50 am
content: QuoteTraditionally, if it MUST be a macro (and I agree with everyone who says it should be function), you would do something like:Code:#define StepperInit do { \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); \} while (0)The "\" characters are line continuation, making the #define behave like a single long line, but easier to read. The "do { } while (0)" surrounding the statements causes the whole multi-statement combination to behave like a single statement at the C level, so that things don't go badly when you use:Code:if (buttonPressed()) StepperInit;Your original code uses the "comma operator", which is an uncommon bit of C that is usually avoided. I had to check to make sure that "(a,b)" is guaranteed to evaluate "a" first, and I would be nervous about "(a,b,c)" without testing!

author: langwadt
author_rank: Super Contributor
timestamp: December 13, 2018, 03:22:08 am
content: additionally I'd probably chance#define StepperA GPIO_PIN_9HAL_GPIO_WritePin(GPIOB, StepperA, GPIO_PIN_RESET);to#define StepperA GPIOB,GPIO_PIN_9HAL_GPIO_WritePin(StepperA, GPIO_PIN_RESET);to keep the pin mapping in one place