|
楼主
查看: 4561回复: 1
发表于 2019-7-10 10:40:01
只看该作者
串口不知道为啥只能在中断中发送,不能在主函数中发送。
以下为串口代码:
#include <msp430.h>
unsigned char str1[]="Hello world";
void WatchDog_close();
void UART0_Init();
void Send_byte(unsigned char data);
void Send_string(unsigned char *s);
void Send_byte(unsigned char data)
{
while((UCA0IFG&UCTXIFG) == 0);
UCA0TXBUF = data;
// UCA0IE &= ~UCTXIE;
}
void Send_string(unsigned char *s)
{
//while(*str != '\0')
while(*s)
Send_byte(*s++);
}
void WatchDog_close()
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
}
void UART0_Init()
{
P3SEL |= BIT3+BIT4; // P3.3,4 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 9; // 1MHz 115200 (see User's Guide)
UCA0BR1 = 0; // 1MHz 115200
UCA0MCTL |= UCBRS_1 + UCBRF_0; // Modulation UCBRSx=1, UCBRFx=0
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
__no_operation(); // For debugger
}
void delay(unsigned int num)
{
while(num--);
}
int main(void)
{
WatchDog_close();
UART0_Init();
_EINT();
while(1)
{
// Send_string("Welcome to use MSP420 MCU!");
UCA0TXBUF = 0xFF;
UCA0TXBUF = 'A';
}
}
// Echo back RXed character, confirm TX buffer is ready first
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCI_A0_VECTOR))) USCI_A0_ISR (void)
#else
#error Compiler not supported!
#endif
{
unsigned char buf = 0;
buf = UCA0RXBUF;
Send_string(str1);
Send_string("Welcome to use MSP420 MCU!");
// if(buf == 0x10)
// Send_byte('T');
// else
// Send_byte('F');
// switch(__even_in_range(UCA0IV,4))
// {
// case 0:break; // Vector 0 - no interrupt
// case 2: // Vector 2 - RXIFG
// while (!(UCA0IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
// UCA0TXBUF = UCA0RXBUF; // TX -> RXed character
// break;
// case 4:break; // Vector 4 - TXIFG
// default: break;
// }
}
以下为定时器代码:
以下为中断器代码:
产生的信号的频率不应该是 1*10^-6S*5000*2吗?
|
|