Sei sulla pagina 1di 9

//=================================================MSP430=========

===========================================================

#include <msp430.h>

#include <stdint.h>

//=================================================INCLUIR
LIBRERIAS=========================================================

//=================================================DEFINICIONES
DEL USUARIO==================================================

#define FREC_MCLK 16000000


#define FREC_ACLK 12000

#define BYTE_BUFFER 32
#define TIME_MAX 120
#define TIME_MIN 4
#define TIME_LED_PLAY FREC_ACLK/5
#define TIME_LED_STOP FREC_ACLK

#define START_COMMAND '{'


#define END_COMMAND '}'

#define BANDERA_PROCESAR (uint8_t)(0x01)


#define BANDERA_ENVIAR (uint8_t)(0x02)
#define BANDERA_PLAY (uint8_t)(0x04)
#define BANDERA_PAUSE (uint8_t)(0x08)
#define BANDERA_LED (uint8_t)(0x08)

//=================================================DECLARACION DE
VARIABLES GLOBALES=========================================

typedef struct DatoByteAdc {


uint8_t low;
uint8_t high;
} DatoByteAdc;

typedef union DatoAdc {


uint16_t data;
DatoByteAdc dataByte;
} DatoAdc;

DatoAdc adcData;

uint8_t banderasProceso = 0;

uint8_t pointerUartRx = 0;
uint8_t bufferUartRx[BYTE_BUFFER] = { 0 };
//=================================================DECLARACION DE
FUNCIONES==================================================

void stopWDT(void);

void configureDCO(void);

void gpioInit(void);

void timer0Start(uint16_t time);


void timer0Stop(void);

void timer1Init(uint16_t time);


void timer1LoadTime(uint16_t time);

void adcInit(uint8_t adcChannel);


void adcSelectChannel(uint16_t adcChannel);
uint16_t adcStartWaitConversion(void);
void adcStartConversion(void);
uint16_t adcReadConversion(void);

void uartInit(void);
void uartWrite(uint8_t byte);
void uartWriteMessage(const uint8_t message[]);
void uartDataDec(uint16_t dataExport, uint8_t quantityDigits);
void uartDoubleFormat(double dataExport, uint8_t quantityDigits,
uint8_t quantityDecimals);

void cleanBufferChar(uint8_t *buffer, uint8_t numeroDatos, uint8_t


dato);
void procesarBufferUartRx(void);

void TIMER0_A0_ISR_HOOK(void);
void TIMER1_A0_ISR_HOOK(void);
void USCI0RX_ISR_HOOK(void);

//=================================================MAIN===========
===========================================================

void main(void) {
//======CONFIGURACION
WDT===============================================================
=================================

stopWDT();

//======CONFIGURACION
DCO===============================================================
=================================

configureDCO();
//======CONFIGURACION PUESTOS DIGITALES
I/O===============================================================
===============

gpioInit();

//======CONFIGURACION Y HABILITACION
ADC10=============================================================
==================

adcInit(BIT5);
adcSelectChannel(INCH_5);
adcStartConversion();

//======CONFIGURACION Y HABILITACION
UART==============================================================
==================

uartInit();

//======CONFIGURACION Y HABILITACION
TIMER1============================================================
==================

timer1Init(TIME_LED_STOP);

//======HABILITACION INTERRUPCIONES
GLOBALES==========================================================
===================

uint16_t i = 0;
for (i = 0; i < 20; i++) {
P1OUT ^= BIT0;
__delay_cycles(FREC_MCLK / 10);
}

__enable_interrupt();

//======CONDICIONES
INICIALES=========================================================
===================================

adcData.data = 0;
banderasProceso = 0;
pointerUartRx = 0;
cleanBufferChar(bufferUartRx, BYTE_BUFFER, 0x00);

//===========================================================
============================================================

do {
if (banderasProceso & BANDERA_PROCESAR) {
__disable_interrupt();
procesarBufferUartRx();
banderasProceso &= ~BANDERA_PROCESAR;
__enable_interrupt();
}

if (banderasProceso & BANDERA_LED) {


P1OUT ^= BIT0;
banderasProceso &= ~BANDERA_LED;
}

if (banderasProceso & BANDERA_PLAY) {


if (banderasProceso & BANDERA_ENVIAR) {
P2OUT |= BIT5;
adcData.data = adcReadConversion();
uartDataDec(adcData.data, 4);
//uartWrite(adcData.dataByte.high);
//uartWrite(adcData.dataByte.low);
uartWrite('\n');
uartWrite('\r');
banderasProceso &= ~BANDERA_ENVIAR;
P2OUT &= ~BIT5;
}
}

} while (1);
}

//=================================================DESARROLLO DE
FUNCIONES DEL USUARIO=======================================

void stopWDT(void) {
WDTCTL = WDTPW + WDTHOLD;
}

void configureDCO(void) {
BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;
if (CALBC1_16MHZ != 0xFF) {
__delay_cycles(100000);
DCOCTL = 0x00;
BCSCTL1 = CALBC1_16MHZ;
DCOCTL = CALDCO_16MHZ;
}
BCSCTL1 |= XT2OFF | DIVA_0;
BCSCTL3 = XT2S_0 | LFXT1S_2 | XCAP_1;
}

void gpioInit(void) {
P2SEL &= ~(BIT6 | BIT7);

P1DIR |= BIT0;
P1OUT &= ~BIT0;
P2DIR |= BIT5;
P2OUT &= ~BIT5;
}

void timer0Start(uint16_t time) {


TA0CCTL0 = CM_0 | CCIS_0 | OUTMOD_0 | CCIE;
TA0CCR0 = time - 1;
TA0CTL = TASSEL_1 | ID_0 | MC_1;
}

void timer0Stop(void) {
TA0CCTL0 = 0x00;
TA0CTL = MC_0;
}

void timer1Init(uint16_t time) {


TA1CCTL0 = CM_0 | CCIS_0 | OUTMOD_0 | CCIE;
TA1CCR0 = time - 1;
TA1CTL = TASSEL_1 | ID_0 | MC_1;
}

void timer1LoadTime(uint16_t time) {


TA1CCR0 = time - 1;
}

void adcInit(uint8_t adcChannel) {


ADC10CTL0 &= ~ENC;
ADC10CTL0 = ADC10ON | MSC | ADC10SHT_3 | SREF_0;
ADC10CTL1 = CONSEQ_2 | ADC10SSEL_0 | ADC10DIV_0 | SHS_0 |
INCH_0;
ADC10AE0 = adcChannel;
ADC10CTL0 |= ENC;
}

void adcSelectChannel(uint16_t adcChannel) {


ADC10CTL0 &= ~(ENC);
ADC10CTL1 &= 0x0FFF;
ADC10CTL1 |= adcChannel;
ADC10CTL0 |= ENC;
}

uint16_t adcStartWaitConversion(void) {
ADC10CTL0 |= ADC10SC;
while (ADC10CTL1 & ADC10BUSY)
;
return ADC10MEM;
}

void adcStartConversion(void) {
ADC10CTL0 |= ADC10SC;
}
uint16_t adcReadConversion(void) {
return ADC10MEM;
}

void uartInit(void) {
UCA0CTL1 |= UCSWRST;

P1SEL2 |= (BIT1 + BIT2);


P1SEL |= (BIT1 + BIT2);

UCA0CTL1 = UCSSEL_2 | UCSWRST;

//Baud: 115200
//UCA0MCTL = UCBRF_0 | UCBRS_7;
//UCA0BR0 = 138;

//Baud: 230400
//UCA0MCTL = UCBRF_0 | UCBRS_4;
//UCA0BR0 = 69;

//Baud: 921600
//UCA0MCTL = UCBRF_0 | UCBRS_3;
//UCA0BR0 = 17;

//Baud: 1382400
UCA0MCTL = UCBRF_0 | UCBRS_5;
UCA0BR0 = 11;

UCA0CTL1 &= ~UCSWRST;

IE2 |= UCA0RXIE;
}

void uartWrite(uint8_t byte) {


while (!(IFG2 & UCA0TXIFG))
;
UCA0TXBUF = byte;
}

uint8_t uartRead(void) {
while (!(IFG2 & UCA0RXIFG))
;
return UCA0RXBUF;
}

void uartWriteMessage(const uint8_t message[]) {


uint16_t i = 0;
while (message[i] != 0) {
uartWrite(message[i]);
i++;
}
}

void uartDataDec(uint16_t dataExport, uint8_t quantityDigits) {


uint8_t i = 0;
uint32_t divisor = 1;
uint8_t digitExport = 0;

for (i = 1; i < quantityDigits; i++) {


divisor *= 10;
}

for (i = 1; i <= quantityDigits; i++) {


digitExport = (dataExport / divisor) + 0x30;
uartWrite(digitExport);
dataExport %= divisor;
divisor /= 10;
}
}

void uartDoubleFormat(double dataExport, uint8_t quantityDigits,


uint8_t quantityDecimals) {
uint8_t i = 0;
uint16_t divisorDigits = 1;
uint16_t divisorDecimals = 1;
uint16_t digitPart = 0;
uint16_t decimalPart = 0;
uint8_t digitExport = 0;

for (i = 1; i < quantityDigits; i++) {


divisorDigits *= 10;
}

for (i = 1; i < quantityDecimals; i++) {


divisorDecimals *= 10;
}

digitPart = (uint8_t) dataExport;

dataExport -= digitPart;
dataExport *= (divisorDecimals * 10);
decimalPart = (uint8_t) dataExport;

for (i = 1; i <= quantityDigits; i++) {


digitExport = (digitPart / divisorDigits) + 0x30;
uartWrite(digitExport);
digitPart %= divisorDigits;
divisorDigits /= 10;
}

uartWrite('.');

for (i = 1; i <= quantityDecimals; i++) {


digitExport = (decimalPart / divisorDecimals) + 0x30;
uartWrite(digitExport);
decimalPart %= divisorDecimals;
divisorDecimals /= 10;
}
}

void cleanBufferChar(uint8_t *buffer, uint8_t numeroDatos, uint8_t


dato) {
uint8_t k = 0;
for (k = 0; k < numeroDatos; k++) {
buffer[k] = dato;
}
}

void procesarBufferUartRx(void) {
if ((bufferUartRx[0] == 'M') && (bufferUartRx[1] == 'S')
&& (bufferUartRx[2] == 'P') && (bufferUartRx[3] ==
'4')
&& (bufferUartRx[4] == '3') && (bufferUartRx[5] ==
'0')
&& (bufferUartRx[6] == '_')) {
if ((bufferUartRx[7] == 'P') && (bufferUartRx[8] == 'L')
&& (bufferUartRx[9] == 'A') &&
(bufferUartRx[10] == 'Y')
&& (bufferUartRx[11] == ',')) {
banderasProceso |= BANDERA_PLAY;
banderasProceso &= ~BANDERA_ENVIAR;
uint16_t time = 0;
uint8_t i = 0;
for (i = 12; i < pointerUartRx; i++) {
time = (uint16_t) ((uint16_t) (time * 10)
+ (uint16_t) (bufferUartRx[i] -
48));
}
//if (time > TIME_MAX) {
// time = TIME_MAX;
//} else if (time < TIME_MIN) {
// time = TIME_MIN;
//}
timer0Start(time);
timer1LoadTime(TIME_LED_PLAY);
} else if ((bufferUartRx[7] == 'S') && (bufferUartRx[8]
== 'T')
&& (bufferUartRx[9] == 'O') &&
(bufferUartRx[10] == 'P')) {
banderasProceso &= ~BANDERA_PLAY;
banderasProceso &= ~BANDERA_ENVIAR;
timer1LoadTime(TIME_LED_STOP);
timer0Stop();
}
}
pointerUartRx = 0;
cleanBufferChar(bufferUartRx, BYTE_BUFFER, 0);
}

//=================================================DESARROLLO DE
FUNCIONES DE INTERRUPCION===================================

#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR_HOOK(void) {
banderasProceso |= BANDERA_ENVIAR;
}

#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR_HOOK(void) {
banderasProceso |= BANDERA_LED;
}

#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR_HOOK(void) {
uint8_t datoUart = uartRead();
if (datoUart == START_COMMAND) {
pointerUartRx = 0;
} else if (datoUart == END_COMMAND) {
banderasProceso |= BANDERA_PROCESAR;
} else {
bufferUartRx[pointerUartRx] = datoUart;
pointerUartRx++;
pointerUartRx &= (BYTE_BUFFER - 1);
}
}

//================================================================
===========================================================

Potrebbero piacerti anche