This code is for a 12f683 pic, connected to TIP31 transistors and white LEDs. It generates a variety of strobe effects. Randomly it will create either darkness (none), synchronized strobe on both channels, random strobe on both channels, the duration of the on pulse and off pulse also varies randomly. This makes a great Halloween effect.
/*
* File: strobemain.c
* Author: hmikelson
*
* Created October 2016
*/
#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> /*rand()*/
#pragma config MCLRE=OFF,CP=OFF,WDTE=OFF,FOSC=INTOSCIO
#define _XTAL_FREQ 4000000
uint8_t sGPIO;
void init()
{
//Configure GPIO Port
ANSEL = 0b00000000; //Configure all GPIO pins as digital
TRISIO = 0b11001000; //Set GP3 as input and the rest as outputs
// GP0 = eye 1, GP2 = eye 2, GP5 = speaker, GP4 = piezo
OPTION_REGbits.nGPPU = 0;
WPU = 0b00000100; //Enable weak pullups on GP2
//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)
{
int i;
for (i=0;i<=n;i++)
{
__delay_us(1);
}
}
void strobe1(int r)
{
int i,n;
n=200/r;
for (i=0;i<n;i++)
{
GPIO = 0b11000011;
vdelay(100);
GPIO = 0b11000000;
vdelay(r*1000);
}
}
void strobe2(int r)
{
int i,n;
n=200/r;
for (i=0;i<n;i++)
{
GPIO = 0b11000011;
vdelay(200);
GPIO = 0b11000000;
vdelay(r*1000);
}
}
void strobe3(int r)
{
int i,n,r1;
n=200/r;
for (i=0;i<n;i++)
{
r1 = rand()%16;
GPIO = 0b11000011;
vdelay(100);
GPIO = 0b11000000;
vdelay(r1*1000);
}
}
void strobe4(int r)
{
int i,n,r1;
n=200/r;
for (i=0;i<n;i++)
{
r1 = rand()%16;
GPIO = rand();
vdelay(100);
GPIO = 0b11000000;
vdelay(r1*500);
}
}
void darkness(void) // PWM sweep high f to low f
{
GPIO = 0b11000000;
__delay_ms(2000);
}
void main()
{
uint8_t r,r2;
init();
while(1)
{
r=rand()%8;
//r=2;
r2 = rand()%32+1;
switch (r)
{
case 1:
strobe1(r2);
break;
case 2:
strobe2(r2);
break;
case 3:
strobe3(r2);
break;
case 4:
strobe4(r2);
break;
default:
darkness();
}
}
}
No comments:
Post a Comment