miércoles, 6 de julio de 2016

RGB_PIT

OBJETIVO
Desarrollar un programa que muestre los siete colores (black, red, green, ambar, blue, magenta, cyan, white) e imprima en pantalla el color que se observe empleando el PIT (Programmable Interrupt Timer) para el retardo.
DESCRIPCIÓN DEL PROGRAMA

En el programa principal dentro de while(1) se llama a las funciones  LED_RGB (color), Printf_Color (color) que son las encargadas de hacer las combinaciones para generar los colores, mandar a imprimir en pantalla el color que se esté observando, pero esto solo será posible bajo la condición de que no halla retardo que es una variable que se declaró con un valor, esta variable se decrementara en cada interrupción del PIT.


DIAGRAMAS


CODIGO


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

#include "rgb.h"

 void Init_PIT(void);
int color = BLUE;
/*!
 * @brief Application entry point.
 */
unsigned int delay;


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

}

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


  /* Add your code here */
  Ini_RGB();
  Init_PIT();
  delay=250;
  while(1)
  {

 if(!delay){     //si no hay retardo
 delay=250;
 LED_RGB (color);
 Printf_Color(color);


 color++;
 if(color>WHITE){
  color=BLACK;
 }

  }
  }

  }

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);
}






#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);




#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 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);
}




No hay comentarios:

Publicar un comentario