2019-03-24 09:08发布
one55 发表于 2016-7-22 14:44 这是我写的uart发送与接收子函数,但不知道为什么一直不能用 //发送字符 void USCIA0_sendchar(unsigne ...
//******************************************************************************
// MSP430G2xx3 Demo - USCI_A0, Ultra-Low Pwr UART 9600 String, 32kHz ACLK
//
// Description: This program demonstrates a full-duplex 9600-baud UART using
// USCI_A0 and a 32kHz crystal. The program will wait in LPM3, and will
// respond to a received 'u' character using 8N1 protocol. The response will
// be the string 'Hello World'.
// ACLK = BRCLK = LFXT1 = 32768Hz, MCLK = SMCLK = DCO ~1.2MHz
// Baud rate divider with 32768Hz XTAL @9600 = 32768Hz/9600 = 3.41
//* An external watch crystal is required on XIN XOUT for ACLK *//
// MSP430G2xx3
// -----------------
// /|| XIN|-
// | | | 32kHz
// --|RST XOUT|-
// | |
// | P1.2/UCA0TXD|------------>
// | | 9600 - 8N1
// | P1.1/UCA0RXD|<------------
// D. Dang
// Texas Instruments Inc.
// February 2011
// Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10
#include <msp430.h>
const char string1[] = { "Hello World " };
unsigned int i;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR = 0xFF; // All P1.x outputs
P1OUT = 0; // All P1.x reset
P2DIR = 0xFF; // All P2.x outputs
P2OUT = 0; // All P2.x reset
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P3DIR = 0xFF; // All P3.x outputs
P3OUT = 0; // All P3.x reset
UCA0CTL1 |= UCSSEL_1; // CLK = ACLK
UCA0BR0 = 0x03; // 32kHz/9600 = 3.41
UCA0BR1 = 0x00; //
UCA0MCTL = UCBRS1 + UCBRS0; // Modulation UCBRSx = 3
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
__bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/ int until Byte RXed
}
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCI0TX_ISR(void)
UCA0TXBUF = string1[i++]; // TX next character
if (i == sizeof string1 - 1) // TX over?
IE2 &= ~UCA0TXIE; // Disable USCI_A0 TX interrupt
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
if (UCA0RXBUF == 'u') // 'u' received?
i = 0;
IE2 |= UCA0TXIE; // Enable USCI_A0 TX interrupt
UCA0TXBUF = string1[i++];
最多设置5个标签!
发送中断产生后一搬是判断你的发送数组(缓冲)里有没有待发的数据如果有继续发送
接收中断产生后一搬是读取一个字节追加到你的接收数组(缓冲)中当满足某一条件后对接收数组里的数据做相应的处理
这是MSP430G2553的UART例程,你可以参考下
//******************************************************************************
// MSP430G2xx3 Demo - USCI_A0, Ultra-Low Pwr UART 9600 String, 32kHz ACLK
//
// Description: This program demonstrates a full-duplex 9600-baud UART using
// USCI_A0 and a 32kHz crystal. The program will wait in LPM3, and will
// respond to a received 'u' character using 8N1 protocol. The response will
// be the string 'Hello World'.
// ACLK = BRCLK = LFXT1 = 32768Hz, MCLK = SMCLK = DCO ~1.2MHz
// Baud rate divider with 32768Hz XTAL @9600 = 32768Hz/9600 = 3.41
//* An external watch crystal is required on XIN XOUT for ACLK *//
//
// MSP430G2xx3
// -----------------
// /|| XIN|-
// | | | 32kHz
// --|RST XOUT|-
// | |
// | P1.2/UCA0TXD|------------>
// | | 9600 - 8N1
// | P1.1/UCA0RXD|<------------
//
// D. Dang
// Texas Instruments Inc.
// February 2011
// Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10
//******************************************************************************
#include <msp430.h>
const char string1[] = { "Hello World " };
unsigned int i;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR = 0xFF; // All P1.x outputs
P1OUT = 0; // All P1.x reset
P2DIR = 0xFF; // All P2.x outputs
P2OUT = 0; // All P2.x reset
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P3DIR = 0xFF; // All P3.x outputs
P3OUT = 0; // All P3.x reset
UCA0CTL1 |= UCSSEL_1; // CLK = ACLK
UCA0BR0 = 0x03; // 32kHz/9600 = 3.41
UCA0BR1 = 0x00; //
UCA0MCTL = UCBRS1 + UCBRS0; // Modulation UCBRSx = 3
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
__bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/ int until Byte RXed
}
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCI0TX_ISR(void)
{
UCA0TXBUF = string1[i++]; // TX next character
if (i == sizeof string1 - 1) // TX over?
IE2 &= ~UCA0TXIE; // Disable USCI_A0 TX interrupt
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
if (UCA0RXBUF == 'u') // 'u' received?
{
i = 0;
IE2 |= UCA0TXIE; // Enable USCI_A0 TX interrupt
UCA0TXBUF = string1[i++];
}
}
一周热门 更多>