|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
本程序是单片机MSP430G2553 0 v1 ^5 y- m2 G) k
P1.2----------------------------UCA0TXD 串口发送端2 B# b0 s" d+ ^4 ^( M4 Y( p
P1.1----------------------------UCA0RXD 串口接受端
5 h) Y- g' I4 E6 Q. x P1.0----------------------------红灯指示
; F& G+ _. X& U% x P1.6----------------------------绿灯指示
7 l$ y; e( u7 n+ Y6 K P1.5----------------------------模拟电压输入3 a- L" Q- }) T! ~8 W! W7 l' k
利用主辅时钟为1MHZ,串口波特率9600,5 C& X1 @# H6 M- }: U
波特率:1000000/9600=104# @) e- v5 b- r( [
UCA0BR0 = 104; // 1MHz 9600
* f+ ^& j7 F5 `" h! k1 O, ~! P* [7 ] UCA0BR1 = 0; // 1MHz 9600' _6 |$ I% R) E: h) i
! C1 j$ v% V6 Z. n" d. p8 w: u
现象:* ?2 \! e) f& T
功能(1)看到 P1.5口输入不同的电压经AD采样后发送 , E, l6 N9 V9 ?
功能(2)打开电脑串口调试助手,发送区有数据待发送时,在接受区将看到相应的数据。4 C: O3 a* J2 c; u5 W, \9 n
******************************************************************************/+ ]( F( t) p) d+ t7 }# w% ]
#include <msp430g2553.h>% ]( o+ q6 F5 M( F$ V; y0 a3 Q L
float adc_voltage;
. y+ x; f9 l. b6 W/ Junsigned int k;
" [$ [) T+ C: L9 A7 B: v8 yvoid sys_clk_config(void)# F3 R+ J8 i- M7 ]
{
$ A2 t9 [, i5 ?0 P1 p) h //ACLK = ; MCLK = SMCLK = DCOCLK = 1.1MHZ. t9 o6 b: T6 @3 {9 I& b1 ]
DCOCTL = 0;
+ z+ `3 j& A/ Q' _, ] BCSCTL1 = CALBC1_1MHZ;
- |0 y; s" L, ~1 U DCOCTL = CALDCO_1MHZ;
* @" _; l& B# \% o}
4 Q a/ m% r; o' ]void uart_gpio_init(void)1 K+ Z1 `5 k2 C$ W* g0 ^
{5 s I |' R% g2 y! B
//P1SEL |= BIT1 + BIT2;
; X# \% B P" D' R( V //P1SEL2 |= BIT1 + BIT2;
# D. M& {4 ?+ T& Y8 Y //P1DIR |= BIT2;- p' i7 D% T& Y, w8 T
P1DIR = 0xFF; // All P1.x outputs3 [. J& u# K- x+ Z/ J
P1OUT = 0; // All P1.x reset: o* k2 o% i+ k* i) R
P1SEL |= BIT1 + BIT2 + BIT4; // P1.1 = RXD, P1.2=TXD
+ q% c8 D" t+ r P1SEL2 |= BIT1 + BIT2; // P1.4 = SMCLK, others GPIO
6 Y- {; D0 `0 v9 g! q P2DIR = 0xFF; // All P2.x outputs
/ }# n6 ]) `: ~- X k& f}
3 N) f% m, B; x" x* gvoid led_gpio_init(void)( I! P; K8 \/ C! y* Y; b6 g
{- M2 ^* F& l4 Y6 X w3 h' J! q0 y
} ?/ Y/ n. ]( C% c. r! S/ n" I- b$ N5 ]
void ad_gpio_init(void)
: v6 t: @, K& \) N) y0 n+ g: w{
8 N" ^1 k7 `7 U z+ o- N}
9 f& _7 R g _# b+ nvoid uart_config(void)6 C: e) z( Y2 O
{
. F0 z0 v7 |# l, h0 E6 c7 HUCA0CTL1 |= UCSSEL_2;: O3 Q, c9 P& @ L" L
$ T; t. P/ {0 p
UCA0BR0 = 104; // 1MHz 9600
6 b/ }8 s I* E' B; l UCA0BR1 = 0; // 1MHz 96009 }6 z6 F+ j; Q2 e
UCA0MCTL = UCBRS2 + UCBRS0;
) S, j6 r6 m% p$ ~6 k UCA0CTL1 &= ~UCSWRST;$ x( V) t) }' N/ c" l& L& M
IE2 |= UCA0RXIE;
" D: m" d$ b8 G7 X+ ^+ v 0 H+ O( I' q) Q- J9 f
}
1 Z2 ?6 s9 R+ Dvoid ad_config(void)
4 X& ?7 g0 ^- m" B4 T{6 b# Z- u+ b1 J2 @0 C2 j' c
ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE;! d! V" w, x, T! M
7 J3 i$ U9 r2 {# L. `$ q ADC10CTL1 = INCH_5 + CONSEQ_2 ;
, ^* ~; C8 L+ m: O3 X ADC10AE0 |= 0x10;8 ~4 e& h5 {# x+ c) b" n
r) |5 M# K. p. r( M& v) R
ADC10CTL0 |= ENC + ADC10SC;
8 I/ z: ]( l# a
% ? B" ^3 R* Y: S- ]' A}
& h$ P9 b# y& X% gvoid main()4 q: K6 |5 ~# k! T
{
& ~3 q# S( f' ~7 \' l6 g WDTCTL = WDTPW + WDTHOLD;! Y& b, B4 U% W* w5 u; l
8 F3 ?4 x' w4 E$ h
//sys_clk_config();
. n& |9 l) @! m2 H1 S' ^6 t- T8 C+ `5 i% W
uart_gpio_init();
# M3 s+ v( t. ?
# O. B) g7 R2 L led_gpio_init();6 p# r: u1 ^# S) k
% ?$ T$ j; q. K5 f8 \ ad_gpio_init();
h/ `( Z, J8 O0 i
- n0 v/ t+ f+ o. `8 }+ h* O3 T uart_config();
/ l! L% v {$ Y! ^2 S. u5 t# o1 U" E( |; l% A* H4 L1 l) n
ad_config();* \0 j& L/ P# ]
__bis_SR_register(GIE);
; }. _% h2 v$ R2 T# k5 _ while(1); R6 H( x& [% I! S
3 Y( O1 u a2 T
6 w1 t5 W7 ?9 P}/ y. |, k* o8 D9 E7 E
#pragma vector = USCIAB0RX_VECTOR! b3 e& k& x, X. I7 I" \! Z% |9 B
__interrupt void USCI0RX_ISR(void)
* I* s& s6 Y; d* c: [0 `; \{7 p$ H( k g" L' ?
while(!(IFG2 & UCA0TXIFG)); k4 d; t) g6 }& t) t |5 N0 X- U
// UCA0TXBUF =0x33 ;// UCA0RXBUF;) {/ k) i: S% b( s' O
}& N+ S3 g9 r2 j: Y# M; L
#pragma vector = ADC10_VECTOR
7 o$ n) f( d u3 N2 R__interrupt void ADC10_A0_ISR(void): [6 i/ r8 Y/ o6 s# h: B4 j1 U5 J
{4 f3 h* ]4 N# w4 |1 @$ ~9 `
2 I# X9 Q3 Q4 N" ^% w) b while(!(IFG2 & UCA0TXIFG));3 h z$ |) q7 r7 f1 V9 M' x
UCA0TXBUF = ((ADC10MEM*33)/1023);
+ o# w+ Q9 ~1 B$ p4 M0 Q L k = 10000;7 B7 o. b% b* S
while(k--);
5 S y/ l, U9 N; w, e) l$ c/ r* q' m' u/ \2 g3 d2 T
} |
|