variable lenght of array not accept by keil uvision4

Status
Not open for further replies.

jitendrabaraiya

Junior Member level 2
Joined
Sep 24, 2014
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
282
Hi Everyone ......

in the keil ..i did not write variable/volatile array size. plz give me a suggetion...how we declair variable array size in my code plz..for exp,......did not write char a[var]....and value of var is not constant its variable..
 

Re: 80c51 microcontroller questiond

Not clear what you want to achieve. Your memory allocation for arrays or strings will be static, according the maximum size used during run time.

In any case you are restricted to legal C syntax, additionally need to consider the limited ROM and RAM resources of a small 8051 microcontroller.

If you have difficulties to implement legal C constructs with Keil compiler, please give an example.
 

variable lenght of array not accept by keil uvision4


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
typedef struct
{
int len;
char api_identifier;
long DH;
long DL;
int source_addr_16bit;
char options;
char frame_id;
char AT_com[2];
char status;
int data_len=10;        // stores the length of the data
char data[20];  // also stores the "value" for at command response
char checksum;
 
} RxPacket;
 
.
.
.
.
.
.
char buff[ RxPacket.data_len];

consider the above code written in c for ARM 7 in keil uvision4
I'd taken structure of few data, when i'm trying to access data_len by it's pointer through array then IDE shows error.....

main.c(402): error: #60: this operator is not allowed in an integral constant expression char buff[RxPacket.data_len];

any idea, how do i solve this error?:bang:
 
Last edited by a moderator:

I realize that you are talking about ARM7 rather than 8051. General C rules are still valid, however.

As already mentioned, variables must be declared with constant size. You are free to use only part of it.

To create memory objects with variable size, you can use dynamic memory allocation. But it's usuage is rarely advantageous in embedded programming of small µCs.
 

In Keil there are one macros that returns array size - sizeof(array)
But it can't be used inside the procedure if it receives pointer as the parameter, sure. I shouldn't explain why, yes?
 

guys before that, i have compiled this code on atmel studio 6 for avr AT mega 32 (code is zigbee library in api mode) and it's not shows any error and my hardware is also happy with it (as i'm getting result as per expectation) but what about keil IDE??????

why i'm getting this error?
any idea??? i'll really appreciate the solution!
 

The code snippet you have shown so far doesn't look like legal C code. But it misses many important details and there may be still misunderstandings.

You should show at least a complete code unit where the problem occurs.
 

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include<lpc214x.h>      //Includes LPC2148 register definitions
#include<stdio.h>
#include<stddef.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
//************************************************************************************************************* 
 
typedef struct
                            {
                                        int len;
                                        char api_identifier;
                                        long DH;
                                        long DL;
                                        int source_addr_16bit;
                                        char options;
                                        char frame_id;
                                        char AT_com[2];
                                        char status;
                                      char data_len;    // stores the length of the data
                                        char data[20];  // also stores the "value" for at command response  
                                        char checksum;
                                
                            } RxPacket;
 
typedef struct 
                                {
                                        long sl;
                                        long sh;
                                    
                                }addr64;
                                
                                
//*************************************************************************************************************                             
                                
#define Fosc            12000000                    
#define Fcclk           (Fosc * 5)                  
#define Fcco            (Fcclk * 4)                 
#define Fpclk           (Fcclk / 4) * 1             
 
#define  UART_BPS   9600     //Set Baud Rate here
 
#define TX_BUFFER_IS_FULL() ((U0LSR & (1 << U0THR)) == 0)
#define RX_BUFFER_IS_FULL() ((U0LSR & (1 << U0RBR)) == 0)
#define XBEE_UDR U0RBR                          
 
 
#define UART0_CH_NUM   6
#define VIC_UART0_CH   (0x01 << UART0_CH_NUM)
#define VIC_INT_CH_EN   (0x01 << 5)     
#define TOGGLE_LED() IO0SET = (1 << 2)
#define TOGGLE_LED0(); IO0CLR=(1<<2)
#define hex rx_data.data_len
 
//******************************************************************************************************************
void  Delay_Ticks(unsigned int Delay);
void  Init_UART0(void);
void  UART0_SendByte(unsigned char data);
void  UART0_SendStr(unsigned char msg[]);
 
int XbeeInit();
void ZigBee_TX_Request(char Frame_ID, long DH, long DL, int _16bitAddr, char Hops, char Options, char *RF_Data, int len );
void send_Msg(char *data, int len);
void setNewPacketCB(void (*funptr)());
void gas();
void receive_Msg(RxPacket *rx_data);
RxPacket getPacket();
 
//************************************************************************************************************
 RxPacket rx_pkt;
// Global Variables
char RSSI;
int MY;
int cb; // callback flag -- set if a callback function for new packets has been specified
void (*cbFunPtr)(void); // callback function pointer
 
char test[] = {'T', 'E', 'S', 'T'};
//*************************************************************************************************************************
 
void UART0_ISR(void) __irq
{
if(XBEE_UDR == 0x7E)
    {
        RxPacket pkt;
        //TOGGLE_LED();
        receive_Msg(&pkt);
        rx_pkt = pkt;
        // Call function for responding to new packet
        if(cb)
        (*cbFunPtr)();
        //TOGGLE_LED0();
      gas();
    }
    
    VICVectAddr = 0x00;        /* ACK the VIC */
        
 }
int  main(void)
{  
   PINSEL0 = 0x00000005;            // Enable UART0 Rx and Tx pins
   PINSEL1 = 0x00000000;
   PINSEL2 = 0x00000000;
 
     
    
   XbeeInit();
     ZigBee_TX_Request(0x10, 0x13A200, 0x40B0E9FE, 0xFFFE, 0x01, 0x01, test, 4);
    
     //flexsize(10);
    
   VICVectAddr8 = (unsigned int) UART0_ISR;
    // setNewPacketCB(gas);
   while(1) 
   { 
         
   }
   return(0);
}
 
//****************************************************************************************************************
 
void  Delay_Ticks(unsigned int Delay)  //Function to generate finite delay
{  
   unsigned int i;
   for(; Delay>0; Delay--) 
   for(i=0; i<50000; i++);
}
 
 
void  Init_UART0(void)                  //This function setups UART0
{  
   unsigned int Baud16;
   U0LCR = 0x83;                    // DLAB = 1
   Baud16 = (Fpclk / 16) / UART_BPS;  
   U0DLM = Baud16 / 256;                            
   U0DLL = Baud16 % 256;                        
   U0LCR = 0x03;
    
     U0FCR |= 0x01;
   /* Configure the VIC for Interrupt Handling */
   VICVectCntl8 |= (VIC_INT_CH_EN | UART0_CH_NUM);
   VICIntSelect &= (~VIC_UART0_CH);   /* This is optional to ensure the IRQ Mode */
   VICIntEnable |= VIC_UART0_CH;         /* Enabel the Interrupt for listening request */
   /* Enable UART Interrupt */
  U0IER   |= 0x01;
}
                
 
void  UART0_SendByte(unsigned char data)       //A function to send a byte on UART0
{  
   U0THR = data;                    
   while( (U0LSR&0x40)==0 );        
}
 
 
void  UART0_SendStr(unsigned char msg[])     //A function to send a string on UART0
{  
   while(*msg!='\0')
   { 
      UART0_SendByte(*msg);
      msg++;         
   }
}
 
 
 
int XbeeInit()
{
        Init_UART0();
        return 1;
}
 
 
//******************************* tx frame **************************************************************
 
void ZigBee_TX_Request(char Frame_ID, long DH, long DL, int _16bitAddr, char Hops, char Options, char *RF_Data, int len )
{
    int i; // counting variable
    //char *buff = (char*) malloc(sizeof(char*)*(20));  //temporary buffer for transmitting
    char buff[20]; 
    buff[0] = 0x10;
    // Identifies the UART data frame for the host to correlate with a 
    // subsequent ACK (acknowledgment). Setting Frame ID to ‘0' will disable response frame.
    buff[1] = Frame_ID; 
    // MSB first, LSB last. Broadcast = 0x000000000000FFFF
    buff[2] = (DH >> 24);
    buff[3] = (DH >> 16);
    buff[4] = (DH >> 8);
    buff[5] = DH;
    buff[6] = (DL >> 24);
    buff[7] = (DL >> 16);
    buff[8] = (DL >> 8);
    buff[9] = DL;
    // 16 bit address
    buff[10] = (_16bitAddr >> 8);
    buff[11] = _16bitAddr;
    // Number of hops for message to take
    buff[12] = Hops;
    // Options
    buff[13] = Options;
    for(i = 0; i < len; i++)
        buff[14+i] = RF_Data[i];
    send_Msg(buff, 14+len); 
}
 
 
void send_Msg(char *data, int len)
{
    //Generate checksum
    char checksum;
    int counter = 0, sum = 0;
    for(counter = 0; counter <= len - 1; counter++)
        sum += data[counter];
    //Checksum is calculated by adding the data values together, and subtracting the 
    //last 8 bits from 0xFF.
    checksum = 0xFF - (sum & 0x00FF); 
    //Transmit data
    UART0_SendByte(0x7E);  //Start delimiter
    UART0_SendByte(8 >> len);  //Length MSB
    UART0_SendByte(len);    //Length LSB
    for(counter = 0; counter <= len - 1; counter++)  //Transmit data
    UART0_SendByte(data[counter]);
    UART0_SendByte(checksum);  //Transmit checksum
}
 
 
//******************************************** rx packet *************************************************
 
 
void setNewPacketCB(void (*funptr)())
{
    cb = 1;
    cbFunPtr = funptr;
}
 
 
void receive_Msg(RxPacket *rx_data)
{
    int count;
    char temp;
 
    rx_data->data_len = 0;
    
    while (RX_BUFFER_IS_FULL()) {}; // Do nothing until data have been received and is ready to be read from UDR
    temp = XBEE_UDR;    //next incoming byte is the MSB of the data size
    while (RX_BUFFER_IS_FULL()) {}; // Do nothing until data have been received and is ready to be read from UDR
 
    rx_data->len = (temp << 8) | XBEE_UDR;  //merge LSB and MSB to obtain data length
    
    while (RX_BUFFER_IS_FULL()) {}; // Do nothing until data have been received and is ready to be read from UDR
    rx_data->api_identifier = XBEE_UDR;
    switch(rx_data->api_identifier) // Select proper sequence for receiving various packet types(getting api identifier)
    {
        case 0x90:  // Zigbee Receive Packet (Series 2 only)    0x90 --> receive request
        for(count = 1; count < rx_data->len; count++)
        {
            while (RX_BUFFER_IS_FULL()) {}; // Do nothing until data have been received and is ready to be read from UDR
            if(count == 1)
            rx_data->DH = ((long)XBEE_UDR << 24);
            else if(count == 2)
            rx_data->DH |= ((long)XBEE_UDR << 16);
            else if(count == 3)
            rx_data->DH |= ((long)XBEE_UDR << 8);
            else if(count == 4)
            rx_data->DH |= (long)XBEE_UDR;
            else if(count == 5)
            rx_data->DL = ((long)XBEE_UDR << 24);
            else if(count == 6)
            rx_data->DL |= ((long)XBEE_UDR << 16);
            else if(count == 7)
            rx_data->DL |= ((long)XBEE_UDR << 8);
            else if(count == 8)
            rx_data->DL |= (long)XBEE_UDR;
            else if(count == 9)
            rx_data->source_addr_16bit = (int)(XBEE_UDR << 8);
            else if(count == 10)
            rx_data->source_addr_16bit = (int)XBEE_UDR;
            else if(count == 11)
            rx_data->options = XBEE_UDR;
            else
            {
                rx_data->data[count - 12] = XBEE_UDR;
                
                rx_data->data_len++;
            }
            
            
        }
        UART0_SendByte(rx_data->data_len);
        while (RX_BUFFER_IS_FULL()) {}; // Do nothing until data have been received and is ready to be read from UDR
        rx_data->checksum = XBEE_UDR;   //store checksum
        break;
        default:
        break;
    }
}
void gas()
{
    int i=0;
    char temp;
    
    RxPacket rx_data = getPacket();
    //UART0_SendByte(rx_data.data_len);
 // ZigBee_TX_Request(0x00, 0, 0x00FFFF, 0, 0x01, 0x01, test, 4);
    [B]unsigned char buff1[rx_data.data_len];[/B]              //----------------->(getting error at this point)
    for(i=0;i<rx_data.data_len;i++)
    {
        temp = (rx_data.data[i]);
        buff1[i]=temp;
    }
  UART0_SendStr(buff1);
    VICVectAddr8 = (unsigned int) UART0_ISR;
}
 
RxPacket getPacket()
{
    return rx_pkt;
}
 
 
 
 
//####################################################################


above code is for zigbee receive and transmit in API code, transmitter section is working perfect and i'm able to transmit string, but the problem is when i came to receiving section to receive string then i need to calculate received data length(after filtering start delimiter,MAC ID ...etc ) to manipulate array size(i need dynamic array size instead of static) where compiler shows error which I've described earlier

guys before that, I've implemented same application on AVR mega 32 using Atmel studio 6.2 where my compiler is happy with my dynamic size of array and I'm able to send and receive the string in api mode using zigbee.

but at ARM 7 using keil compiler shows error....
" error: #60: this operator is not allowed in an integral constant expression"

I've find some remedy, as if i use fix size of array then I'm getting garbage value on terminal tool, I've also used Dynamic memory allocation, try with pointer etc, but they all are worth less

can any one help me ????:|
I'll really appreciate the guidance!:-|
 
Last edited by a moderator:

There's no doubt about illegal C syntax used in your code. When I understand right, the upper bound of datalen is 20, so char buff[20] as used in ZigBee_TX_Request() should perfectly work.

If the code doesn't work as expected, you are going to debug it.
 

i debug my code it tx is properly worked but when i send any packet it received properly by xbee but data is not display on my serial terminal/ its saw garbage value because of length of buffer .. in my code data length is variable so i dint fix buffer size .....and data received in void gas() function...so what shall i do for it...

- - - updated - - -

and also i shall give buff size one byte less or extra its give me a garbage value...........
 
Last edited by a moderator:

Code:
void gas()
{
    int i=0;
    char temp;
    
    RxPacket rx_data = getPacket();
    //UART0_SendByte(rx_data.data_len);
 // ZigBee_TX_Request(0x00, 0, 0x00FFFF, 0, 0x01, 0x01, test, 4);
    [B]unsigned char buff1[rx_data.data_len];[/B]   // -->(getting error at this point)
    for(i=0;i<rx_data.data_len;i++)
    {
        temp = (rx_data.data[i]);
        buff1[i]=temp;
    }
  UART0_SendStr(buff1);
    VICVectAddr8 = (unsigned int) UART0_ISR;
}


hello everyone .....


In above code i got receive data one less starting byte..... if i take char buff[rx_data.data_len-1] than it gives correct data but in keil i dint take dynamic array size so ..... i dint get actual data from receiver side anybody help me .....

- - - updated - - -

i send jitenda then its received itendra' like this ....
 
Last edited by a moderator:

Your code misses to write a terminating 0 into buff, it's just bad programming.
 

Variable length arrays are a C99 feature, are you running the compiler in C99 mode?

It is possible that one of your platforms supported variable length arrays as an extension even when working to an earlier standard (GCC did for example).

Regards, Dan.
 
Reactions: ud23

    ud23

    Points: 2
    Helpful Answer Positive Rating
Variable length arrays are a C99 feature
Yes, thanks for remembering.

But in the present code, there's no actual need to use variable length arrays. The problem is apparently that using variable length arrays involves side effects that are compensating coding errors. It's not understandable why this pretty basic code shouldn't work without dynamic arrays.
 

in c99 compiler its for 8051 and now i am working on the lpc2148. and i dont know this pretty basic code why dint work without dynamic array size ...and without dynamic size i dint get first byte of data.... so any other solution for it ...plz....
 

hello mills ......


Shall you send me the link of c99 compiler where i can download . ?
 

and i dont know on which compiler i am working ...
 

hello everyone .....

my above problem is solved ... and now i created point to point network using xbee and its totaly bidirectional . now i want to create a mesh network shall anyone help me that how and create routing algorithm for it using xbee series 2 ..shall any one tell me the algorithm for that how can i data transfer from one router to another router ...

thanks in advance..
 

Hello everyone .............

My problem is that I want to create mesh network using xbee and pass data one xbee to others xbees and also I created point to point network but now i dont know how to data pass one xbee to others like ..I have one coordinator and 7 route so i want to data pass coordinator to 7 router like

1) coordinator -> router-1 -> router-2 ->router-3 ->router4 ->router5 ->router6->route7

like this type and alternative shall me me to how can i create fram to data pass like this...

thank in advance...
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…