Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
3.6 kB
2
Indexable
Never
#include <msp430.h>

void sendString(const char *str);

int main(void)
{

    WDTCTL = WDTPW | WDTHOLD;                          // Stop watchdog timer

       //(MCLK= 8 Mhz 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(Mclock 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 default REFO(~32768Hz) 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
                                                              // to activate previously configured port settings

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

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

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


       // Configure UART A1
       UCA1CTLW0 |= UCSWRST;
       UCA1CTLW0 |= UCSSEL_2;                    // set SMCLK as BRCLK

       // Baud Rate calculation. Referred to UG 17.3.10
       // Baud Rate calculation
       //N= 8000000/9600=833.33 >16
       // OS16=1, UCBRx=INT(N/16)=52.08
       // Fractional portion = 0.08

       UCA1BR0 = 52;
       UCA1BR1 = 0x00;
       UCA1MCTLW= 0x4911;



       UCA1CTLW0 &= ~UCSWRST;                    // Initialize eUSCI
       UCA1IE |= UCRXIE;                         // Enable USCI_A0 RX interrupt

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


//  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