下面是TI官方的示例程序:
//*****************************************************************************
// MSP-FET430P140 Demo – USART0, Ultra-Low Pwr UART 19200 Echo ISR, 32kHz ACLK
//
// Description: Echo a received character, USART0 RX ISR at high-speed used
// with ultra-low power techniques. Normal operation in LPM3, Set_DCO
// subroutine needs to be called to configure DCO that is used for UART baud
// generation. On valid RX character, character echoed back. Use start-bit
// edge detect – URXSE – to automatically (re)enable DCO and trigger ISR. ISR
// must make sure DCO clock source remains enabled for the UART to receive
// full character.
// Software needs to make sure a character has been completely TX'ed, or RX'ed
// before entering LPM3, which disables DCO required for the USART baud rate
// generator. In the example, TX'ing is checked using the TXEPT bit directly.
// RX'ing is checked using the SSEL0 clock select bit as a flag. This is
// possible because UCLK0 = SMCLK when either both SSEL1 and SSEL0 or just
// SSEL1 = 1. In the example, when SSEL1 = SSEL0 = 1 there is no RX'ing, and
// LPM3 is allowed. When SSEL 1 = 1 and SSEL0 = 0 SMCLK is selected, but
// RX'ing is active and the DCO is required, thus LPM3 is not allowed.
// ACLK = LFXT1/8 = 32768/8, MCLK = SMCLK = UCLK0 = DCOCLK = 1048576
// Baud rate divider with 1048576hz= 1048576Hz/19200 ~ 55 (0036h)
// //* An external 32kHz watch crystal on XIN XOUT is required for ACLK *////
//
// MSP430F149
// —————–
// /|\| XIN|-
// | | | 32768Hz
// –|RST XOUT|-
// | |
// | P3.4|————>
// | | 19200 – 8N1
// | P3.5|<————
//
//
// M. Buccini
// Texas Instruments Inc.
// Feb 2005
// Built with IAR Embedded Workbench Version: 3.21A
//*****************************************************************************
#include <msp430x14x.h>
void Set_DCO (void);
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
BCSCTL1 |= DIVA_3; // ACLK= LFXT1CLK/8
Set_DCO(); // Set DCO
ME1 |= UTXE0 + URXE0; // Enabled USART0 TXD/RXD
UCTL0 |= CHAR; // 8-bit character, SWRST = 1
UTCTL0 |= SSEL1 + SSEL0 + URXSE; // UCLK = SMCLK, start edge detect
UBR00 = 0x36; // 19200 from 1Mhz
UBR10 = 0x00;
UMCTL0 = 0x6B; // Modulation
UCTL0 &= ~SWRST; // Initialize USART state machine
IE1 |= URXIE0; // Enable USART0 RX interrupt
for (;;)
{
while (!(UTCTL0 & TXEPT)); // Confirm no TXing before –> LPM3
_DINT(); // Disable interrupts for flag test
_NOP();
if (!(UTCTL0 & SSEL0))
_BIS_SR(LPM0_bits + GIE); // RX'ing char, enter LPM0, int's active
else
_BIS_SR(LPM3_bits + GIE); // Enter LPM3, int's active
}
}
#pragma vector=UART0RX_VECTOR
__interrupt void usart0_rx (void)
{
if ((IFG1 & URXIFG0)) // Test URXIFG0
{
while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready?
TXBUF0 = RXBUF0; // RXBUF0 to TXBUF0
_BIC_SR_IRQ(LPM3_bits); // Exit LPM3 after reti
UTCTL0 |= SSEL0; // SSEL0 = 1, no RX activity
}
else // Start edge
{
UTCTL0 &= ~URXSE; // Clear URXS signal
UTCTL0 |= URXSE; // Re-enable edge detect
_BIC_SR_IRQ(SCG1 + SCG0); // DCO reamins on after reti
UTCTL0 &= ~SSEL0; // SSEL0= 0, RX activity
}
}
void Set_DCO (void)
{
……..省略
}
我的问题:
从说明来看,改代码工作在LPM3与AM两种模式下,串口时钟使用的是UCLK = SMCLK, 在LPM3模式下SMCLK是关闭的,那么此时串口是怎样工作的?不需要时钟吗?
望解答!
tian liu:
没人解答,这个问题太简单吗?
Triton Zhang:
例程代码采用SMCLK 作为UART的时钟源,在LMP3的模式下SMCL是关闭的,所以在LPM3模式下UART TX是不能工作的.
如果UTCTL0 寄存器的SSEL1和SSEL0都设置1,那么在LMP3模式下会检测起始位的下降沿,从而触发UART中断,在中断函数欧诺个处理玩接收数据后,代码退出LPM3模式.
例程中的注释写得很清楚
UTCTL0 |= SSEL1 + SSEL0 + URXSE; // UCLK = SMCLK, start edge detect
以及
RX'ing is checked using the SSEL0 clock select bit as a flag. This is // possible because UCLK0 = SMCLK when either both SSEL1 and SSEL0 or just // SSEL1 = 1. In the example, when SSEL1 = SSEL0 = 1 there is no RX'ing, and // LPM3 is allowed. When SSEL 1 = 1 and SSEL0 = 0 SMCLK is selected, but // RX'ing is active and the DCO is required, thus LPM3 is not allowed.
tian liu:
回复 Triton Zhang:
感谢楼上的回答,但还是有些不理解的地方,如下:
__interrupt void usart0_rx (void){ if ((IFG1 & URXIFG0)) // Test URXIFG0 { while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready? TXBUF0 = RXBUF0; // RXBUF0 to TXBUF0 _BIC_SR_IRQ(LPM3_bits); // Exit LPM3 after reti UTCTL0 |= SSEL0; // SSEL0 = 1, no RX activity } else // Start edge { UTCTL0 &= ~URXSE; // Clear URXS signal UTCTL0 |= URXSE; // Re-enable edge detect
(1) _BIC_SR_IRQ(SCG1 + SCG0); // DCO reamins on after reti
(2) UTCTL0 &= ~SSEL0; // SSEL0= 0, RX activity }}
1. 代码中设置了URXSE,来检测起始位的下降沿,这个可以理解;
2. 对于上面代码段中用蓝色标记的语句(1)还是不明白,从手册中的说明来看,是开启SMCLK和DCO时钟发生器(clock generator),是不是可以理解为执行了该函数,中断返回后就退出了LMP3模式呢?
3. 从代码的英文注释来看,SSEL0在整个程序中只是被用作一个标记(flag),用于判断何时进入LMP0模式,这样理解正确吗?
4. 程序何时进入LMP0? 从代码来看,进入LMP0的条件是SSEL0位被清零,而SSEL0位被清零只有一处,就是检测到起始位的下降沿的时候,是不是用蓝色标记的语句(2)执行后中断返回就退出了LMP3,然后进入LMP0?
5. 如果能将程序中LMP0与LMP3的切换过程简单的描述一下那就太好了。
问题有点多,望解答,再次感谢!!