Code zur Erfassung der Drehzahlen

Dependencies:   mbed-dev

Committer:
David_90210
Date:
Wed May 10 09:35:28 2017 +0000
Revision:
0:9eba4fa208fb
Code zur Erfassung der Drehzahlen

Who changed what in which revision?

UserRevisionLine numberNew contents of line
David_90210 0:9eba4fa208fb 1 #include "mbed.h"
David_90210 0:9eba4fa208fb 2
David_90210 0:9eba4fa208fb 3 //AnalogOut aout1(PA_5); //A4 Motor 1
David_90210 0:9eba4fa208fb 4 //AnalogOut aout2(PA_6); //A5 Motor 2
David_90210 0:9eba4fa208fb 5
David_90210 0:9eba4fa208fb 6 InterruptIn rising2(D3); //Inkrementalgeber Spur A Motor 1
David_90210 0:9eba4fa208fb 7 //DigitalIn direction2(D2); //Inkrementalgeber Spur B Motor 1
David_90210 0:9eba4fa208fb 8 InterruptIn rising1(D8); //Inkrementalgeber Spur A Motor 2
David_90210 0:9eba4fa208fb 9 //DigitalIn direction1(D9); //Inkrementalgeber Spur B Motor 2
David_90210 0:9eba4fa208fb 10
David_90210 0:9eba4fa208fb 11 Timer t1; //Timer für den Inkrementalgeber des Motor 1
David_90210 0:9eba4fa208fb 12
David_90210 0:9eba4fa208fb 13 int t_period1 = 0; // This is the period between interrupts in microseconds
David_90210 0:9eba4fa208fb 14 float t_freq1 = 0; // Umrechnung in Frequenz
David_90210 0:9eba4fa208fb 15 int calculation1; // Kennlinien Berechnung
David_90210 0:9eba4fa208fb 16 int calculation2;
David_90210 0:9eba4fa208fb 17 int t_period2 = 0; // This is the period between interrupts in microseconds
David_90210 0:9eba4fa208fb 18 float t_freq2 = 0; // Umrechnung in Frequenz
David_90210 0:9eba4fa208fb 19 int tneu1,tneu2, tdiff1, tdiff2, talt1, talt2;
David_90210 0:9eba4fa208fb 20
David_90210 0:9eba4fa208fb 21 void extTrigger1();
David_90210 0:9eba4fa208fb 22 void extTrigger2();
David_90210 0:9eba4fa208fb 23
David_90210 0:9eba4fa208fb 24 int main()
David_90210 0:9eba4fa208fb 25 {
David_90210 0:9eba4fa208fb 26 RCC->APB1ENR |= 0x24000000; // Enable Interface clock for DAC1 and DAC2
David_90210 0:9eba4fa208fb 27 GPIOA->MODER |= 0x00003f00; // MODE PortA, PA4 PA5 & PA6 are analog!
David_90210 0:9eba4fa208fb 28 DAC->CR |= 0x00030003; // DAC control reg, both channels ON
David_90210 0:9eba4fa208fb 29 DAC2->CR |= 0x00000003; // DAC2 control reg channel 1 ON
David_90210 0:9eba4fa208fb 30
David_90210 0:9eba4fa208fb 31 wait(0.1);
David_90210 0:9eba4fa208fb 32
David_90210 0:9eba4fa208fb 33 rising1.mode(PullDown);
David_90210 0:9eba4fa208fb 34 rising1.rise(&extTrigger1);
David_90210 0:9eba4fa208fb 35 rising2.mode(PullDown);
David_90210 0:9eba4fa208fb 36 rising2.rise(&extTrigger2);
David_90210 0:9eba4fa208fb 37 t1.start();
David_90210 0:9eba4fa208fb 38
David_90210 0:9eba4fa208fb 39 while(1)
David_90210 0:9eba4fa208fb 40 {
David_90210 0:9eba4fa208fb 41 //printf("tdiff1: %d \n", tdiff1);
David_90210 0:9eba4fa208fb 42 //printf("tdiff2: %d \n", tdiff2);
David_90210 0:9eba4fa208fb 43 t_freq1 = (1.0/((float)tdiff1)*(1000000.0/256.0));
David_90210 0:9eba4fa208fb 44 t_freq2 = (1.0/((float)tdiff2)*(1000000.0/256.0));
David_90210 0:9eba4fa208fb 45 calculation1 = (((0.66f*t_freq1+1.65f)/3.3f)*4095.0f);
David_90210 0:9eba4fa208fb 46 if (calculation1 >= 4095)
David_90210 0:9eba4fa208fb 47 {
David_90210 0:9eba4fa208fb 48 calculation1 = 4095;
David_90210 0:9eba4fa208fb 49 }
David_90210 0:9eba4fa208fb 50 DAC2->DHR12R1 = calculation1;
David_90210 0:9eba4fa208fb 51 //printf("Motor 1: %.2f Hz %i Bitwert\n",t_freq1, calculation1);
David_90210 0:9eba4fa208fb 52
David_90210 0:9eba4fa208fb 53 calculation2 = (((0.66f*t_freq2+1.65f)/3.3f)*4095.0f);
David_90210 0:9eba4fa208fb 54 if (calculation2 >= 4095)
David_90210 0:9eba4fa208fb 55 {
David_90210 0:9eba4fa208fb 56 calculation2 = 4095;
David_90210 0:9eba4fa208fb 57 }
David_90210 0:9eba4fa208fb 58 //printf("Motor 2: %.2f Hz %i Bitwert\n",t_freq2, calculation2);
David_90210 0:9eba4fa208fb 59 if (calculation2 >= 4095000){ printf("a");} //Printf Funktion wird gebracuht damit das Programm Ordnungsgemäß arbeitet
David_90210 0:9eba4fa208fb 60 DAC->DHR12R2 = calculation2;
David_90210 0:9eba4fa208fb 61 }
David_90210 0:9eba4fa208fb 62 }
David_90210 0:9eba4fa208fb 63
David_90210 0:9eba4fa208fb 64 void extTrigger1()
David_90210 0:9eba4fa208fb 65 {
David_90210 0:9eba4fa208fb 66 tneu1 = t1.read_us();
David_90210 0:9eba4fa208fb 67 tdiff1 = tneu1 - talt1;
David_90210 0:9eba4fa208fb 68 //printf("tdiff1: %d \n", tdiff1);
David_90210 0:9eba4fa208fb 69 if (tdiff1 > 0)
David_90210 0:9eba4fa208fb 70 {
David_90210 0:9eba4fa208fb 71 talt1=tneu1;
David_90210 0:9eba4fa208fb 72 }
David_90210 0:9eba4fa208fb 73 }
David_90210 0:9eba4fa208fb 74
David_90210 0:9eba4fa208fb 75 void extTrigger2()
David_90210 0:9eba4fa208fb 76 {
David_90210 0:9eba4fa208fb 77 tneu2 = t1.read_us();
David_90210 0:9eba4fa208fb 78 tdiff2 = tneu2 - talt2;
David_90210 0:9eba4fa208fb 79 //printf("tdiff2: %d \n", tdiff2);
David_90210 0:9eba4fa208fb 80 if (tdiff2 > 0)
David_90210 0:9eba4fa208fb 81 {
David_90210 0:9eba4fa208fb 82 talt2=tneu2;
David_90210 0:9eba4fa208fb 83 }
David_90210 0:9eba4fa208fb 84 }