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
}
{
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
}
}
一周热门 更多>