|
MSP430G2553 模数转换器 ADC10代码示例
[复制链接]
利用ADC10,每2秒读取一次A6(P1.6)通道上的电压,通过串口打印显示。
复制代码
1 // voltage meter 2015.10.1
2 // for MSPG2 launchpad, VCC = 3.6V; and if Vcc changes,
3 // ref_vcc should be modified.
4
5 #include "io430.h"
6
7 #define A6 BIT6 // ADC input pin P1.6
8 #define RXD BIT1 //P1.1
9 #define TXD BIT2 //P1.2
10
11 #define IDLE 0
12 #define BUSY 1
13
14 // function definitions
15 void initADC10(void);
16 void initTA0(void);
17
18 void sendByte(unsigned char);
19 void printf(char *, ...);
20 void initUART(void);
21
22 // global variables
23 float volts = 0.0; // unit: mV
24 float ref_vcc = 3600.0;
25 char i = 0;
26 char status = IDLE;
27
28 void main( void )
29 {
30 // Stop watchdog timer to prevent time out reset
31 WDTCTL = WDTPW + WDTHOLD;
32
33 // DCO setup
34 BCSCTL1 = CALBC1_1MHZ;
35 DCOCTL = CALDCO_1MHZ;
36
37 // ADC10 setup
38 initADC10();
39
40 // UART setup
41 initUART();
42
43 // Timer0 setup
44 initTA0();
45
46 __enable_interrupt();
47
48 LPM0;
49
50 while(1)
51 {
52 switch(status)
53 {
54 case IDLE:
55 break;
56 case BUSY:
57 ADC10CTL0 |= ADC10SC; // start a new conversion
58 while ((ADC10CTL1 & ADC10BUSY) == 0x01); // wait for conversion to end
59 volts = ADC10MEM * ref_vcc /1023.0;
60 printf("Voltage: %i mV\r\n", (int)volts);
61 status = IDLE;
62 break;
63 }
64 LPM0;
65
66 }
67 }
68
69 void initADC10(void)
70 {
71 // Use Vcc(3.6V)/GND for Refs, 16 x ADC10CLKs, reference buffer on, turn on ADC
72 ADC10CTL0 = SREF_0 + ADC10SHT_2 + ADC10SR + ADC10ON;
73
74 // A6 input, use ADC10CLK div 1, single channel mode
75 ADC10CTL1 = INCH_6 + SHS_0 + ADC10SSEL_0 + ADC10DIV_0 + CONSEQ_0;
76
77 // ADC input on P1.6
78 ADC10AE0 = A6;
79
80 // Enable conversions
81 ADC10CTL0 |= ENC;
82 }
83
84 void initTA0(void)
85 {
86 TA0CCR0 = 62550 - 1;
87 TA0CCTL0 = CCIE; // enable compare interrupt
88 TA0CTL = TASSEL_2 + ID_3 + MC_1 + TACLR;
89 // input clock: SMCLK/8 -> 125kHz; Up to CCR0 mode
90 // timer overflow freq: 125k/(TA0CCR0+1) -> 2Hz -> 500ms
91 // clear and start the timer, up mode
92 }
93
94 void initUART(void) {
95 //config P1.1 RXD, P1.2 TXD
96 P1SEL |= TXD + RXD;
97 P1SEL2 |= TXD + RXD;
98
99 //reset UCA0, to be configured
100 UCA0CTL1 = UCSWRST;
101 //config
102 UCA0CTL1 |= UCSSEL_2; //SMCLK
103 UCA0BR0 = 104;
104 UCA0BR1 = 0;//1MHz baut rate = 9600
105 UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
106 //make UCA0 out of reset
107 UCA0CTL1 &= ~UCSWRST;
108 }
109
110 void sendByte(unsigned char byte )
111 {
112 while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
113 UCA0TXBUF = byte; // TX -> RXed character
114 }
115
116 //interrupt service routines
117 #pragma vector = TIMER0_A0_VECTOR
118 __interrupt void CCR0_ISR(void)
119 {
120 if(++i == 4) // interval: 1/2 * 4 = 2s, freq: 0.5Hz
121 {
122 // automatic flag clearing
123 i = 0;
124 status = BUSY;
125 LPM0_EXIT;
126 }
127 }
复制代码
|
|