Jump to content
Sign in to follow this  
Reynard

Case And Shift Operator

Recommended Posts

The code below gives errors:

 

CaseTest.c(13): error: missing right brace

CaseTest.c(10): error: missing semicolon

CaseTest.c(10): error: failure

 

It appears that the case statement constant does not like having the left shift operator in it.

(It may not like the right shift operator either but have not tried it.)

 

Replacing the I2C_STATE_1 definition with (0x09) works OK.

 

#include <system.h>

#define	SSPSTAT_MASK	(1 << D_A | 1 << S | 1 << R_W | 1 << BF)
#define	I2C_STATE_1		(1 << S | 1 << BF)				// I2C write, last byte an address.

void main(void) {

}

void I2C_SerialInterrupt(void) {
pir1.SSPIF = false;							// clear interrupt flag.
switch (sspstat & SSPSTAT_MASK) {
case (I2C_STATE_1):
	break;
} // end switch.
}

void interrupt(void) {
if (pir1.SSPIF) {							// I2C interrupt ?
	I2C_SerialInterrupt();					// yes, process I2C interrupt.
}
}

 

Using BoostC 6.81, PIC16F873A, WinXP Pro SP2.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

It looks like a bug when bit-wise operators are used in a case expression.

/*
  File: bug21.C
  Target: PIC16F876A
  OS: WinXP, SP2
  SourceBoostIDE: 6.81
  Compiler: BoostC 6.81
  Reproducible: always
  Expected behavior: compile without error

  Description: 
  case statment reports a compile error when bit-wise
  operator is used in expression.

  "case (I2C_STATE_1):" reports an error
  "case ((1 << S | 1 << BF)):" reports an error
  "case (0x08 | 0x01):" reports an error

  changing #define for I2C_STATE_1 to an enum is a work around

*/

#include <system.h>

#define	SSPSTAT_MASK	(1 << D_A | 1 << S | 1 << R_W | 1 << BF)
//#define	I2C_STATE_1		(1 << S | 1 << BF)
enum { I2C_STATE_1 = (1 << S | 1 << BF) };


void main(void) {

}

void I2C_SerialInterrupt(void) {
pir1.SSPIF = false;				// clear interrupt flag.
switch (sspstat & SSPSTAT_MASK) {
//	case ((1 << S | 1 << BF)):
//	case (0x08 | 0x01):
  case (I2C_STATE_1):
	break;
} // end switch.
}

void interrupt(void) {
if (pir1.SSPIF) {				  // I2C interrupt ?
	I2C_SerialInterrupt();		 // yes, process I2C interrupt.
}
}

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...