1 | /*
|
2 | Hello World example.
|
3 | ---- Code 248 Byte ----
|
4 |
|
5 | MSP430g2231
|
6 | --------------
|
7 | +3,5V --|DVCC GND|-- DVSS
|
8 | rote LED --|P1.0 P2.6|--
|
9 | TXD --|P1.1 P2.7|--
|
10 | --|P1.2 TEST|-- TEST/SBWTCK
|
11 | Taster --|P1.3 RESET|-- RST/NMI/SBWTDIO
|
12 | --|P1.4 P1.7|--
|
13 | --|P1.5 P1.6|-- grüne LED
|
14 | --------------
|
15 | */
|
16 |
|
17 | #include "msp430g2231.h"
|
18 | #include "inttypes.h"
|
19 |
|
20 | #define TXD BIT1 // TXD an P1.1
|
21 | #define BitTime 104 // 9600 Baud, SMCLK=1MHz (1MHz/9600)=104
|
22 |
|
23 | // globale Variablen
|
24 | uint16_t TXByte; // TXByte + Start/Stop BIT's
|
25 |
|
26 | // Prototypes
|
27 | void putChar (uint8_t c);
|
28 | void printfEasy (const char *s);
|
29 |
|
30 | void main (void)
|
31 | {
|
32 | WDTCTL = WDTPW | WDTHOLD; // Stop WDT
|
33 | BCSCTL1 = CALBC1_1MHZ; // Set range
|
34 | DCOCTL = CALDCO_1MHZ; // SMCLK = DCO = 1MHz
|
35 |
|
36 | P1SEL |= TXD; // TXD connected @ Timer
|
37 | P1DIR |= TXD; // als Ausgang
|
38 |
|
39 | // ---- Soft. UART Timer Setup ----
|
40 | CCTL0 = OUT; // TXD Idle as Mark = '1'
|
41 | TACTL = TASSEL_2 | MC_2; // SMCLK, continuous mode
|
42 |
|
43 | __bis_SR_register (GIE); // interrupts enabled
|
44 |
|
45 | printfEasy ("Hello World\n\r");
|
46 |
|
47 | while (1) {}
|
48 | }
|
49 |
|
50 | // ---- simple printf ----
|
51 |
|
52 | void printfEasy (const char *s)
|
53 | {
|
54 | while (*s) {
|
55 | putChar (*s++);
|
56 | }
|
57 | }
|
58 |
|
59 | // TXing a byte
|
60 | void putChar (uint8_t c)
|
61 | {
|
62 | while (CCTL0 & CCIE); // Warten bis die ISR alle BITs ausgesendet hat
|
63 | TXByte = 0x200 | (c << 1); // Char + Stop BIT in TXByte einfügen
|
64 | CCR0 = TAR + 16;
|
65 | CCTL0 = CCIS0 | OUTMOD0 | CCIE; // re-Enable Interrupts, trigger Transmit ISR
|
66 | }
|
67 |
|
68 | // Timer A0 interrupt service routine
|
69 | #pragma vector=TIMERA0_VECTOR
|
70 | __interrupt void Timer_A (void)
|
71 | {
|
72 | if (TXByte) { // Wenn alle BITs ausgesendet sind...
|
73 | (TXByte & 1) ? (CCTL0 &= ~ OUTMOD2) : (CCTL0 |= OUTMOD2);
|
74 | TXByte >>= 1;
|
75 | CCR0 += BitTime; // BIT Zeit aufaddieren
|
76 | } else {
|
77 | CCTL0 &= ~ CCIE; // ...disable interrupt
|
78 | }
|
79 | }
|