10 years, 5 months ago.

How to make a function call ?

Hello. I'll get right to it . I recently commenncer to program with mbed . I wrote a program Id - cyl makes a cylinder identification. This function , I would like to integrate it into my main program test_dwell ; all this to gain lines of code. Please do you know how?

-Voici ma fonction Id-cycle

#include "mbed.h"
#include "TextLCD.h"
 
TextLCD lcd(p15, p14, p17, p18, p19, p20); // rs, e, d4-d7  //déclaration de p15,p16,P17... dans lcd
Ticker tick;
InterruptIn in(p10); // déclaration de p10 dans in
DigitalIn  bougie(p7); //commande l'info bougie; sert à identifier chaque cylindre
DigitalIn rupt(p6);//Commande le rupteur
DigitalOut cc(p5);//commande le court-circuit; sert aussi à enlevé un des cylindres

// Déclaration des bp
DigitalIn  bp1(p21);
DigitalIn  bp2(p22);
DigitalIn  bp3(p23);
DigitalIn  bp4(p24);
DigitalIn  bp5(p25);
DigitalIn  bp6(p26);
// Déclaration des led

DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);


int main() { 
short int cyl1,cyl2,cyl3,cyl4,cyl5,cyl6;
int ref_cyl2; // déclaration de cette entrée qui va servir de référence dans l'identification des cylindres
if(bp1==0){ //condition pour que le reperage de bougie fonctionne
if ((bougie and rupt)==1)
 {cyl1=1; // allumer la led1
 myled1=1;}
  

 
 if ((rupt and cyl1)==1)
 {   cyl1=0;
     cyl3=1;
 myled3=1;
 myled1=0;}
 
 if ((rupt and cyl3)==1)
 {   cyl3=0;
     cyl4=1;
     myled4=1;
     myled3=0;}
    
 if ((rupt and cyl4)==1)
 {   
     cyl4=0;
     cyl2=1;
     myled4=0;
     myled2=1;}

}
 //mise à la masse
 if((bp2 and cyl2)==1)
 {
     cc=1;
    if (cyl1==1)cc=0;
    }
    
  if((bp3 and cyl3)==1)
  {cc=1;
  if (cyl4==1) cc=0;
  }
  if((bp4 and cyl3)==1)
  {cc=1;
  if (cyl2==1) cc=0;
  }
 /*Problème
  if((bp1 and cyl1)==1)
  {cc=1;
  if (cyl3==1) cc=0;
  }
  */
  
  if((ref_cyl2)==1)
  {cyl1=1;
  cyl2=0;
  cyl3=0;
  cyl4=0;}
  
 } 
 

-Voici ma fonction test_dwell

#include "mbed.h"
#include "TextLCD.h"
 
TextLCD lcd(p15, p14, p17, p18, p19, p20); // rs, e, d4-d7  //déclaration de p15,p16,P17... dans lcd
Ticker tick;
InterruptIn in(p10); // déclaration de p10 dans in
DigitalIn  bougie(p7); //commande l'info bougie
DigitalIn rup(p6);//Commande le rupteur
DigitalOut cc(p5);//commande le court-circuit; sert aussi à enlevé un des cylindres

// Déclaration des bp
DigitalIn  bp1(p21);
DigitalIn  bp2(p22);
DigitalIn  bp3(p23);
DigitalIn  bp4(p24);
DigitalIn  bp5(p25);
DigitalIn  bp6(p26);
// Déclaration des led

DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);




Timer t;
Timer t_on;
Timer t_off;
 
int t_period = 0; // This is the period between interrupts in microseconds 
float t_freq = 0;  // <-- Set in an interrupt but read in main. Should be volatile or the compiler could optimise it out.
 
void flip(); //Renvoie la frequence
 
int main() { 
int trmin;
int dwell=0;
float alpha=0;
float angle1=0;float angle2=0;float angle3=0;float angle4=0;float angle5=0;float angle6=0;
short int cyl1,cyl2,cyl3,cyl4,cyl5,cyl6;

in.mode(PullDown); // Set the pin to Pull Down mode.
in.rise(&flip); //Set up the interrupt for rising edge
 
t.start(); //start the timer
 
while (1) {
 wait_ms(100);
 trmin=(t_freq*4*30)/4; 

 if (in==1) {  // <--- the input that you have created not the pin number.
    t_on.start();
    t_off.stop();
  } else {
    t_on.stop();
    t_off.start();
    alpha=t_on/(t_on+t_off);
    dwell=alpha*100;
}
 
   //lcd.printf("nbre de cylindres \n");
   
 
 // switch(s){
    // case e:
   if(bp1==1) {
        angle1=(dwell*360)/100;
         lcd.printf("ac 1cy=%f\r\n",angle1);}
 //        break;
         
   // case f:
   else if(bp2==1){
         angle2=(dwell*360)/200;
         lcd.printf("ac 2cy=%f\r\n",angle2);}
     //    break;
         
   // case g:
   else if(bp3==1){
         angle3=(dwell*360)/300;
         lcd.printf("ac 3cy=%f\r\n",angle3);
         break;}
    else if(bp4==1){
   // case h:
         angle4=(dwell*360)/400;
         lcd.printf("ac 4cy=%f\r\n",angle4);}
       //  break;  
         
    // case i:
    else if(bp5==1){
         angle5=(dwell*360)/500;
         lcd.printf("ac 5cy=%f\r\n",angle5);}
     //    break;
         
   // case j:
   else if(bp6==1){
         angle6=(dwell*360)/600;
         lcd.printf("ac 6cy=%f\r\n",angle6);}
       //  break;
         
    //  default:
       //   lcd.printf("choisir un cylindre\n"); 
     
     //} 
     
if ((bougie and rupt)==1)
 {cyl1=1; // allumer la led1
 myled1=1;}
  

 
 if ((rupt and cyl1)==1)
 {   cyl1=0;
     cyl3=1;
 myled3=1;
 myled1=0;}
 
 if ((rupt and cyl3)==1)
 {   cyl3=0;
     cyl4=1;
     myled4=1;
     myled3=0;}
    
 if ((rupt and cyl4)==1)
 {   
     cyl4=0;
     cyl2=1;
     myled4=0;
     myled2=1;}

}
 //mise à la masse
 if((bp2 and cyl2)==1)
 {
     cc=1;
    if (cyl1==1)cc=0;
    }
    
  if((bp3 and cyl3)==1)
  {cc=1;
  if (cyl4==1) cc=0;
  }
  if((bp4 and cyl3)==1)
  {cc=1;
  if (cyl2==1) cc=0;
  }
 /*Problème
  if((bp1 and cyl1)==1)
  {cc=1;
  if (cyl3==1) cc=0;
  }
  */
  
  if((ref_cyl2)==1)
  {cyl1=1;
  cyl2=0;
  cyl3=0;
  cyl4=0;}
  
 } 
 

I dont understand what you are trying to do exactly.

posted by Erik - 15 May 2015

1 Answer

10 years, 5 months ago.

Are you asking how to use a function in c?

void printHello() {
  printf("Hello\r\n");
}

main () {
  while (true) {
    printHello();
  }
}

However if you need to ask questions like that you really should go and work your way through a c programming tutorial, this is about as basic as it gets.