|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2010-11-5 10:04:56
|
显示全部楼层
应该是时钟问题,之前我系统时钟用的是DCO_1MHz,而串口时钟用的是32.768khz,现在我统一都用DCO_1mhz,两者通信正常了,先把代码贴出来,因为只是刚刚调试,代码还不是很完善,仅当参考。
void Init_uart1(void) // TX or RX between the GSM , P3.6 and P3.7
{
/* UCA1CTL0 = 0x0;
UCA1CTL1 |= UCSSEL_1; // CLK = ACLK
UCA1BR0 = 0x03; // 32kHz/9600 = 3.41
UCA1BR1 = 0x00; //
UCA1MCTL = UCBRS1 + UCBRS0; // Modulation UCBRSx = 3*/
if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
{
while(1); // If calibration constants erased
// do not load, trap CPU!!
}
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
UCA1CTL1 |= UCSSEL_2; // SMCLK
UCA1BR0 = 104; // 1MHz 9600; (104)decimal = 0x068h
UCA1BR1 = 0; // 1MHz 9600
UCA1MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UC1IE |= UCA1RXIE; // Enable USCI_A1 RX interrupt
// UC1IE |= UCA1TXIE; // Enable USCI_A1 TX interrupt 实际调试串口时,再将此命令打开
// _EINT();
}
void Set_DCO(unsigned int Delta) // Set DCO to selected frequency
{
unsigned int Compare, Oldcapture = 0;
BCSCTL1 |= DIVA_3; // ACLK = LFXT1CLK/8
TACCTL2 = CM_1 + CCIS_1 + CAP; // CAP, ACLK
TACTL = TASSEL_2 + MC_2 + TACLR; // SMCLK, cont-mode, clear
while (1)
{
while (!(CCIFG & TACCTL2)); // Wait until capture occured
TACCTL2 &= ~CCIFG; // Capture occured, clear flag
Compare = TACCR2; // Get current captured SMCLK
Compare = Compare - Oldcapture; // SMCLK difference
Oldcapture = TACCR2; // Save current captured SMCLK
if (Delta == Compare)
break; // If equal, leave "while(1)"
else if (Delta < Compare)
{
DCOCTL--; // DCO is too fast, slow it down
if (DCOCTL == 0xFF) // Did DCO roll under?
if (BCSCTL1 & 0x0f)
BCSCTL1--; // Select lower RSEL
}
else
{
DCOCTL++; // DCO is too slow, speed it up
if (DCOCTL == 0x00) // Did DCO roll over?
if ((BCSCTL1 & 0x0f) != 0x0f)
BCSCTL1++; // Sel higher RSEL
}
}
TACCTL2 = 0; // Stop TACCR2
TACTL = 0; // Stop Timer_A
BCSCTL1 &= ~DIVA_3; // ACLK = LFXT1CLK
}
void main(void)
{
unsigned char i;
//_DINT(); //关闭所有中断
CLR_DOG; // 禁止看门狗定时器
// SET_DOG; // 开启看门狗定时器
P1DIR = 0xFF; // All P1.x outputs
P1OUT = 0; // All P1.x reset
P2DIR = 0xFF; // All P2.x outputs
P2OUT = 0; // All P2.x reset
P3SEL = 0xF0; // P3.4,5 = USCI_A0 TXD/RXD
P3DIR = 0xFF; // All P3.x outputs
P3OUT = 0; // All P3.x reset
P4DIR = 0xFF; // All P4.x outputs
P4OUT = 0; // All P4.x reset
P5DIR = 0xFF; // All P5.x outputs
P5OUT = 0; // All P5.x reset
P6DIR = 0xFF; // All P6.x outputs
P6OUT = 0; // All P6.x reset
Set_DCO(DELTA_1MHZ); // Set DCO and obtain constants
// Init_uart0();
Init_uart1();
/******************** make Three LEDs flash 5 *********************/
for ( i = 0; i<10 ; i++ )
{
delay(10);
P1OUT = ~ P1OUT;
}
P1OUT = 0xFF;
/******************** Final Trun off after flash 5*****************/
Timer0_Init(); //50ms中断
// Delay_ms(1000); //等待 GSM模块上电后的硬件初始化。
_EINT(); //总中断打开
// GPS_Init();
sim900_init();
// __bis_SR_register(LPM3_bits + GIE); // Enter LPM3, interrupts enabled
for(;;)
{
Uart1_SendString("AT\r");
for ( i = 0; i<10 ; i++ )
{
delay(100);
}
}
} |
|