understanding of some C or microcontroller commands

Status
Not open for further replies.

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
Visit site
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
 


this is for sure HW specific and not C compiler or any another stuff!
From where you copy this....?
...and mean:
Up=14, Down=13, Left=11, Right=7
only macro commands!
 

If i want to connect buttons and give Up=14 and Down=13 ,so how to implement on microcontroller port.

Means that if i pressed Up button LED blinks and when i pressed Down LED stops blinking.

- - - Updated - - -

I want to make this type of program that is,

if(button_press == Up)
PORTC = 0x00;

else if(button_press == Down)
PORTC = 0xFF;

Please let me know about that how to take different value the variable button_press?
 

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.
 


I want to make this type of program that is,

if(button_press == Up)
PORTC = 0x00;

else if(button_press == Down)
PORTC = 0xFF;

Please let me know about that how to take different value in the variable button_press?
 

#define is a preprocessor directive for the Compiler. When the file is Compiled for the below code, Whereever there is ON, they are all replaced with 1. First tell what is button_press? If button_press is a switch connected to some port pin then you can compare it only with 0 or 1. So, you r code should be like below



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;
        }
    }
 
}

 

press_button is a variable define in program but i want that the number should be 34 or 35 instead of 0 or 1?
 

I think you are asking what actually makes pressing a button return that particular number. If I'm right, the number is decided by the circuit around the button and without a schematic there is no way we can help you.

Brian.
 

#define Up 34
#define Down 35
#define Right 36
#define Left 37

Yes, let suppose if 4 buttons are connected to PORTA i.e PA0=Up , PA1=Down , PA2=Left , PA3=Right.

and values define in macros for buttons and a variable is also predefined button_press=0;

and i want to use same variable for different inputs so how to program them?

Please see attached documents.

- - - Updated - - -

Please see post# 10.
 

Attachments

  • Button.txt
    582 bytes · Views: 66
  • pic.png
    27.8 KB · Views: 68
Last edited by a moderator:


Code C - [expand]
1
2
3
4
#define Up 34
#define Down 35
#define Right 36
#define Left 37

are not macros. They are preprocessor directives to tell the compiler to replace every instance of Up, Down, Right, and Left in the code with the values 34, 35, 36, and 37.

To define PORTS RA0, RA1, RA2, RA3 as Up, Down, Right, and Left you have to use the below code

Code C - [expand]
1
2
3
4
#define Up PORTA.F0
#define Down PORTA.F1
#define Right PORTA.F2
#define Left PORTA.F3


The above code is for mikroC

So you can't have Up = 34 and also PORTA.F0

I see in your code that you are using button_pressed in if statements. You cannot use it before assigning some value to button_pressed variable. button_pressed is 0 by default. So, in the if...else if... statements it is checked if button_pressed is equal to 34, 35, 36, or 37 but that will result in false. So, it skips all the if...else if... statements.
 
Last edited:

Dear betwixt,

Please see attached code.How to function these #define Up 14 and so on etc.
 

Attachments

  • CodeELM.txt
    34.9 KB · Views: 59

I'm no expert in AVR code but a quick check on Google tells me that PINA is the instruction to read the pin states from port A. So the line "button_pressed = PINA;" in the main() section is what produces the numbers. This means the buttons are connected to port A of the processor and the numbers are the values that appear on port A when they pressed. As the buttons are not read if the port value is 15 (1111 in binary) it suggests the buttons connect the port pins to ground and somethig pulls them to high level when the button isn't pressed. The values for each button are therefore:

port = value
1111 = no button pressed
0111 = 7
1011 = 11
1101 = 13
1110 = 14

You can see that in each case one of the bits is a zero because the switch connected to that bit is pressed.

Brian.
 

Thank you very much for explanation and for help,

This program i build in AVR studio 4, it is build successfully but in proteus simulation the buttons cannot do anything.Only problems are in buttons working.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…