Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
3.1 kB
1
Indexable
Never
#include <msp430.h>

void sendString(const char *str);

int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;  // Stop watchdog timer

    // Set up MCLK= 8 MHz and SMCLK=8 MHz
    FRCTL0 = FRCTLPW | NWAITS_2;

    __bis_SR_register(SCG0);  // Disable FLL
    CSCTL3 |= SELREF__REFOCLK;  // Set REFO as FLL reference source
    CSCTL0 = 0;  // Clear DCO and MOD registers
    CSCTL1 |= DCORSEL_3;  // Set DCO = 8MHz
    CSCTL2 = FLLD_0 + 243;  // DCOCLKDIV = 8MHz
    __delay_cycles(3);
    __bic_SR_register(SCG0);  // Enable FLL
    while (CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1));  // FLL locked

    CSCTL4 = SELMS__DCOCLKDIV | SELA__REFOCLK;  // Set REFO as ACLK source, ACLK = 32768Hz
    CSCTL5 |= DIVM_0 | DIVS_0;  // Default DCOCLKDIV as MCLK and SMCLK source

    PM5CTL0 &= ~LOCKLPM5;  // Disable the GPIO power-on default high-impedance mode

    // Configure pins for UART
    P4SEL0 |= BIT2 | BIT3;  // Set 2-UART pin as second function

    // Configure P1.1 as input for IR sensor
    P1DIR &= ~BIT1;  // Set P1.1 as input
    P1REN |= BIT1;  // Enable pull-up/pull-down resistor
    P1OUT |= BIT1;  // Configure pull-up resistor

    // Configure P4.5 as output for green LED
    P4DIR |= BIT5;  // Set P4.5 as output
    P4OUT &= ~BIT5;  // Turn off green LED

    // Configure P5.1 as output for red LED (if available on your board)
    P5DIR |= BIT1;  // Set P5.1 as output
    P5OUT &= ~BIT1;  // Turn off red LED

    // Configure UART A1
    UCA1CTLW0 |= UCSWRST;
    UCA1CTLW0 |= UCSSEL_2;  // Set SMCLK as BRCLK
    UCA1BR0 = 52;  // Baud Rate calculation for 9600 bps
    UCA1BR1 = 0;
    UCA1MCTLW = 0x4911;  // Set modulation
    UCA1CTLW0 &= ~UCSWRST;  // Initialize eUSCI
    UCA1IE |= UCRXIE;  // Enable USCI_A1 RX interrupt

    __bis_SR_register(GIE);  // Enter LPM3, interrupts enabled
}

// UART interrupt routine
#pragma vector = USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
{
    switch (__even_in_range(UCA1IV, USCI_UART_UCTXCPTIFG))
    {
        case USCI_UART_UCRXIFG:
            if (UCA1RXBUF == 'p') {
                if (P1IN & BIT1) {  // Check if IR sensor detects an obstacle
                    sendString("liber");
                    P4OUT |= BIT5;  // Turn on green LED
                    P5OUT &= ~BIT1;  // Turn off red LED
                } else {
                    sendString("ocupat");
                    P4OUT &= ~BIT5;  // Turn off green LED
                    P5OUT |= BIT1;  // Turn on red LED
                }
                sendString("\n\r");
            } else {
                while (!(UCA1IFG & UCTXIFG));
                UCA1TXBUF = UCA1RXBUF;
            }
            __no_operation();
            break;
        case USCI_UART_UCTXIFG:
        case USCI_UART_UCSTTIFG:
        case USCI_UART_UCTXCPTIFG:
            break;
        default:
            break;
    }
}

void sendString(const char *str)
{
    while (*str) {
        while (!(UCA1IFG & UCTXIFG));
        UCA1TXBUF = *str++;
    }
}
Leave a Comment