miércoles, 6 de julio de 2016

SEMÁFORO

Objetivo: Desarrollar un programa que simule la función de un semáforo.


Se plantea en primer paso el diagrama de estados en que se desarrollará el programa, se describe en la siguiente figura.


Descripción de diagrama de estados:

(a)   Siempre se inicia en el estado de color rojo.
(b)    En el estado de color rojo se toma un retardo (5 seg), para pasar al siguiente estado (Verde).
(c)   En el estado de color  verde se toma un retado (10 seg), para pasar al siguiente estado( verde con flash).
(d)   En el estado color verde con flash se tomo retado de (250 mseg), para pasar al siguiente estado (Amarillo).
(e)    En el estado de color  amarillo se toma un retado (5 seg), para pasar al siguiente estado (Amarillo con flash).
(f)    En el estado color amarillo con flash se tomo retado de (250 mseg), para pasar al siguiente estado (Rojo).

 Por seguridad se anexan dos condiciones
  Se pueda reiniciar desde el estado de color rojo
  Se pueda ir al estado amarillo con flash.
Funciones implementadas:
* Función de toggle (para el parpadeo del led).
*Función de PIT (Se utilizara como contar  el tiempo en el que cambia de un estado a otro).
*Función de Estado en color rojo
*Función de Estado en color verde
*Función de Estado en color  verde con flash
*Función de Estado en color amarillo
*Función de Estado en color amarillo con flash

Interrupciones implementadas:
* PIT
* Por  SW3
* Por  SW2

Diagrama de bloques
Programa principal






CÓDIGO


#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_pit.h"
#include "fsl_debug_console.h" //imprimir
#include "rgb.h"
#include "sw.h"

#define T_ROJO 5000
#define T_VERDE 10000
#define T_V_F 250
#define T_AMARILLO 5000
int n_Flash=10;

enum{EDO_ROJO, EDO_VERDE, EDO_VF,EDO_AMA, EDO_AF};
void Init_PIT(void);
int color = BLUE;
/*!
 * @brief Application entry point.
 */
unsigned int delay;
unsigned int num_Flash;
int next_Edo;

void PIT0_IRQHandler(void)
{
    /* Clear interrupt flag.*/
    PIT_ClearStatusFlags(PIT, kPIT_Chnl_0, PIT_TFLG_TIF_MASK);
 if(delay)
delay--;

}

void BOARD_SW2_IRQ_HANDLER(void)
{
    /* Clear external interrupt flag. */
    GPIO_ClearPinsInterruptFlags(BOARD_SW2_GPIO, 1U << BOARD_SW2_GPIO_PIN);
    /* Change state of button. */
    delay=0;
    next_Edo=EDO_AF;
   }

void BOARD_SW3_IRQ_HANDLER(void)
{
    /* Clear external interrupt flag. */
    GPIO_ClearPinsInterruptFlags(BOARD_SW3_GPIO, 1U << BOARD_SW3_GPIO_PIN);
    /* Change state of button. */
    delay=0;
   // next_Edo=EDO_ROJO;
}

int main(void) {
  /* Init board hardware. */
  BOARD_InitPins();
  BOARD_BootClockRUN();
  BOARD_InitDebugConsole();


  /* Add your code here */
  Ini_RGB();
  Ini_SW2_IRQ();
  Ini_SW3_IRQ();
  Init_PIT();
  delay=0;
  next_Edo=EDO_ROJO;

  while(1)
  {

 if(!delay){     //si no hay retardo
 switch(next_Edo)
 {
 case EDO_VERDE:
   f_Verde();
   break;
 case EDO_VF:
   f_VerdeF();
   break;
 case EDO_AMA:
   f_Amarillo();
   break;
 case EDO_AF:
      f_AmarilloF();
      break;

 default:
 case EDO_ROJO:
 f_Rojo();
 break;


 }

  }
  }

  }

void Init_PIT(void)
{
/* Structure of initialize PIT */
   pit_config_t pitConfig;


   /*
    * pitConfig.enableRunInDebug = false;
    */
   PIT_GetDefaultConfig(&pitConfig);

   /* Init pit module */
   PIT_Init(PIT, &pitConfig);

   /* Set timer period for channel 0 */
   PIT_SetTimerPeriod(PIT, kPIT_Chnl_0, USEC_TO_COUNT(1000U,CLOCK_GetFreq(kCLOCK_BusClk)));

   /* Enable timer interrupts for channel 0 */
   PIT_EnableInterrupts(PIT, kPIT_Chnl_0, kPIT_TimerInterruptEnable);

   /* Enable at the NVIC */
   EnableIRQ(PIT0_IRQn);

   /* Start channel 0 */
   PRINTF("\r\nStarting channel No.0 ...");
   PIT_StartTimer(PIT, kPIT_Chnl_0);
}

void f_Rojo()
{
LED_RGB(RED);
delay=T_ROJO;
next_Edo=EDO_VERDE;
}

void f_Verde()
{
LED_RGB(GREEN);
delay=T_VERDE;
next_Edo=EDO_VF;
}

void f_Amarillo()
{
LED_RGB(AMBAR);
delay=T_AMARILLO;
next_Edo=EDO_ROJO;
}

void f_VerdeF()
{

LED_TOGGLE(GREEN);
delay=T_V_F;
n_Flash--;
if(n_Flash==0)
{n_Flash=10;
next_Edo=EDO_AMA;
}
}

void f_AmarilloF()
{
LED_TOGGLE(AMBAR);
delay=T_V_F;
}



//"rgb.h"

#ifndef SOURCE_RGB_H_
#define SOURCE_RGB_H_

#include <string.h>

#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_debug_console.h"

#include "fsl_device_registers.h"
#include "fsl_port.h"

enum {RED_BIT=1,GREEN_BIT=2,BLUE_BIT=4};

enum {BLACK,RED,GREEN,AMBAR,BLUE,MAGENTA,CYAN,WHITE};

void Ini_RGB(void);
void LED_RGB(int);
void Printf_Color( int cont);
void LED_TOGGLE(int color);



#endif /* SOURCE_RGB_H_ */



#include "rgb.h"

void Ini_RGB(void)
{
  // Enable the clock to the PORT module that the LED is on.
      CLOCK_EnableClock(kCLOCK_PortB);

      // Setup the red LED pin as GPIO
      PORT_SetPinMux(BOARD_LED_RED_GPIO_PORT,
              BOARD_LED_RED_GPIO_PIN,
              kPORT_MuxAsGpio);

      LED_RED_INIT(LOGIC_LED_OFF);

      // Setup the blue LED pin as GPIO
            PORT_SetPinMux(BOARD_LED_BLUE_GPIO_PORT,
                    BOARD_LED_BLUE_GPIO_PIN,
                    kPORT_MuxAsGpio);

      LED_BLUE_INIT(LOGIC_LED_OFF);

      CLOCK_EnableClock(kCLOCK_PortE);

       // Setup the green LED pin as GPIO
       PORT_SetPinMux(BOARD_LED_GREEN_GPIO_PORT,
               BOARD_LED_GREEN_GPIO_PIN,
               kPORT_MuxAsGpio);

       LED_GREEN_INIT(LOGIC_LED_OFF);
}

void LED_RGB(int color)
{

if(color&RED_BIT)
LED_RED_ON();
else
LED_RED_OFF();
if(color&GREEN_BIT)
LED_GREEN_ON();
else
LED_GREEN_OFF();
if(color&BLUE_BIT)
LED_BLUE_ON();
else
LED_BLUE_OFF();
}

void LED_TOGGLE(int color)
{
static bool toggle = true;
if(toggle)
{
//toggle=false;
LED_RGB(color);
}
else
{
//toggle=true;
LED_RGB(BLACK);
}
toggle=!toggle;
}

void Printf_Color( int color)
{
char *color_names[] = {"Black","Red","Green","Yellow","Blue","Magenta","Cyan","White"};
char *aux_pointer =color_names[color];
PRINTF("Color Led %s\r\n",aux_pointer);
}




/*
 * sw.h

 */

#ifndef SOURCE_SW_H_
#define SOURCE_SW_H_
#include "fsl_device_registers.h"
#include "fsl_port.h"
#include "board.h"
#include "pin_mux.h"


void Ini_SW2_IRQ(void);
void Ini_SW3_IRQ(void);



#endif /* SOURCE_SW_H_ */




#include "sw.h"


void Ini_SW2_IRQ(void)
{
    /* Define the init structure for the input switch pin */
    gpio_pin_config_t sw_config = {
        kGPIO_DigitalInput, 0,
    };
    CLOCK_EnableClock(kCLOCK_PortC);
    PORT_SetPinMux(BOARD_SW2_PORT,BOARD_SW2_GPIO_PIN,kPORT_MuxAsGpio);
PORT_SetPinInterruptConfig(BOARD_SW2_PORT, BOARD_SW2_GPIO_PIN, kPORT_InterruptFallingEdge);
EnableIRQ(BOARD_SW2_IRQ);
GPIO_PinInit(BOARD_SW2_GPIO, BOARD_SW2_GPIO_PIN, &sw_config);
}


void Ini_SW3_IRQ(void)
{
    /* Define the init structure for the input switch pin */
    gpio_pin_config_t sw_config = {
        kGPIO_DigitalInput, 0,
    };
    CLOCK_EnableClock(kCLOCK_PortA);
    PORT_SetPinMux(BOARD_SW3_PORT,BOARD_SW3_GPIO_PIN,kPORT_MuxAsGpio);
PORT_SetPinInterruptConfig(BOARD_SW3_PORT, BOARD_SW3_GPIO_PIN, kPORT_InterruptFallingEdge);
EnableIRQ(BOARD_SW3_IRQ);
GPIO_PinInit(BOARD_SW3_GPIO, BOARD_SW3_GPIO_PIN, &sw_config);
}




No hay comentarios:

Publicar un comentario