PICKit 1 and Simple LED

Thread Starter

SteveO

Joined May 15, 2008
33
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
while(1){
if (PORTC==0){
PORTC = 4; //set RC2 high
}
else{
PORTC = 0; //set RC2 low
}
for(x = 0; x<=127; x++){
__delay_ms(2); //delay 2ms
CLRWDT();
}
}
}

All help and suggestions is appreciated.

Thanks.
 

THE_RB

Joined Feb 11, 2008
5,438
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.
 

Thread Starter

SteveO

Joined May 15, 2008
33
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.
 

Thread Starter

SteveO

Joined May 15, 2008
33
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.
 

THE_RB

Joined Feb 11, 2008
5,438
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.

Rich (BB code):
#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
CMCON0 = 0x07;
ANSEL = 0;

while(1)
{
  PORTC += 4;  // toggle PORTC.2

  for(x = 0; x<=127; x++)
  {
    __delay_ms(2); //delay 2ms
    CLRWDT();
  }
}
}
 

eng1

Joined Oct 25, 2009
13
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.


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.
 

Thread Starter

SteveO

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

Appreciated.
 

Thread Starter

SteveO

Joined May 15, 2008
33
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.
 
Top