EDA365电子工程师网
标题:
[代码] 单片机MSP430G2553 AD采样电压后经串口发送至PC
[打印本页]
作者:
85486952
时间:
2016-6-29 10:49
标题:
[代码] 单片机MSP430G2553 AD采样电压后经串口发送至PC
本程序是单片机MSP430G2553
0 Y s6 S1 I, i0 Q
P1.2----------------------------UCA0TXD 串口发送端
0 }! Z8 k' r* z+ b# j& c, w
P1.1----------------------------UCA0RXD 串口接受端
8 N4 V! _" S4 j; q& v5 u% W, b6 L
P1.0----------------------------红灯指示
N5 ^/ O* p5 `: O- g
P1.6----------------------------绿灯指示
/ m/ O4 k% S/ a- E
P1.5----------------------------模拟电压输入
; P( c+ a; h% V; B1 c$ A0 a
利用主辅时钟为1MHZ,串口波特率9600,
5 s$ \) H( f8 ? {& Y$ b& e
波特率:1000000/9600=104
8 I# P5 e N7 O, Z, x0 f
UCA0BR0 = 104; // 1MHz 9600
: O! n+ l0 I+ D i+ a4 q& a
UCA0BR1 = 0; // 1MHz 9600
6 f) \ R: a, J3 N# h' |3 e
! T' j' t- T# T! `6 ~3 ?. d$ K6 p
现象:
p: m2 T2 z- Q) Q" ^
功能(1)看到 P1.5口输入不同的电压经AD采样后发送
9 z2 n7 e1 c6 f/ K: F* S2 ?% c
功能(2)打开电脑串口调试助手,发送区有数据待发送时,在接受区将看到相应的数据。
/ k r$ y1 x q! a4 `5 Q% }
******************************************************************************/
/ _# x' U7 a# i5 C
#include <msp430g2553.h>
) J- [. ?; w' @/ @5 N: L$ b& H
float adc_voltage;
% k5 J/ x+ i$ O
unsigned int k;
* v6 }1 _7 N1 b4 G* Y. M
void sys_clk_config(void)
3 y1 ~* M/ z9 o7 S& x
{
) b4 j" J3 h4 Y( I
//ACLK = ; MCLK = SMCLK = DCOCLK = 1.1MHZ
4 a/ J8 ]7 I$ v! C
DCOCTL = 0;
$ E) i4 P. t4 N) w7 N: Q
BCSCTL1 = CALBC1_1MHZ;
* b( H% \$ x/ b l
DCOCTL = CALDCO_1MHZ;
5 {1 `# E, F3 Y
}
* v4 K1 Z; r% T' l' u
void uart_gpio_init(void)
% i2 a% P9 I9 \4 b# N% n
{
- f1 `% B1 X) C6 D. O
//P1SEL |= BIT1 + BIT2;
F$ j0 R1 _2 x9 e- @5 G/ S
//P1SEL2 |= BIT1 + BIT2;
* e: h9 X7 t; E! w* `. g
//P1DIR |= BIT2;
. A4 R; h3 D. ?0 z# I* p. ?
P1DIR = 0xFF; // All P1.x outputs
% U/ V" O4 w: g4 ]
P1OUT = 0; // All P1.x reset
, o* W" J' }; W b+ i9 |5 i: c# }
P1SEL |= BIT1 + BIT2 + BIT4; // P1.1 = RXD, P1.2=TXD
0 r3 _5 y: @- {" [- Y- D" k
P1SEL2 |= BIT1 + BIT2; // P1.4 = SMCLK, others GPIO
, W# z1 T) {3 Q z( W0 O& Q5 t) w
P2DIR = 0xFF; // All P2.x outputs
2 Q* w9 d2 C. j$ P- @
}
. d: W: R! w+ {% E- B$ I
void led_gpio_init(void)
; n u, ?7 {, i( i
{
3 u3 D( y- V& G6 D% x" Z7 l- f
}
3 [: M. \; G3 t5 I' Y
void ad_gpio_init(void)
r. o# f; g! X
{
9 X- L6 q4 J' h+ ?+ U# D" o. P
}
; d+ j3 m( R( @" g; x6 d
void uart_config(void)
3 T! G! g5 B; Y0 j% U& W' Y
{
/ n9 {9 _$ u* a7 m! @
UCA0CTL1 |= UCSSEL_2;
3 t6 G; ?& M K' o p- Q Y" C
5 Q* C8 M! h* y4 H6 h2 _6 z
UCA0BR0 = 104; // 1MHz 9600
" i9 g" |9 @8 f( [+ e
UCA0BR1 = 0; // 1MHz 9600
' o8 x- I/ [2 N& @ h
UCA0MCTL = UCBRS2 + UCBRS0;
3 |( u6 y4 z) @. M( _; [( b0 t
UCA0CTL1 &= ~UCSWRST;
/ O$ V" I1 q g
IE2 |= UCA0RXIE;
N7 K3 p9 B v9 _
6 _% ?6 V- k5 |- R
}
, H7 n" h- U K' u; U1 G
void ad_config(void)
6 v0 v5 o4 e) }2 z6 r
{
: x& y( l8 A) \. R& v# o
ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE;
/ p; l/ e2 @+ P
7 w& Z2 v" @' D* Y! z
ADC10CTL1 = INCH_5 + CONSEQ_2 ;
* w: n y! r* \% n+ N
ADC10AE0 |= 0x10;
, B/ b5 Y) }( m# n
' b0 R' h m" Z. [' H# p' `
ADC10CTL0 |= ENC + ADC10SC;
' y& o+ ]7 V: V2 J( i
9 o" L( c- o( r
}
( V/ x b9 ^3 v# A% e9 X9 a* V
void main()
3 e; T; q u A0 O$ y
{
; ]9 j, x/ q u
WDTCTL = WDTPW + WDTHOLD;
2 M! y, M9 B; k3 U) q+ u
2 [% W: H6 o z- n+ e( Q
//sys_clk_config();
7 c% j' D5 `2 j- O& e
; n, X% L( t9 U- u" |( i* o8 k
uart_gpio_init();
9 F* `2 h& s- h1 i( K; n6 A( v. `. l
1 Z* @# d2 B( ^
led_gpio_init();
' R& `2 c" S6 b) a
8 t1 r9 |1 ^9 x- L. \4 Y1 e+ u
ad_gpio_init();
2 W0 w- Z" T8 Z& G
" k7 v7 o0 B$ I" M5 k8 R
uart_config();
) |; L" X1 [9 c2 J4 E" }
) B8 @/ Z* D0 M& e; _2 k
ad_config();
/ s3 w1 G- W8 Y: `# e
__bis_SR_register(GIE);
; J# a) l* g4 L3 `! P/ G
while(1);
8 } Y6 d5 x+ U
: {* A* e' V* C) R) Q3 f/ Q
& v0 S0 ^/ |. b; _: Y% K
}
0 N+ O7 i0 t' x# E, O. [0 ~; Y
#pragma vector = USCIAB0RX_VECTOR
0 d G, I* r7 Z5 _
__interrupt void USCI0RX_ISR(void)
1 v: N1 W( l0 @; B+ V
{
' f8 I; G3 y5 t
while(!(IFG2 & UCA0TXIFG));
* y% C- B9 E7 H4 @; q$ P0 L) g
// UCA0TXBUF =0x33 ;// UCA0RXBUF;
3 F9 k% N) ?+ X9 S
}
* U5 {* d" [* `5 w9 y/ l, b0 [
#pragma vector = ADC10_VECTOR
2 Z' ~$ U% p3 r3 O6 k( W* O9 U; q
__interrupt void ADC10_A0_ISR(void)
; e4 g7 n' I6 u- r- I/ [! H
{
$ C- v6 f& p: J! B
- \4 S' c0 K: |0 k+ x+ S+ d" I! Z' S
while(!(IFG2 & UCA0TXIFG));
8 s. U1 Y" M$ P+ W
UCA0TXBUF = ((ADC10MEM*33)/1023);
" U( C/ D2 m0 V# Z/ w
k = 10000;
0 B( e2 m+ g/ P' d: Y
while(k--);
2 o% W5 }5 U3 H. [$ p9 Q
" e+ v, a6 c# h: f9 I" _, a* D$ J
}
作者:
zhoumi
时间:
2016-6-29 15:46
学习中,谢谢分享
$ u+ Q" c- f( u+ }0 r
作者:
wu68aq
时间:
2016-6-30 11:14
楼主辛苦了!
2 U9 z5 T6 s0 k! r
欢迎光临 EDA365电子工程师网 (http://bbs.elecnest.cn/)
Powered by Discuz! X3.2