#include <msp430.h>
#include<stdio.h>
void USCI_A0_init(void)
{
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
}
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
USCI_A0_init();
unsigned char sbuf[8]; /*待发送的内容*/
unsigned int vol,curr,index=0;
while(1)
{
sbuf[0] = (unsigned char)0x55; //头部,固定
sbuf[1] = (unsigned char)((0x80)|(index & (0x3F<<16))); //前两个bit为类型,此处以10B电压源数据为例,后6bit为index前6位
sbuf[2] = (unsigned char)(index & (0xFF<<8)); //去除头6bit后的一个字节
sbuf[3] = (unsigned char)(index & 0xFF); //取index最后一个字节
vol = (unsigned int)(20); //电压,先按固定值20取
curr = (unsigned int)(30); //电流,先按固定值30取
sbuf[4] = (unsigned char)(vol & (0xFF<<4));//电压数据前八bit放入第一个Byte
sbuf[5] = (unsigned char)(((vol&0xF)<<4)|(curr & (0xF<<8)));//电压数据的后四位和电流数据的前4位
sbuf[6] = (unsigned char)(curr & 0xFF);//电流数据的后8位
sbuf[7] = (unsigned char)0xAA; //尾部
unsigned int i;
for(i=0;i<8;i++)
UartSend(sbuf);
}
}
#pragma vector=USCIAB0RX_VECTOR
void UartSend(unsigned char c)//发送字节函数
{
while(!(IFG2 & UCA0TXIFG)); //待发送为空
UCA0TXBUF=c;
IFG2 &=~UCA0RXIFG;
}
目前问题:连接串口,串口调试助手里显示的“RX”没有变化;若成功的发送的话不是应该在串口调试助手里看到“RX”增加吗?
求大神指教 |