<html xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8"><meta name="generator" content="pdf2htmlEX"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><link rel="stylesheet" href="https://csdnimg.cn/release/download_crawler_static/css/base.min.css"><link rel="stylesheet" href="https://csdnimg.cn/release/download_crawler_static/css/fancy.min.css"><link rel="stylesheet" href="https://csdnimg.cn/release/download_crawler_static/8555533/raw.css"><script src="https://csdnimg.cn/release/download_crawler_static/js/compatibility.min.js"></script><script src="https://csdnimg.cn/release/download_crawler_static/js/pdf2htmlEX.min.js"></script><script>try{pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({});}catch(e){}</script><title></title></head><body><div id="sidebar" style="display: none"><div id="outline"></div></div><div id="pf1" class="pf w0 h0" data-page-no="1"><div class="pc pc1 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://csdnimg.cn/release/download_crawler_static/8555533/bg1.jpg"><div class="c x0 y1 w2 h2"><div class="t m0 x1 h3 y2 ff1 fs0 fc0 sc0 ls0 ws0">msp430g2553<span class="_ _0"> </span><span class="ff2">与串口通信的驱动程序</span></div><div class="t m0 x1 h4 y3 ff1 fs0 fc0 sc0 ls0 ws0">#include "uart.h"</div><div class="t m0 x1 h4 y4 ff1 fs0 fc0 sc0 ls0 ws0">#include <msp430g2553.h></div><div class="t m0 x1 h4 y5 ff1 fs0 fc0 sc0 ls0 ws0">#include "typedef.h"</div><div class="t m0 x1 h3 y6 ff1 fs0 fc0 sc0 ls0 ws0">rece_data uart_buf; //<span class="ff2">串口缓冲区</span></div><div class="t m0 x1 h4 y7 ff1 fs0 fc0 sc0 ls0 ws0">void init_uart_buf(void)</div><div class="t m0 x1 h4 y8 ff1 fs0 fc0 sc0 ls0 ws0">{</div><div class="t m0 x1 h4 y9 ff1 fs0 fc0 sc0 ls0 ws0">&&&&uart_buf.head = 0;</div><div class="t m0 x1 h4 ya ff1 fs0 fc0 sc0 ls0 ws0">&&&&uart_buf.tail = uart_buf.head;</div><div class="t m0 x1 h4 yb ff1 fs0 fc0 sc0 ls0 ws0">}</div><div class="t m0 x1 h3 yc ff1 fs0 fc0 sc0 ls0 ws0">//<span class="ff2">获取串口数据</span></div><div class="t m0 x1 h4 yd ff1 fs0 fc0 sc0 ls0 ws0">u8 get_uart_data(u8* data)</div><div class="t m0 x1 h4 ye ff1 fs0 fc0 sc0 ls0 ws0">{</div><div class="t m0 x1 h4 yf ff1 fs0 fc0 sc0 ls0 ws0">&&&&if(uart_buf.tail == uart_buf.head)</div><div class="t m0 x1 h4 y10 ff1 fs0 fc0 sc0 ls0 ws0">&&&&{</div><div class="t m0 x1 h4 y11 ff1 fs0 fc0 sc0 ls0 ws0">&&&&&&&&return 0;</div><div class="t m0 x1 h4 y12 ff1 fs0 fc0 sc0 ls0 ws0">&&&&}</div><div class="t m0 x1 h4 y13 ff1 fs0 fc0 sc0 ls0 ws0">&&&&*data = uart_buf.buf[uart_buf.head];</div><div class="t m0 x1 h4 y14 ff1 fs0 fc0 sc0 ls0 ws0">&&&&uart_buf.head = (uart_buf.head + 1) % BUF_SIZE;</div><div class="t m0 x1 h4 y15 ff1 fs0 fc0 sc0 ls0 ws0">&&&&return 1;</div><div class="t m0 x1 h4 y16 ff1 fs0 fc0 sc0 ls0 ws0">}</div><div class="t m0 x1 h3 y17 ff1 fs0 fc0 sc0 ls0 ws0">//<span class="ff2">保存串口数据</span></div><div class="t m0 x1 h4 y18 ff1 fs0 fc0 sc0 ls0 ws0">void save_uart_data(u8 data)</div><div class="t m0 x1 h4 y19 ff1 fs0 fc0 sc0 ls0 ws0">{</div><div class="t m0 x1 h4 y1a ff1 fs0 fc0 sc0 ls0 ws0">&&&&uart_buf.buf[uart_buf.tail] = data;</div><div class="t m0 x1 h4 y1b ff1 fs0 fc0 sc0 ls0 ws0">&&&&uart_buf.tail = (uart_buf.tail + 1) % BUF_SIZE;</div><div class="t m0 x1 h4 y1c ff1 fs0 fc0 sc0 ls0 ws0">}</div><div class="t m0 x1 h3 y1d ff1 fs0 fc0 sc0 ls0 ws0">//<span class="ff2">串口初始化</span></div><div class="t m0 x1 h4 y1e ff1 fs0 fc0 sc0 ls0 ws0">void uart_Init(void)</div><div class="t m0 x1 h4 y1f ff1 fs0 fc0 sc0 ls0 ws0">{</div><div class="t m0 x1 h4 y20 ff1 fs0 fc0 sc0 ls0 ws0">&&&&WDTCTL<span class="_ _1"></span> = WD<span class="_ _1"></span>TPW + <span class="_ _2"></span>WDTHOLD;&&&&&&&&&&&&&&&&&// Stop WDT</div><div class="t m0 x1 h4 y21 ff1 fs0 fc0 sc0 ls0 ws0">&&&&BCSCTL1 = CALBC1_1MHZ;&&&&&&&&&&&&&&&&&&&&// Set DCO</div><div class="t m0 x1 h4 y22 ff1 fs0 fc0 sc0 ls0 ws0">DCOCTL=CALDCO_1MHZ;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&</div><div class="t m0 x1 h4 y23 ff1 fs0 fc0 sc0 ls0 ws0">&&&&P1SEL<span class="_ _1"></span> = BIT1 + BIT2 ;&&&&&&&&&&&&&&&&&&&&&// P1.1 = RXD, P1.2=TXD</div><div class="t m0 x1 h4 y24 ff1 fs0 fc0 sc0 ls0 ws0">&&&&P1SEL2 = BIT1 + BIT2 ;&&&&&&&&&&&&&&&&&&&&&// P1.1 = RXD, P1.2=TXD</div><div class="t m0 x1 h4 y25 ff1 fs0 fc0 sc0 ls0 ws0">&&&</div><div class="t m0 x1 h4 y26 ff1 fs0 fc0 sc0 ls0 ws0">&&&&UCA0CTL1 |= UCSSEL_2;&&&&&&&&&&&&&&&&&&&&&// SMCLK</div><div class="t m0 x1 h4 y27 ff1 fs0 fc0 sc0 ls0 ws0">&&&&UCA0BR0 = 104;&&&&&&&&&&&&&&&&&&&&&&&&&&&&// 1MHz 9600</div></div></div><div class="pi" data-data='{"ctm":[1.611850,0.000000,0.000000,1.611850,0.000000,0.000000]}'></div></div></body></html>