发送LED_ON字符串点亮接收MCU的LED,发送LED_OFF,关闭接收MCU的LED,发送LED_TEST字符串,返回当前的LED状态。//引用的库
#include "driverlib.h"
//需要传输的字符和LED状态变量
volatile uint8_t TXDataOne[8] = "LED_OFF";
volatile uint8_t TXDataTwo[8] = "LED_ON";
volatile uint8_t TXDataThree[8] = "LED_TEST";
volatile uint8_t RXData[8];
volatile uint8_t LEDState=0;
//配置UART和设置LED相对应的IO口
void Configuration()
{
//波特率为115200 使用的是SMCLK时钟为16.384MHz
EUSCI_A_UART_initParam uartConfig = {
EUSCI_A_UART_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
8, // BRDIV = 8
14, // UCxBRF = 14
34, // UCxBRS = 34
EUSCI_A_UART_NO_PARITY, // No Parity
EUSCI_A_UART_MSB_FIRST, // MSB First
EUSCI_A_UART_ONE_STOP_BIT, // One stop bit
EUSCI_A_UART_MODE, // UART mode
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION // Oversampling Baudrate
};
// Setting the DCO to use the internal resistor. DCO will be at 16.384MHz
CS_setupDCO(CS_INTERNAL_RESISTOR);
// SMCLK should be same speed as DCO. SMCLK = 16.384MHz
CS_initClockSignal(CS_SMCLK, CS_CLOCK_DIVIDER_1);
// Settings P1.2 and P1.3 as UART pins. P1.4 as LED output
//设置P1.2和P1.3作为UART引脚,P1.4位LED管脚
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
GPIO_PIN2 | GPIO_PIN3,
GPIO_PRIMARY_MODULE_FUNCTION);
GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
//使能中断
EUSCI_A_UART_init(EUSCI_A0_BASE, &uartConfig);
EUSCI_A_UART_enable(EUSCI_A0_BASE);
EUSCI_A_UART_enableInterrupt(EUSCI_A0_BASE,
EUSCI_A_UART_RECEIVE_INTERRUPT);
}
int main(void) {
WDT_hold(WDT_BASE);
uint8_t i=0;
Configuration();
//发送字符
while(1)
{
for(i=0;i<9;i++)
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, TXDataOne[i]);
__delay_cycles(500000);
for(i=0;i<9;i++)
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, TXDataTwo[i]);
__delay_cycles(500000);
for(i=0;i<9;i++)
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, TXDataThree[i]);
__delay_cycles(500000);
}
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=USCI_A0_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(USCI_A0_VECTOR)))
#endif
void USCI_A0_ISR(void) {
uint8_t i=0;
switch(__even_in_range(UCA0IV, USCI_UART_UCTXCPTIFG))
{
case USCI_NONE: break;
case USCI_UART_UCRXIFG:
for(i=0;i<9;i++)
RXData[i] = EUSCI_A_UART_receiveData(EUSCI_A0_BASE);
//检查字符,如果接收到的,是LED_ON字符串点亮接收MCU的LED,是LED_OFF,关闭接收MCU的LED,是LED_TEST字符串返回当前的LED状态
if(RXData == "LED_ON")
{
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
LEDState=1;
}
else if(RXData == "LED_OFF")
{
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
LEDState=0;
}
else if(RXData == "LED_TEST")
{
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, LEDState);
}
else break;
case USCI_UART_UCTXIFG: break;
case USCI_UART_UCSTTIFG: break;
case USCI_UART_UCTXCPTIFG: break;
default: break;
}
}
|