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 support@ccsinfo.com

Missing a very fundamental point

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



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

Missing a very fundamental point
PostPosted: Tue Nov 11, 2008 11:19 am     Reply with quote

I am new to C programming and am missing a very fundamental point. I do a lot of programming in assembler and use subroutines all the time. I can't find a way to go to a function (subroutines?), do something and then return.

void buff_data()
{
if (k==0x23) // test for end char
clear_pw(); // check the password entered ***
}
void clear_pw()
{
// my clear pw code would go here
// nothing need to be returned
}

I get a error 12 ,,, Undefinded Identfier -- clear_pw

Can any one give me some help on this.
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Tue Nov 11, 2008 11:32 am     Reply with quote

In C the compiler starts at the top and works down trying to compile.
I think your problem is not showing a function prototype.
So it doesn't know about the functions that show up latter.
Code:


//I am new to C programming and am missing a very fundamental point.
//I do a lot of programming in assembler and use subroutines all the
//time. I can't find a way to go to a function (subroutines?), do something
//and then return.
#define FIRM_MAJOR 'D'
#define FIRM_MINOR  02
#define HARD_MAJOR  2
#define HARD_MINOR  '+'
//#include <18F4525.h>
//#include <18F2221.h>
#include <16F877.h>
#use delay(clock=10000000, restart_wdt)
#fuses hs,nowdt,noprotect,nolvp,put
#use rs232(baud=19200,xmit=PIN_B0,invert,stream=DEBUG) // stderr(same as debug)
#case
#zero_ram

int8 k=0;

//function prototypes
void buff_data(void);  //     <----------------------------------------
void clear_pw(void);//        <----------------------------------------

//=== MAIN ===//
void main(void)
{
  setup_adc_ports(NO_ANALOGS);
  setup_adc(ADC_OFF);
  set_tris_a(0xFF);set_tris_e(0xFF);set_tris_c(0xFF);
  set_tris_b(0xFF);set_tris_d(0xFF);port_b_pullups(TRUE);
  fprintf(DEBUG,"Firm=%C_%03u.c\n\r",FIRM_MAJOR,FIRM_MINOR);
  fprintf(DEBUG,"Hard=%u.%u\n\r",HARD_MAJOR,HARD_MINOR);
  while(1)
  {
    buff_data();
    clear_pw();
  }
}

void buff_data(void)
{
if (k==0x23) // test for end char
clear_pw(); // check the password entered ***
}
void clear_pw()
{
// my clear pw code would go here
// nothing need to be returned
}

frierray



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PostPosted: Tue Nov 11, 2008 1:28 pm     Reply with quote

Thanks so much. that's a big help. I recoded it and it is working now.
dbotkin



Joined: 08 Sep 2003
Posts: 197
Location: Omaha NE USA

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Tue Nov 11, 2008 3:44 pm     Reply with quote

Or just write it all bass-ackwards. Put main() at the end, everything else above it in reverse order. You get used to it.

Code:

void clear_pw()
{
}

void buff_data(void)
{
if (k==0x23) // test for end char
clear_pw(); // check the password entered ***
}

void main(void)
{
  setup_adc_ports(NO_ANALOGS);
  setup_adc(ADC_OFF);
  set_tris_a(0xFF);set_tris_e(0xFF);set_tris_c(0xFF);
  set_tris_b(0xFF);set_tris_d(0xFF);port_b_pullups(TRUE);
  fprintf(DEBUG,"Firm=%C_%03u.c\n\r",FIRM_MAJOR,FIRM_MINOR);
  fprintf(DEBUG,"Hard=%u.%u\n\r",HARD_MAJOR,HARD_MINOR);
  while(1)
  {
    buff_data();
    clear_pw();
  }
}
}
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Nov 12, 2008 3:08 am     Reply with quote

Or use header files.

Create a .h file and put your prototypes in that. Then include it in your c file.

#include "mycode.h"

This way if you use multiple c files you can include required headers in the c file. There are however issues with this, especially with CCS.

If you have the same .h file included in many .c files you may end up with a comipler error/warning of multiple declarations, you can get around this by using the following in the header file

#ifndef _MYHEADER
#define _MYHEADER 1
//prototypes and definitions go here
#endif

Also, the CCS compiler requires (well in my code anyway) that you inlude ALL the c files in the main build file...

#include "fred.c"
#include "bill.c"
// rest of code
frierray



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PostPosted: Wed Nov 12, 2008 10:48 am     Reply with quote

Wayne_

I like your idea. Do the include files need to be in any special place?

Also, the CCS compiler requires (well in my code anyway) that you inlude ALL the c files in the main build file...

#include "fred.c"
#include "bill.c"
// rest of code


Again, thanks for all the help.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Nov 12, 2008 11:07 am     Reply with quote

You would normally keep them in the same place as your .c files so the compiler can find them.

you normally have a .h file for each .c file all though this is not required.
You could put ALL the prototypes/defines in a single .h file if you wanted.
I tend to write code with multiple .c files and a .h file for each one.

But like I said, with CCS and my setup I have to manually include all the .c files in the main .c file. There is an option for using multiple compile files (.c) in CCS but I had trouble making it work the way I assumed it should, so I ended up doing it this way.

In each of your .c files you would normally include any .h files it requires, including the one for itself.
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