Flashing LED and PICs

Thread Starter

RhysGM

Joined Nov 4, 2012
10
Hello, I'm currently working on building the into darkness star trek enterprise it's fairly large with lots of room inside.

I'm currently running some small but bright 20mA LED's into the nacelle's front and back, a couple in engineering and one in the saucer section. I've left the deflector detachable to fit a 9v battery located in the secondary hull.

This is all simple and straightforward, wiring a 470r resistor to each LED then to the battery.

However I would like to put a tri colour LED in the bridge, I was going to use a couple transistors and capacitors to create a flashing light circuit. But I would like to learn how to use the PIC programmer I already have. I've never used it and I've also got a wide selection of PIC16Fxxx and PIC12Fxxx IC's.

It's only a few simple operations I want to do ie, flash green 5 times, flash red twice, flash amber then repeat for example. But where do I start?
 

t06afre

Joined May 11, 2009
5,934

Thread Starter

RhysGM

Joined Nov 4, 2012
10
Thanks for your quick response, I've never used C before and have some BASIC experience.

I'll give it a go. but I'm sure I'll be back. :)
 

ErnieM

Joined Apr 24, 2011
8,377
OshonSoft has a very reasonable Basic compiler for PICs. I havn't used it in a few years (I use Microchip's free C now) but it was a very serviceable package, far better then PicBasicPro for over 10 times the price.

It's as good a tool as any to start leaning micros.

Engage!
 

Thread Starter

RhysGM

Joined Nov 4, 2012
10
Hello again,

I'm completely lost, I've installed the software and I have all the components and hardware. Can anyone point me in the direction of a tutorial that shows me how to use a PIC16F628A to flash a single LED using an XTAL for timing?

Any help is appreciated.
 

Thread Starter

RhysGM

Joined Nov 4, 2012
10
I saw this however the circuit diagram confused me as the pins are not in the same order as the datasheet I have for the pic.


I couldn't find any really simple examples on here.


I didn't see this one, so will have a read and see if I understand any of it. :)

You should mention what compiler you are trying to use...

which?
MPLAB IDE v8.91
 
The 16F628A has two internal oscillators 48kHz & 4MHz, so no need for a crystal. I'd go with the 48kHz for flashing LEDs. Add a 0.1µF cap across the PIC power pins and a 10K pull-up on MCLR and you're good to go.

XC8 is a pretty handy compiler, that's what I'm using now and it's a really nice PIC compiler.

As for the RGB LED each color will have a different forward voltage so if you want equal brightness you need 3 different resistors.

Are you going to blink other LEDs too? If so describe the sequence(s).
 

ErnieM

Joined Apr 24, 2011
8,377
I thought an IDE is just a visual interface for a compiler, my assumption was that this is both and IDE and complier in one.
All MPLAB comes with is the assembler, which I don't recommend as compilers are easier and more productive to use.

Microchip gives away several C compilers for free. The XC8 is probably as good as any for you to start with.

This (and many other) compilers nest along with MPLAB allowing you to use various tools in the same development environment.
 

absf

Joined Dec 29, 2010
1,968
Thanks for your quick response, I've never used C before and have some BASIC experience.

I'll give it a go. but I'm sure I'll be back. :)
Ok here is one for 16f628 I wrote in Pic Basic Pro. for your reference.

Rich (BB code):
'******************************************************************
'*  Name    : 16F628 LED FLASHER.BAS                               *
'*  Author  : absf                                                                                *
'*  Notice  : Copyright (c) 2013 [NOBODY]                              *
'*          :                                                                                                *
'*  Date    : 7/14/2013                                                                       *
'*  Version : 1.0                                                                                  *
'*  Notes   : flash led GN_3T RD_2T AM_1T & REPEAT   *
'*          :                                                                                                 *
'*******************************************************************

'INCLUDE  "16F628A.INC"
@device  pic16F628A, fosc_intoscio, wdt_on, mclr_on, lvp_off, protect_off
'_FOSC_INTOSCIO       EQU  H'3FFC'    ; INTOSC oscillator: I/O function on 

DEFINE  OSC 4           '4 MHZ INTERNAL OSC
G_LED    var  PORTB.0   'Geeen LED
R_LED    VAR  PORTB.1   'Red LED
A_LED    Var  PORTB.2   'Amber LED
n        Var  Byte

   TRISB = $00         ' Set segment pins and RB0 to output
   CMCON = $07         ' disable analog
   
mainloop:
    for n=1 to 5
        low g_led      ' switch on LED
        pause 500      ' on for 0.5 seconds
        high g_led     ' switch off LED
        pause 500      ' off for 0.5 seconds
    next n

    for n=1 to 2
        low R_led
        pause 500      ' on for 0.5 seconds
        high R_led
        pause 500      ' off for 0.5 seconds
    next n

    low A_led
    pause 500
    HIGH a_led
    pause 500

   GoTo mainloop       ' Do it forever


   END
I have added the schematics in case you want to experiment on breadboard. The power supply section is optional if you already have the 5V DC available
 

Attachments

Last edited:

ErnieM

Joined Apr 24, 2011
8,377
Good tutorials are getting hard to find right now as Microchip is attempting to "simplify" how to program any PIC. They seem to be doing a good job, but it means much of the existing documentation, sample programs, and tutorials don't work anymore. They are easily fixed by an experienced person, but that leaves the new people out in the cold.

I still recommend starting with a Microchip development kit that comes with a PIC on a board along with a programmer/debugger. The tutorials have aged but there is lots of help around (here, the Microchip Forums) to get you over the speed bumps.

Anyway I used your task to try out the CX8 compiler on a new computer. Here's how the program looked:

Rich (BB code):
#include <xc.h>

 // blinkie leds for the PIC16F627A
  
#pragma config FOSC=INTOSCIO, WDTE=OFF, PWRTE=OFF, MCLRE=ON, BOREN=OFF, LVP=OFF, CPD=OFF, CP=OFF,

#define _XTAL_FREQ 4000000  // note: FOSC=INTOSCIO gives a 4MHz clock

#define RED_LED     PORTAbits.RA0
#define GREEN_LED   PORTAbits.RA1
#define AMBER_LED   PORTAbits.RA2

#define OFF 0
#define ON  1

void main(void) 
{
    CMCON = 0b00000111;     // since port pins default to analog if they
                            // have an analog function we start by
                            // turning the comparators off 
        
    RED_LED     = OFF;      // clear the LEDs before we turn them on
    GREEN_LED   = OFF;
    AMBER_LED   = OFF;
    
    TRISA = 0b00000000;     // set all port bits to 0utput (not 1nput)
    
    while(1)                // loop here forever and ever
    {
        // flash green 5 times
        GREEN_LED   = ON; 
        __delay_ms(100);        // on for .1 sec
        GREEN_LED   = OFF;
        __delay_ms(500);        // off for .5 sec

        GREEN_LED   = ON; 
        __delay_ms(100);        // on for .1 sec
        GREEN_LED   = OFF;
        __delay_ms(500);        // off for .5 sec

        GREEN_LED   = ON; 
        __delay_ms(100);        // on for .1 sec
        GREEN_LED   = OFF;
        __delay_ms(500);        // off for .5 sec

        GREEN_LED   = ON; 
        __delay_ms(100);        // on for .1 sec
        GREEN_LED   = OFF;
        __delay_ms(500);        // off for .5 sec

        GREEN_LED   = ON; 
        __delay_ms(100);        // on for .1 sec
        GREEN_LED   = OFF;
        __delay_ms(500);        // off for .5 sec

        // flash red twice
        RED_LED     = ON; 
        __delay_ms(100);        // on for .1 sec
        RED_LED     = OFF;
        __delay_ms(500);        // off for .5 sec

        RED_LED     = ON; 
        __delay_ms(100);        // on for .1 sec
        RED_LED     = OFF;
        __delay_ms(500);        // off for .5 sec

        // flash amber        
        AMBER_LED   = ON; 
        __delay_ms(100);        // on for .1 sec
        AMBER_LED   = OFF;
        __delay_ms(500);        // off for .5 sec
    }
}
It compiles and runs OK in the simulator (but you may want to comment out the pauses to do that). If I get some more time I'll explain what is going on here.
 

Attachments

hey choose PIC 16F877A.is this enough for led blinking project.i give another tips to you choose Mp lab software and GCC compiler and finally PIC programmer device these are necessary for dumping program in to the controller.



BacklinksVault
 
Last edited:
Top