Grundlage war das Programm 12_PingSensor.
Wie es funktioniert, seht ihr hier im kurzem YouTube-Video: http://youtu.be/fEGlwN2663k
Den Code habe ich angehängt, er ist aber noch fertig. Die Formatierung ist leicht chaotisch, da die vom Editor erzeugten Tabs nicht korrekt angezeigt werden.
Ich wünsche allen ein wunderschönes Jahr 2014!
Chris
Code: Select all
/*
* Drehzahlmesser für Plattenspieler
* Ausgabe in Umdrehungen pro Minute
* Date: Dec, 2014
*/
#include "main.h"
#include "lcd.h"
#include "meineFunktionen.h"
#define Impuls BIT3
main()
{
unsigned long width, timeout =300000; // 3s in 10us
volatile unsigned long ump100, umpVorkomma, umpNachkomma,duration_us;
vCPU_init(); // sets DCO to 1MHz
vLCD_init(16); // init display with 16 columns, Cursor at 0,0
vLCD_puts("UMP V0.1 DL1CR"); // write string to first line
P1DIR &= ~Impuls; // P1.3 ist Eingang
P1OUT |= Impuls; // P1.3 ist Eingang, aber pull-up/down auf "up" setzen
P1REN |= Impuls; // Widerstand zuschalten
while( (P1IN & Impuls)); // Warte auf L-Impuls , Marke erscheint
while( !(P1IN & Impuls)); // Warte auf H-Impuls
vLCD_gotoxy(0,0);
vLCD_puts(" ");
vLCD_gotoxy(0,1);
vLCD_puts(" ");
while(42) {
width=0; //reset pulse width counter
while( (P1IN & Impuls)); // Warte auf L-Impuls
while( !(P1IN & Impuls)); // Warte auf H-Impuls
while( (P1IN & Impuls)) // Warte auf L-Impuls und nimm die Zeit
width++; // it took abt 10 cycles@1MHz per count, Einheit 10us
while( (P1IN & Impuls)) // Warte auf h-Impuls und nimm die Zeit
width++; // it took abt 10 cycles@1MHz per count, Einheit 10us
if (width > timeout) width = timeout; //limit to 3s //
// ump = 1/width Umdrehungen pro 10 Microsekunde
// ump = (1E5)/width Umdrehungen pro Sekunde
// ump = (60*1E5)/width Umdrehungen pro Minute
ump100 = (6000*1E5)/width; // 100 * Umdrehungen pro Minute
umpVorkomma = ump100/100; // Vorkommaanteil Umdrehungen pro Minute
umpNachkomma = ump100 - 100*umpVorkomma; // Nachkommaanteil Umdrehungen pro Minute
vLCD_gotoxy(0,0);
vLCD_puts(" ");
vLCD_gotoxy(0,0);
vLCD_putui(width/100);
vLCD_puts(" ms");
vLCD_gotoxy(0,1);
vLCD_puts(" ");
vLCD_gotoxy(0,1);
vLCD_putui(umpVorkomma);
vLCD_puts(".");
vLCD_putui(umpNachkomma);
vLCD_puts(" U/min");
vDelay_ms(5000); //limit update rate
}
}