xuwenhua625 发表于 2015-1-16 19:06:01

arduino 如何测定输入信号频率?

是这样的,我需要测定一个转轴的实时转速,买了如下图所示的转速仪,600脉冲/转
我想通过实时测定信号频率,确定转速
请教如何通过arduino 测定输入信号频率?

zoologist 发表于 2015-1-16 22:11:13

这是我测风扇转速的,你可以参考一下

//code by Crenn from http://thebestcasescenario.com
//project by Charles Gantt from http://themakersworkbench.com


/*To disable interrupts:
cli();                // disable global interrupts

and to enable them:
sei();                // enable interrupts
*/


                                 //Varibles used for calculations
int NbTopsFan;
int Calc;

                                  //The pin location of the sensor
int hallsensor = 2;

                        
typedef struct{                  //Defines the structure for multiple fans and their dividers
char fantype;
unsigned int fandiv;
}fanspec;

//Definitions of the fans
fanspec fanspace={{0,1},{1,2},{2,8}};

char fan = 1;   //This is the varible used to select the fan and it's divider, set 1 for unipole hall effect sensor
               //and 2 for bipole hall effect sensor


void rpm ()      //This is the function that the interupt calls
{
NbTopsFan++;
}

            //This is the setup function where the serial port is initialised,
             //and the interrupt is attached
void setup()
{
pinMode(hallsensor, INPUT);
Serial.begin(9600);
attachInterrupt(0, rpm, RISING);
}
void loop ()
{
   NbTopsFan = 0;        //Set NbTops to 0 ready for calculations
   sei();                //Enables interrupts
   delay (1000);        //Wait 1 second
   cli();                //Disable interrupts
   Calc = ((NbTopsFan * 60)/fanspace.fandiv); //Times NbTopsFan (which is apprioxiamately the fequency the fan is spinning at) by 60 seconds before dividing by the fan's divider
   Serial.print (Calc, DEC); //Prints the number calculated above
   Serial.print (" rpm\r\n"); //Prints " rpm" and a new line
}

林定祥 发表于 2015-1-17 17:19:19

用中断计时

zoologist 发表于 2015-1-17 19:43:10

这是我用来测cpu风扇转速的程序,很好用,你这个应该也能适用

//code by Crenn from http://thebestcasescenario.com
//project by Charles Gantt from http://themakersworkbench.com


/*To disable interrupts:
cli();                // disable global interrupts

and to enable them:
sei();                // enable interrupts
*/


                                 //Varibles used for calculations
int NbTopsFan;
int Calc;

                                  //The pin location of the sensor
int hallsensor = 2;

                        
typedef struct{                  //Defines the structure for multiple fans and their dividers
char fantype;
unsigned int fandiv;
}fanspec;

//Definitions of the fans
fanspec fanspace={{0,1},{1,2},{2,8}};

char fan = 1;   //This is the varible used to select the fan and it's divider, set 1 for unipole hall effect sensor
               //and 2 for bipole hall effect sensor


void rpm ()      //This is the function that the interupt calls
{
NbTopsFan++;
}

            //This is the setup function where the serial port is initialised,
             //and the interrupt is attached
void setup()
{
pinMode(hallsensor, INPUT);
Serial.begin(9600);
attachInterrupt(0, rpm, RISING);
}
void loop ()
{
   NbTopsFan = 0;        //Set NbTops to 0 ready for calculations
   sei();                //Enables interrupts
   delay (1000);        //Wait 1 second
   cli();                //Disable interrupts
   Calc = ((NbTopsFan * 60)/fanspace.fandiv); //Times NbTopsFan (which is apprioxiamately the fequency the fan is spinning at) by 60 seconds before dividing by the fan's divider
   Serial.print (Calc, DEC); //Prints the number calculated above
   Serial.print (" rpm\r\n"); //Prints " rpm" and a new line
}

lm4766 发表于 2015-1-17 20:31:34

用中断计数,我用stm32最高测到1.37M
页: [1]
查看完整版本: arduino 如何测定输入信号频率?