Issue with define statements

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Code:
FWU.cpp:

#include "FWU.h"
#include "BitMath.h"
FWU::FWU(MCU *x) {
	// TODO Auto-generated constructor stub
	mcu=x;

}
void FWU::run(void){
static int8_t x = 0x01;
mcu->pb.setReg(0x00);
#ifndef BIT3
#error "bit 3 not initialized"
#endif
//uint8_t y=BIT3;
mcu->pb.setDdrReg(0x00);
mcu->pb.setBit(BIT3, true);  //THIS IS WHAT FAILS
mcu->wd.enable();  //enable watchdog
	while(1){
		if (mcu->wd.mFlag){  //if an interrupt occurred...

			mcu->wd.mFlag=false;
			mcu->pb.setReg(x);
			x = (x << 1);
		}

	}
}

FWU::~FWU() {
	// TODO Auto-generated destructor stub
}

I'm a bit confused with this problem. It's the first time I'm using define statements so this might be a very easy to solve issue...

I made a BitMath.h file which defines each bit like this:

#define BIT4 0b00010000;

When I pass one of these bits into my setBit method of the port class(code below), i get errors(also below):

Port class's setBit method:

void Port::setBit(unsigned char bit, bool value){
	if ( value ){//set bit
		SET_BIT(*port,bit); 
	} else { //clear bit
		CLEAR_BIT(*port,bit);
	}
}

note: the SET_BIT and CLEAR_BIT are both simple macros:

#define SET_BIT(p,m) ((p) |= (m))
#define CLEAR_BIT(p,m) ((p) &= ~(m)) 

Anyways, these are the errors I get:

../../../workspacec/720MPC/Code/New/FWU.cpp: In member function 'void FWU::run()':
../../../workspacec/720MPC/Code/New/FWU.cpp:23: error: expected primary-expression before ')' token
../../../workspacec/720MPC/Code/New/FWU.cpp:23: error: expected `)' before ';' token
../../../workspacec/720MPC/Code/New/FWU.cpp:23: error: expected primary-expression before ')' token
../../../workspacec/720MPC/Code/New/FWU.cpp:23: error: expected `;' before ')' token

What I've figured out so far is that BIT3 is defined. the ifndef statement in the code didn't throw an error when i tested it. I also know that if I set a variable equal to BIT3 before passing it into the port's setBit method it works fine.

Why doesn't this work??:

mcu->pb.setBit(BIT3, true);
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The #define is not a C statement, it only defines preprocessor text replacement.

#define BIT3 0b00001000;

mcu->pb.setBit(BIT3, true); 

Now replace "BIT3" in the second line by "0b00001000;".

mcu->pb.setBit(0b00001000;, true);

Do you see the problem?

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i'm retarded. thanks.