imranahmed
Advanced Member level 3
- Joined
- Dec 4, 2011
- Messages
- 822
- Helped
- 3
- Reputation
- 6
- Reaction score
- 3
- Trophy points
- 1,298
- Location
- Karachi,Pakistan
- Activity points
- 6,533
Please let me know that what is the purpose of numbers mention below after up,down,left and right command.
#define Up 14 //Green- Up
#define Down 13 //Yellow- Down
#define Left 11 //Orange- Left
#define Right 7 //Brown- Right
These are DEFINES not macros.
It simply associates a name with a number to make the program easier to understand. For example "#define Up 14" means where you use the word "Up" the compiler/assembler should use the value 14 when it produces the executable.
So the line "if(button_press == Up)" is functionally the same as "if(button_press == 14)".
The big advantage of using #defines, apart from making the program clearer is that if you change the value in the #define statement, all references to it are changed at once. Suppose you wanted the value for "Up" to be 25 instead, you could go through your program line by line changing every ocurrence of 14 into 25 but it would be much easier to simply change the defined value to 25 at the top of the program.
Brian.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #define button_press PORTA.F0 #define ON 1 #define OFF 0 #define LED PORTB.F0 void main() { TRISA = 0xFF; PORTA = 0x00; TRISB = 0x00; PORTB = 0x00; if(button_press == ON) { Delay_ms(30); if(button_press == ON) { LED = ~LED; } } }
Code C - [expand] 1 2 3 4 #define Up 34 #define Down 35 #define Right 36 #define Left 37
Code C - [expand] 1 2 3 4 #define Up PORTA.F0 #define Down PORTA.F1 #define Right PORTA.F2 #define Left PORTA.F3
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?