Multiplexer lauffähig

Dependencies:   USBDevice mbed

Fork of Multiplexer-Test by H2M Teststand

Multiplexer_read.cpp

Committer:
O_Shovah
Date:
2014-09-09
Revision:
8:fd403cfcaa0a
Parent:
0:42c1addaf061
Child:
9:341c831b162b

File content as of revision 8:fd403cfcaa0a:

#include "mbed.h"


//Outputs

DigitalOut timetest_0 (p30);


BusOut    unused(p18);


//Inputs

InterruptIn Drehzahl_lichtschranke(p29);

Timer Umlaufzeit;

//Communication

Serial pc(USBTX, USBRX);

volatile int Drehzeit_counter = 0;
#define DREHZEIT_SIZE 3
volatile int Drehzeit[DREHZEIT_SIZE];


void Motor_drehzahl()
{
    timetest_0 = 1;
    static bool first_run = true;
    int tmp = Umlaufzeit.read_us();
    if (first_run) {
        Umlaufzeit.start();
        first_run = false;
        return;
    }
    if (tmp < 1000) return;

    // Cache last 3 values for averaging
    Drehzeit[Drehzeit_counter % DREHZEIT_SIZE] = tmp;
    ++Drehzeit_counter;
    Umlaufzeit.reset();
    
    timetest_0 = 0;
}

int main(void)
{


    Drehzahl_lichtschranke.fall(&Motor_drehzahl);


    // Time counters
    Timer timer_print;
    timer_print.start();


    float motor_n_cur = 0;



    while(true) {



        // Calculate motor_n_cur by averaging
        int drehzeit_sum = 0;
        for (int i=0; i != DREHZEIT_SIZE; ++i)
            drehzeit_sum += Drehzeit[i];

        motor_n_cur = (drehzeit_sum ? (1.0e6/drehzeit_sum)*DREHZEIT_SIZE : 0.0)*60;


        // Set motor_n_cur to 0 if the interrupt wasn't called for a specified time
        if (Umlaufzeit.read_ms() > 200)
            motor_n_cur = 0.0;

        pc.printf("%f\n\r",motor_n_cur);
        
        wait(0.5);
    }
}