Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pulsating LEDs

Status
Not open for further replies.

Noggin

Member
My wife carved a pumpkin for a company contest last night. They weren't allowed to put a light bulb or a candle in it, so I threw this together in about two hours last night. The LEDs pulsate smoothly. Other effects could be added as well to make the LEDs flicker randomly, or whatever else you can come up with.

Few seconds of video: https://www.dropbox.com/s/v7w4u3jskt02jv7/2012-10-30 21.50.07.mp4

Code:
#include <xc.h>

__CONFIG(FOSC_RC & WDTE_OFF & PWRTE_OFF & BOREN_OFF & BODENV_25 & CP_OFF);

/* **** Global Function Prototypes **** */

/* **** Global Variable Definitions **** */

/* **** Local Definitions **** */

/* **** Local Function Prototypes **** */

/* **** Local Variable Definitions **** */
unsigned char table[] = {
	0,	1,	2,	3,	4,	5,	6,	7,
	8,	9,	10,	11,	12,	13,	14,	15,
	16,	17,	18,	19,	20,	22,	24,	26,
	28,	30,	33,	36,	39,	42,	46,	50,
	55,	60,	66,	72,	79,	86,	94,	103,
	113,	124,	136,	149,	163,	179,	196,	215,
	236,	255,
};

/*******************************************************************************
* Created by		: 
* Date created		: OCT 2012
* Description		: 
*******************************************************************************/
void main( void )
{

unsigned long long timer = 0;
unsigned char index = 0;
unsigned char count_direction = 1;

	TRISBbits.TRISB3 = 0;			// Set PWM pin as output
	PORTBbits.RB3 = 0;				// Turn on LEDs

	CCP1CONbits.CCP1M = 0x0C;		// RB3 set for PWM
	PR2 = 0xFF;					// Set period
	CCPR1L = 0x00;				// Duty to 0%
	
	T2CONbits.TOUTPS = 0x07;		// Set timer 2 prescaler
	T2CONbits.TMR2ON = 1;			// Enable timer 2 (used as time base for PWM)

	while (1)
	{
		while (timer++ < 0x3FF)		// Adjust this value to control how quickly the LEDs pulse
			continue;

		timer = 0;

		if (index == 0)
		{
			CCPR1L = table[index++];
			count_direction = 1;
		}
		else if (index == sizeof(table))
		{
			CCPR1L = table[--index];
			count_direction--;
		}
		else if (count_direction)
			CCPR1L = table[index++];
		else
			CCPR1L = table[--index];
	}
}
 
Cool! :D

I heard there was a rat in Canada that actually died of natural causes.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top