CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

error after adding interrupt code to main program

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
M11



Joined: 30 Apr 2011
Posts: 9

View user's profile Send private message

error after adding interrupt code to main program
PostPosted: Sun May 22, 2011 10:35 am     Reply with quote

Hi,

I have written a program to pull up one of the pins high if a certain time has passed. It worked fine when I download it to the pic. But when I added it to my main program (which also works fine), I get 19 errors when compiling. None of the two programs gives error if I compile them separately.
Here is the interrupt code.

Code:

#include <16F877.H>
#use Delay(clock=4000000)
#define HIGH_START 916
long high_count;


#INT_RTCC

clock_isr() {
   if(--high_count==0) {
      output_high(PIN_B0);
      delay_us(500);
      output_low(PIN_B0);
      high_count=HIGH_START;
   }
}

main() {
   high_count=HIGH_START;
   set_rtcc(0);
   setup_counters(RTCC_INTERNAL, RTCC_DIV_256);
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);

   while(TRUE);
}


Thanks
temtronic



Joined: 01 Jul 2010
Posts: 9241
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun May 22, 2011 10:44 am     Reply with quote

It'd help if you told us what the 19 errors were.....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun May 22, 2011 2:02 pm     Reply with quote

The program you posted compiles with only 2 warnings with compiler
vs. 4.121. The warnings can be fixed by adding 'void' to the front of
the function declarations for main() and clock_isr(). Example:
Code:
void main()

void clock_isr()

Is the code that you posted, the true actual code that you are trying
to compile ? Because I don't get 19 errors. Also, what's your CCS
compiler version. Version numbers look like this:
http://www.ccsinfo.com/devices.php?page=versioninfo
M11



Joined: 30 Apr 2011
Posts: 9

View user's profile Send private message

PostPosted: Mon May 23, 2011 10:44 am     Reply with quote

Thanks for the response.


this is the main program:

Code:
#include <16F877.H>
#device adc=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)


//****************************************************************************************
// "read":  Function to get the reading of the sensors
int16 read(int n){
  int i;
  int16 range[1]=0;
  i2c_start();                // initiate start condition
  i2c_write(0xE0 + n);        // device address
  i2c_write(0x00);            // register address
  i2c_write(0x54);            // set units to cm
  i2c_stop();
  delay_ms(70);               // wait for returning ping
  i2c_start();                // initiate a new start condition
  i2c_write(0xE0 + n);        // device address
  i2c_write(0x02);            // address of high byte register
  i2c_start();
  i2c_write(0xE1 + n);        // device address put to read
  for (i=0;i<17;i++)
  {
  range[i] = i2c_read(1);     // read first byte and shift to the left
  range[i] = range[i] << 8;
  if(i<16)
  range[i] += i2c_read(1);    // read second byte and add to 1st
  else
  range[i] += i2c_read(0);
  }
  i2c_stop();

for (i=0;i<1;i++)
   {
      printf("range in cm = %lu\n\r", range[i]);
   }
return range[i];

} // end of "read" function

//****************************************************************************************

// "GetRef" : function to get the reference value of the buttom sensor
int16 GetRef (void) 
{
 while(1)
  if(input(PIN_A0) == 1)    //wait for push button to be pressed
    return read(0);         // return reading of the ground sensor
}//end of GetRef function

//****************************************************************************************

// Audio instructions functions

void GoRight (void)
{
  output_low(Pin_B7);   
  delay_ms(65);
  output_high(Pin_B7);
  delay_ms(1300);
  output_low(Pin_B7);   
  delay_ms(65);
  output_high(Pin_B7);
  delay_ms(130);   
}

void GoLeft (void)
{
  output_low(Pin_B6); 
  delay_ms(65);
  output_high(Pin_B6);
  delay_ms(1300);
  output_low(Pin_B6);
  delay_ms(65);
  output_high(Pin_B6);
  delay_ms(130);     
}

void RightClear (void)
{
   output_low(Pin_B5);   
  delay_ms(65);
  output_high(Pin_B5);
  delay_ms(1300);
  output_low(Pin_B5);
  delay_ms(65);
  output_high(Pin_B5);
  delay_ms(130);   
}

void LeftClear (void)
{
   output_low(Pin_B4); 
  delay_ms(65);
  output_high(Pin_B4);
  delay_ms(1300);
  output_low(Pin_B4);
  delay_ms(65);
  output_high(Pin_B4);
  delay_ms(130);     
}

void PitAhead (void)
{
  output_low(Pin_B3);   
  delay_ms(65);
  output_high(Pin_B3);
  delay_ms(1300);
  output_low(Pin_B3);
  delay_ms(65);
  output_high(Pin_B3);
  delay_ms(130);
}

void LowBattery (void)
{
  output_low(Pin_B2); 
  delay_ms(65);
  output_high(Pin_B2);
  delay_ms(1300);
  output_low(Pin_B2);
  delay_ms(65);
  output_high(Pin_B2);
  delay_ms(130);
}




//****************************************************************************************


main()
{



int16 A,B,C,D,ADCval;  // A=> Right  , B=>front , C=> left, D=> buttom
int flag=0;
int counter1=0,counter2=0;
int16 ref,ref1,ref2,ref3,min;

ref=read(6); // read the buttom sensor to get the reference value
ref1=read(6);
ref2=read(6);
ref3=read(6);
min=ref;
if(min<ref1)
min=ref1;
if(min<ref2)
min=ref2;
if(min<ref3)
min=ref3;



setup_adc_ports(RA0_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);


  input(Pin_C2);  // sleep mode pin
  output_high(Pin_B7);
  output_high(Pin_B6);
  output_high(Pin_B5);
  output_high(Pin_B4);
  output_high(Pin_B3);
  output_high(Pin_B2);
  output_high(Pin_B1);
  output_high(Pin_B0);
  delay_ms(200);


while(1)
 {



   
/*

   ADCval=read_adc();
  // volt = (float)(ADCval * 5)/1023.0;
   if(ADCval<550)
   {
     LowBattery();
     delay_ms(600);
     continue;
   } 

*/

   A= read(0);       // Read Right Sensor
   B= read(2);       // Read Front Sensor
   C= read(4);       // Read Left Sensor
   D= read(6);       // read buttom sensor



if(  counter1==25 || counter2==25 )         
{
output_high(Pin_C2);   // switch off the system
delay_ms(500);
}


 
   if(B<90)
   { 
     output_high(pin_C0);
     if(A>C)
     {       

       GoRight();
       flag=1;
       delay_ms(200); //wait for the user to turn
       counter1=counter1+1;
       counter2=0;
       continue;
     }
     else
     {
       GoLeft();
       flag=2;
       delay_ms(200); //wait for the user to turn
       counter2=counter2+1;
       counter1=0;
       continue;
     }
      output_low(pin_C0);
   }

  counter2=0;
  counter1=0; 
 
  if (flag==1)
  {
     if(C>100)
     {
      LeftClear();
      flag=0;
      continue;
     }
  }




if (flag==2)
  {
    if(A>100)
    {
     RightClear();
     flag=0;
     continue;
    }
  }


if (D>min+15)
  {
    PitAhead();
  }




 } //End of While loop


} // End of main





This is how I added the interupt code:
First part:

Code:
#include <16F877.H>
#device adc=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
[b]#define HIGH_START 916
long high_count;
#INT_RTCC

void clock_isr() {
   if(--high_count==0) {
      output_high(PIN_B0);
      delay_us(500);
      output_low(PIN_B0);
      high_count=HIGH_START;
   }
}[/b]


Second part:

Code:
void main()

{

[b]   high_count=HIGH_START;
   set_rtcc(0);
   setup_counters(RTCC_INTERNAL, RTCC_DIV_256);
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL); [/b]

// line 158 (where I get error) is the line below

int16 A,B,C,D,ADCval;  // A=> Right  , B=>front , C=> left, D=> buttom
int flag=0;
int counter1=0,counter2=0;
int16 ref,ref1,ref2,ref3,min;
.
.
.




These are the errors I get:
Line 158(1,6): A numeric expression must appear here
Line 159(1,6): A numeric expression must appear here
Line 160(1,6): A numeric expression must appear here
Line 161(1,6): A numeric expression must appear here

The rest of the errors says: undefined identifier for each of the variables that I have defined.

Undefined identifier ref
Undefined identifier ref1
Undefined identifier ref2
Undefined identifier A
Undefined identifier B

and so on
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 23, 2011 11:59 am     Reply with quote

Please post the actual code that fails. You posted good code and then
some edits that I'm supposed to do, to make it fail. I may not do the
edits in exactly the same way you do it, so I may not duplicate the
problem. Post the program that fails.

Also, you need to post the compiler version. Every version has different
bugs and there are hundreds of versions.
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Mon May 23, 2011 1:29 pm     Reply with quote

First thing, I'd guess the latest compiler.
In C, declarations of variables _must_ be at the start of a code block.
Now the latest compilers allow you to not do this, but if you do you may still get errors, and it is safer even with these to keep to the old C form. You show the declarations being put into the middle of the code block, after five functions calls. Hence they are not accepted, and the variables have not been declared....
Then your new code and the old use the same variable names....
I'm afraid you need to actually learn a bit about programming rather than how to cut and paste.

Best Wishes
M11



Joined: 30 Apr 2011
Posts: 9

View user's profile Send private message

PostPosted: Tue May 24, 2011 12:32 pm     Reply with quote

Thanks a lot for your help and advices.

I just moved the declarations to the top of the code and it built successfully.

Just to make things clear,
I am only used to C++ programming and this is my second C program.
Regarding copying and pasting, I wouldn't do it unless I fully understand how the code works and what it does exactly.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group