Martin schrieb:
> P1OUT |= BUTTON;
> P1DIR &= ~BUTTON;
P1OUT |= BUTTON;
Damit gebe ich aber doch Saft auf den Button. Nicht das was ich möchte.
P1DIR &= ~BUTTON;
P1.3 must stay as input (this is the default)! - Also es ist von anfang
an ein Eingang. P1DIR sieht zu Laufzeit so aus:
P0 P7
100000010
Scheint mir korrekt.
Okay, mit Martins Änderung sieht mein Programm so aus:
1 | #include <msp430g2231.h>
|
2 |
|
3 | /* MSP430 PushButton that toggles LEDs On and Off */
|
4 | /* */
|
5 | /* Description: PushButton in P1.3 through interrupt turns on and off the LED at P1.0 */
|
6 | /* and P1.6. By changing the P1.3 interrupt edge, the interrupt is called every time */
|
7 | /* the button is pushed and pulled; toggling the LED everytime. */
|
8 |
|
9 |
|
10 | /* (#1 Pull Up)LaunchPad revisions 1.3 and 1.4 come with R34 populated. The 47-kO resistor is used as a pullup for the button S2. */
|
11 | /* If the port P1.3 is driven to ground, as suggested to keep the power consumption down, the pullup resistor generates an additional current of approximately 77 µA. */
|
12 | /* To reduce the power consumption, the port should stay in input mode or the resistor should be removed if button S2 is not used. The internal pullup of the MSP430G2xx can be used instead. */
|
13 |
|
14 | #define LED1 BIT0
|
15 | #define LED2 BIT6
|
16 | #define BUTTON BIT3
|
17 |
|
18 | int main(void)
|
19 | {
|
20 | WDTCTL = WDTPW + WDTHOLD; /* Stop watchdog timer */
|
21 | P1DIR |= (LED1 + LED2); /* Set LEDs to output direction */
|
22 | /* P1.3 must stay at input (this is the default) */
|
23 |
|
24 | P1OUT &= ~(LED1 + LED2); /* turn LEDs off */
|
25 | P1OUT |= BUTTON;
|
26 | P1DIR &= ~BUTTON;
|
27 | P1IE |= BUTTON; /* P1.3 interrupt enabled */
|
28 | P1IFG &= ~BUTTON; /* P1.3 IFG cleared */
|
29 | P1REN |= BUTTON; /*(see #1) Enable the Pull Up? */
|
30 | __enable_interrupt(); /* enable all interrupts */
|
31 |
|
32 | while(1) {
|
33 | __delay_cycles(100000);
|
34 | P1OUT ^= LED1 ;
|
35 | }
|
36 | /* endless loop */
|
37 | }
|
38 |
|
39 |
|
40 | // Port 1 interrupt service routine
|
41 | #pragma vector=PORT1_VECTOR
|
42 | __interrupt void Port_1(void)
|
43 | {
|
44 | P1OUT ^= (LED1 + LED2); /* LED toggle */
|
45 | P1IFG &= ~BUTTON; /* P1.3 IFG cleared */
|
46 | }
|
Und es läuft. Wenn ich Einen Pin als Eingang setze und dann aber Saft
draufgebe. Was tut dies dann? - Scheinbar aktiviere ich damit den
internen Pull-Up. Aber was für einen Sinn hat dann P1REN? Wähle ich
damit ob ich Pull-Up oder Pull-Down möchte?
//edit: Hab die Erklärung gefunden
PxREN
The resistor enable register is a very useful feature for the ports.
Sometimes it is helpful to pull the voltage up to Vcc or down to Vss,
such as when you attach a push button to a pin. The resistor enable
register lets you turn on that ability. When a PxREN bit is enabled,
you can select it as a pull-up or pull-down resistor by setting the
corresponding bit in PxOUT, 1 for up, 0 for down.