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.

[PIC] How to solve this error related to usage of structure variable ?

Status
Not open for further replies.

baileychic

Advanced Member level 3
Joined
Aug 2, 2017
Messages
728
Helped
56
Reputation
112
Reaction score
57
Trophy points
28
Activity points
7,033
How to solve this error related to usage of structure variable ?

I am using PIC18F4620 and mikroC PRO PIC.

My issue is the below flag declaration is giving error. Error message is below.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
typedef struct {
    unsigned long B00:1;
    unsigned long B01:1;
    unsigned long B02:1;
    unsigned long B03:1;
    unsigned long B04:1;
    unsigned long B05:1;
    unsigned long B06:1;
    unsigned long B07:1;
 
    unsigned long B08:1;
    unsigned long B09:1;
    unsigned long B10:1;
    unsigned long B11:1;
    unsigned long B12:1;
    unsigned long B13:1;
    unsigned long B14:1;
    unsigned long B15:1;
 
    unsigned long B16:1;
    unsigned long B17:1;
    unsigned long B18:1;
    unsigned long B19:1;
    unsigned long B20:1;
    unsigned long B21:1;
    unsigned long B22:1;
    unsigned long B23:1;
 
    unsigned long B24:1;
    unsigned long B25:1;
    unsigned long B26:1;
    unsigned long B27:1;
    unsigned long B28:1;
    unsigned long B29:1;
    unsigned long B30:1;
    unsigned long B31:1;
} ULONG_BIT_FIELDS_TYPE;
 
typedef struct {
    ULONG_BIT_FIELDS_TYPE my_32_bit_flag;
    unsigned int buttons_port_value;
    unsigned int motor_control_port_value;
    unsigned int pwm_duties;
    unsigned long checksum;
} REMOTE_CONTROL_TYPE;
 
REMOTE_CONTROL_TYPE my_remote_control;
 
sbit momentary_or_latch_mode_flag at my_remote_control.my_32_bit_flag.B00;



PHP:
128 300 Syntax Error: 'b' expected,  but 'my_32_bit_flag' found TRx1.c
 

hello,


it seems compiler can't do it
it is waiting a simple global variable , not a structure
but maybe this could work ( and it compiles OK)
Code:
typedef struct {
    unsigned B0:1;
    unsigned B1:1;
    unsigned B2:1;
    unsigned B3:1;
    unsigned B4:1;
    unsigned B5:1;
    unsigned B6:1;
    unsigned B7:1;

    unsigned B8:1;
    unsigned B9:1;
    unsigned B10:1;
    unsigned B11:1;
    unsigned B12:1;
    unsigned B13:1;
    unsigned B14:1;
    unsigned B15:1;

    unsigned B16:1;
    unsigned B17:1;
    unsigned B18:1;
    unsigned B19:1;
    unsigned B20:1;
    unsigned B21:1;
    unsigned B22:1;
    unsigned B23:1;

    unsigned B24:1;
    unsigned B25:1;
    unsigned B26:1;
    unsigned B27:1;
    unsigned B28:1;
    unsigned B29:1;
    unsigned B30:1;
    unsigned B31:1;
} ULONG_BIT_FIELDS_TYPE;


typedef struct {
    ULONG_BIT_FIELDS_TYPE my_32_bit_flag;
    unsigned int buttons_port_value;
    unsigned int motor_control_port_value;
    unsigned int pwm_duties;
    unsigned long checksum;
} REMOTE_CONTROL_TYPE;

REMOTE_CONTROL_TYPE my_remote_control;

#define  momentary_or_latch_mode_flag  my_remote_control.my_32_bit_flag.B0
 
You could consider using the ## macro operator, that is suited to handle variable names during pre compilation stage.
 
Thank you paulfjujo and andre_teprom. I will try them.
 

Just a guess at the reason because I don't use that compiler: maybe 32 unsigned longs is too big to fit in a structure.
Paulfjujo's example uses unsigned ints rather than longs.

Brian.
 

Anyway, the above usage of sbit specific instruction seems somewhat different from what I have seen, like missing the ^ bit operator.
 

Manual:
sbit type

The mikroC PRO for PIC compiler has sbit data type which provides access to bitaddressable SFRs.

Thus sbit can't be used in this context. The error message 'b' expected isn't understandable though. Either it's referring to different code than the posted snippet or it's erroneous.

I don't exactly understand the purpose of sbit in this place. If it's just intended as shortcut, use a #define statement.
 

The sbit define is correct but it is giving error when used with a structure variable.

This is the code which compiles. The commented code gives error. There is no other code in the project as it is a new project which I have started. I am just declaring the required variables for the project.

PIC18F26K22 is used for the project.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
typedef struct {
    unsigned long B00:1;
    unsigned long B01:1;
    unsigned long B02:1;
    unsigned long B03:1;
    unsigned long B04:1;
    unsigned long B05:1;
    unsigned long B06:1;
    unsigned long B07:1;
 
    unsigned long B08:1;
    unsigned long B09:1;
    unsigned long B10:1;
    unsigned long B11:1;
    unsigned long B12:1;
    unsigned long B13:1;
    unsigned long B14:1;
    unsigned long B15:1;
 
    unsigned long B16:1;
    unsigned long B17:1;
    unsigned long B18:1;
    unsigned long B19:1;
    unsigned long B20:1;
    unsigned long B21:1;
    unsigned long B22:1;
    unsigned long B23:1;
 
    unsigned long B24:1;
    unsigned long B25:1;
    unsigned long B26:1;
    unsigned long B27:1;
    unsigned long B28:1;
    unsigned long B29:1;
    unsigned long B30:1;
    unsigned long B31:1;
} ULONG_BIT_FIELDS_TYPE;
 
typedef struct {
    ULONG_BIT_FIELDS_TYPE my_32_bit_flag;
    unsigned int buttons_port_value;
    unsigned int motor_control_port_value;
    unsigned int pwm_duties;
    unsigned long checksum;
} REMOTE_CONTROL_TYPE;
 
REMOTE_CONTROL_TYPE my_remote_control;
 
unsigned char my_uchar_flags = 0;
 
sbit mode_flag at my_uchar_flags.B0;
//sbit momentary_or_latching_mode_flag at my_remove_control.my_32_bit_flag.B00;
 
void main() {
 
    OSCCON = 0x57;
    OSCTUNE = 0x00;
 
    CM1CON0 = 0x00;
    CM2CON0 = 0x00;
 
    SLRCON = 0x00;
 
    ANSELA = 0x00;
    ANSELB = 0x00;
    ANSELC = 0x00;
 
    TRISA = 0x00;
    TRISB = 0x00;
    TRISC = 0x00;
 
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
 
    LATA = 0x00;
    LATB = 0x00;
    LATC = 0x00;
 
    Delay_ms(100);
 
    while(1) {
 
           my_remote_control.my_32_bit_flag.B00 = ~my_remote_control.my_32_bit_flag.B00;
    }
}

 

Attachments

  • Using Structure Variable For A Flag.rar
    12.8 KB · Views: 128

i think you have wrongly written my_remove_control instead of my_remote_control.
 

I'm confused (no comment please) by what you are trying to do here. I can understand the concept of 32 BITS in what seems to be a remote control bit sequence but if that's it's purpose, why not declare them as bits instead of unsigned longs where each entry in the struct is using 32 bits. It would seems more sensible to declare an array of unsigned longs, one entry per remote command then serially read the bits within it.

I'm working on a 'remote' remote control at the moment, its to operate a security recorder which is located some distance away and it doesn't have a network connection. I'm making a duplicate of the real remote control but with a network feed to operate the controls. The distance from controlling system to recorder will be about 600Km.

Brian.
 

Paulfjujo's example uses unsigned ints rather than longs.
Brian.

No, it used only one bit instead of a long 32 bit per field bit
i check it with the debugger
you can see that my_32_bit_flag occupiert 4 bytes adresse 0x00D6 ..0x00DA

Decompose_32bits.jpg
 

you can see that my_32_bit_flag occupiert 4 bytes adresse 0x00D6 ..0x00DA
Of course it does, that's how C bitfields are supposed to work.

The sbit define is correct but it is giving error when used with a structure variable.
Yes, expectable according to manual. Why didn't you remove the illegal sbit declaration?

Which other problems do you experience?
 

Yes, expectable according to manual. Why didn't you remove the illegal sbit declaration?

Which other problems do you experience?

I need the sbit declaration which uses a structure variable for the flag or can I use union or union with structure ?

I need a sbit flag variable whose base is my_remote_control.

Code:
my_remote_control

in the sbit declaration had a typ and it was types as
Code:
my_remove_control

I fixed it but that doesn't solve my problem.

- - - Updated - - -

Anyway, the above usage of sbit specific instruction seems somewhat different from what I have seen, like missing the ^ bit operator.

The ^ operator is used with sbit in Keil. In mikroC PRO PIC it is different.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top