Sunday, November 4, 2018

This program creates a lighting effect that includes a very slow fade, strobe lights, and flashing lights.  It uses a 12f683 and pin 3 for input so you need to provide a pull up resistor (10k) and a pull down resistor (1k) when you push the button.  It  does not use an interrupt so you have to wait until the routine gets around to checking on the button, in other words you may have to hold the button down for a while especially during the slow cross fade.  Use a TIP31 for the LEDs (9V->LED->R->TIP31->Gnd) and a high current 7805 power regulator.  9V 2 A DC power supply.

/* 
 * File:   ls5main.c
 * Author: Hans Mikelson
 *
 * Created on December 17, 2014, 8:59 AM
 */

#if defined(__XC)
    #include <xc.h>         /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>        /* HiTech General Include File */
#endif

#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */
#include <stdlib.h>        /* includes rand() */

#pragma config MCLRE=OFF,CP=OFF,WDTE=OFF,FOSC=INTOSCIO
#define _XTAL_FREQ 4000000
uint8_t sGPIO, gr1=0, gr2=0;  // Global shadow register and random variables

void init()
{
    //Configure GPIO Port
    ANSEL =  0b00000000;  //Configure all GPIO pins as digital
    TRISIO = 0b110001000;  //Set GP3 as input the rest as outputs
    OPTION_REGbits.nGPPU = 0;
    WPU = 0b00000000;     //Disable weak pullups
    //Configuer AD Convertor
    ADCON0 = 0x00;        //AD disabled
    ADRESH = 0x00;        //Init the AD Register
    //Configure Comparator
    CMCON0 = 0xFF;   // Comparator is turned off
    CMCON1 = 0x00;   // Comparator is turned off
    //Interrupt configuration
    INTCON = 0x00;   //Disable all interrupts
}

void vdelay(int n) //Variable time delay
{
    int i;
    for (i=0;i<=n;i++)
    {
     __delay_us(100);
    }
}

void xfade(int n)  //Cross fade between two global random #s
{                  //n controls the very slow fade time
    int j, k, i;
        for (j=0;j<255;j++)
         {
          for (i=0;i<n;i++)
           {
            for (k=0;k<255;k++)
             {
              if (j<k)
               {
                GPIO=gr2;
               }
              else
               {
                GPIO=gr1;
               }
             }
           }
         }
        gr2 = gr1;
        gr1 = rand() & rand();
}

void strobe(int n)  //Create a strobing cross fade
{
    int j, k, i;
        for (j=0;j<63;j++)
         {
          for (i=0;i<1;i++)
           {
            for (k=0;k<63;k++)
             {
              if (j<k)
               {
                GPIO=gr2;
                vdelay(n);
               }
              else
               {
                GPIO=gr1;
                vdelay(n);
               }
             }
           }
         }
        gr2 = gr1;
        gr1 = rand() & rand();
}

void main()
{
    int i=1;
    init();
    GPIO = 0b00000000;

    while(1)
    {
     if (GPIObits.GP3 == 0)
      {
       i=(i+1) % 9;
       GPIO = 0b00000000;
       vdelay(10000);
      }
     switch (i)
      {
       case 1:                  //Medium flash
        GPIO = rand() & rand(); 
        vdelay(4000);
        break;
       case 2:                  //Fast flash 
        GPIO = rand() & rand();
        vdelay(2000);
        break;
       case 3:
           xfade(1);
        break;
       case 4:
           xfade(8);
        break;
       case 5:
           xfade(32);
        break;
       case 6:
           strobe(4);
        break;
       case 7:
           strobe(8);
        break;
       case 8:
           strobe(32);
        break;
      default:                    // Slow flash
       GPIO = rand() & rand();
       vdelay(8000);
       break;
      }
    }
}

No comments:

Post a Comment