急!!从5438开发板上读取温度传感器数据通过串口发送到PC机问题
下面是我写的UART函数,各位帮我看看,谢谢了,就实现把数据发送到上位机上。void Uart_Init(void)
{
P3SEL = 0x30; // P3.4,5 = USCI_A0 TXD/RXD
P3DIR |= BIT4;//bit4 tx
P3DIR &= ~BIT5; // bit5 rx
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL0 = UCMODE_0;//uart mode;
UCA0CTL0 &= ~UC7BIT; // 8 bit
UCA0CTL1 |= UCSSEL_1;// CLK = ACLK
UCA0BR0 = 0x8a; // 115200 (see User's Guide)
UCA0BR1 = 0x00; //
UCA0MCTL |= UCBRS_7+UCBRF_0; // Modulation UCBRSx=7, UCBRFx=0
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
// UCA0IE |= UCRXIE;//UCTXIE; // Enable USCI_A1 RX interrupt
// __bis_SR_register( GIE);// LPM3_bits + // Enter LPM3, interrupts enabled
// __no_operation(); // For debugger
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;//关狗
Init_Clock();
Uart_Init();
while(1)
{
LPM3;
while (!(UCA0IFG&UCTXIFG)); // USCI_A1 TX buffer ready?
temture_h = temture & 0xff00;// 高8位
UCA0TXBUF = temture >> 8;
while (!(UCA0IFG&UCTXIFG));
temture_l = temture & 0x00ff;// 低8位
UCA0TXBUF = temture_l;
}
}
因为没有从PC发送给下位机的数据就没开接收中断,没写中断子函数; 怎么没人回答啊? 现在的问题是我在PC上用串口调试助手收不到数据,请问我程序出什么问题了? 问题1:P3SEL = 0x30; 一句已经将P3.4,5设置为UART用途了,不需要P3DIR |= BIT4;//bit4 tx P3DIR &= ~BIT5; // bit5 rx
问题2:你的UART时钟选择为ACLK=32.768KHz,波特率计算UCA0BR0+UCA0BR1=32768/115200=0.284≈0,你的UCA0BR0 = 0x8a;怎么来的?
问题3:进入了LPM3;没有退出,此行可以不要,确实要做低功耗试验要加中断触发CPU退出低功耗模式。
强烈建议楼主多看看TI的例程,写的非常详细。
//******************************************************************************
// MSP430F54x Demo - USCI_A0, UART 9600 Full-Duplex Transceiver, 32K ACLK
//
// Description: USCI_A0 communicates continuously as fast as possible
// full-duplex with another device. Normal mode is LPM3, with activity only
// during RX and TX ISR's. The TX ISR indicates the UART is ready to send
// another character.The RX ISR indicates the UART has received a character.
// At 9600 baud, a full character is tranceived ~1ms.
// The levels on P1.4/5 are TX'ed. RX'ed value is displayed on P1.0/1.
// ACLK = BRCLK = LFXT1 = 32768, MCLK = SMCLK = DCO~ 1048k
// Baud rate divider with 32768hz XTAL @9600 = 32768Hz/9600 = 3.41 (0003h 4Ah)
//
//
// MSP430F5438 MSP430F5438
// ----------------- -----------------
// /|\ | XIN|- /|\ | XIN|-
// || | 32KHz || | 32KHz
// --|RST XOUT|- --|RST XOUT|-
// | | | |
// | | | |
// | | | |
// ->|P1.4 | | P1.0|-> LED
// ->|P1.5 | | P1.1|-> LED
// LED <-|P1.0 | | P1.4|<-
// LED <-|P1.1 | | P1.5|<-
// | UCA0TXD/P3.4|--------->|P3.5/UCA0RXD |
// | | 9600 | |
// | UCA0RXD/P3.5|<---------|P3.4/UCA0TXD |
//
//
// W. Goh
// Texas Instruments Inc.
// November 2008
// Built with CCE Version: 3.2.2 and IAR Embedded Workbench Version: 4.11B
//******************************************************************************
#include "msp430x54x.h"
void main(void)
{
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
P7SEL |= 0x03; // Port select XT1
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
// Clear XT2,XT1,DCO fault flags
SFRIFG1 &= ~OFIFG; // Clear fault flags
__delay_cycles(100000); // Delay for Osc to stabilize
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
P1OUT = 0x000; // P1.0/1 setup for LED output
P1DIR |= BIT0+BIT1; //
P3SEL |= BIT4+BIT5; // P3.4,5 UART option select
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL1 |= UCSSEL_1; // CLK = ACLK
UCA0BR0 = 0x03; // 32k/9600 - 3.41
UCA0BR1 = 0x00; //
UCA0MCTL = 0x06; // Modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCTXIE + UCRXIE; // Enable USCI_A0 TX/RX interrupt
__bis_SR_register(LPM3_bits + GIE); // Enter LPM3 w/ interrupts enabled
__no_operation(); // For debugger
}
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
unsigned char tx_char;
switch(__even_in_range(UCA0IV,4))
{
case 0: break; // Vector 0 - no interrupt
case 2: // Vector 2 - RXIFG
P1OUT = UCA0RXBUF; // RXBUF1 to TXBUF1
break;
case 4: // Vector 4 - TXIFG
__delay_cycles(5000); // Add small gap between TX'ed bytes
tx_char = P1IN;
tx_char = tx_char >> 4;
UCA0TXBUF = tx_char; // Transmit character
break;
default: break;
}
} 回复【2楼】wanpujame
问题1:p3sel = 0x30; 一句已经将p3.4,5设置为uart用途了,不需要p3dir |= bit4;//bit4 tx p3dir &= ~bit5; // bit5 rx
问题2:你的uart时钟选择为aclk=32.768khz,波特率计算uca0br0+uca0br1=32768/115200=0.284≈0,你的uca0br0 = 0x8a;怎么来的?
问题3:进入了lpm3;没有退出,此行可以不要,确实要做低功耗试验要加中断触发cpu退出低功耗模式。
强烈建议楼主多看看ti的例程,写的非常详细。
//******************************************************************************
问题2:时钟源用了高频XT1 16MHz,看了UART的对应115200是138,7,0;那个LPM3我程序里其他地方有中断的
首先谢谢,我想问的是,如果波特率设置对了,只通过串口给PC发数据而不接受的话不用中断可以吗?就像我上面写的,还有没有其他地方要大改的? 可以不用中断,只判断标志位就可以了。上面的程序没有什么大改的,还有一点就是你需要确认一下你的16MHz XT1是否起振了。 mark
页:
[1]