| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| Mrinmoy Dey 
 
 
 Joined: 23 Jan 2018
 Posts: 44
 
 
 
			    
 
 | 
			
				| Bit Field Error in Data Structure |  
				|  Posted: Thu Mar 30, 2023 5:30 am |   |  
				| 
 |  
				| Hi, I am using PIC18F66K40 in one of my project to communicate with a peripheral
 device and for register mapping of the peripheral I am using data structure with bit field.
 
  	  | Code: |  	  | typedef struct _ST_REG_0x01
 {
 unsigned int8 ADC_MODE : 2;
 unsigned int8 CS_SEL   : 2;
 unsigned int8 CLK_SEL  : 2;
 unsigned int8 CONFIG0  : 2;
 }ST_REG_0x01;
 typedef union _UN_CONFIG0_REG_01h
 {
 ST_REG_0x01     m_ST_REG_0x01_bits;
 unsigned int8   m_ui8Reg_0x1;
 
 }UN_CONFIG0_REG_01h_t;
 
 
 
 typedef struct _ST_REG_0x02
 {
 unsigned int8 Reserved  : 2;
 unsigned int8 OSR       : 4;
 unsigned int8 PRESCALER : 2;
 }ST_REG_0x02;
 typedef union _UN_CONFIG1_REG_02h
 {
 ST_REG_0x02     m_ST_REG_0x02_bits;
 unsigned int8   m_ui8Reg_0x2;
 
 }UN_CONFIG1_REG_02h_t;
 
 | 
 The first register data structure does not generate any error whereas the second one generates errors like :
 Error#28  Expecting an identifier
 Error#36  Expecting a ; or ,
 Error#34  Unknown type
 
 My compiler version is 5.101
 I can't find out any possible wayout from the errors.
 Anyone with any idea about that will be helpful.
 |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Mar 30, 2023 6:29 am |   |  
				| 
 |  
				| As posted, both compile without errors for me, on your compiler version. I'd suggest that possibly you have something else possibly defined, that
 is causing the issue. So perhaps 'Reserved' is #defined somewhere, or
 'PRESCALER'. The latter in particular might well be #defined in one of
 the standard libraries.
 
 If you are in the standard IDE, and this is happening, the item that is
 causing this should be underlined in red when the compile fails.
 |  | 
	
		|  | 
	
		| Mrinmoy Dey 
 
 
 Joined: 23 Jan 2018
 Posts: 44
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Mar 30, 2023 11:10 am |   |  
				| 
 |  
				| @Ttelmah You are absolutely right; the Reserved member variable in the data structure was the problem and that may be #defined somewhere in the standard library with the exactly same spelling. Once I make it in caps the code is successfully compiled.
 
 Thank you so much.
  |  | 
	
		|  | 
	
		| Ttelmah 
 
 
 Joined: 11 Mar 2010
 Posts: 19966
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Mar 31, 2023 1:54 am |   |  
				| 
 |  
				| Glad I got it. 
 Also shows though how to debug such a problem. I made a bare program
 and tried your structures, which then worked. Gave an instant hint to what
 was happening, so I then added a #define for 'Reserved', and immediately
 the problem appeared.
  |  | 
	
		|  | 
	
		|  |