Undefined Identifier with Hi TECH C

Discussion in 'Programmer's Corner' started by Jaden5165, Sep 27, 2011.

  1. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    Code ( (Unknown Language)):
    1.  
    2. #include<pic.h>
    3. //===============configuration=================================
    4. __CONFIG (0x3F32);
    5. //===============define IO port================================
    6. #define      lcd         PORTB
    7. #define      RS         RA2
    8. #define      E           RA5
    9. #define      CHANNEL0    0b10000001   // AN0
    10. #define      buzzer_led  RD0
    11. #define      fan         RD1
    12. //==============FUNCTION PTOTOTYPE=========================
    13. void e_pulse(void);
    14. void delay(unsigned short i);
    15. void send_char(unsigned char data);
    16. void send_config(unsigned char data);
    17. void lcd_goto(unsigned char data);
    18. void lcd_clr(void);
    19. void dis_num(unsigned long data);
    20. void increment(unsigned long data);
    21. void read_adc(void);
    22. unsigned short read_temp(void);
    23. //====================MAIN================================
    24. unsigned short result;
    25. unsigned short temp,tempA;
    26. void main(void)
    27. {
    28.    ADRESH=0;            //clear A/D result
    29.    ADRESL=0;            //clear A/D result
    30.    //setting ADCON1 Register
    31.    ADCON1=0b11000101;         // A/D result right justified,            
    32.                        // configure RA2 and RA5 as digital I/O
    33.    TRISA=0b11011011;         //configure PORTA I/O direction
    34.    TRISB=0b00000000;         //configure PORTB as output
    35.    TRISD=0b00000000;         //configure PORTD as output
    36.    PORTA=0;
    37.    PORTB=0;
    38.  
    39.    while(1)              
    40.    {
    41.       send_config(0b00000001);       //clear display at lcd
    42.       send_config(0b00000010);     //Lcd Return to home
    43.       send_config(0b00000110);       //entry mode-cursor increase 1
    44.       send_config(0b00001100);       //diplay on, cursor off and cursor blink off
    45.       send_config(0b00111000);       //function set
    46.  
    47.       lcd_goto(0);                //cursor start from beginning
    48.  
    49.       //display character on LCD
    50.       send_char(' ');
    51.       send_char('T');
    52.       send_char('E');
    53.       send_char('M');
    54.       send_char('P');
    55.       send_char('.');
    56.       send_char('A');
    57.       send_char('=');
    58.       while(1)                  //infinity loop
    59.       {
    60.       //Temperature sensor
    61.       ADCON0=CHANNEL0;   //CHANNEL0=0b10000001            
    62.       lcd_goto(8);
    63.  
    64.       read_adc();
    65.       temp=read_temp();
    66.       dis_num(temp/10);
    67.       send_char('.');
    68.       dis_num(temp%10);
    69.       send_char(0b11011111);
    70.       send_char('C');
    71.       send_char(' ');
    72.       send_char(' ');
    73.  
    74.       tempA=temp;
    75.  
    76.            if(tempA>300)                    
    77.             {                        
    78.                buzzer_led=1;                  
    79.                fan=1;                    
    80.  
    81.             }  
    82.          else if(tempA<250)                        
    83.             {                        
    84.                buzzer_led=1;                  
    85.                fan=0;                    
    86.  
    87.             }  
    88.  
    89.          else
    90.             {
    91.                buzzer_led=0;
    92.                fan=0;
    93.             }      
    94.       delay(2000);
    95.  
    96.       }
    97.  
    98.    }
    99.  
    100. }
    101.  
    102.  
    103. //==================subroutine LCD setting ==========================
    104. void send_config(unsigned char data)
    105. {
    106.     RS=0;
    107.    lcd=data;
    108.    delay(500);
    109.    e_pulse();
    110. }
    111. void e_pulse(void)
    112. {
    113.    E=1;
    114.    delay(500);
    115.    E=0;
    116.    delay(500);
    117. }
    118. void send_char(unsigned char data)
    119. {
    120.     RS=1;
    121.    lcd=data;
    122.    delay(500);
    123.    e_pulse();
    124. }
    125.  
    126. void lcd_goto(unsigned char data)
    127. {
    128.     if(data<16)
    129.    {
    130.        send_config(0x80+data);
    131.    }
    132.    else
    133.    {
    134.        data=data-20;
    135.       send_config(0xc0+data);
    136.    }
    137. }
    138.  
    139. void lcd_clr(void)
    140. {
    141.    RS=0;
    142.     send_config(0x01);
    143.    delay(600);  
    144. }
    145.  
    146. void dis_num(unsigned long data)
    147. {
    148.    unsigned char hundred_thousand;
    149.    unsigned char ten_thousand;
    150.    unsigned char thousand;
    151.    unsigned char hundred;
    152.    unsigned char tenth;
    153.    hundred_thousand = data/100000;              
    154.    data = data % 100000;
    155.    ten_thousand = data/10000;
    156.    data = data % 10000;
    157.    thousand = data / 1000;
    158.    data = data % 1000;
    159.    hundred = data / 100;
    160.    data = data % 100;
    161.    tenth = data / 10;
    162.    data = data % 10;
    163.    if(hundred_thousand>0)
    164.    {
    165.       send_char(hundred_thousand + 0x30);   //0x30 added to become ASCII code
    166.       send_char(ten_thousand + 0x30);
    167.       send_char(thousand + 0x30);
    168.       send_char(hundred + 0x30);
    169.       send_char(tenth + 0x30);
    170.       send_char(data + 0x30);
    171.    }
    172.    else if(ten_thousand>0)
    173.    {
    174.       send_char(ten_thousand + 0x30);   //0x30 added to become ASCII code
    175.       send_char(thousand + 0x30);
    176.       send_char(hundred + 0x30);
    177.       send_char(tenth + 0x30);
    178.       send_char(data + 0x30);
    179.    }
    180.    else if(thousand>0)
    181.    {
    182.        send_char(thousand + 0x30);      //0x30 added to become ASCII code
    183.       send_char(hundred + 0x30);
    184.       send_char(tenth + 0x30);
    185.       send_char(data + 0x30);
    186.    }
    187.    else if(hundred>0)
    188.    {
    189.        send_char(hundred + 0x30);      //0x30 added to become ASCII code
    190.       send_char(tenth + 0x30);
    191.       send_char(data + 0x30);
    192.    }
    193.    else if(tenth>0)
    194.     {
    195.       send_char(tenth + 0x30);      //0x30 added to become ASCII code
    196.       send_char(data + 0x30);
    197.    }
    198.    else send_char(data + 0x30);         //0x30 added to become ASCII code
    199. }
    200. void increment(unsigned long data)
    201. {  
    202.    unsigned short j;
    203.    for(j=10;j>0;j--)
    204.    {   lcd_goto(32);
    205.       data=data+1;
    206.       dis_num(data);
    207.       delay(10000);
    208.    }
    209. }
    210. //==================subroutine ADC=========================
    211. void read_adc(void)
    212. {
    213.    unsigned short i;
    214.    unsigned long result_temp=0;
    215.    for(i=2000;i>0;i-=1)            //looping 2000 times for getting average value
    216.    {
    217.      ADCON0bits.GO = 1;
    218.  //ADGO is the bit 2 of the ADCON0 register
    219.  while(ADCON0bits.GO==1);
    220.  //ADC start, ADGO=0 after finish ADC progress
    221.  result=ADRESH;
    222.  result=result<<8;
    223.  //shift to left for 8 bit
    224.  result=result|ADRESL;
    225.  //10 bit result from ADC
    226.  result_temp+=result;
    227.  }
    228.    result = result_temp/2000;         //getting average value
    229. }
    230. unsigned short read_temp(void)
    231. {
    232.    unsigned short temp;
    233.    temp=result;
    234.    return temp;
    235. }
    236. //==================subroutine DELAY==========================
    237. void delay(unsigned short i)
    238. {  
    239.    for(;i>0;i--);
    240. }
    241.  
    -----------------------------------------------------------------------
    Code ( (Unknown Language)):
    1. Clean: Deleting intermediary and output files.
    2. Clean Warning: File "C:\Users\Jaden_Ng Chun Hau\Desktop\FYP\HTC_MPLAB_CODE\New folder\gg.p1" doesn't exist.
    3. Clean: Deleted file "C:\Users\Jaden_Ng Chun Hau\Desktop\FYP\HTC_MPLAB_CODE\New folder\ds.mcs".
    4. Clean: Done.
    5. Build C:\Users\Jaden_Ng Chun Hau\Desktop\FYP\HTC_MPLAB_CODE\New folder\ds for device 16F877A
    6. Using driver D:\Program Files\HI-TECH Software\PICC\9.80\bin\picc.exe
    7. Executing: "D:\Program Files\HI-TECH Software\PICC\9.80\bin\picc.exe" --pass1 "C:\Users\Jaden_Ng Chun Hau\Desktop\FYP\HTC_MPLAB_CODE\New folder\gg.c" -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error   [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
    8. Error   [192] C:\Users\Jaden_Ng Chun Hau\Desktop\FYP\HTC_MPLAB_CODE\New folder\gg.c; 243.1 undefined identifier "ADCON0bits"
    9. Error   [196] C:\Users\Jaden_Ng Chun Hau\Desktop\FYP\HTC_MPLAB_CODE\New folder\gg.c; 243.15 struct/union required
    10. Error   [196] C:\Users\Jaden_Ng Chun Hau\Desktop\FYP\HTC_MPLAB_CODE\New folder\gg.c; 245.20 struct/union required
    11. ********** Build failed! **********
    12.  




    I got this code from my friends and they can compile successful but why i build failed.they used project wizard to start and me too.but why i cant build success.
     
  2. stahta01

    Member

    Jun 9, 2011
    133
    21
    Did you try to include <htc.h> instead of <pic.h>

    If that fails try defining _LEGACY_HEADERS before including the header file <htc.h>.

    Tim S.
     
    Last edited: Sep 27, 2011
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Go to the ....\HI-TECH Software\PICC\9.81\include folder. Open the header file for your PIC. Then browse (search) to you find the definition for the ADCON register. It will be something like this example that is for PORTA
    Code ( (Unknown Language)):
    1.  
    2. // Register: PORTA
    3. volatile unsigned char           PORTA               @ 0x005;
    4. // bit and bitfield definitions
    5. volatile bit RA0                 @ ((unsigned)&PORTA*8)+0;
    6. volatile bit RA1                 @ ((unsigned)&PORTA*8)+1;
    7. volatile bit RA2                 @ ((unsigned)&PORTA*8)+2;
    8. volatile bit RA3                 @ ((unsigned)&PORTA*8)+3;
    9. volatile bit RA4                 @ ((unsigned)&PORTA*8)+4;
    10. volatile bit RA5                 @ ((unsigned)&PORTA*8)+5;
    11.  
     
  4. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0


    @Tim S
    what do you meant by _LEGACY_HEADERS ?
     
  5. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0



    i followed the step u said but the include files dont have ADCON.i try to use search the words ADCON also cannot find.
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    which header file did you try searching in. it should be pic16f877.h or the 16f877a.h if you use the a version
     
  7. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0
    @t06afre
    i reinstall again my mplab and also the htc.my htc now is 9.7version.weird thing is when i compile the code 1st time build failed but when i build again it build successful.but if i click rebuild then it will be build failed.and i try rebuild still failed,but i try build then can.why?
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    PLOD,PLOD,PLOD Try this header file pic1687x.h
     
    Last edited: Sep 27, 2011
  9. Jaden5165

    Thread Starter Member

    Sep 9, 2011
    69
    0


    I tried.but still the same.when the 1st time i build failed.1nd time click build then success.
     
Loading...