Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
===========================================================
#include <msp430.h>
#include <stdint.h>
//=================================================INCLUIR
LIBRERIAS=========================================================
//=================================================DEFINICIONES
DEL USUARIO==================================================
#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
//=================================================DECLARACION DE
VARIABLES GLOBALES=========================================
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 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 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();
}
} 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 timer0Stop(void) {
TA0CCTL0 = 0x00;
TA0CTL = MC_0;
}
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;
//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;
IE2 |= UCA0RXIE;
}
uint8_t uartRead(void) {
while (!(IFG2 & UCA0RXIFG))
;
return UCA0RXBUF;
}
dataExport -= digitPart;
dataExport *= (divisorDecimals * 10);
decimalPart = (uint8_t) dataExport;
uartWrite('.');
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);
}
}
//================================================================
===========================================================