主题中讨论的其他器件:MSP430G2533
工具与软件:
#include <msp430.h>
#include <intrinsics.h>
#if defined(__IAR_SYSTEMS_ICC__)
int16_t __low_level_init(void) {
// Stop WDT (Watch Dog Timer)
WDTCTL = WDTPW + WDTHOLD;
return(1);
}
#endif
#define LED BIT0
#define RXD BIT1
#define TXD BIT2
void initGPIO() {
P1OUT &= ~LED; // Clear LED output
P1DIR |= LED; // Set P1.0 as output
P1SEL = BIT1 + BIT2; // P1.1 = RX, P1.2 = TX
P1SEL2 = BIT1 + BIT2; // P1.1 = RX, P1.2 = TX
}
void initUART() {
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine
}
void initWatchdog() {
WDTCTL = WDT_MRST_32; // Watchdog timer +/-32ms with SMCLK/32768
// IE1 |= WDTIE; // Enable WDT interrupt
}
void initClock() {
if (CALBC1_1MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
//DCOCTL = 0; // Select lowest DCOx and MODx settings
//BCSCTL1 = CALBC1_1MHZ; // Set DCO
//DCOCTL = CALDCO_1MHZ;
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ;
BCSCTL2 &= ~(DIVS_3); // SMCLK = DCO = 1MHz
}// Clock calbirated at 1 MHz
void sendUART(char *text) {
unsigned int i = 0;
while(text[i] != '\0') {
while (!(IFG2 & UCA0TXIFG)); // Wait for TX buffer to be ready
UCA0TXBUF = text[i];
i++;
}
}
// Watchdog Timer interrupt service routine
int timerA_counter=0;
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A(void) {
timerA_counter++;
}
void initTimerA(void) {
TACTL = TASSEL_2 + MC_1 + ID_3; // SMCLK, up mode, input divider = 8
TACCR0 = (unsigned int)(125000 - 1); // Count up to 125000 (1-second interval with 1 MHz SMCLK/8)
TACCTL0 = CCIE; // Enable interrupt for CCR0
}
int main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
initClock(); // Setup clock
initGPIO(); // Initialize GPIO
initUART(); // Initialize UART
initTimerA(); // Initialize Timer A
initWatchdog(); // Initialize watchdog timer
// __bis_SR_register(GIE); // Enable global interrupts
_enable_interrupts();
char out_string[] = "Hello World\n";
// sendUART(out_string);
while (1) {
__no_operation(); // For debugger
if (timerA_counter > 1)
{
P1OUT ^= LED; // Toggle LED
sendUART(out_string);
timerA_counter = 0;
}
WDTCTL = WDTPW | WDTCNTCL; // Kick the watchdog
}
}
我编写了一个我可以想象的最简单的代码、通过 HW UART 定期打印"Hello world"。
我预计波特率为9600bps、但 TeraTerm 仅在配置为2400bps 时才显示数据、但 我不知道为什么。
有什么提示吗?
我在理论上、因此 firs #ifdef 不适用。