为什么串口通信RS232 仿真正常,MCU工作就会出现乱码,工作一段时间才正常
LPM3模式下,唤醒通信
你表达的不清楚,说明白点,语句有问题,不知道怎么回答你
FET430UIF仿真,外部供电 串口通信RS232正常
写入MSP430FR4133 就会出现乱码,工作一段时间才正常
LPM3模式下,唤醒通信
MSP430FR4133 code example1有用过,可以
但他晶振用DCO=8MHZ分频
我直接用外部32768
void CLOCK_Config()
{
P4SEL0 |= BIT1 + BIT2; // Select pin4.1 pin4.2 as crystal function
do
{
CSCTL7 &= ~(XT1OFFG + DCOFFG); // Clear XT1 and DCO fault flag 清除 XT1 DCO 失效标志
SFRIFG1 &= ~OFIFG;
}while (SFRIFG1 & OFIFG); // Test oscillator fault flag 如果失效标志存在
CSCTL4 = SELA__XT1CLK; // IMPORTANT. Select ACLK = XT1 = 32768Hz
}
void eUSCIA0_Config()
{
UART_PORT_SEL |= UART_BIT_TXD + UART_BIT_RXD; // set pin as UART function
UCA0CTLW0 |= UCSWRST; // Put eUSCI in reset
UCA0CTLW0 |= UCSSEL__SMCLK; // 8-e-1 LSB SMCLK
UCA0BR0 = 6; // 9600 baud
UCA0BR1 = 0;
UCA0MCTLW = 0x2200 + UCOS16 + UCBRF_13; // 1.04MHz/9600
UCA0CTLW0 &= ~UCSWRST; // Initialize eUSCI
UCA0IE |= UCRXIE; //开接收中断
//UCA0IE &= ~UCTXIE; //关闭发送中断
}
uint8_t Uart_SendFrame(uint8_t *pPtr, uint8_t ui8Length)
{
if(UCA0IE & UCTXIE) return 0; // Last frame hasn't been sent out
if(ui8Length > TXBUF_SIZE) return 0; // Length overflow the buffer
ui8TX_counter = 0; // Clear transfer counter
UCA0TXBUF = pPtr[ui8TX_counter++];
ui8TX_number = ui8Length; // Number of bytes to be sent
UCA0IE |= UCTXIE; // Enable transfer interrupt
return 1; // Send successfully
}
void Spi_uart2_rx_deal(uint8_t *rx_pbuf,uint8_t *tx_pbuf)
{
………
Uart_SendFrame(ui8TX_buffer,Buffcn);
}
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
switch(__even_in_range(UCA0IV,USCI_UART_UCTXCPTIFG))
{
case USCI_NONE: break;
case USCI_UART_UCRXIFG:
{ //接收数据
ui8RX_buffer[ui8RX_counter]= UCA0RXBUF;
……..
if(uart2_crc_flag)
{
uart2_crc_flag=false;
if((CRC16_verify(ui8RX_buffer, ui8RX_counter+1))==1)
{
//UCA0IE&=~UCRXIE;//关闭接收中断
bRX_finished = true;//允许发送
}else ui8RX_counter=0;
}
}
break;
case USCI_UART_UCTXIFG:
UCA0TXBUF = ui8TX_buffer[ui8TX_counter++];
if(ui8TX_counter>=ui8TX_number)
{
UCA0IE &= ~UCTXIE; //关闭发送中断
//UCA0IE |= UCRXIE; //开接收中断
ui8RX_counter=0;
}
break;
case USCI_UART_UCSTTIFG: break;
case USCI_UART_UCTXCPTIFG: break;
}
}
void main(void)
{
WM_Initialize(); // System initialize
#ifdef Comm_RS485
HAL_RS485_INIT();
#endif
……
while(1)
{
__bis_SR_register(LPM3_bits + GIE); // 进入 LPM3 w/ interrupt
WDTCTL = WDTPW + WDTCNTCL + WDTSSEL0 + WDTIS1; // clear watchdog 4m16s
if(bWorking_flag)
{
…..
}
#ifdef Comm_RS485
if(bRX_finished)
{
// rs485
Spi_uart2_rx_deal(ui8RX_buffer,ui8TX_buffer);
bRX_finished = false;
}
#endif
}
}
昨天写了个测试程序 只有有收到数据,就上传数据01 02 03 04 05 06 00 00
01 02 03 04 05 06固定数据 00 00 是收到的数据,相同数据发两次
刚上电收到是
01 19 74 10 A0 81 0E 01 07 FE
01 19 74 10 A0 81 0E 01 80
01 19 34 18 A0 81 07 00 00
01 09 36 18 A0 81 07 00 00
01 02 03 04 05 06 80 80
最后才收到正确数据,感觉波特率不同步
while(1)
{
__bis_SR_register(LPM3_bits + GIE); // 进入 LPM3 w/ interrupt
WDTCTL = WDTPW + WDTCNTCL + WDTSSEL0 + WDTIS1; // clear watchdog 4m16s
if(bWorking_flag)
{
…..
}
#ifdef Comm_RS485
_DELAY_CYCLES(1000);
if(bRX_finished)
{
// rs485
Spi_uart2_rx_deal(ui8RX_buffer,ui8TX_buffer);
bRX_finished = false;
}
#endif
}
在程序中加了 _DELAY_CYCLES(1000);差不多30秒后,就通讯正常,比原来快了很多
我有点不明白,平常串口始终在接收状态,一有信号来,就唤醒,晶振和时钟如何稳定下来,延时一小也就对发送启作用