ive got this code and dont know what is wrong.
#define a 1 #define b 2 #define c 3 #define 22 37 #define binary1 0x01 #define binary2 0x02 #define binary3 0x04 #define binary4 0x08 #define binary5 0x09 #define binary6 0x10 #define binary7 0x20 int toggle__pin ( int port , int pin ) { if ( port == a && pin == 1 ) { P0 = binary1; delay(1); P0 = ~binary1; } if ( port == a && pin == 2 ) { P0 = binary2; delay(1); P0 = ~binary2; } // repeated return 23; }
A delay function that is optimized by the compiler...? show it please.
Do you think "help" is a descriptive summary of your question? Don't you think it would be ridiculous if everyone who wants help just writes "help"?
#define a 1
That was not a descriptive name of a symbol. If I say "a", would you be able to guess what I may have chosen to let the name "a" represent? Maybe my first car. Maybe the first step of the staircase. Maybe the first folder on the E: partition in my computer. A symbol named "a" is completely meaningles, since it does not contain any information about usage. Are you worrying that the code will be too easy to read if you use the name "PORT_P0"?
It is a common practice to use upper-case names for #defined symbols, to let a reader know that they have to think twice about the meaning of the symbol.
#define 22 37
Are you into chiffers and codes? Who will be able to read code, if every single token in the code may be a magic remapping to something else?
Most languages don't allow symbol names that starts with a digit. And a symbol name only containing digits is meaningles unless it does represent the numeric value indicated by the digits.
When talking with people, would you then have a secret dictionary saying:
#define WHEEL hamburger #define TIRE tomato #define BUMPER fanta #define GEARBOX sushi
Don't ever make meaningles remappings - from the symbol name of the #define, the reader should be able to figure out what the symbol is intended for.
Your list of single-bit values are incorrect. 0x09 is the sum of two non-zero bits. And since the port of a 8-bit processor is 8 bits wide - where is 0x40 and 0x80?
#define binary1 0x01 #define binary2 0x02 #define binary3 0x04 #define binary4 0x08 #define binary5 0x09 #define binary6 0x10 #define binary7 0x20
The following code will not do what you think it does. To toggle one single bit, you must update just that bit, without affecting the other 7 bits.
P0 = binary1; delay(1); P0 = ~binary1;
The first assign will write 0x01. The other will write 0xfe. So all 8 bits will be changed by your two assigns.
Maybe you should try:
P0 |= binary1; delay(1); P0 &= ~binary1;
What the xxx does your return statement mean?
return 23;
Do not use magic values in your code. And it is quite meaningles to have a function that always returns the same value. Why bother looking at the return value if you already know what value that will be returned?
First take at look at your defines....
Everywhere your code has an a it will be substituted, not just where you think it will be.
If you use PREPRINT compiler directive it will give you a list of the code the compiler will see after the #define stuff has been added to your code. This will show you a great deal and teach you a lot about how defines work in your code. Simply, each define must have a unique label
#define PORT_A 1 for eg
"Everywhere your code has an a it will be substituted, not just where you think it will be."
The preprocessor works on tokens, not on individual characters, so the C preprocessor will not replace any a that is part of a longer token or that is inside a C string.
So where did you "get" it from?
it wouldn't, perchance, be a homework exercise in which you are supposed to spot the deliberate mistakes - would it...?