1 | #include <msp430.h> //einbinden der MSP430 Programmbibiothek
|
2 | #include <stdio.h>
|
3 |
|
4 | #define RXD BIT1
|
5 | #define TXD BIT2
|
6 |
|
7 | unsigned int samples[3]; //Array für ADC Speicherung
|
8 | char string2[12];
|
9 | unsigned int i; //Counter
|
10 | float I;
|
11 |
|
12 |
|
13 | int main(void)
|
14 | {
|
15 | /**************ADC10 Settings**********************/
|
16 | ADC10CTL0 = SREF_0 + ADC10SHT_2 + REF2_5V + REFON + MSC + ADC10ON;
|
17 | ADC10CTL1 = INCH_5 + ADC10DIV_0 + CONSEQ_3 + SHS_0; //Multi-channel repeated conversion starting from channel 3
|
18 | ADC10AE0 = BIT5 + BIT4 + BIT3; //Kanal A5 (BIT5), A4 (Bit4) und A3 (BIT3) aktivieren
|
19 | ADC10DTC1 = 3; // Alle drei Kanäle bei einer Einlesung übertragen
|
20 |
|
21 | /**************DO Settings**********************/
|
22 | //P1DIR |= 0x08;
|
23 | //P1DIR |= 0x10; // P1.3, P1.4, P1.5 als DO
|
24 | //P1DIR |= 0x20;
|
25 | P2DIR |= 0xFF; // All P2.x outputs
|
26 | P2OUT &= 0x00; // All P2.x reset
|
27 |
|
28 | /**************Clock Settings**********************/
|
29 | WDTCTL = WDTPW + WDTHOLD; // Watchdog deaktivieren
|
30 | DCOCTL = 0;
|
31 | BCSCTL1 = CALBC1_1MHZ; // Setze Taktfrequenz der System Clock auf 1MHz
|
32 | DCOCTL = CALDCO_1MHZ; // Digitally Controlled Oscilator auf 1MHz einstellen
|
33 |
|
34 | /***************Timer_A Settings******************/
|
35 | CCTL0 = CCIE; // Aufruf der Interruptfunktion ermöglichen
|
36 | CCR0 = 8000; // Gehe in den Interrupt nach 1000 Durchläufen (bei 1MHz jede 1ms)
|
37 | TACTL = TASSEL_2 + MC_1; // TASSEL_2: Wähle ACLK (Auxiliary clock) als Quellclock
|
38 | // MC_1: Der Timer zählt bis zum Wert von CCR0
|
39 |
|
40 |
|
41 | /*************Serielle Übertragung Setup*************/
|
42 | P1SEL |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD
|
43 | P1SEL2 |= RXD + TXD ; // P1.1 = RXD, P1.2=TXD
|
44 | UCA0CTL1 |= UCSSEL_2; // SMCLK
|
45 | UCA0BR0 = 0x08; // 1MHz 115200
|
46 | UCA0BR1 = 0x00; // 1MHz 115200
|
47 | UCA0MCTL = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5
|
48 | UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
|
49 |
|
50 |
|
51 | _BIS_SR(LPM0_bits + GIE); // Interrupteintritt ermöglichen
|
52 | }
|
53 |
|
54 | // Timer A0 interrupt service routine
|
55 | #pragma vector=TIMER0_A0_VECTOR
|
56 | __interrupt void Timer_A (void)
|
57 | {
|
58 | ADC10CTL0 &= ~ENC;
|
59 | while (ADC10CTL1 & BUSY);
|
60 | ADC10SA = (unsigned int)samples;
|
61 | //automatische Speicherung der Daten in das Array "sample"
|
62 |
|
63 | ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
|
64 |
|
65 | /*
|
66 | if (sample[3]>=900) //Sensor 1 über 4,39V, dann Sensor 2
|
67 | {
|
68 | j=2;
|
69 | }
|
70 | else if (sample[2]>=900)//Sensor 2 über 4,39V, dann Sensor 3
|
71 | {
|
72 | j=3;
|
73 | }
|
74 | else //sonst Sensor 1
|
75 | {
|
76 | j=1;
|
77 | }
|
78 |
|
79 | if (j=1)
|
80 | {
|
81 | k= 43,54//A/V
|
82 | }
|
83 |
|
84 | if (j=2)
|
85 | {
|
86 | k= 113,79//A/V
|
87 | }
|
88 |
|
89 | if (j=3)
|
90 | {
|
91 | k= 258,07//A/V
|
92 | }
|
93 |
|
94 | I = 5*sample[j]*k
|
95 | */
|
96 | I=347.145;
|
97 |
|
98 |
|
99 | sprintf(string2, "I = %f", I);
|
100 |
|
101 |
|
102 | UC0IE |= UCA0TXIE; // Enable USCI_A0 TX interrupt
|
103 |
|
104 | }
|
105 |
|
106 | #pragma vector=USCIAB0TX_VECTOR
|
107 | __interrupt void USCI0TX_ISR(void)
|
108 | {
|
109 | // TX next character
|
110 |
|
111 | UCA0TXBUF = string2[i++]; // TX next character
|
112 | if (i == sizeof string2 - 1) // TX over?
|
113 | {
|
114 | i=0; //Zähler für Übertragung reseten
|
115 | UC0IE &= ~UCA0TXIE; // Disable USCI_A0 TX interrupt
|
116 | }
|
117 | }
|