Anda di halaman 1dari 2

/

***********************************************************************************
***
* Description: This code uses usart. Receives a string ending in / n or / r. It
also
* turns a led on and off when the string "led \ r" is received.
*
* Pins:
* - 1.0: Led
* - 1.1: Rx UART
* - 1.2: Tx UART
*
* Author : Matias Sepulveda Pradenas
* Email : mesepulveda@uc.cl
* Last modification date : 30/04/17

***********************************************************************************
**/

/************************************** Includes
**************************************/
#include <msp430g2553.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "UART/UART.h"
/************************************** Includes
**************************************/

/*************************************** UART.c
***************************************/
// Rx Interrupt: Receive a character and add it to the buffer
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void){
char letra = UCA0RXBUF;
add_to_list(letra, "rx");
}

/* Transmission interruption: When the interrupt is activated and the transmission


* buffer is empty, it loads one character to the buffer.If there is nothing to
load,
* turn off the interrupt. */
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCI0TX_ISR(void){
if(head_tx != NULL){
UCA0TXBUF= head_tx->letra;
struct buff *siguiente = head_tx->next;
free(head_tx); // Free the buffer
head_tx = siguiente;
}
else{
IE2 &= ~UCA0TXIE; // Turn off the interruption
}
}
/*************************************** UART.c
***************************************/

/*************************************** PORTS
***************************************/
// All pins as inputs, except 1.0. (UART pins are automatically set up)
void init_ports(void){
P1DIR = 0x00;
P1REN = 0x00;
P1OUT = 0x00;
P2DIR = 0x00;
P2REN = 0x00;
P2OUT = 0x00;
P3DIR = 0x00;
P2REN = 0x00;
P3OUT = 0x00;
P1DIR |= (1 << 0); // 1.0 output
}
/*************************************** PORTS
***************************************/

/*************************************** CLOCK
***************************************/
// Clock to 16MHz
void init_clock(void){
DCOCTL = CALDCO_16MHZ; // Calibrate DCO to 16MHz
BCSCTL1 = CALBC1_16MHZ; // Calibrate system clock to 16MHz
BCSCTL2 &= ~SELS; // Select DCOCLK as SMCLK
}
/*************************************** CLOCK
***************************************/

/*************************************** MAIN
***************************************/
int main(void){
WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog timer

init_clock(); // Initialize clock


init_ports(); // Initialize ports
init_uart(); // Initialize UART

_BIS_SR(GIE); // Enable global interrupts


__enable_interrupt(); // Enable interrupts

USART_Transmit_String("Hello, send strings!\r\n");

char string[50]; // Buffer

while(1){
USART_Receive_String(string, 50);
if (strlen(string)){
if (strcmp(string, "led\r") == 0){
P1OUT ^= (1 << 0);
}
USART_Transmit_String("Received: ");
USART_Transmit_String(string);
USART_Transmit_String("\n");
}
}
}
/*************************************** MAIN
***************************************/

Anda mungkin juga menyukai