闲着没事,就把课后题给写了一下,根据提供的例程改写。用的是STC单片机串口接收思维,贴出来请各位大神指正,新手,多多包涵
#include <msp430.h>
volatile uint8_t T[7] = "LED_OFF";
volatile uint8_t R[7] = "LED_ON";
volatile uint8_t E[7] = "ERROR";
volatile uint8_t Rec[10];
volatile uint8_t flag=3;
int main(void) {
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
};
WDT_hold(WDT_BASE);
// 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
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);
// Configure and enable the UART peripheral
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);
while(1)
{
uint8_t i;
while(EUSCI_A_UART_receiveData(EUSCI_A0_BASE)!='') //读取接收到的信息,如果不为空,则开始接收
for(i=0;i<8;i++)
Rec[i]=EUSCI_A_UART_receiveData(EUSCI_A0_BASE); //将接收到的信息放到数组Rec中
if(Rec[5]=='N') //判断接收到数组的第五位。因为LED_ON、LED_OFF与LED_TEST从第五位开始不同,如果为N,则为LED_ON
{
GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
flag=0; //点亮LED的同时,置标志位flag=0;当flag=0时,说明是灯是开着的
}
else if(Rec[5]=='F') //判断接收到数组的第五位。因为LED_ON、LED_OFF与LED_TEST从第五位开始不同,如果为F,则为LED_OFF
{
GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4);
GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN4);
flag=1; //关闭LED的同时,将flag=1;说明此时为LED_OFF
}
else if(Rec[5]=='E') //判断接收到数组的第五位。因为LED_ON、LED_OFF与LED_TEST从第五位开始不同,如果为E,则为LED_TEST
{
if(flag==0) //读取此时的标志位,若flag=0;则说明是LED_ON,然后将R【7】=LED_ON发送出去
{
for(i=0;i<8;i++)
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, R[i]);
}
else if(flag==1) //读取此时的标志位,若flag=1;则说明是LED_OFF,然后将R【7】=LED_OFF发送出去
{
for(i=0;i<8;i++)
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, T[i]);
}
else //读取此时的标志位,若flag既不是0也不是1,;则发送ERROR
{
for(i=0;i<8;i++)
EUSCI_A_UART_transmitData(EUSCI_A0_BASE, E[i]);
}
}
__bis_SR_register(LPM0_bits | GIE);
}
}
|