Okay MikroC isn't helping program a P16F684

Thread Starter

ajm113

Joined Feb 19, 2011
174
Alright well I got the PICkit 1 today, and the first thought that came through my mind, "Well alright the tutorials have got to be pretty useful!" After the first tutorial I was already lost in the example assembly source code (I got the code to compile and run on a PIC fine, it's just with Assembly I'm lost).

So I tried going though the booklet and try to look at the documentation provided with the source code and kinda felt overwhelmed on how much I would have to remember and decided to go with something I know pretty well is C.

After going through compilers and trying and testing things out and got the MikroC compiler and tried to run this source code:
Rich (BB code):
void main() {

  ANSEL  = 0;            // Configure AN pins as digital
 // ANSELH = 0;
 // C1ON_bit = 0;          // Disable comparators
 // C2ON_bit = 0;

  TRISA = 0x00;          // set direction to be output
  //TRISA = 0x00;          //  set direction to be output
  //TRISC = 0x00;          // set direction to be output
  //TRISD = 0x00;          // set direction to be output

  do {
    PORTA = 0x00;        // Turn OFF LEDs on PORTA
    //PORTB = 0x00;        // Turn OFF LEDs on PORTB
  //  PORTC = 0x00;        // Turn OFF LEDs on PORTC
    //PORTD = 0x00;        // Turn OFF LEDs on PORTD
    Delay_ms(5000);      // 1 second delay

    PORTA = 0xFF;        // Turn ON LEDs on PORTA
    //PORTB = 0xFF;        // Turn ON LEDs on PORTB
   // PORTC = 0xFF;        // Turn ON LEDs on PORTC
    //PORTD = 0xFF;        // Turn ON LEDs on PORTD
    Delay_ms(5000);      // 1 second delay
  } while(1);            // Endless loop
}
All it does, no matter what I do is turn the D2 and D3 LEDs on and off on the PICKit and the Delay_ms seems to be working. It's just not turning one LED on and off I expected. :(

So I don't know what I'm doing wrong, the code compiles fine, but I don't get much different results and I know I should use C since it's easier for me to use, but I just have no idea what is up with finding a good C compiler that I can use the PIC16F648 with.
 
Last edited:

DumboFixer

Joined Feb 10, 2009
217
Probably a very stupid question but have you loaded the program into the PicKit ?

The code you've given has nothing to do with PortD so I reckon that the previous program is still present.

I'm also slightly cofused as you post C code but discuss Assembly Tutorials.
 

Thread Starter

ajm113

Joined Feb 19, 2011
174
Yes, or at least it's failing to work, like you mentioned it's using an older program, but it's so odd it takes the delay from my program into account with the older program. That makes no sense to me when I'm getting no errors from the compiler, or the programmer from PICKit1.

As for your last sentence that was a typo, and I mean the source code that came in the CD that had example code. I was hopping they would at least go into more detail or have tutorials that would tell you how to put code together to do simple functions in the PIC, before making it do physical stuff such as turning a LED on and off with a button.
 
Last edited:

DerStrom8

Joined Feb 20, 2011
2,390
I haven't used mikroC, but perhaps you could try:
while(1)
{
...
}
Instead of saying the "do {...} while(1)". It may be worth a shot ;)
 

THE_RB

Joined Feb 11, 2008
5,438
The 16F684 needs the line;
CMCON0 = 0x07;
added in your setup to turn off the comparators and make the pins digital!

You could also UNcomment these lines;
// ANSELH = 0;
// C1ON_bit = 0; // Disable comparators
// C2ON_bit = 0;

Which do the same thing, ie turn off the comparators and ADC and set all the pins to digital.

These are the first things you should check with any PIC chip, along with setting the TRIS registers to set the pin directions. :)

Please check in the datasheet for specifics, as it changes from PIC to PIC.
 

Thread Starter

ajm113

Joined Feb 19, 2011
174
Well before I can say using the while is better or not I still have issues, the reasons why I commented some of those variables out is because they are undefined such as this area:

Rich (BB code):
  ANSELH = 0;
  C1ON_bit = 0;          // Disable comparators
  C2ON_bit = 0;
They don't seem to work on this specific type of chip, but I'm guesting this sort of thing is documented in the Datasheet for this certain pic?

How do the pins in the header have PORTA and all the sudden jump to PORTC without PORTB or PORTD even defined?

Sadly I'm still getting the same results. :S
 

THE_RB

Joined Feb 11, 2008
5,438
You could have a number of issues including your PIC oscillator/xtal not being set up properly.

If you move your LED to a PORTB pin and flash all the PORTB pins on and off that will help diagnose if the PIC is running. Make sure to set TRISB so PORTB pins are outputs.

Please read through the datasheet for your PIC, you will be doing that a lot in the early stages. Read the oscillator section, first and make sure the PIC osc is running and PORTB flashes ok.

Once that is working read the sections on the comparator and ADC as they have great effect on PORTA pins. :)
 
Top