Código en C para Dado Digital con LPC1768: Timer, Interrupciones y Display de 7 Segmentos
Código en C para un Dado Electrónico con LPC1768
A continuación se presenta el código fuente en lenguaje C para implementar un dado electrónico sobre el microcontrolador LPC1768, utilizando interrupciones, temporizadores y un display de 7 segmentos.
Variables globales
uint8_t Dado[]={0x79,0x24,0x30,0x19,0x12,0x02};
uint8_t Numero;
uint8_t Display[4] = {1, 2, 4, 8};
uint8_t Jugador = 0;
uint8_t Tirada;
uint8_t Inicio=1;
uint8_t indice =0;
uint8_t ValDado[4]={0x79,0x79,0x79,0x79};Inicialización de puertos
void inicializa (void){
//Configuración de puertos del display 0.0-0.6
LPC_PINCON-> PINSEL0 &= ~(0x3FFF << 0);
LPC_PINCON-> PINMODE0 &= ~(0x3FFF << 0);
LPC_PINCON-> PINMODE0 |= (0x2AAA << 0);
LPC_GPIO0-> FIODIR |= (0x7F << 0);
//Configuración de puertos de selección 0.7-0.10
LPC_PINCON-> PINSEL0 &= ~(0xFF << 14);
LPC_PINCON-> PINMODE0 &= ~(0xFF << 14);
LPC_PINCON-> PINMODE0 |= (0xAA << 14);
LPC_GPIO0-> FIODIR |= (0xF << 7);
//Configuro pulsador 2.10 y 2.11
LPC_PINCON-> PINSEL4 &= ~(0x3 << 20);
LPC_PINCON-> PINSEL4 |= (0x1 << 20);
LPC_PINCON-> PINMODE4 &= ~(0x3 << 20);
LPC_GPIO2-> FIODIR &= ~(0x1 << 10);
LPC_PINCON-> PINSEL4 &= ~(0x3 << 22);
LPC_PINCON-> PINSEL4 |= (0x1 << 22);
LPC_PINCON-> PINMODE4 &= ~(0x3 << 22);
LPC_GPIO2-> FIODIR &= ~(0x1 << 11);
LPC_SC-> EXTINT = 0x7;
LPC_SC->EXTMODE |= (0x3<<0);
LPC_SC->EXTPOLAR &= ~(0x3<<0);
}Configuración del Timer, retardos y SysTick
void configTimer1 (void){
LPC_SC->PCLKSEL0 |= 1 << 5;
LPC_TIM1->PR = 0;
LPC_TIM1->MR0 = 60000;
LPC_TIM1->MCR |= (0x3<<0); // interrumpe y para
LPC_TIM1->TCR |= 1 << 1; // RESETEO DEL TIMER1
LPC_TIM1->TCR &= ~(1<<1);
NVIC_EnableIRQ(TIMER1_IRQn);
LPC_TIM1->TCR |= 1 << 0; // EMPIEZA EL TIMER1
}
void delay(uint32_t count) {
volatile uint32_t i;
for (i = 0; i < count; i++);
}
void Configuracion_Systick(void)
{
SysTick->LOAD = 99999999; // cada 10 ms
SysTick->VAL = 0;
SysTick->CTRL = 0x7;
}Prioridades e interrupciones
void inicioIRQs()
{
// Asignación de prioridades
NVIC_SetPriorityGrouping(4);
NVIC_SetPriority(EINT0_IRQn, 0x4);
NVIC_SetPriority(EINT1_IRQn, 0x5);
NVIC_SetPriority(TIMER1_IRQn, 0x3);
NVIC_SetPriority(SysTick_IRQn, 0x6);
// Habilitación de las interrupciones
NVIC_EnableIRQ(EINT0_IRQn);
NVIC_EnableIRQ(EINT1_IRQn);
NVIC_EnableIRQ(SysTick_IRQn);
}
void init_DAC(void)
{
LPC_PINCON->PINSEL1 |= (2<<20); // DAC output = P0.26 (AOUT)
LPC_PINCON->PINMODE1 |= (2<<20); // Deshabilita pullup/pulldown
LPC_DAC->DACCTRL = 0;
}
void SysTick_Handler(void){
if(Inicio==1) Numero++;
ValDado[Jugador] = Dado[Numero-1];
if(Numero>5) Numero=0;
}Manejadores de interrupción del Timer y pulsadores
void TIMER1_IRQHandler(void) {
LPC_TIM1->IR |= (1 << 0); // Borrar flag de interrupción MR0
LPC_GPIO0->FIOPIN &= ~(0xF << 7);
LPC_GPIO0->FIOPIN &= ~(0x7F<<0);
LPC_GPIO0->FIOSET = (ValDado[indice]<<0);
LPC_GPIO0->FIOSET = (1<<(7 + indice)); // Encender display correspondiente
indice++;
if (indice>3) indice=0;
}
void EINT0_IRQHandler()
{
//NVIC_ClearPendingIRQ(EINT0_IRQn);
delay(10000);
Inicio=1;
Jugador++;
LPC_SC->EXTINT |= (1);
if(Jugador>3){
Inicio=0;
Jugador=0;
}
}
void EINT1_IRQHandler()
{
LPC_SC->EXTINT |= (1 << 1); // Borrar el flag de la EINT1 --> EXTINT.1
NVIC_ClearPendingIRQ(EINT1_IRQn);
Inicio = 1;
ValDado[0]=0x79;
ValDado[1]=0x79;
ValDado[2]=0x79;
ValDado[3]=0x79;
//if(Jugador == 3) LPC_DAC->DACR= 1023; // Entre 0 y 1023 -> 1023 = 3v3
}Función principal
int main(){
inicializa();
configTimer1();
Configuracion_Systick();
inicioIRQs();
while(1);
}