|
EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
本程序是单片机MSP430G2553
7 v2 }4 A0 x! g- M' E" @ P1.2----------------------------UCA0TXD 串口发送端
9 S+ w6 c, v* R; W$ _ P1.1----------------------------UCA0RXD 串口接受端
- i3 Y1 G1 g+ S8 Z0 e P1.0----------------------------红灯指示
+ y5 t& Y6 U( B5 P P1.6----------------------------绿灯指示/ M/ R# N+ S5 g
P1.5----------------------------模拟电压输入
) g n' Q, N0 b利用主辅时钟为1MHZ,串口波特率9600,- c" k5 L; c) V8 ]& {& q9 y. G
波特率:1000000/9600=1042 m. \& {/ a2 Z
UCA0BR0 = 104; // 1MHz 9600
9 Q a) {8 |/ P. j UCA0BR1 = 0; // 1MHz 96004 o5 }' K3 {& T; _9 q8 w$ {
# S: Q8 D E4 ?. l现象:
: L% J3 k2 s5 J, x2 Z0 t 功能(1)看到 P1.5口输入不同的电压经AD采样后发送 , ]6 O( D- m) u0 }7 g' z9 ?8 q8 I+ {
功能(2)打开电脑串口调试助手,发送区有数据待发送时,在接受区将看到相应的数据。 E. c) D) K& G5 @, a
******************************************************************************/+ Z: Z+ J& @' y3 x* y
#include <msp430g2553.h>2 {. U# x& M; ]
float adc_voltage;
* ]: w. s) B- U, J8 ~2 S* Punsigned int k;" x' S$ |7 e. j7 w, c
void sys_clk_config(void)6 s, I! K& }2 R1 h
{: v4 C& [# ~) F7 ^: a S
//ACLK = ; MCLK = SMCLK = DCOCLK = 1.1MHZ
* v( |; z# Z. v7 u+ a$ j6 r DCOCTL = 0;: o. _7 q( W( Y( [1 t& ^$ B
BCSCTL1 = CALBC1_1MHZ;+ K3 _" K% O$ a l) \8 X" B' {% {
DCOCTL = CALDCO_1MHZ;
0 U, Z2 ?, J5 P$ i# R}
- P8 W% v/ b4 J# y4 X" X6 pvoid uart_gpio_init(void)
4 C' L* N' X( Z c4 R{
4 o, V7 v3 j5 @5 Y+ V8 `$ e b //P1SEL |= BIT1 + BIT2;. f* I6 `4 ]" [ t H9 p
//P1SEL2 |= BIT1 + BIT2;
" Z7 u1 V4 |2 `, V1 {+ y) X //P1DIR |= BIT2;. x) \3 y5 `: j! @4 R! K( h* Q# c
P1DIR = 0xFF; // All P1.x outputs7 K$ \6 N$ ^0 Q8 T/ Y" K/ W
P1OUT = 0; // All P1.x reset
+ Q. R. S% Y4 w0 Y2 x% e, G) S P1SEL |= BIT1 + BIT2 + BIT4; // P1.1 = RXD, P1.2=TXD
; s4 _' O) x* H* n- u. R P1SEL2 |= BIT1 + BIT2; // P1.4 = SMCLK, others GPIO
- r2 h6 m9 Z$ K" e. q P2DIR = 0xFF; // All P2.x outputs4 O5 P! w$ F8 y; f- L6 s y, u8 j
}
! [) A+ k2 P6 Y' P% L8 q, zvoid led_gpio_init(void)! `: q) a& T' ~; P
{
2 i; U M* O/ S- a/ s. a}
7 T/ N* F) x, d. t g4 w: Xvoid ad_gpio_init(void)' A( [7 i" E# F N% L2 @4 R( b% _
{8 d+ G# K ]& a6 N# J: P% d9 [
}
" L) z. D3 w {# \void uart_config(void)! ?1 ?" [8 b( L( K/ r: ?! I5 ^
{
8 k2 M- ?) i. u- J |; }* ^! vUCA0CTL1 |= UCSSEL_2;8 C8 A7 l7 t* y
6 x+ q" ]+ [1 i# U% N1 W. X UCA0BR0 = 104; // 1MHz 96008 `2 H. h }; j
UCA0BR1 = 0; // 1MHz 9600
+ B5 B' G7 D* y' s8 | UCA0MCTL = UCBRS2 + UCBRS0;
( O( C3 @6 C) M" | UCA0CTL1 &= ~UCSWRST;
/ c' A2 y! _9 p1 t+ p$ Q5 a IE2 |= UCA0RXIE;
" w* n" i+ X2 V; M; S+ K6 t7 \6 s . m" o1 @2 C6 f- H o% w
}1 b" s! x f% d* q
void ad_config(void)" c; G2 [' s& z' y3 y3 E
{
2 m/ |1 w/ a6 I ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE;- v* m+ z# F+ _ n
; M. ]( g1 I8 Z6 Q$ Z
ADC10CTL1 = INCH_5 + CONSEQ_2 ;8 Q q9 S6 x) B5 @$ \; r
ADC10AE0 |= 0x10;
" P: {% r4 d4 p. i1 ~7 z4 W* V* F7 t
1 K9 H& ? D+ C; h' v& u. b( @. j ADC10CTL0 |= ENC + ADC10SC;
^: l- H/ i2 v+ D. w$ S
- N% J$ |% t8 v% _/ i& p" V- _}
1 y$ t4 X' @: q4 G/ \1 V. mvoid main()( [6 m; W) [# G, ^+ Q
{0 _; J& k/ u. K) e' j+ X
WDTCTL = WDTPW + WDTHOLD;1 O" o% b' C! h$ ^
/ E, H& A& l* G* c8 Q8 g+ H //sys_clk_config();- ^# }. R/ @' t5 [ o
( h# l i) B: W4 a8 i7 N uart_gpio_init();. n% Z% U- ^* `( g2 N
0 {( g7 u e; @, V2 m' E q led_gpio_init();
3 q( I- F4 W- _* o
2 {* R0 q) }# B9 K& B ad_gpio_init();
! Q: z `8 @/ d. b5 J
- {! R9 n g9 B$ F5 S uart_config();2 ^, }! S$ L- s6 F( H/ |. \$ T* Z
6 Z0 Z2 F" } J) p5 | ad_config();3 h I* X4 j3 p% z, h
__bis_SR_register(GIE);
' X. B6 _0 r1 T: c while(1);1 D; |7 r# z s
) K# E7 i0 V& y2 o# S
* q, N4 F% \. _}
- \: n8 J) w9 n7 u#pragma vector = USCIAB0RX_VECTOR, ?5 |+ D' f; w, I
__interrupt void USCI0RX_ISR(void)) n* i$ w4 r- V0 Z
{- w: _1 W6 s# i9 N3 M7 @5 O$ g
while(!(IFG2 & UCA0TXIFG));) V/ s8 @$ ]* U3 x* W, \
// UCA0TXBUF =0x33 ;// UCA0RXBUF;
2 D1 s6 v3 g$ d- s# G# ]- T9 w! I}) F* m5 E8 o- i
#pragma vector = ADC10_VECTOR
7 ], b t& _7 C9 p4 [/ g__interrupt void ADC10_A0_ISR(void)
; {# I( D7 |6 m5 s{
* ^9 r' o6 O& Q( I' p$ c5 } f3 b$ t* ^: I# ]/ X m8 }+ W
while(!(IFG2 & UCA0TXIFG));
# H$ K& u$ N5 w. ?5 @! o UCA0TXBUF = ((ADC10MEM*33)/1023);0 H; T b+ j& q. s6 B" N
k = 10000;$ }3 }2 ]( ^' @" N) ]+ Z$ A5 L
while(k--);6 }; t0 o6 o5 |; X: i a
8 P- l5 A. G4 p( Z7 u
} |
|