Hello!
Some comments:
1. Don't overuse parentheses.
Example: #define BIT0 (0x0001).
You need parentheses when you have more than one term.
Example: #define SWITCH_ON (P1IN & 0x01)
But 0x0001 is an atomic expression, so whatever happens, it will be seen as 0x0001.
I know that TI defines BIT0 as (0x0001u).
When you use (BIT6) in your code, it is translated by the preprocessor to ((0x0001)),
which looks like suspenders + belt for somebody without trousers.
Note that it works, but it overloads your code unnecessarily.
2. By looking at your comments, there might be a misunderstanding of what it does.
P10DIR |= BIT6; //Put P10.6 to Direct Register
I don't know what you mean by "direct register", but what this instruction does is simply
configuring bit 6 of port10 to output, so "// Set Port 10 bit 6 to output" would be more
understandable for most programmers.
Then, P10OUT |= (BIT6) ; // P10.6 as an output, High level
P10 bit 6 is already an output, so your comment does not really correspond to what it
does. It should br "// P10.6 high level", "// Set P10.6 high" or something like that.
3. I think BIT0 ~ BIT7 are already defined. Probably it depends on the CPU header file,
but in my present case (MSP430F5510), it is defined.
4. I'm not sure stdio.h makes sense for a microcontroller without terminal output,
file system, etc... It doesn't make sense in this program anyway.
5. Beside this, about the functionality of your code:
When you configure your port as output, you should also set the initial conditions
because when entering the loop, the ports' state might not be defined. Note that I'm
not sure of this, you can check in the 5438 manual what the default status after a
reset can be. But just in case, set the PxOUT status BEFORE setting the direction
to output, otherwise, you may have an unwanted status after you set to output
and before you set the state you want.
Next, and this will be the last for the time being: P10.6 is set high in the loop, but
is never set low in the whole program, so there is no need to repeatedly set it high
in the loop and the initial conditions are enough.
6. This time, it's the last one: be careful with the indentation: your while is aligned
with an else. The else should be aligned with an if.
Suggestion:
Code C - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #include "io430x54x.h"
int main( void ) {
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
// Set port 10 initial conditions
P10OUT |= BIT6; // Set P10.6 initially high
P10OUT &= ~BIT7; // Set P10.7 initially low
// Configure ports' direction
P10DIR |= (BIT6 + BIT7); // Set port 10, bits 6 & 7 to output
P4DIR &= ~BIT0; // Set port 4, bit 0 to input
// Loop
while (1) {
if((P4IN & BIT0) == 0x01) { // P4.0 as an input
P10OUT|= BIT7; // Set P10. high
}
else {
P10OUT &= ~BIT7 ; // Set P10.7 low
}
}
} |
Dora.