PICKit 1 and Simple LED

Discussion in 'Embedded Systems and Microcontrollers' started by SteveO, Nov 12, 2009.

  1. SteveO

    Thread Starter Active Member

    May 15, 2008
    Hi all,

    I am very new to microcontrollers but am very excited to learn the fundamentals of them. I have purchased a PICkit1 for just this reason.

    I have tried to create a simple blinking LED using a PIC16F684 and PICkit 1 development board from Microchip. However, all I am getting is constant LED luminance and it does not seem to be pulsing at all.

    I have created it in c using the HI-TECH C Compilier.

    Does anyone see any problems in my few lines of code here? Basically have a 2ms delay nested inside a for-loop for 128 repetitions. This should give me approximately 4Hz on the output.

    #include <htc.h>

    #define _XTAL_FREQ 4000000 // define oscillator freq = 4Mz

    void main(void){
    int x;
    TRISC2 = 0; //set RC2 as output
    PORTC = 0; // initialize PORTC low
    if (PORTC==0){
    PORTC = 4; //set RC2 high
    PORTC = 0; //set RC2 low
    for(x = 0; x<=127; x++){
    __delay_ms(2); //delay 2ms

    All help and suggestions is appreciated.

  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    You need to turn the comparators off, try adding
    CMCON0 = 0x07; // comps OFF
    in your port setup code. See page 61 of the datasheet.

    There may be other issues with that PIC too, you should have a read through the datasheet in chapter 4 the PORT sections.

    Also your code is flawed because you are testing if the whole PORTC==0 but only 1 pin is an output. So it will probably never match PORTC==0.

    It's much better form to use a variable for your bit control sequencing, then just write to the port.
  3. SteveO

    Thread Starter Active Member

    May 15, 2008
    Thanks guys. I will try turning the comparators off.

    I first initialize PORTC = 0 so the first time through the if/else statement it should set PORTC = 4, then it comes back through and PORTC =/ 0 so it then sets PORTC = 0. Am I missing something how PORTC may not ever be identically 0?

    I will try messing around with this too.

    For the delay the 2ms delay is nested inside a for-loop for 128 times which is equivalent to a 256ms delay. Thus 'ON', 256ms delay, 'OFF', 256ms delay. I will correct my first statement but this should give me approximately 2Hz frequency of the LED.

    Again I appreciate the help and I'll let you know where I get.
  4. SteveO

    Thread Starter Active Member

    May 15, 2008
    Hmm still having no luck.

    I have added the CMCON0 = 7; and ANSEL = 0; to turn off the comparators and ADC ports and am having the same output. I have tried a few different ways of implementing this delay whether it be a couple of for loops, a string of delay statements or a for-loop with a delay statement and still get the same response.

    It seems I am missing something critical because if I try to even change the pin that I am using for my output I still get the same LED (D0) illuminated on my PICKit1 demo board.

    Thanks for the help.
  5. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    Yes the problem is that test you are doing. You are reading the whole PORTC which is no good.

    I have changed your code to remove the test, instead what it does is just make a delay, and toggle the PORTC.2 bit.

    Code ( (Unknown Language)):
    2. #include <htc.h>
    4. #define _XTAL_FREQ 4000000 // define oscillator freq = 4Mz
    6. void main(void)
    7. {
    8. int x;
    9. TRISC2 = 0; //set RC2 as output
    10. PORTC = 0; // initialize PORTC low
    11. CMCON0 = 0x07;
    12. ANSEL = 0;
    14. while(1)
    15. {
    16.   PORTC += 4;  // toggle PORTC.2
    18.   for(x = 0; x<=127; x++)
    19.   {
    20.     __delay_ms(2); //delay 2ms
    21.     CLRWDT();
    22.   }
    23. }
    24. }
  6. eng1

    New Member

    Oct 25, 2009

    Are you setting the configuration bits in the MPLAB enviroment? specifically, which oscillator are you going to use in your circuit? It would be advisable to use the CONFIG statement at the beginning of the program and set the configuration bits in code.
  7. SteveO

    Thread Starter Active Member

    May 15, 2008
    THE_RB and eng1, thanks very much for your time/help. I will look more into the config statement, and try the revised code.

  8. SteveO

    Thread Starter Active Member

    May 15, 2008
    Thanks all. Now have the code working. The only issue I still have is that initially when I load the program the LED is illuminated. It seems that the program does not actually start running i.e. LED turning on and off, until I push the push-button switch on the pickit 1 board. Is this normal for these pickits, or is there a way I can go at by-passing this?

    Thanks again.