Flashing LED and PICs

Discussion in 'Embedded Systems and Microcontrollers' started by RhysGM, Jun 21, 2013.

  1. RhysGM

    Thread Starter New Member

    Nov 4, 2012
    10
    0
    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?
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  3. RhysGM

    Thread Starter New Member

    Nov 4, 2012
    10
    0
    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. :)
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    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!
     
  5. RhysGM

    Thread Starter New Member

    Nov 4, 2012
    10
    0
    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.
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    What software? Knowing what compiler (very good) or assembler (kinda scary) you want to use would help us help you.
     
  8. RhysGM

    Thread Starter New Member

    Nov 4, 2012
    10
    0
    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. :)

    MPLAB IDE v8.91
     
  9. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    This is not a compiler... this is an IDE... MikroC, XC8, Hi-Tech, are examples of compilers...
     
  10. RhysGM

    Thread Starter New Member

    Nov 4, 2012
    10
    0
    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.
     
  11. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    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).
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    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.
     
  13. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    Ok here is one for 16f628 I wrote in Pic Basic Pro. for your reference.

    Code ( (Unknown Language)):
    1.  
    2. '******************************************************************
    3. '*  Name    : 16F628 LED FLASHER.BAS                               *
    4. '*  Author  : absf                                                                                *
    5. '*  Notice  : Copyright (c) 2013 [NOBODY]                              *
    6. '*          :                                                                                                *
    7. '*  Date    : 7/14/2013                                                                       *
    8. '*  Version : 1.0                                                                                  *
    9. '*  Notes   : flash led GN_3T RD_2T AM_1T & REPEAT   *
    10. '*          :                                                                                                 *
    11. '*******************************************************************
    12.  
    13. 'INCLUDE  "16F628A.INC"
    14. @device  pic16F628A, fosc_intoscio, wdt_on, mclr_on, lvp_off, protect_off
    15. '_FOSC_INTOSCIO       EQU  H'3FFC'    ; INTOSC oscillator: I/O function on
    16.  
    17. DEFINE  OSC 4           '4 MHZ INTERNAL OSC
    18. G_LED    var  PORTB.0   'Geeen LED
    19. R_LED    VAR  PORTB.1   'Red LED
    20. A_LED    Var  PORTB.2   'Amber LED
    21. n        Var  Byte
    22.  
    23.    TRISB = $00         ' Set segment pins and RB0 to output
    24.    CMCON = $07         ' disable analog
    25.    
    26. mainloop:
    27.     for n=1 to 5
    28.         low g_led      ' switch on LED
    29.         pause 500      ' on for 0.5 seconds
    30.         high g_led     ' switch off LED
    31.         pause 500      ' off for 0.5 seconds
    32.     next n
    33.  
    34.     for n=1 to 2
    35.         low R_led
    36.         pause 500      ' on for 0.5 seconds
    37.         high R_led
    38.         pause 500      ' off for 0.5 seconds
    39.     next n
    40.  
    41.     low A_led
    42.     pause 500
    43.     HIGH a_led
    44.     pause 500
    45.  
    46.    GoTo mainloop       ' Do it forever
    47.  
    48.  
    49.    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
     
    Last edited: Jul 18, 2013
  14. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    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:

    Code ( (Unknown Language)):
    1. #include <xc.h>
    2.  
    3.  // blinkie leds for the PIC16F627A
    4.  
    5. #pragma config FOSC=INTOSCIO, WDTE=OFF, PWRTE=OFF, MCLRE=ON, BOREN=OFF, LVP=OFF, CPD=OFF, CP=OFF,
    6.  
    7. #define _XTAL_FREQ 4000000  // note: FOSC=INTOSCIO gives a 4MHz clock
    8.  
    9. #define RED_LED     PORTAbits.RA0
    10. #define GREEN_LED   PORTAbits.RA1
    11. #define AMBER_LED   PORTAbits.RA2
    12.  
    13. #define OFF 0
    14. #define ON  1
    15.  
    16. void main(void)
    17. {
    18.     CMCON = 0b00000111;     // since port pins default to analog if they
    19.                             // have an analog function we start by
    20.                             // turning the comparators off
    21.        
    22.     RED_LED     = OFF;      // clear the LEDs before we turn them on
    23.     GREEN_LED   = OFF;
    24.     AMBER_LED   = OFF;
    25.    
    26.     TRISA = 0b00000000;     // set all port bits to 0utput (not 1nput)
    27.    
    28.     while(1)                // loop here forever and ever
    29.     {
    30.         // flash green 5 times
    31.         GREEN_LED   = ON;
    32.         __delay_ms(100);        // on for .1 sec
    33.         GREEN_LED   = OFF;
    34.         __delay_ms(500);        // off for .5 sec
    35.  
    36.         GREEN_LED   = ON;
    37.         __delay_ms(100);        // on for .1 sec
    38.         GREEN_LED   = OFF;
    39.         __delay_ms(500);        // off for .5 sec
    40.  
    41.         GREEN_LED   = ON;
    42.         __delay_ms(100);        // on for .1 sec
    43.         GREEN_LED   = OFF;
    44.         __delay_ms(500);        // off for .5 sec
    45.  
    46.         GREEN_LED   = ON;
    47.         __delay_ms(100);        // on for .1 sec
    48.         GREEN_LED   = OFF;
    49.         __delay_ms(500);        // off for .5 sec
    50.  
    51.         GREEN_LED   = ON;
    52.         __delay_ms(100);        // on for .1 sec
    53.         GREEN_LED   = OFF;
    54.         __delay_ms(500);        // off for .5 sec
    55.  
    56.         // flash red twice
    57.         RED_LED     = ON;
    58.         __delay_ms(100);        // on for .1 sec
    59.         RED_LED     = OFF;
    60.         __delay_ms(500);        // off for .5 sec
    61.  
    62.         RED_LED     = ON;
    63.         __delay_ms(100);        // on for .1 sec
    64.         RED_LED     = OFF;
    65.         __delay_ms(500);        // off for .5 sec
    66.  
    67.         // flash amber        
    68.         AMBER_LED   = ON;
    69.         __delay_ms(100);        // on for .1 sec
    70.         AMBER_LED   = OFF;
    71.         __delay_ms(500);        // off for .5 sec
    72.     }
    73. }
    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.
     
  15. ananthapriya

    New Member

    Jul 18, 2013
    10
    3
    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: Jul 25, 2013
  16. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    Hey... absolutely ANY PIC is enough for this blinkie project. That includes the PIC10F200 in the SOT-6 package.
     
Loading...