1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Hi-Tech C Compiler: What am I doing wrong?

Discussion in 'Microcontrollers' started by micro571, Jun 28, 2009.

  1. micro571

    micro571 New Member

    Joined:
    Nov 30, 2003
    Messages:
    62
    Likes:
    0
    I am trying to program a PIC16F877A. I know my hardware is good because I can load on the LED blink program from here: blink a LED and it works.

    I am trying to do an led blink program using the Hi-Tech C Compiler. I have it specified as the tool chain in MPLab.

    Here is my code:
    #include <pic.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include "delay.h"
    //#include "delay.c"
    //void delay(void);
    //#define _16F877
    // __CONFIG(UNPROTECT|BODEN|FOSC1|BKBUG|WRT);
    //__CONFIG( XT & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN & DEBUGDIS & UNPROTECT );
    void main (){

    ADCON1 =0x06 ; // Changes PORTA to digital
    CMCON = 0x07 ; // Disable analog comparators
    PORTA = 0x00 ;
    PORTB = 0X00 ;
    PORTC = 0X00 ;
    PORTD = 0X00 ;
    PORTE = 0X00 ;
    TRISA = 0x00 ; // Configure PORTA as output
    TRISB = 0X00 ;
    TRISC = 0X00 ;
    TRISD = 0X00 ;
    TRISE = 0X00 ;

    while(1){
    PORTA = 0XFF ;
    PORTB = 0XFF ;
    PORTC = 0XFF ;
    PORTD = 0XFF ;
    PORTE = 0Xff ;
    DelayMs(500);
    PORTA = 0X00 ;
    PORTB = 0X00 ;
    PORTC = 0X00 ;
    PORTD = 0X00 ;
    PORTE = 0X00 ;
    DelayMs(500);
    }
    }

    The code compiles, but nothing happens when I load the hex file.
    It's a simple program. I must be missing something small. Any suggestions?
     
  2. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    just a thought how many leds do you have hooked to that chip it cant lite 33 all at one time
     
    Last edited: Jun 28, 2009
  3. micro571

    micro571 New Member

    Joined:
    Nov 30, 2003
    Messages:
    62
    Likes:
    0
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn

    try this and see if it works change the xtal setting to match yours
    Code (text):

    #include <htc.h>    // Required to interface with delay routines

    #ifndef _XTAL_FREQ
     // Unless already defined assume 4MHz system frequency
     // This definition is required to calibrate __delay_us() and __delay_ms()
     #define _XTAL_FREQ 4000000
    #endif
    void main(void){
    ADCON1 =0x06 ; // Changes PORTA to digital
    CMCON = 0x07 ; // Disable analog comparators
    PORTA = 0x00 ;
    PORTB = 0X00 ;
    PORTC = 0X00 ;
    PORTD = 0X00 ;
    PORTE = 0X00 ;
    TRISA = 0x00 ; // Configure PORTA as output
    TRISB = 0X00 ;
    TRISC = 0X00 ;
    TRISD = 0X00 ;
    TRISE = 0X00 ;

    while(1){
    PORTA = 0XFF ;
    PORTB = 0XFF ;
    PORTC = 0XFF ;
    PORTD = 0XFF ;
    PORTE = 0Xff ;
    __delay_ms(100);
    PORTA = 0X00 ;
    PORTB = 0X00 ;
    PORTC = 0X00 ;
    PORTD = 0X00 ;
    PORTE = 0X00 ;
    __delay_ms(100);
    }
    }
     
     
  6. micro571

    micro571 New Member

    Joined:
    Nov 30, 2003
    Messages:
    62
    Likes:
    0
    I changed your reference code value from 4MHz to 20MHz and compiled.

    As is, the new code produces an error:

    Error [499] ; . undefined symbol:
    ___delay_ms (877.obj)

    I'm using the Hi-Tech C compiler, v9.60.

    At the top of the delay.h file I was referencing in my code, I had defined:
    #define PIC_CLK 20000000
     
  7. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    Can you try this,

    Code (text):

    #include <pic.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>

    #define _16F877
    __CONFIG( HS & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN & DEBUGDIS & UNPROTECT );

    #define XTAL_FREQ   20MHZ
    #include "delay.c"

    void main (){
        ADCON1 =0x06 ; // Changes PORTA to digital
        CMCON = 0x07 ; // Disable analog comparators
        PORTA = 0x00 ;
        PORTB = 0X00 ;
        PORTC = 0X00 ;
        PORTD = 0X00 ;
        PORTE = 0X00 ;
        TRISA = 0x00 ; // Configure PORTA as output
        TRISB = 0X00 ;
        TRISC = 0X00 ;
        TRISD = 0X00 ;
        TRISE = 0X00 ;    
        while(1){
            PORTA = 0XFF ;
            PORTB = 0XFF ;
            PORTC = 0XFF ;
            PORTD = 0XFF ;
            PORTE = 0Xff ;
            DelayMs(500);
            PORTA = 0X00 ;
            PORTB = 0X00 ;
            PORTC = 0X00 ;
            PORTD = 0X00 ;
            PORTE = 0X00 ;
            DelayMs(500);
        }
    }
     
    Mike.
     
    Last edited: Jun 28, 2009
  8. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    I'm using the Hi-Tech C compiler, v9.60.
    I have v9.65 and it wouldn't build your code
     
  9. micro571

    micro571 New Member

    Joined:
    Nov 30, 2003
    Messages:
    62
    Likes:
    0
    I tried the code as-is and got a bunch of errors:
    Error [482] ; . symbol "_DelayMs_interrupt" is defined more than once in "877.obj"
    Error [482] ; . symbol "_DelayS" is defined more than once in "877.obj"
    Error [482] ; . symbol "_DelayMs" is defined more than once in "877.obj"
    Error [482] ; . symbol "_DelayBigMs" is defined more than once in "877.obj"
    Error [482] ; . symbol "_DelayBigUs" is defined more than once in "877.obj"
    Error [482] ; . symbol "_delayus_variable" is defined more than once in "877.obj"
    Error [482] ; . symbol "_DelayMs_interrupt" is defined more than once in "877.obj"
    Error [482] ; . symbol "_DelayS" is defined more than once in "877.obj"
    Error [482] ; . symbol "_DelayMs" is defined more than once in "877.obj"
    Error [482] ; . symbol "_DelayBigMs" is defined more than once in "877.obj"
    Error [482] ; . symbol "_DelayBigUs" is defined more than once in "877.obj"
    Error [482] ; . symbol "_delayus_variable" is defined more than once in "877.obj"

    I changed "delay.c" to "delay.h" and compiled...

    It worked! I've got a blinking led now.

    Thanks for your help. I'm guessing it's the config parameter that did it.

    I had tried it with this config earlier:
    __CONFIG( XT & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN & DEBUGDIS & UNPROTECT );
    but that didn't work.

    Thanks again for your speedy responses!

    Now it's time to upgrade blinky led program to something more useful. :)
     
  10. micro571

    micro571 New Member

    Joined:
    Nov 30, 2003
    Messages:
    62
    Likes:
    0
    Doing a bit of trial and error with the config line; I needed to specify HS (presumably high-speed) instead of XT (external??) for the oscillator selection bits.
     
  11. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    HS and XT are both external here data sheet has about it
     
  12. millwood

    millwood Banned

    Joined:
    Jun 17, 2009
    Messages:
    191
    Likes:
    2
    a few things:

    1) you will need to make sure that the fuses are set in a way consistent with your circuit. do you use the internal rc oscillator? for example.

    2) I am assuming that the port settings are right. but you typically set TRISA/B... registers before you set the ports.

    3) the errors with regarding to delay() is due to the header files.

    how about this one?

    Code (text):

    #include    <pic.h>
    #include    <conio.h>
    #include    <stdlib.h>
    #include <stdio.h>
    #include    <ctype.h>
    #include "delay.h"
    //#include "delay.c"  // you may or may not be able to comment out this. depending on what's in delay.h and delay.c
    //void delay(void);   // it is always a good idea to declare functions explicitly
    //#define _16F877
    //  __CONFIG(UNPROTECT|BODEN|FOSC1|BKBUG|WRT);
    //__CONFIG( XT & WDTDIS & PWRTDIS & BORDIS & LVPDIS & WRTEN & DEBUGDIS & UNPROTECT );
    void main (){

    ADCON1 =0x06 ; // Changes PORTA to digital
    CMCON = 0x07 ; // Disable analog comparators
    TRISA = 0x00 ; // Configure PORTA as output
    TRISB = 0X00 ;
    TRISC = 0X00 ;
    TRISD = 0X00 ;
    TRISE = 0X00 ;
    PORTA = 0x00 ;
    PORTB = 0X00 ;
    PORTC = 0X00 ;
    PORTD = 0X00 ;
    PORTE = 0X00 ;

    while(1){
      DelayMs(500);
      PORTA = ~PORTA ;
      PORTB = ~PORTB ;
      PORTC = ~PORTC ;
      PORTD = ~PORTD ;
      PORTE = ~PORTE ;
      }
    }
     
     
    Last edited: Jun 29, 2009
  13. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    You no I've tried toggle like that and it doesn't blink the led
    Code (text):

    while(1){
      DelayMs(500);
      PORTA = ~PORTA ;
      PORTB = ~PORTB ;
      PORTC = ~PORTC ;
      PORTD = ~PORTD ;
      PORTE = ~PORTE ;
      }
    }
     
     
  14. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    Use the port latches rather then the ports. The latches return what was written.

    LATA=~LATA;
     
  15. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    Where that on a 16f877a not a 18fchip
     
  16. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    That's what I get for only reading one post down, but it illustrates the point, and a good reason to use an 18F.

    On the 16F you need to toggle a variable and write it to the port
    Code (text):

    char toggleMe;
    ....
    toggleMe = 0x00;
    ...
    while(1)
    {
      toggleMe = ~toggleMe;
      PORTA = toggleMe;
    }
     
     
  17. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    I found why the chip I was using has adc on portC so I needed this
    Code (text):

    void main (){
    ANSEL = 0;                  // makes PORTC digital
    PORTC = 0;                 // Initialize PORTC
    TRISC = 0;                 // Configure PORTC as output
    while(1){
      Delay_Ms(100);
      PORTC = ~PORTC;
     }
    }
     
    works find like this LOL
     
  18. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    If PORTC=~PORTC doesn't work then some possible reasons are,

    1. You have the pins still set to analogue as pointed out by Burt.
    2. You have overloaded the pins and this is forcing them high/low. Normally by connecting LEDs without series resistors.
    3. You forgot to switch the pins to output.
    4. The pins are configured for some other peripheral.
    5. One of the other reasons for pics not working - not programmed, no oscillator, no power etc.

    Mike.
     
    Last edited: Jun 29, 2009
  19. millwood

    millwood Banned

    Joined:
    Jun 17, 2009
    Messages:
    191
    Likes:
    2
    the fact using an intermediate variable can toggle a port suggests that the port itself is in the right mode.

    I am curious about this as well. I have never encountered an example where PORTA=~PORTA; does not work.

    3v0, do you have an actual code where this is known to not work?
     
  20. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    Nope.

    I was thinking about the Read-Modify-Write problem on the 16F's.

    Burt said PORT=~PORT did not work. According to his post it did not work on any of the ports. I did not suspect he had not configured the port correctly.
     
  21. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    Try it on any pic chip with analogue pins. Any pins that are analogue work well as digital outputs but when you read them they always return zero. Hence why a copy will work.

    Mike.
     

Share This Page