Vaisakhan Ku
Junior Member level 1
- Joined
- Jan 19, 2014
- Messages
- 16
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 259
how can I update the value of global variables declared as extern variables in the main function inside the ISR.
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 extern int *Ref_high,*CC_ON,*Ref_count_need,*Count_Avail,*Count,*Ref_count,*New_count,*Old_count,*Diff; extern char *temp; #include "Final_header.h" char Int_to_Char(int) void calculate() //char Int_to_Char(int); // Lcd pinout settings sbit LCD_D4 at RC0_bit; sbit LCD_D5 at RC1_bit; sbit LCD_D6 at RC2_bit; sbit LCD_D7 at RC3_bit; sbit LCD_RS at RC4_bit; sbit LCD_EN at RC5_bit; // Pin direction sbit LCD_D4_Direction at TRISC0_bit; sbit LCD_D5_Direction at TRISC1_bit; sbit LCD_D6_Direction at TRISC2_bit; sbit LCD_D7_Direction at TRISC3_bit; sbit LCD_RS_Direction at TRISC4_bit; sbit LCD_EN_Direction at TRISC5_bit; void initialize(void) { ANSEL=ANSELH=0; //all inputs are digital OPTION_Reg =0xa0; //0b11100000 Enabled: RBPU:disabled,INTEDG:rising,T0CS:Ra4 pin,PSA:Timer,PS2:0,PS1:0,PS0:0 INTCON =0xa8; //0b10111000 enabled: GIE:Disabled,PEIE:Disabled,T0IE:Enabled,INTE:Enabled,RBIE:enabled,T0IF:Cleared,INTF:Cleared,RBIF:Cleared trisb=0xff; trisa=0x10; IOCB.b1=0; IOCB.b2=1; IOCB.b3=0; IOCB.b4=0; IOCB.b5=0; trisd=0; portd=0x00000000; lcd_init(); } interrupt() { if(INTCON.RBIF) { if(PORTB.B2==1) { CC_ON=1; *Ref_count_need=1; IOCB.B1=1; IOCB.B3=1; IOCB.B4=1; IOCB.B5=1; if(PORTB.B1==1) { *Ref_High=1; } if(PORTB.B1==0) { portd.B3=1; *Ref_High=0; } goto Exit_to_main; } if(PORTB.B3==1) { CC_ON=0; *Count_Avail=0; IOCB.B1=0; IOCB.B3=0; IOCB.B4=0; IOCB.B5=0; goto Exit_to_main; } if(PORTB.B1==1) { if(*Ref_high && *Ref_count_need) { *Ref_high=0; } tmr0=0; goto Exit_to_main; } if(PORTB.B1==0) { if(*Ref_High==1) { *Ref_Count_Need=1; goto Exit_to_main; } if(*Ref_High==0) { if(*Ref_Count_need==1) { Ref_Count_Need=0; Ref_Count=tmr0; New_Count=tmr0; Count_Avail=1; goto Exit_to_main; } if(*Ref_Count_need==0) { *Old_Count=*New_count; *New_Count=tmr0; *Count_Avail=1; goto Exit_to_main; } } } INTCON.RBIF=0; } Exit_to_main:; } void Int_to_Char(int *temp_int) { char *temp_char; inttostr(*temp_int,*temp_char); *temp=*temp_char; ltrim(*temp); //return(*temp_char); } void calculate() { char Display_Val; if(Count_Avail==0) { goto exit_from_calc; } diff=Ref_count-New_Count; Count_Avail=0; if(diff>0) { if(Diff==0) { goto exit_from_calc; } Lcd_Out(1,1,"Count is High "); Lcd_Out(2,1,"Ref:"); Display_val=int_to_char(Ref_count); Lcd_Out_cp(Display_val); Lcd_Out(2,10,"New:"); Display_val=int_to_char(New_Count); Lcd_Out_cp(Display_val); goto exit_from_calc; } if(diff<0) { Lcd_Out(1,1,"Count is Low "); Lcd_Out(2,1,"Ref:"); Display_val=int_to_char(Ref_count); Lcd_Out_cp(Display_val); Lcd_Out(2,10,"New:"); Display_val=int_to_char(New_Count); Lcd_Out_cp(Display_val); goto exit_from_calc; } exit_from_calc:; } void main() { initialize(); /*Lcd_Out(1,6,"CC Turning ON"); Lcd_Out(2,1,"Welcomne to Cruice Control"); Delay_ms(750); Lcd_Cmd(_lcd_clear);*/ Lcd_out(1,1,"Initializing...."); Delay_ms(50); Lcd_Cmd(_lcd_clear); while(1) { /*if(CC_ON==1) {*/ //calculate(); lcd_cmd(_lcd_clear); Lcd_out(1,1,"CC_ON:"); *cc_on=12345; Int_to_Char(&cc_on); Lcd_out_cp(temp); Lcd_out_cp(" Ref_high:"); Lcd_chr_cp(temp); Lcd_out_cp(" Ref_count_need:"); Lcd_out_cp(Ref_count_need); Lcd_out(2,1," Count_Avail:"); Lcd_out_cp(Count_Avail); delay_ms(100); //change_pwm(); // } } }
void interrupt(){
...
}
Code C - [expand] 1 2 3 4 volatile char *y; volatile int a,b; volatile char m[20]; volatile char x;
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 #include "cruice_header.h" volatile extern char *y; volatile extern int a,b; volatile extern char m[20]; volatile extern char x; // Lcd pinout settings sbit LCD_D4 at RC0_bit; sbit LCD_D5 at RC1_bit; sbit LCD_D6 at RC2_bit; sbit LCD_D7 at RC3_bit; sbit LCD_RS at RC4_bit; sbit LCD_EN at RC5_bit; // Pin direction sbit LCD_D4_Direction at TRISC0_bit; sbit LCD_D5_Direction at TRISC1_bit; sbit LCD_D6_Direction at TRISC2_bit; sbit LCD_D7_Direction at TRISC3_bit; sbit LCD_RS_Direction at TRISC4_bit; sbit LCD_EN_Direction at TRISC5_bit; void initialize() { ANSEL=ANSELH=0; //all inputs are digital OPTION_Reg =0xa0; //0b11100000 Enabled: RBPU:disabled,INTEDG:rising,T0CS:Ra4 pin,PSA:Timer,PS2:0,PS1:0,PS0:0 INTCON =0xa8; //0b10111000 enabled: GIE:Disabled,PEIE:Disabled,T0IE:Enabled,INTE:Enabled,RBIE:enabled,T0IF:Cleared,INTF:Cleared,RBIF:Cleared trisb=0xff; trisa=0x10; IOCB.b1=1; IOCB.b2=0; IOCB.b3=0; IOCB.b4=0; IOCB.b5=0; trisd=0; Lcd_init(); } interrupt() { if(intcon.rbif==1) { if(portb.b1==1) { lcd_out(1,1,"inside Interrupt"); lcd_out(2,1,"M:-"); lcd_out_cp(m); delay_ms(250); lcd_cmd(_lcd_clear); m[20]="M value has changed"; lcd_out(1,1,"Contents of M changed"); lcd_out(2,1,"M:-"); lcd_out_cp(m); } INTCON.rbif=0; } } void main() { initialize(); m[20]="assigned from main"; while(1) { }**broken link removed**[ATTACH]101277._xfImport[/ATTACH] }
volatile char *y;
volatile int a,b;
volatile char m[20];
volatile char x;
// Lcd pinout settings
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
// Pin direction
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
void initialize()
{
ANSEL=ANSELH=0; //all inputs are digital
OPTION_Reg =0xa0; //0b11100000 Enabled: RBPU:disabled,INTEDG:rising,T0CS:Ra4 pin,PSA:Timer,PS2:0,PS1:0,PS0:0
INTCON =0xa8; //0b10111000 enabled: GIE:Disabled,PEIE:Disabled,T0IE:Enabled,INTE:Enabled,RBIE:enabled,T0IF:Cleared,INTF:Cleared,RBIF:Cleared
trisb=0xff;
trisa=0x10;
IOCB.b1=1;
IOCB.b2=0;
IOCB.b3=0;
IOCB.b4=0;
IOCB.b5=0;
trisd=0;
Lcd_init();
}
interrupt()
{
if(intcon.rbif==1)
{
if(portb.b1==1)
{
lcd_out(1,1,"inside Interrupt");
lcd_out(2,1,"M:-");
lcd_out_cp(m);
delay_ms(150);
lcd_cmd(_lcd_clear);
m[20]="M value has changed";
lcd_out(1,1,"Contents M changed");
lcd_out(2,1,"M:-");
lcd_out_cp(m[20]);
}
INTCON.rbif=0;
}
}
void main()
{
initialize();
m[20]="assigned from main";
while(1)
{
}
}
# define LCD_PRINT(r,c,val) { inttostr(val,temp1); ltrim(temp1);Lcd_Out(r,c,temp1);}
volatile int a,b,e;
volatile char temp1[7];
// Lcd pinout settings
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
// Pin direction
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
void initialize()
{
ANSEL=ANSELH=0; //all inputs are digital
OPTION_Reg =0xa0; //0b11100000 Enabled: RBPU:disabled,INTEDG:rising,T0CS:Ra4 pin,PSA:Timer,PS2:0,PS1:0,PS0:0
INTCON =0xa8; //0b10111000 enabled: GIE:Disabled,PEIE:Disabled,T0IE:Enabled,INTE:Enabled,RBIE:enabled,T0IF:Cleared,INTF:Cleared,RBIF:Cleared
trisb=0xff;
trisa=0x10;
IOCB.b1=1;
IOCB.b2=0;
IOCB.b3=0;
IOCB.b4=0;
IOCB.b5=0;
trisd=0;
Lcd_init();
}
interrupt()
{
if(intcon.rbif==1)
{
e=890;
INTCON.rbif=0;
}
}
void main()
{
initialize();
a=123;
b=345;
e=0;
while(1)
{
lcd_cmd(_lcd_cursor_off);
LCD_PRINT(1,1,a);
LCD_PRINT(2,1,b);
LCD_PRINT(2,8,e);
}
}
void LCD_PRINT(char Row, char Col, unsigned int Value)
{
LCD_Out(Row, Column, ltrim(inttostr(Value)));
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?