how can i do a seperate function thats out of the main function?

Discussion in 'Programmer's Corner' started by rocker123uk, Feb 4, 2016.

  1. rocker123uk

    Thread Starter New Member

    Dec 6, 2015
    10
    0
    hey guys,
    thank you for reading my thread, i am creating a program where a stepper motor does 1 step and reads sensor values, however, to read the sensor values i have to always do the following code:

    char adc []= "ADC value=";
    char *temp = "0000";
    int a=0;
    unsigned int adc_value;
    ADCON0 = 0b10010000; //AN2 analog channel selected
    ADCON1 = 0b01001001; //PORT configuratoin to AN2
    TRISA = 0b00000100; //RA2 analogue input
    Lcd_Init();
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Out(1,1,adc);
    do {
    adc_value = ADC_Read(2); //read from channel 1
    temp [0] = adc_value/1000 +48 ; //add 48 to get the ascii value
    temp [1] = (adc_value/100) %10 +48 ;
    temp [2] = (adc_value/10) %10 +48;
    temp [3] = (adc_value/1)%10 +48;
    Lcd_Out(1,11,temp);
    Delay_ms(100);

    if (adc_value>=900){
    Lcd_Out(2,1,"Too hard ");

    the above is quite long as i have to copy and paste the highlighted part again and again in the main code, i have tried doing it in a separate function, however, as i try to read the adc_value in the main function, it says that the function is undeclared (i know i have to copy the declaration but that would be the same thing all over), any suggestions would be appreciated, of if theres a better way...Thanks guys :) i just want to save the time of copying all that again and again and saving the hedache of checking incase there is a fault or change needed in the future
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,788
    4,808
    It might, possibly, be helpful if you told people what language you are using. Sure, we can guess that it is probably some variant of C, but engineering is not about guessing.

    You can't call a function before it is declared (declared and defined are two different things -- and that's relevant here). But once it is declared, either via the definition or a prototype, you can call it until the cows come home.
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,019
    Have you heard of a period at the end of a sentence and a capital letter at the start of a sentence? You post is VERY hard to read.

    What function is undeclared?
     
  4. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,771
    971
    Code (Text):
    1.  
    2.  
    3. //Declare all functions used in this code
    4. void setup();
    5. void loop();
    6. void stepmotor();
    7. void readvalue();
    8.  
    9. //declare global variables.
    10.  
    11.  
    12. void setup() {
    13.   // put your setup code here, to run once:
    14.  
    15. }
    16.  
    17. void loop() { // put your main code here, to run repeatedly:
    18.   readvalue(); //go to the readvalue function
    19.   stepmotor(); //go the the stepmotor function
    20.  
    21. }
    22.  
    23. void readvalue() {
    24.   //put sensor read code here.. it will return back into the loop when done
    25. }
    26.  
    27. void stepmotor() {
    28.   //code to turn motor based on the sensor value goes here
    29. }
    30.  
    31.  
     
    Last edited: Feb 5, 2016
  5. vpoko

    Member

    Jan 5, 2012
    258
    47
    mcgyvr's answer pretty much covered it, but in C you have to declare a function (using just the function signature) separately from defining it (where you actually specify the code in the function body).

    So if you have a function that you define as:
    Code (Text):
    1.  
    2. void doSomething(int n)
    3. {
    4. /* some code */
    5. }
    6.  
    Then at the top of your program (before any function definitions, often done in a header file but can be in your c file) you should have:
    Code (Text):
    1.  
    2. void doSomething(int n);
    3.  
    See http://www.tutorialspoint.com/ansi_c/c_using_functions.htm for a more detailed explanation.
     
    Last edited: Feb 5, 2016
  6. WBahn

    Moderator

    Mar 31, 2012
    17,788
    4,808
    No, you do not have to declare a function separately from its definition. I almost never declare function prototypes except in header files for functions that I want to use in files other than the ones in which they are defined. I subscribe to the school of thought (and it is just one school) that it is better to, as much as possible, let the function definitions serve as the declaration. This does a few things -- it eliminates the need to keep the declaration and the definition synced and it ensures that you do not inadvertently end up with circular function call chains.
     
    Jswale likes this.
  7. Picbuster

    Member

    Dec 2, 2013
    376
    50
    This allows you to have always the last value available if not wanted trigger each time with the 'get new value'command
    /in your interrupt

    if (PIR1bits.ADIF)
    {
    PIR1bits.ADIF = 0; // Clear A/D conversion complete interrupt flag
    Delay1KTCYx(5); // Delay for 5000 instruction cycles
    Vin=(float)(ADRESH*256 + ADRESL);
    Vin=Vin*0.0357;
    ADCON0bits.GO = 1; get new value
    }

    //in main loop

    printf( "value %ld",Vin);
     
  8. vpoko

    Member

    Jan 5, 2012
    258
    47
    In ANSI C99, it's required if you're calling a function that hasn't been defined by the time you call it (i.e., it's defined further down in the code, even in the same source file). To me, being allowed to "call up" but not "call down" is too weird of a restriction, so personally I'd prototype everything.
     
    Last edited: Feb 6, 2016
  9. vpoko

    Member

    Jan 5, 2012
    258
    47
    WBahn, could you clarify your circular call chains comment? Even after giving it some thought, it's going over my head.
     
  10. WBahn

    Moderator

    Mar 31, 2012
    17,788
    4,808
    I'm simply talking about indirect recursion.

    Consider the following:

    Code (C):
    1.  
    2. int bob(int n)
    3. {
    4.    return sue(n);
    5. }
    6.  
    7. int sue(int n)
    8. {
    9.    return bob(n);
    10. }
    11.  
    If I have prototypes above this code, it will compile without warnings or errors. But if I don't, then I will at least get warnings about the call to sue() within bob() using an assumed interface. The deliberate use of indirect recursion is rare enough that most instances when it occurs are the result of logic errors (typically when updating code), thus developing a coding style that catches indirect recursion and makes you do something out of the ordinary (use a function prototype, in this case) to allow it has value.
     
    vpoko likes this.
Loading...