Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Want to define a single macro.Is it possible?

Status
Not open for further replies.

love_electronic

Member level 5
Joined
Nov 16, 2010
Messages
93
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,964
Hi guys
I am using ARM controller and want to change the state of pin ( 0 or 1) which i assigned as OUTPUT.
The port output register in arm is GPIOx_PDOR which is 32bit.
where x is port A,B,C..etc

i have made a macro like
Code:
#define DATA(x)	  GPIOB_PDOR |= (x<<2) & 0xFFFFFFFF

initially i high some pins by loading this value
Code:
GPIOx_PDOR = 0x06000080;
Suppose i want to high pin 3 i set
Code:
DATA(1)
then it will set PIN 3 HIGH with previously pins remain same, and it works fine. GPIOx_PDIR now has a value 0x06000084

when i set pin 3 to low
Code:
DATA(0)
the code have no effect. GPIOx_PDIR has the same value 0x06000084; But what i required is 0x06000080

After analyzing the macro i conclude that OR operation is not changing the state of the pin

so i write 2 seperate macro's like that
Code:
#define DATA_1(x)	GPIOB_PDOR |= (x<<2) & 0xFFFFFFFF
#define DATA_0(x)	GPIOB_PDOR &= (x<<2) | 0xFFFFFFF0

when i load DATA_1(1) it works fine and when i load DATA_0(0) it also works fine

My Question is that can i write a single macro which perform both functions? like if receive 1 OUTPUT becomes HIGH and if receive 0 OUTPUT becomes LOW?
Also by using the 2nd macro i am restricted to change the state of only 3rd pin, if i want any other pin to go low i can't.
 

Re: want to define a single macro.Is it possible?

void PIN_SW (uint32_t pin, bool state){
(state) ? DATA_1(pin) : DATA_0(pin);}
But I'm not sure, that GPIOB_PDOR have read access.
 

Re: want to define a single macro.Is it possible?

void PIN_SW (uint32_t pin, bool state){
(state) ? DATA_1(pin) : DATA_0(pin);}
why are state and pin for?
they should be same.it represent logic level HIGH or LOW

your function also uses the 2 macros.
 

Re: want to define a single macro.Is it possible?

void PIN_SW (uint32_t pin, bool state){
(state) ? DATA_1(pin) : DATA_0(pin);}
This is not a macro...

I had to think a while about it but I can propose something like that (tested only on MSVC compiler and works for me)
Code:
struct enumeration {
	union 
	{
		unsigned char r;
		struct {
			int a : 1;
			int b : 1;
			int c : 1;
			int d : 1;
			int e : 1;
			int f : 1;
			int g : 1;
			int h : 1;
		};
	};
};

#define SET_D(bit) ((enumeration*)(&GPIOB_PDOR))->d = bit;
 

You could consider something like that (not tested):

Code C - [expand]
1
2
3
4
5
6
7
8
#define DATA(x,y) \
#if (y==1) \
GPIOB_PDOR |= (x<<2) & 0xFFFFFFFF \
#elif (y==0) \
GPIOB_PDOR &= (x<<2) | 0xFFFFFFF0 \
#else \
#pragma message "invalid argument" \
#endif

 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top