• 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.

C Program error in Keil compiler

Status
Not open for further replies.

Parth86

Member
Hello
I need help on C Program

compiler : keil
mcu: P89V51R1D2

c program
Code:
#include<regx51.h>             
             
#define led P2

void delay_ms (unsigned int i)

{
unsigned int j = 0;

   for ( i = 0 ; j<1; j++);
}
void main()                  
{
    while (1)                    
    {                       
        led = 0x01;
        delay_ms (500);
        led = 0x01;
        delay_ms (500) ;
    }
}

error in program
Code:
Build target 'Target 1'
compiling LED.c...
LED.c(1): warning C318: can't open file 'regx51.h'
LED.C(16): error C202: 'P2': undefined identifier
LED.C(18): error C202: 'P2': undefined identifier
Target not created
how to remove the errors
 

Mikebits

Well-Known Member
Make sure you have the file in the proper location. I have that file in a subdirectory of \keil\pk51\9.51\C51\INC\Atmel

Your directory tree is probably different but it should be a sub directory of keil.
Also see here: http://www.keil.com/forum/21907/

You can copy this if you don't have the include file.

Code:
/*--------------------------------------------------------------------------
AT89X51.H

HeaderfileforthelowvoltageFlashAtmelAT89C51andAT89LV51.
Copyright(c)1988-2002KeilElektronikGmbHandKeilSoftware,Inc.
Allrightsreserved.
--------------------------------------------------------------------------*/

#ifndef__AT89X51_H__
#define__AT89X51_H__

/*------------------------------------------------
ByteRegisters
------------------------------------------------*/
sfrP0=0x80;
sfrSP=0x81;
sfrDPL=0x82;
sfrDPH=0x83;
sfrPCON=0x87;
sfrTCON=0x88;
sfrTMOD=0x89;
sfrTL0=0x8A;
sfrTL1=0x8B;
sfrTH0=0x8C;
sfrTH1=0x8D;
sfrP1=0x90;
sfrSCON=0x98;
sfrSBUF=0x99;
sfrP2=0xA0;
sfrIE=0xA8;
sfrP3=0xB0;
sfrIP=0xB8;
sfrPSW=0xD0;
sfrACC=0xE0;
sfrB=0xF0;

/*------------------------------------------------
P0BitRegisters
------------------------------------------------*/
sbitP0_0=0x80;
sbitP0_1=0x81;
sbitP0_2=0x82;
sbitP0_3=0x83;
sbitP0_4=0x84;
sbitP0_5=0x85;
sbitP0_6=0x86;
sbitP0_7=0x87;

/*------------------------------------------------
PCONBitValues
------------------------------------------------*/
#defineIDL_0x01

#defineSTOP_0x02
#definePD_0x02/*Alternatedefinition*/

#defineGF0_0x04
#defineGF1_0x08

#defineSMOD_0x80

/*------------------------------------------------
TCONBitRegisters
------------------------------------------------*/
sbitIT0=0x88;
sbitIE0=0x89;
sbitIT1=0x8A;
sbitIE1=0x8B;
sbitTR0=0x8C;
sbitTF0=0x8D;
sbitTR1=0x8E;
sbitTF1=0x8F;

/*------------------------------------------------
TMODBitValues
------------------------------------------------*/
#defineT0_M0_0x01
#defineT0_M1_0x02
#defineT0_CT_0x04
#defineT0_GATE_0x08
#defineT1_M0_0x10
#defineT1_M1_0x20
#defineT1_CT_0x40
#defineT1_GATE_0x80

#defineT1_MASK_0xF0
#defineT0_MASK_0x0F

/*------------------------------------------------
P1BitRegisters
------------------------------------------------*/
sbitP1_0=0x90;
sbitP1_1=0x91;
sbitP1_2=0x92;
sbitP1_3=0x93;
sbitP1_4=0x94;
sbitP1_5=0x95;
sbitP1_6=0x96;
sbitP1_7=0x97;

/*------------------------------------------------
SCONBitRegisters
------------------------------------------------*/
sbitRI=0x98;
sbitTI=0x99;
sbitRB8=0x9A;
sbitTB8=0x9B;
sbitREN=0x9C;
sbitSM2=0x9D;
sbitSM1=0x9E;
sbitSM0=0x9F;

/*------------------------------------------------
P2BitRegisters
------------------------------------------------*/
sbitP2_0=0xA0;
sbitP2_1=0xA1;
sbitP2_2=0xA2;
sbitP2_3=0xA3;
sbitP2_4=0xA4;
sbitP2_5=0xA5;
sbitP2_6=0xA6;
sbitP2_7=0xA7;

/*------------------------------------------------
IEBitRegisters
------------------------------------------------*/
sbitEX0=0xA8;/*1=EnableExternalinterrupt0*/
sbitET0=0xA9;/*1=EnableTimer0interrupt*/
sbitEX1=0xAA;/*1=EnableExternalinterrupt1*/
sbitET1=0xAB;/*1=EnableTimer1interrupt*/
sbitES=0xAC;/*1=EnableSerialportinterrupt*/
sbitET2=0xAD;/*1=EnableTimer2interrupt*/

sbitEA=0xAF;/*0=Disableallinterrupts*/

/*------------------------------------------------
P3BitRegisters(Mnemonics&Ports)
------------------------------------------------*/
sbitP3_0=0xB0;
sbitP3_1=0xB1;
sbitP3_2=0xB2;
sbitP3_3=0xB3;
sbitP3_4=0xB4;
sbitP3_5=0xB5;
sbitP3_6=0xB6;
sbitP3_7=0xB7;

sbitRXD=0xB0;/*Serialdatainput*/
sbitTXD=0xB1;/*Serialdataoutput*/
sbitINT0=0xB2;/*Externalinterrupt0*/
sbitINT1=0xB3;/*Externalinterrupt1*/
sbitT0=0xB4;/*Timer0externalinput*/
sbitT1=0xB5;/*Timer1externalinput*/
sbitWR=0xB6;/*Externaldatamemorywritestrobe*/
sbitRD=0xB7;/*Externaldatamemoryreadstrobe*/

/*------------------------------------------------
IPBitRegisters
------------------------------------------------*/
sbitPX0=0xB8;
sbitPT0=0xB9;
sbitPX1=0xBA;
sbitPT1=0xBB;
sbitPS=0xBC;
sbitPT2=0xBD;

/*------------------------------------------------
PSWBitRegisters
------------------------------------------------*/
sbitP=0xD0;
sbitF1=0xD1;
sbitOV=0xD2;
sbitRS0=0xD3;
sbitRS1=0xD4;
sbitF0=0xD5;
sbitAC=0xD6;
sbitCY=0xD7;

/*------------------------------------------------
InterruptVectors:
InterruptAddress=(Number*8)+3
------------------------------------------------*/
#defineIE0_VECTOR0/*0x03ExternalInterrupt0*/
#defineTF0_VECTOR1/*0x0BTimer0*/
#defineIE1_VECTOR2/*0x13ExternalInterrupt1*/
#defineTF1_VECTOR3/*0x1BTimer1*/
#defineSIO_VECTOR4/*0x23Serialport*/

#endif
 

Parth86

Member
C:\Keil\C51\INC\Philips
when I look the header file for p89v51r1d2 I don't see header file for p89v51r1d2
where to download header file for p89v51r1d2 ?
 

Mikebits

Well-Known Member
That's a really old part. You might try using p89c51.h, I dunno? You might post your issue on the Keil web forum.
 

Parth86

Member
Make sure you have the file in the proper location. I have that file in a subdirectory of \keil\pk51\9.51\C51\INC\Atmel

Your directory tree is probably different but it should be a sub directory of keil.
Also see here: http://www.keil.com/forum/21907/

You can copy this if you don't have the include file.
I just copy paste , now I am not getting error. but when I burn code. my LED is not blinking
Code:
/*--------------------------------------------------------------------------
P89V51Rx2.H
Registers definition for Philips P89V51Rx2

Copyright (c) 2007 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
-------------------------------------------------------------------------- */

/* Byte Addresses */
sfr   P0        = 0x80;
sfr   SP        = 0x81;
sfr   DPL        = 0x82;
sfr   DPH        = 0x83;
sfr   WDTD        = 0x85;
sfr   SPDAT        = 0x86;
sfr   PCON        = 0x87;
sfr   TCON        = 0x88;
sfr   TMOD        = 0x89;
sfr   TL0        = 0x8A;
sfr   TL1        = 0x8B;
sfr   TH0        = 0x8C;
sfr   TH1        = 0x8D;
sfr   AUXR        = 0x8E;
sfr   P1        = 0x90;
sfr   SCON        = 0x98;
sfr   SBUF        = 0x99;
sfr   P2        = 0xA0;
sfr   AUXR1        = 0xA2;
sfr   IEN0        = 0xA8;
sfr   SADDR        = 0xA9;
sfr   SPCFG        = 0xAA;
sfr   P3        = 0xB0;
sfr   FCF        = 0xB1;
sfr   FST        = 0xB6;
sfr   IP0H        = 0xB7;
sfr   IP0        = 0xB8;
sfr   SADEN        = 0xB9;
sfr   WDTC        = 0xC0;
sfr   T2CON        = 0xC8;
sfr   T2MOD        = 0xC9;
sfr   RCAP2L        = 0xCA;
sfr   RCAP2H        = 0xCB;
sfr   TL2        = 0xCC;
sfr   TH2        = 0xCD;
sfr   PSW        = 0xD0;
sfr   SPCTL        = 0xD5;
sfr   CCON        = 0xD8;
sfr   CMOD        = 0xD9;
sfr   CCAPM0        = 0xDA;
sfr   CCAPM1        = 0xDB;
sfr   CCAPM2        = 0xDC;
sfr   CCAPM3        = 0xDD;
sfr   CCAPM4        = 0xDE;
sfr   ACC        = 0xE0;
sfr   IEN1        = 0xE8;
sfr   CL        = 0xE9;
sfr   CCAP0L        = 0xEA;
sfr   CCAP1L        = 0xEB;
sfr   CCAP2L        = 0xEC;
sfr   CCAP3L        = 0xED;
sfr   CCAP4L        = 0xEE;
sfr   B        = 0xF0;
sfr   IP1H        = 0xF7;
sfr   IP1        = 0xF8;
sfr   CH        = 0xF9;
sfr   CCAP0H        = 0xFA;
sfr   CCAP1H        = 0xFB;
sfr   CCAP2H        = 0xFC;
sfr   CCAP3H        = 0xFD;
sfr   CCAP4H        = 0xFE;

/* Bit Addresses */

/* P0 */
sbit   AO        = P0^0;
sbit   EXTRAM        = P0^1;

/* TCON */
sbit   IT0        = TCON^0;
sbit   IE0        = TCON^1;
sbit   IT1        = TCON^2;
sbit   IE1        = TCON^3;
sbit   TR0        = TCON^4;
sbit   TF0        = TCON^5;
sbit   TR1        = TCON^6;
sbit   TF1        = TCON^7;

/* P1 */
sbit   T2EX        = P1^1;
sbit   ECI        = P1^2;
sbit   CEX0        = P1^3;
sbit   CEX1        = P1^4;
sbit   CEX2        = P1^5;
sbit   CEX3        = P1^6;
sbit   CEX4        = P1^7;

/* SCON */
sbit   RI        = SCON^0;
sbit   TI        = SCON^1;
sbit   RB8        = SCON^2;
sbit   TB8        = SCON^3;
sbit   REN        = SCON^4;
sbit   SM3        = SCON^5;
sbit   SM1        = SCON^6;
sbit   SM0        = SCON^7;

/* P2 */
sbit   DPS        = P2^0;
sbit   GF2        = P2^3;

/* IEN0 */
sbit   EX0        = IEN0^0;
sbit   ET0        = IEN0^1;
sbit   EX1        = IEN0^2;
sbit   ET1        = IEN0^3;
sbit   ES0        = IEN0^4;
sbit   ET2        = IEN0^5;
sbit   EC        = IEN0^6;
sbit   EA        = IEN0^7;

/* P3 */
sbit   RxD        = P3^0;
sbit   TxD        = P3^1;
sbit   INT0        = P3^2;
sbit   INT1        = P3^3;
sbit   T0        = P3^4;
sbit   T1        = P3^5;
sbit   WR        = P3^6;
sbit   RD        = P3^7;

/* IP0 */
sbit   PX0        = IP0^0;
sbit   PT0        = IP0^1;
sbit   PX1        = IP0^2;
sbit   PT1       = IP0^3;
sbit   PS        = IP0^4;
sbit   PT2        = IP0^5;
sbit   PPC        = IP0^6;

/* WDTC */
sbit   SWDT        = WDTC^0;
sbit   WDT        = WDTC^1;
sbit   WDTS        = WDTC^2;
sbit   WDRE        = WDTC^3;
sbit   WDOUT        = WDTC^4;

/* T2CON */
sbit   RL2        = T2CON^0;
sbit   T2        = T2CON^1;
sbit   TR2        = T2CON^2;
sbit   EXEN2        = T2CON^3;
sbit   TCLK        = T2CON^4;
sbit   RCLK        = T2CON^5;
sbit   EXF2        = T2CON^6;
sbit   TF2        = T2CON^7;

/* PSW */
sbit   P        = PSW^0;
sbit   F1        = PSW^1;
sbit   OV        = PSW^2;
sbit   RS0        = PSW^3;
sbit   RS1        = PSW^4;
sbit   F0        = PSW^5;
sbit   AC        = PSW^6;
sbit   CY        = PSW^7;

/* CCON */
sbit   CCF0        = CCON^0;
sbit   CCF1        = CCON^1;
sbit   CCF2        = CCON^2;
sbit   CCF3        = CCON^3;
sbit   CCF4        = CCON^4;
sbit   CR        = CCON^6;
sbit   CF        = CCON^7;           
#define led P2

void delay_ms (unsigned int i)

{
unsigned int j = 0;

   for ( i = 0 ; j<1; j++);
}
void main()                 
{
    while (1)                   
    {                     
        led = 0x01;
        delay_ms (500);
        led = 0x01;
        delay_ms (500) ;
    }
}
 

Parth86

Member
Just use the generic... #include<8051.h> or what ever the generic one is.... Even the atmel at89c51.h will do as the registers are almost the same...
when I use #include<8051.h>
I see following massage
Code:
Build target 'Target 1'
compiling led.c...
led.c(1): warning C318: can't open file '8051.h'
linking...
Program Size: data=9.0 xdata=0 code=45
creating hex file from "led"...
"led" - 0 Error(s), 1 Warning(s).
should I burn code with 1 warning ?

Q2. I am not sure that my program has been burned successfully into MCU. I am seeing status massage ' attempting , then finished '' please look the image and tell me that program has been burn successfully or not ?
 

Attachments

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
I don't use keil... I use SDCC... so the differences are subtle!

Navigate to the "include" directory and look at the header files... Find ANY phillips header and use that!!!

Warnings are there for a reason!!! If you save "that" definition file in post 6 as "p89c51rx2.h" in your working directory.. Then you get rid of the #include<8051.h> and use this instead #include"p89c51rx2.h"

All should be okay! The "" will make the linker look in the working directory!!

Two things.... Your LED is only ever set to on and your delay routine will crash!!!
C:
void delay_ms (unsigned int i)
{
unsigned int j = 0;

   for ( i = 0 ; j<1; j++);   // this end  as I is made 0 on the loop start!
}
use these two instead..

C:
void delayUs(int x)
   {
   x>>=3;       // this allows for @ 12Mhz crystal!
   while(x--);
   }

void delayMs(int x)
   {
   while(x--)
      delayUs(1000);
   }
 

Parth86

Member
ok I try to solve problem
please look post #7 and Q.2 . how to confirm that program has been burned successfully

new program

Code:
#include<P89V66x.H>
         
#define led P2


void delayUs(int x)
   {
   x>>=3;       // this allows for @ 12Mhz crystal!
   while(x--);
   }

void delayMs(int x)
   {
   while(x--)
      delayUs(1000);
   }
void main()               
{
    while (1)                 
    {                   
        led = 0x01;
        delayMs (1000);
        led = 0x01;
        delayMs (1000) ;
    }
}
 
Last edited by a moderator:

Parth86

Member
when Power is on , LED is ON when I burn code LED does not blink It goes turn off, that mean program has been burned successfully
I think there is fault in program

Code:
#define LED         P2
#define LED_ON        1
#define LED_OFF     0

void delayUs(int x)
   {
   x>>=3;       // this allows for @ 12Mhz crystal!
   while(x--);
   }

void delayMs(int x)
   {
   while(x--)
      delayUs(1000);
   }
void main()            
{
  P2 = 0x00;
    while (1)              
    {                
        LED = LED_ON;
        delayMs (1000);
        LED = LED_OFF;
     
    }
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Okay!! There is nothing wrong other than you can't see it.. How is your LED connected!!! LED OFF may be turning the LED on and visa versa.... You are turning LED on (which I think is off ) and waiting 1000mS THEN!! you turn it off ( which I think is on ) and immediately switch it back on ( off)... There is no second delay

Try this..
C:
#define LED         P2
#define LED_ON        1
#define LED_OFF     0

void delayUs(int x)
   {
   x>>=3;       // this allows for @ 12Mhz crystal!
   while(x--);
   }

void delayMs(int x)
   {
   while(x--)
      delayUs(1000);
   }
void main()            
{
  P2 = 0x00;
    while (1)              
    {                
        LED = LED_ON;
        delayMs (1000);
        LED = LED_OFF;
        delayMs (1000);
    }
 

Parth86

Member
problem solved , thank you very much Mr. Ian

Code:
#define LED         P2
#define LED_ON        0xff
#define LED_OFF     0x00



void delay (unsigned int i)
{
    unsigned int j;
    for (j = 0; j < 40000; j++);
    {
     
    }
}

void main()
{
 
 
 

    while (1)
    {
        LED = LED_ON;
        delay(500);
        LED = LED_OFF;
        delay(5000);
        LED = LED_OFF;
     
    }
}
 
Last edited by a moderator:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading

 
Top