This is the code used to generate fading Christmas lights and to play jingle bells.
/*
* File: main.c
* Author: Hans Mikelson
*
* Created on November 30, 2014
*/
#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 */
#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 = 0b11101000; //Set GP0, GP1, GP2 as outputs and the rest as inputs
WPU = 0x00; //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)
{
int i;
for (i=0;i<=n;i++)
{
__delay_us(10);
}
}
void tone(int n, int m)
{
int i;
for (i=0;i<=m;i++)
{
GPIO = 0b00000000;
vdelay(n);
GPIO = 0b00010000;
vdelay(n);
}
vdelay(100);
}
void main()
{
int i,j,k,l,ri,rv,gi,gv,bi,bv;
signed char ra[7]={1,0,-1,0,0,0,0};
signed char ga[7]={0,0,0,0,1,0,-1};
signed char ba[7]={0,0,1,0,-1,0,0};
rv=0;gv=0;bv=0;
init();
GPIO = 0b00000000;
sGPIO= 0b00000000;
vdelay(1000);
tone(40,100);
tone(40,100);
tone(40,200);
tone(40,100);
tone(40,100);
tone(40,200);
tone(40,100);
tone(34,118);
tone(50,160);
tone(45,45);
tone(40,250);
while(1)
{
for (j=0;j<7;j++)
{
for (k=0;k<63;k++)
{
ri=ra[j];gi=ga[j];bi=ba[j];
for(l=0;l<16;l++)
{
for (i=0;i<63;i++)
{
if (i<rv)
{
sGPIO = 0b00000001 | sGPIO;
}
else
{
sGPIO = 0b11111110 & sGPIO;
}
if (i<gv)
{
sGPIO = 0b00000010 | sGPIO;
}
else
{
sGPIO = 0b11111101 & sGPIO;
}
if (i<bv)
{
sGPIO = 0b00000100 | sGPIO;
}
else
{
sGPIO = 0b11111011 & sGPIO;
}
GPIO = sGPIO;
}
}
rv=rv+ri;gv=gv+gi;bv=bv+bi;
}
}
}
}