Arduino电机测速原理及代码实现

发布时间 2023-07-30 12:59:37作者: asandstar

 

 

 

//Include the TimerOne Library from Paul Stoffregen
#include "TimerOne.h"

//Constants for Interrupt Pins
//Change values if not using Arduino Uno

const byte MOTOR1 = 2;  //Motor 1 Interrupt Pin - INT 0
const byte MOTOR2 = 3;  //Motor 2 Interrupt Pin - INT 1

//Integers for pulse counters
unsigned int counter1 = 0;
unsigned int counter2 = 0;
//Float for number of slots in encoder disk
float diskslots 30; //Change to match value of encoder disk

//Interrupt Service Routines

//Motor 1 pulse count ISR
void ISR_count1()
{
    counter1++;    // increment Motor 1 counter value
}
//Motor 2 pulse count ISR
void ISR_count2()
{ 
   counter2++;    // increment Motor 2 counter value
}

//TimerOne ISR
void ISR timerone (
{
    Timerl.detachInterrupt ();    //    Stop the timer
    Serial.print ("Motor Speed 1:");
    float rotationl = (counterl / diskslots) * 60.00;    //calculate RPM for Motor 1
    Serial.print (rotationl);
    Serial.print (" RPM - ");
    counterl = 0; // reset counter to zero
    Serial.print ("Motor Speed 2:");
    float rotation2 = (counter2 / diskslots) * 60.00; //calculate RPM for Motor 2
    Serial.print (rotation2);
    Serial.println (" RPM");
    counter2 = 0; // reset counter to zero
    Timerl.attachInterrupt ( ISR_timerone ); // Enable the timer
}

void setup()
{
    Serial.begin(9600);

    Timerl.initialize(1000000); // set timer for lsec
    attachInterrupt (digitalPinToInterrupt (MOTOR1), ISR_count1, RISING); // Increase counter 1 when speed sensor pin goes High
    attachInterrupt (digitalPinToInterrupt (MOTOR2), ISR_count2, RISING); // Increase counter 2 when speed sensor pin goes High
    Timer1.attachInterrupt( ISR_timerone ); // Enable the timer
}

void loop()
{
    //Nothing in the loop!
    //You can place code here
}