TI中文支持网
TI专业的中文技术问题搜集分享网站

MSP430FR2433: MSP430fr243串口收发配置

Part Number:MSP430FR2433

#include "include.h"

void Init_GPIO();

int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
// Configure GPIO
Init_GPIO();
PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode
// to activate 1previously configured port settings
// Configure one FRAM waitstate as required by the device datasheet for MCLK
// operation beyond 8MHz _before_ configuring the clock system.
FRCTL0 = FRCTLPW | NWAITS_1;

__bis_SR_register(SCG0); // disable FLL
CSCTL3 |= SELREF__REFOCLK; // Set REFO as FLL reference source
CSCTL0 = 0; // clear DCO and MOD registers
CSCTL1 &= ~(DCORSEL_7); // Clear DCO frequency select bits first
CSCTL1 |= DCORSEL_5; // Set DCO = 16MHz
CSCTL2 = FLLD_0 + 487; // DCOCLKDIV = 16MHz
__delay_cycles(3);
__bic_SR_register(SCG0); // enable FLL
while(CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1)); // FLL locked

CSCTL4 = SELMS__DCOCLKDIV | SELA__REFOCLK; // set default REFO(~32768Hz) as ACLK source, ACLK = 32768Hz
// default DCOCLKDIV as MCLK and SMCLK source

// Configure UART pins
P2SEL0 |= BIT5 | BIT6; // set 2-UART pin as second function

// Configure UART
UCA1CTLW0 |= UCSWRST;
UCA1CTLW0 |= UCSSEL__SMCLK;

// Baud Rate calculation
// 16000000/(16*9600) = 104.167
// Fractional portion = 0.167
// User's Guide Table 14-4: UCBRSx = 0x11
// UCBRFx = int ( (104.167-104)*16) = 2
UCA1BR0 = 104; // 16000000/16/9600
UCA1BR1 = 0x00;
UCA1MCTLW = 0x1100 | UCOS16 | UCBRF_2;

UCA1CTLW0 &= ~UCSWRST; // Initialize eUSCI
UCA1IE |= UCRXIE; // Enable USCI_A0 RX interrupt

__bis_SR_register(LPM3_bits|GIE); // Enter LPM3, interrupts enabled
__no_operation(); // For debugger
}

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCI_A1_VECTOR))) USCI_A1_ISR (void)
#else
#error Compiler not supported!
#endif
{
switch(__even_in_range(UCA1IV,USCI_UART_UCTXCPTIFG))
{
case USCI_NONE: break;
case USCI_UART_UCRXIFG:
while(!(UCA1IFG&UCTXIFG));
UCA1TXBUF = UCA1RXBUF;
__no_operation();
break;
case USCI_UART_UCTXIFG: break;
case USCI_UART_UCSTTIFG: break;
case USCI_UART_UCTXCPTIFG: break;
default: break;
}
}

void Init_GPIO()
{
P1DIR = 0xFF; P2DIR = 0xFF; P3DIR = 0xFF;
P1REN = 0xFF; P2REN = 0xFF; P3REN = 0xFF;
P1OUT = 0x00; P2OUT = 0x00; P3OUT = 0x00;
}

我参考了论坛里所给出的代码,发现使用PC的串口无法与430通信

Susan Yang:

user6518570 说:发现使用PC的串口无法与430通信

能否请您详细说一下?

您可以使用开发板上的虚拟串口来和PC上的串口助手来通信的。

,

user6518570:

如何使用开发板的虚拟串口进行通信?我使用的运行环境是IARFORMSP430 7.10.1,我发现在配置好串口以后,软件内使用串口发送函数向PC机发送0x10,发现PC串口助手并无打印信息。初学430,还请多多指教。

,

user6518570:

这是我的原理图

,

Susan Yang:

建议您先看一下例程

https://dev.ti.com/tirex/explore/node?node=AI9..X0EukhFuJAwALofbA__IOGqZri__LATEST 

,

user6518570:

我也查看了例程 并将例程下载到我的板子上 但是串口并无显示 这个例程移植在我新创建的工作区内是可以运行的吗 我想先看到是怎样的现象。

,

Susan Yang:

// Description: This demo echoes back characters received via a PC serial port.// SMCLK/ DCO is used as a clock source and the device is put in LPM3// The auto-clock enable feature is used by the eUSCI and SMCLK is turned off// when the UART is idle and turned on when a receive edge is detected.// Note that level shifter hardware is needed to shift between RS232 and MSP// voltage levels.

这是一个字符回显的例程。

user6518570 说: 这个例程移植在我新创建的工作区内是可以运行的吗

配合开发板是可以运行的。自己板子的话,应该需要修改串口的IO口。例程中使用的是 P1.4/UCA0TXD和P1.5

,

user6518570:

这就是很奇怪的地方 我按照原理图更改了我的io 并将相关UCA0配置 更改为了UCA1,无论我发送什么串口没有任何反应  这是为什么呢

,

Susan Yang:

例程是回显,您需要在串口助手输入一个字符,如 1

而后点击CCS内的运行按键,暂停后,串口助手就会出现 1

如下图

,

user6518570:

我使用的是IAR进行开发 当然这个没关系 程序也正常下载成功,检查了几遍,实在是看不出问题,现在是有打印信息,但无论发什么都打印的是00

,

Susan Yang:

请问您使用的是什么程序?我提到的例程还是您自己的程序?

,

user6518570:

完全是例程的程序我只改动了IO口与对应的UART1的相关配置与中断

,

Susan Yang:

就是该例程?

https://dev.ti.com/tirex/explore/node?node=AI9..X0EukhFuJAwALofbA__IOGqZri__LATEST  

现在是不能正常回显字符?而是发什么都打印的是00?

,

user6518570:

对的 就是该例程,发什么都是打印的00

,

Susan Yang:

user6518570 说:发什么都是打印的00

是电脑键盘上随便按下什么,都会打印00?

那请您给出全部代码,我拿开发板看一下

,

user6518570:

#include <msp430.h>void Software_Trim(); // Software Trim to get the best DCOFTRIM value#define MCLK_FREQ_MHZ 8 // MCLK = 8MHz

void Init_GPIO();

int main(void){ WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

// Configure GPIO Init_GPIO(); PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode // to activate 1previously configured port settings

__bis_SR_register(SCG0); // disable FLL CSCTL3 |= SELREF__REFOCLK; // Set REFO as FLL reference source CSCTL1 = DCOFTRIMEN | DCOFTRIM0 | DCOFTRIM1 | DCORSEL_3;// DCOFTRIM=3, DCO Range = 8MHz CSCTL2 = FLLD_0 + 243; // DCODIV = 8MHz __delay_cycles(3); __bic_SR_register(SCG0); // enable FLL Software_Trim(); // Software Trim to get the best DCOFTRIM value

CSCTL4 = SELMS__DCOCLKDIV | SELA__REFOCLK; // set default REFO(~32768Hz) as ACLK source, ACLK = 32768Hz // default DCODIV as MCLK and SMCLK source

// Configure UART pins P2SEL0 |= BIT5 | BIT6; // set 2-UART pin as second function

// Configure UART UCA1CTLW1 |= UCSWRST; UCA1CTLW1 |= UCSSEL__SMCLK; // Baud Rate calculation // 8000000/(16*9600) = 52.083 // Fractional portion = 0.083 // User's Guide Table 14-4: UCBRSx = 0x49 // UCBRFx = int ( (52.083-52)*16) = 1 UCA1BR0 = 52; // 8000000/16/9600 UCA1BR1 = 0x00; UCA1MCTLW = 0x4900 | UCOS16 | UCBRF_1;

UCA1CTLW1 &= ~UCSWRST; // Initialize eUSCI UCA1IE |= UCRXIE; // Enable USCI_A0 RX interrupt

__bis_SR_register(LPM3_bits|GIE); // Enter LPM3, interrupts enabled __no_operation(); // For debugger}

#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)#pragma vector=USCI_A1_VECTOR__interrupt void USCI_A1_ISR(void)#elif defined(__GNUC__)void __attribute__ ((interrupt(USCI_A1_VECTOR))) USCI_A1_ISR (void)#else#error Compiler not supported!#endif{ switch(__even_in_range(UCA1IV,USCI_UART_UCTXCPTIFG)) { case USCI_NONE: break; case USCI_UART_UCRXIFG: while(!(UCA1IFG&UCTXIFG)); UCA1TXBUF = UCA1RXBUF; __no_operation(); break; case USCI_UART_UCTXIFG: break; case USCI_UART_UCSTTIFG: break; case USCI_UART_UCTXCPTIFG: break; default: break; }}

void Init_GPIO(){ P1DIR = 0xFF; P2DIR = 0xFF; P3DIR = 0xFF; P1REN = 0xFF; P2REN = 0xFF; P3REN = 0xFF; P1OUT = 0x00; P2OUT = 0x00; P3OUT = 0x00;}

void Software_Trim(){ unsigned int oldDcoTap = 0xffff; unsigned int newDcoTap = 0xffff; unsigned int newDcoDelta = 0xffff; unsigned int bestDcoDelta = 0xffff; unsigned int csCtl0Copy = 0; unsigned int csCtl1Copy = 0; unsigned int csCtl0Read = 0; unsigned int csCtl1Read = 0; unsigned int dcoFreqTrim = 3; unsigned char endLoop = 0;

do { CSCTL0 = 0x100; // DCO Tap = 256 do { CSCTL7 &= ~DCOFFG; // Clear DCO fault flag }while (CSCTL7 & DCOFFG); // Test DCO fault flag

__delay_cycles((unsigned int)3000 * MCLK_FREQ_MHZ);// Wait FLL lock status (FLLUNLOCK) to be stable // Suggest to wait 24 cycles of divided FLL reference clock while((CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1)) && ((CSCTL7 & DCOFFG) == 0));

csCtl0Read = CSCTL0; // Read CSCTL0 csCtl1Read = CSCTL1; // Read CSCTL1

oldDcoTap = newDcoTap; // Record DCOTAP value of last time newDcoTap = csCtl0Read & 0x01ff; // Get DCOTAP value of this time dcoFreqTrim = (csCtl1Read & 0x0070)>>4;// Get DCOFTRIM value

if(newDcoTap < 256) // DCOTAP < 256 { newDcoDelta = 256 – newDcoTap; // Delta value between DCPTAP and 256 if((oldDcoTap != 0xffff) && (oldDcoTap >= 256)) // DCOTAP cross 256 endLoop = 1; // Stop while loop else { dcoFreqTrim–; CSCTL1 = (csCtl1Read & (~(DCOFTRIM0+DCOFTRIM1+DCOFTRIM2))) | (dcoFreqTrim<<4); } } else // DCOTAP >= 256 { newDcoDelta = newDcoTap – 256; // Delta value between DCPTAP and 256 if(oldDcoTap < 256) // DCOTAP cross 256 endLoop = 1; // Stop while loop else { dcoFreqTrim++; CSCTL1 = (csCtl1Read & (~(DCOFTRIM0+DCOFTRIM1+DCOFTRIM2))) | (dcoFreqTrim<<4); } }

if(newDcoDelta < bestDcoDelta) // Record DCOTAP closest to 256 { csCtl0Copy = csCtl0Read; csCtl1Copy = csCtl1Read; bestDcoDelta = newDcoDelta; }

}while(endLoop == 0); // Poll until endLoop == 1

CSCTL0 = csCtl0Copy; // Reload locked DCOTAP CSCTL1 = csCtl1Copy; // Reload locked DCOFTRIM while(CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1)); // Poll until FLL is locked}

,

user6518570:

我已调试完毕,正常了

,

user6518570:

Error[e46]: Undefined external "__close" referred in ?fclose ( D:\IAR_FOR_MSP\430\lib\dlib\dl430xlsff.r43 )

Error[e46]: Undefined external "remove" referred in ?fclose ( D:\IAR_FOR_MSP\430\lib\dlib\dl430xlsff.r43 )

Error[e46]: Undefined external "__write" referred in ?fflush ( D:\IAR_FOR_MSP\430\lib\dlib\dl430xlsff.r43 )

Error[e46]: Undefined external "__lseek" referred in ?xfspos ( D:\IAR_FOR_MSP\430\lib\dlib\dl430xlsff.r43 )

請問這些錯誤是缺少哪個頭文件嗎

,

Susan Yang:

user6518570 说:我已调试完毕,正常了

很高兴您能解决问题

,

Susan Yang:

请问是编译例程还是自己程序?具体情况是怎样?

赞(0)
未经允许不得转载:TI中文支持网 » MSP430FR2433: MSP430fr243串口收发配置
分享到: 更多 (0)