以上是基于msp430实现的单片机与电脑之间的串口通信。
下载:
wirecommunications.zip
(29.83 KB, 下载次数: 22)
- #include "LibraryHead.h"
- unsigned char MST_Data, SLV_Data;
- //master
- #ifdef MASTER_MODE
- unsigned char MST_Data, SLV_Data;
- int main(void)
- {
- volatile unsigned int i;
- WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
- UART_Init();
- SPI_Init();
- MST_Data = 0x00; // Initialize data values
- SLV_Data = 0x00;
- //UCB0TXBUF = MST_Data; // Transmit first character
- __bis_SR_register(LPM0_bits + GIE); // CPU off, enable interrupts
- }
- // Test for valid RX and TX character
- #pragma vector=USCIAB0RX_VECTOR
- __interrupt void USCIA0RX_ISR(void)
- {
- if(IFG2 & UCA0RXIFG)
- {
- MST_Data = UCA0RXBUF;
- while (!(IFG2 & UCB0TXIFG)); // USCI_A0 TX buffer ready?
- UCB0TXBUF = MST_Data; // Send next value
- __delay_cycles(75); // Add time between transmissions to
- // make sure slave can keep up
- }
- else if(IFG2 & UCB0RXIFG)
- {
- }
- else
- {}
- }
- #else
- //slave
- int main(void)
- {
- WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
- UART_Init();
- SPI_Init();
- __bis_SR_register(LPM4_bits + GIE); // Enter LPM4, enable interrupts
- }
- // Echo character
- #pragma vector=USCIAB0RX_VECTOR
- __interrupt void USCI0RX_ISR (void)
- {
- if(IFG2 & UCA0RXIFG)
- {
- }
- else if(IFG2 & UCB0RXIFG)
- {
- volatile unsigned int i;
- MST_Data = UCB0RXBUF;
- while (!(IFG2 & UCB0TXIFG)); // USCI_A0 TX buffer ready?
- if (MST_Data == 0x30) // Test for correct character RX'd
- P1OUT |= BIT0; // If correct, light LED
- else
- P1OUT &= ~BIT0; // If incorrect, clear LED
- while (!(IFG2 & UCA0TXIFG)); // USCI_A0 TX buffer ready?
- UCA0TXBUF = MST_Data;
- }
- else
- {}
- }
- #endif
复制代码
|