PIC = Change LED Flash Rate With Pot

Thread Starter

stringer85

Joined Sep 25, 2008
4
Alright Guys

Please forgive me if this sounds trivial, I'm very new with PIC programing.

Anyway I'm trying to find away to change the rate an LED flashes (NOT Brightness) using a pot input.

Any Ideas?

I'm guessing I would need some sort of ADC input for the pot to work.

Then somehow update the delay loop variable with respect to the ADC Value.

Im using a PICkit1 with the 16f684 PIC.

I understand I could simply make this circuit using a 555 timer but I'm trying to do this digitally.

Thanks
 

SgtWookie

Joined Jul 17, 2007
22,230
Do you still have the source code that you installed with the PICkit 1?

If so, have a look at default12.c and default12.h. They are in the \PICkit 1\DefaultDemonstration directory.
Look at the isr near the bottom of default12.c.

Remember, when you received the PICkit 1, it came with a 12F675 that was programmed to flash the 8 LEDs on the board; and you could change the rate of flash by changing RP1.
 

Thread Starter

stringer85

Joined Sep 25, 2008
4
I'm (trying) programming in C. If you think it'll be easier to program in BASIC then I might have ago at that.

SgtWookie I do have the code, but I can't find any called "default12.c"

I checked the PICkit disc again and all it contains is a couple of tutorials and the code which correlates to the Evil Genius book.

There's one called cPWM.c

This piece of code reads the pot and turns on X amount of the 8 leds corresponding to the value of the pot input:




#include <pic.h>
/* cPWM - Display the PICkit Pot Input Value as an LED Brightness Level

This program samples the voltage on RA0 using the ADC and Displays it using
a PWM value on "P1D" (RC2). This Program is designed for the

myke predko
04.11.16

*/

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
& UNPROTECT & BORDIS & IESODIS & FCMDIS);


int i, j, k;
int ADCState = 0; // Keep Track of ADC Operation
int ADCValue = 0;
int Dlay = 63; // LED Time on Delay Variable
const char PORTAValue[8] = {0b010000, 0b100000, 0b010000, 0b000100,
0b100000, 0b000100, 0b000100, 0b000010};
const char TRISAValue[8] = {0b001111, 0b001111, 0b101011, 0b101011,
0b011011, 0b011011, 0b111001, 0b111001};
const char NOTPORTA[8] = {0, 0, 0, 0, 0, 0, 0, 0};

main()
{

PORTA = 0;
CMCON0 = 7; // Turn off Comparators

ANSEL = 1; // Just RA0 is an Analog Input
ADCON0 = 0b00000001; // Turn on the ADC
// Bit 7 - Left Justified Sample
// Bit 6 - Use VDD
// Bit 4:2 - Channel 0
// Bit 1 - Do not Start
// Bit 0 - Turn on ADC
ADCON1 = 0b00010000; // Selemct the Clock as Fosc/8

PR2 = 0x0FF; // Work the Full Range
T2CON = 0b00000100; // TMR2 On with No Prescaler

CCP1CON = 0b01001100; // Enable PWM with P1D Active High
PWM1CON = 0; // No Turn on/Off Delay
TRISC2 = 0; // Make RC2/P1D Output Active
CCPR1L = 0x080; // Start PWM at Intermediate Value

k = 0;

while(1 == 1) // Loop Forever
{

for (i = 0; i < 8; i++ )
{ // Loop through Each of the 8 LEDS
for (j = 0; j < Dlay; j++); // Display "On" Delay Loop
if ((ADCValue & (1 << i)) == 0)
PORTA = NOTPORTA;
else
PORTA = PORTAValue;
TRISA = TRISAValue;
} // rof

switch (ADCState) // ADC State Machine
{
case 0: // Finished, Start Next Sample
GODONE = 1;
ADCState++;
break;
case 1: // Wait for ADC to complete
if (!GODONE)
ADCState++; // Sample Finished
break;
case 2: // Save Sample Value in "ADCValue"
ADCValue = ADRESH;
ADCState = 0;
break;
} // hctiws

k = (k + 1) % 10; // Update PWM Every 100 ms
if (0 == k)
CCPR1L = ADCValue; // Simply Change the PWM Value to Change the
// LED Output

} // elihw
} // End cPWM
 
Top