Okay MikroC isn't helping program a P16F684

Discussion in 'Embedded Systems and Microcontrollers' started by ajm113, May 7, 2011.

  1. ajm113

    Thread Starter Member

    Feb 19, 2011
    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:
    Code ( (Unknown Language)):
    2. void main() {
    4.   ANSEL  = 0;            // Configure AN pins as digital
    5.  // ANSELH = 0;
    6.  // C1ON_bit = 0;          // Disable comparators
    7.  // C2ON_bit = 0;
    9.   TRISA = 0x00;          // set direction to be output
    10.   //TRISA = 0x00;          //  set direction to be output
    11.   //TRISC = 0x00;          // set direction to be output
    12.   //TRISD = 0x00;          // set direction to be output
    14.   do {
    15.     PORTA = 0x00;        // Turn OFF LEDs on PORTA
    16.     //PORTB = 0x00;        // Turn OFF LEDs on PORTB
    17.   //  PORTC = 0x00;        // Turn OFF LEDs on PORTC
    18.     //PORTD = 0x00;        // Turn OFF LEDs on PORTD
    19.     Delay_ms(5000);      // 1 second delay
    21.     PORTA = 0xFF;        // Turn ON LEDs on PORTA
    22.     //PORTB = 0xFF;        // Turn ON LEDs on PORTB
    23.    // PORTC = 0xFF;        // Turn ON LEDs on PORTC
    24.     //PORTD = 0xFF;        // Turn ON LEDs on PORTD
    25.     Delay_ms(5000);      // 1 second delay
    26.   } while(1);            // Endless loop
    27. }
    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: May 7, 2011
  2. DumboFixer

    Active Member

    Feb 10, 2009
    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.
  3. ajm113

    Thread Starter Member

    Feb 19, 2011
    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: May 7, 2011
  4. DerStrom8

    Well-Known Member

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

    AAC Fanatic!

    Feb 11, 2008
    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.
  6. ajm113

    Thread Starter Member

    Feb 19, 2011
    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:

    Code ( (Unknown Language)):
    2.   ANSELH = 0;
    3.   C1ON_bit = 0;          // Disable comparators
    4.   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
  7. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    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. :)