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 declare Global Variables in MIKROC for PIC

Status
Not open for further replies.

Vaisakhan Ku

Junior Member level 1
Junior Member level 1
Joined
Jan 19, 2014
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
259
Hi all,
Can anyone tell me the right method to declare global variables in MikroC for pic. I have declared variables at the beginning of the program.All variables are "int" are called multiple times from main function and updates their values within the function call. However, I cant get the updated values of these variables back to the main function. I checked if the values are getting updated, to my surprise they are getting updated inside the parenthesis of conditional statements where they are actually changing the values. It seems that these variables changes are not visible outside these functions.

can anyone tell me what all are the procedure to follow while declaring a global variable in MikroC :!!!! :-(
 

All the variables declared before

Code C - [expand]
1
void main() { ... }

are global variables. Post your code.
 

Check if the variable was declared just once at whole program, or whether duplicate the same either as global and local.
 

Hi all,
Please find my code.


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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
int switch_val=0,menu_val=0,enter_noft=0,menu_noft=0,comand=0,comand_noft=0;
   //int right_noft=0,left_noft=0;
   int temp,i;
   int to_main_menu=0,enable_entry=0,disp_refresh=0;
   
   int all_menu_val[8]={100,110,111,120,121,200,210,220};  //All possible menu values
   int date_time[7]={00,00,00,00,00,00,00};
   
   char all_menu_char[6][10]={"Date/Time","Time     ","Shedule  ","Shedule 1","Shedule 2","N / A    "};//,"Date","Time","Shedule","Schedule 1","Schedule 2","N / A"};
   //char menu_time[5][10]={"12 Hrs","24 Hrs", "Time:"};
   
   char name;
   
   //char menu_root[5][10]={"","Time","Date","Schedules"};
 
   //char menu_date[15]={"Today's Date"};
   //char menu_sche[2][15]={"Schedule : 1","Schedule : 2 "};
 
   //......Functions......////  
   void display(int,int, int );
   void display_menu(int);
   void switch_val_processing(int);
   void read_ds1307(void);
   void rd_date_time(void);   //for reading the date from the RTC
   void wr_date_time(void);   //for writing the date from the RTC
   int check_menu_val(int);
   //void process_keypress(void);
   //void menu_sel(void);
   
   
   // Lcd pinout settings
   sbit LCD_D4 at RC0_bit;
   sbit LCD_D5 at RC5_bit;
   sbit LCD_D6 at RC4_bit;
   sbit LCD_D7 at RC3_bit;
   sbit LCD_RS at RC2_bit;
   sbit LCD_EN at RC1_bit;
   
   // Pin direction
   sbit LCD_D4_Direction at TRISC0_bit;
   sbit LCD_D5_Direction at TRISC5_bit;
   sbit LCD_D6_Direction at TRISC4_bit;
   sbit LCD_D7_Direction at TRISC3_bit;
   sbit LCD_RS_Direction at TRISC2_bit;
   sbit LCD_EN_Direction at TRISC1_bit;
   
   //----Buttons configurations----//
   sbit but_menu at RA3_bit;
   sbit but_left at RA2_bit;
   sbit but_right at RA1_bit;
   sbit but_enter at RA0_bit;
   //sbit but_intr_flag at intcon.----_bit;
   //sbit but_intr_clear at ----_bit;
   //sbit enable at comand.b2;
   
 
   void initialize()
     {
       option_reg=0b01001111;
       intcon=0b11001000;
 
       //----disable non used interrupts----//
       pie1=0x0;
       pie2=0x0;
       pie3=0x0;
       pie4=0x0;
 
       //----Enabling negative edge sensing on  A0->A3----//
       IOCAP=0x0;
       IOCAN=0b00001111; //Positive to negative egde detection on A0 --> A3
       IOCAF=0x0;
       IOCBP=0X0;
       IOCBN=0X0;
       IOCBF=0X0;
       
       
 
       //read date and time to the date_time Variable --------------------
       lcd_init();
       
 
     }
     
   void interrupt(void)
     {
       delay_ms(10);
       if(intcon.IOCIF==1)
         {
           disp_refresh=1;
           if(but_menu==1 &&iocan==0b00001000)
             {
               switch_val=1;
               //return(1);
             }
           if(menu_val!=0)
             {
               if(but_left==1 && iocan==0b00000100)
                 {
                   switch_val=2;
                   //return(2);
                 }
               if(but_right==1 && iocan==0b00000010)
                 {
                   switch_val=3;
                   //return(3)
                 }
               if(but_enter==1 && iocan==0b00000001)
                 {
                   switch_val=4;
                   //return(4);
                 }
               if(but_menu==0 && but_left==0)
                 {
                   switch_val=5;
                   //return(5);
                 }
             }
           intcon.iocif=0;
         }
 
     }
     
   void main()
     {
       initialize();
       lcd_out(1,1,"Starting.....");
       delay_ms(500);
       lcd_cmd(_lcd_clear);
       Lcd_cmd(_lcd_cursor_off);
 
       while(1)
         {
           if(disp_refresh==1)
             {
               switch_val_processing(switch_val);
               display_menu(menu_val);
               /*lcd_out(2,1,"S_V");*/display(0,0,switch_val);
               /*lcd_out(2,7,"M_V");*/display(0,0,menu_val);
               delay_ms(1000);
             
             }
 
         }
     }
     
   
   int read_year()
     {
       //start_i2c();
       //wr_i2c(209);
       //wr_i2c(0x00);
       //date=
     }
   
   void read_ds1307(void)
     {
       //wr_single_i2c(int 208,int 7,int 16);
 
     }
   
 
   void switch_val_processing(int switch_number)
    {
      int x,present=0;
      //----Menu Button Pressed----
      if(switch_number==1)
        {
          int temp_100=menu_val%100,temp_10=menu_val%10;
          comand_noft=0; //this will exit the screen even if enter is pressed
          
          if(enable_entry==1)
            {
              menu_val=100;
              menu_noft=1;
            }
          
          if(to_main_menu==1)
            {
              to_main_menu=0;
            }
          
          if(Enter_noft==1)
            {
              menu_val=-temp_100;
              menu_noft++;
              enter_noft--;
              to_main_menu=1;
            }
          
          if(Enter_noft==2)
            {
              menu_val=-temp_10;
              menu_noft++;
              enter_noft--;
            }
 
          if(to_main_menu==0)
            {
              menu_val=0;
              menu_noft=1;
              enable_entry=1;
            }
 
          switch_val=0;
        }
      
      //----Left Button pressed----
      if(switch_number==2)
        {
          int x;
          if(enter_noft==2)
            {
              x=menu_val-1;
            }
          if(enter_noft==1)
            {
              x=menu_val-10;
            }
          for(i=0;i<(sizeof(all_menu_val)/2);i++)
            {
              if(x==all_menu_val[i])
                {
                  present=1;
                }
            }
          (present==1)? (menu_val=x):(comand=2);
          switch_val=0;
        }
 
      if(switch_number==3)
        {
          int x;
          if(enter_noft==2)
            {
              x=menu_val-1;
            }
          if(enter_noft==1)
            {
              x=menu_val-10;
            }
          for(i=0;i<(sizeof(all_menu_val)/2);i++)
            {
              if(x==all_menu_val[i])
                {
                  present=1;
                }
            }
          (present==1)? (menu_val=x):(comand=3);
          switch_val=0;
        }
 
      if(switch_number==4)
        {
          int x;
          enter_noft++;
          if(enter_noft==2)
            {
              x=menu_val-1;
            }
          if(enter_noft==1)
            {
              x=menu_val-10;
            }
          for(i=0;i<(sizeof(all_menu_val)/2);i++)
            {
              if(x==all_menu_val[i])
                {
                  present=1;
                }
            }
          (present==1)? (menu_val=x):(comand=4);
          switch_val=0;
        }
    }
 
  int check_menu_val(int check_num)
    {
      int i;
      for(i=0;i<(sizeof(all_menu_val)/2);i++)
        {
          if(check_num==all_menu_val[i])
            {
               return(1);break;
            }
 
        }
    
    }
  
  
  //char all_menu_char[6][10]={"Date/Time","Time     ","Shedules ","Shedule 1","Shedule 2","N / A    "};
  void display_menu(int value)
    {
      int i;
      lcd_cmd(_lcd_cursor_off);
      switch(value)
        {
          case(100):  {
                        lcd_cmd(_lcd_clear);
                        Lcd_out(1,1,all_menu_char[0]);
                        break; 
                      }
          
          case(110):  {
                        lcd_cmd(_lcd_clear);
                        Lcd_out(1,1,all_menu_char[0]);
                        lcd_out(1,5,"     ");
                        break;
                      }
          
          case(111):  { 
                        lcd_cmd(_lcd_clear);
                        Lcd_out(1,1,all_menu_char[0]);
                        lcd_out(1,5," ");
                        lcd_out_cp("(Press Enter / Change)");
                        delay_ms(200);
                        for(i=0;i<11;i++)
                          {
                            lcd_cmd(_lcd_shift_left);
                            delay_ms(200);
                          }
 
                        lcd_cmd(_lcd_return_home);
                        delay_ms(1000);
 
                        break;
                      }
          
          case(120):  {
                        lcd_cmd(_lcd_clear);
                        Lcd_out(1,1,all_menu_char[1]);
                        break;
                      }
          
          case(121):  {
                        lcd_cmd(_lcd_clear);
                        Lcd_out(1,1,all_menu_char[1]);
                        lcd_out(1,5," ");
                        lcd_out_cp("(Press Enter / Change)");
                        for(i=0;i<11;i++)
                          {
                            lcd_cmd(_lcd_shift_left);
                            delay_ms(200);
                          }
                        lcd_cmd(_lcd_return_home);
                        delay_ms(1000);
                        break;
                      }
                      
          case(200): { 
                       lcd_cmd(_lcd_clear);
                       Lcd_out(1,1,all_menu_char[2]);
                       Lcd_out_cp("       ");
                       break;
                     }
                     
          case(210):  {
                        lcd_cmd(_lcd_clear);
                        Lcd_out(1,1,all_menu_char[3]);
                        break;
                      }
                      
          case(220):  {
                        lcd_cmd(_lcd_clear);
                        Lcd_out(1,1,all_menu_char[4]);
                        break;
                      }
                      
          default:  {
                      lcd_cmd(_lcd_clear);
                      Lcd_out(1,1,all_menu_char[5]);
                      break;
                    }
        
        }
      disp_refresh=0;
    }
  
 
  void display(int row,int col, int temp_int)
    {
      char temp_char[7];
      inttostr(temp_int,temp_char);
      ltrim(temp_char);
      if(row!=0&&col!=0)
        {
          lcd_out(row,col, temp_char);
        }
      if(row==0&&col==0)
        {
          lcd_out_cp(temp_char);
          lcd_out_cp(" ");
        }
 
    }
  
  
     
  /*// returns the value of the switch pressed
     {
       disp_refresh=1;
       if(but_menu==0)
         {
           switch_val=1;
           //return(1);
         }
 
       if(menu_val!=0)
         {
           if(but_left==0)
             {
               switch_val=2;
               //return(2);
             }
           if(but_right==0)
             {
               switch_val=3;
               //return(3)
             }
           if(but_enter==0)
             {
               switch_val=4;
               //return(4);
             }
           if(but_menu==0 && but_left==0)
             {
               switch_val=5;
               //return(5);
             }
         }
     }*/

 
Last edited by a moderator:

What is your exact problem ? Which variable(s) are updating and which are not ?
 

In the above code,
the PIC 16F1829 is used to check four buttons, MENU,LEFT,RIGHT, ENTER to go through diffrent menus of my program. each button press is generating an port change interupt and is getting read inside the ISR to a variable Switch_val. from the switch value, different menu screens are generated based of the menu_val.

However, the updated value of the menu_val variable is not visible to the main function. I am getting "Switch_val" from ISR and isgetting passed to the calling function "switch_val_processing(switch_val)".I also found that the menu value is getting assigned to the "Menu_val" variable inside the "if(enable_entry==1)". But the updated value is not visible out of the parenthesis of that "IF" statement.

I tried to declare these variables as "VOLATILE" and "STATIC",but its not mentioned in the s=code above. In both cases the results were same, "Menu_val" is not getting updated.
 

At a quick glance I detected you declared variable temp either as int and char at different parts of the code above.
 

andre_teprom: There are no duplicate declarations to the variables. I declared all the variables at the beginning of the program.

milan.rajik: I have given the problem in the second post.

- - - Updated - - -

andre_teprom:Could you please tell me where I had made that mistake. I could not find anything other than temp, temp_int, temp_char, temp_100, temp_10. These all are the declared variables. I could not find any same variable name with different type.
 

Please zip and post the complete mikroC PRO PIC project files. Also provide your circuit. It is good if you provide a Proteus design of your circuit.
 

You´re right concerning to the variable temp, in fact I made a mistake. However, note that you duplicated the variable int i either at lines #3 and line #278/294.

The first one will act as a global variable, and if I´m correct, has less precedence if compared with the other declared inside functions.
 

switch_val and Menu_val are declared globally and should work respectively.

But they are assigned at different places, my most likely explanation is that you just didn't keep track of their usage. Assigning switch_val both in interrupt and main level is a typical example of a coding style that might confuse the software designer himself.

I'm not using mikroC and am not aware of possible bugs. My method to narrow down problems like this is to trace code execution in MPLAB debugger. The correct usage of variables can be also verified by reviewing the assembly level code.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top