//==============================================================================
// INCLUDES
//==============================================================================
#if defined (__USE_LPCOPEN)
#if defined(NO_BOARD_LIB)
#include "chip.h"
#else
#include "board.h"
#include <string.h>
#endif
#endif
#include "cr_section_macros.h"
#include "iocon_17xx_40xx.h"
#include "core_cm3.h"
//==============================================================================
// DEFINES
//==============================================================================
#define UART_PORT LPC_UART1 // UART1 en polling
//==============================================================================
// PROTOTIPOS
//==============================================================================
void micro_StartUp(void);
void ControlarDireccionMotor(uint8_t direccion);
void UART1_PinInit(void);
void UART1_Configurar(void);
int UART1_LeerByte(uint8_t *dato);
//==============================================================================
// INICIALIZACIÓN GPIO MOTOR
//==============================================================================
void micro_StartUp(void) {
// ENA (P2.0) como salida low
Chip_GPIO_Init(LPC_GPIO);
Chip_IOCON_PinMux(LPC_IOCON, 2, 0, IOCON_MODE_INACT, IOCON_FUNC0);
Chip_GPIO_SetPinDIROutput(LPC_GPIO, 2, 0);
Chip_GPIO_SetPinOutLow (LPC_GPIO, 2, 0);
// IN1 (P0.0) e IN2 (P0.1)
Chip_IOCON_PinMux(LPC_IOCON, 0, 0, IOCON_MODE_INACT, IOCON_FUNC0);
Chip_IOCON_PinMux(LPC_IOCON, 0, 1, IOCON_MODE_INACT, IOCON_FUNC0);
Chip_GPIO_SetPinDIR (LPC_GPIO, 0, 0, true);
Chip_GPIO_SetPinDIR (LPC_GPIO, 0, 1, true);
Chip_GPIO_SetPinOutLow (LPC_GPIO, 0, 0);
Chip_GPIO_SetPinOutLow (LPC_GPIO, 0, 1);
}
void ControlarDireccionMotor(uint8_t dir) {
switch (dir) {
case 0: // STOP
Chip_GPIO_SetPinOutLow (LPC_GPIO, 0, 0);
Chip_GPIO_SetPinOutLow (LPC_GPIO, 0, 1);
break;
case 1: // AVANZAR
Chip_GPIO_SetPinOutHigh(LPC_GPIO, 0, 0);
Chip_GPIO_SetPinOutLow (LPC_GPIO, 0, 1);
break;
case 2: // RETROCEDER
Chip_GPIO_SetPinOutLow (LPC_GPIO, 0, 0);
Chip_GPIO_SetPinOutHigh(LPC_GPIO, 0, 1);
break;
default: // FALLA SEGURA
Chip_GPIO_SetPinOutLow (LPC_GPIO, 0, 0);
Chip_GPIO_SetPinOutLow (LPC_GPIO, 0, 1);
break;
}
}
//==============================================================================
// CONFIGURACIÓN UART1
//==============================================================================
#define TAM_BUFFER 2 // vamos a leer 2 caracteres (eje Y y eje X)
volatile char buffer_uart[TAM_BUFFER];
volatile uint8_t idx = 0;
void UART1_PinInit(void) {
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 15, IOCON_FUNC1); // TXD1
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 16, IOCON_FUNC1); // RXD1
}
void UART1_Configurar(void) {
UART1_PinInit();
Chip_UART_Init (UART_PORT);
Chip_UART_SetBaud(UART_PORT, 9600);
Chip_UART_ConfigData(UART_PORT,
UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS);
Chip_UART_SetupFIFOS(UART_PORT,
UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS);
Chip_UART_TXEnable(UART_PORT);
}
int UART1_LeerByte(uint8_t *dato) {
if (Chip_UART_ReadLineStatus(UART_PORT) & UART_LSR_RDR) {
*dato = Chip_UART_ReadByte(UART_PORT);
return 1;
}
return 0;
}
//==============================================================================
// MAIN
//==============================================================================
int main(void) {
SystemCoreClockUpdate();
Board_Init(); // Inicializa LED de board y sistema
UART1_Configurar(); // UART1 polling
micro_StartUp(); // Pines motor
Board_LED_Set(0, true);
Board_LED_Set(0, false);
uint8_t dato;
while (1) {
if (UART1_LeerByte(&dato)) {
buffer_uart[idx++] = dato; // guardamos el byte
if (idx >= TAM_BUFFER) {
idx = 0; // reiniciamos índice
// === eje Y ===
switch (buffer_uart[0]) {
case 'A': ControlarDireccionMotor(1);Board_LED_Set(0, true);
break;
case 'B': ControlarDireccionMotor(2);Board_LED_Set(0, true);
break;
case 'C': ControlarDireccionMotor(0);Board_LED_Set(0, false);
break;
default: break; // ignorar comando inválido
}
// === eje X ===
switch (buffer_uart[1]) {
case 'D': /* izquierda */ Board_LED_Set(0, true); break;
case 'E': /* centro */ Board_LED_Set(0, false); break;
case 'F': /* derecha */ Board_LED_Set(0, true); break;
default: break;
}
}
}
__asm volatile ("nop");
}
return 0;
}