Hallo,
ich habe ein Problem. Ich muss eine Füllstandsmessung mit dem Arduino
UNO programmieren. Weiters muss ich mittels Counter die Zufluss und die
Abflussmenge bestimmen. Da der Timer0 für das Delay verantwortlich ist
musste ich das Problem mit dem Timer2 umgehen.
Ich bin also momentan soweit:
Timer0 wird als Counter betrieben und misst mir die Zuflussmenge.
Timer 1 wird ebenfalls als Counter betrieben und misst mir die
Abflussmenge.
Da dies nun alle 15 Minuten an einen PC übertragen werden muss habe ich
den Timer 2 folgendermaßen programmiert:
Prescaler 1024
In der Interruptroutine läuft nun eine if-Abfrage welche alle 15 Minuten
ausgelöst wird.
Gut.
Nun folgendes Problem:
Ich muss nun auch noch eine Abstandsmessung machen. Diese habe ich auch
schon programmiert und sie funktioniert auch, aber ich kann sie nicht in
das Programm einbinden, da ich hier ein delay benötige...
Ich habe folgendes versucht:
In der Interruptroutine des Timer2 Timer 0 stoppen und das Unterprogramm
aufrufen..
Aber das funktioniert nicht.
Hat jemand einen Vorschlag?
LG
Hier mein Programm:
#include <Wire.h>
#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>
uint8_t durchfluss;
uint16_t abfluss;
int ucCount;
int icCount;
void setup(void)
{ Wire.begin(); // join i2c bus (address optional for
master)
Serial.begin(9600); // start serial communication at 9600bps
pinMode(5, INPUT);
pinMode(4, INPUT);
//Timer 0 Init
TCCR0A &= ~(1 << COM1A1);
TCCR0A &= ~(1 << COM1A0);
TCCR0A &= ~(1 << COM1B1);
TCCR0A &= ~(1 << COM1B0);
TCCR0A &= ~(1 << WGM10);
TCCR0A &= ~(1 << WGM11);
TCCR0B &= ~(1 << WGM12);
TCCR0B &= ~(1 << WGM13);
TCCR0B &= ~(1<<FOC1A);
TCCR0B &= ~(1<<FOC1B);
TCCR0B |= (1<<CS10);
TCCR0B |= (1<<CS11);
TCCR0B |= (1<<CS12);
//Timer 1 Init
TCCR1A &= ~(1 << COM1A1);
TCCR1A &= ~(1 << COM1A0);
TCCR1A &= ~(1 << COM1B1);
TCCR1A &= ~(1 << COM1B0);
TCCR1A &= ~(1 << WGM10);
TCCR1A &= ~(1 << WGM11);
TCCR1B &= ~(1 << WGM12);
TCCR1B &= ~(1 << WGM13);
TCCR1B &= ~(1 << ICNC1);
TCCR1B &= ~(1<<ICES1);
TCCR1B |= (1<<CS10);
TCCR1B |= (1<<CS11);
TCCR1B |= (1<<CS12);
TCCR1C &= ~(1<<FOC1A);
TCCR1C &= ~(1<<FOC1B);
//Timer 2 I
TCCR2A &= ~(1 << COM1A1);
TCCR2A &= ~(1 << COM1A0);
TCCR2A &= ~(1 << COM1B1);
TCCR2A &= ~(1 << COM1B0);
TCCR1A &= ~(1 << WGM10);
TCCR2A &= ~(1 << WGM11);
TCCR2B &= ~(1 << WGM12);
TCCR2B &= ~(1 << WGM13);
TCCR2B &= ~(1 << ICNC1);
TCCR2B &= ~(1<<ICES1);
TCCR2B |= (1<<CS10);
TCCR2B |= (1<<CS11);
TCCR2B |= (1<<CS12);
TCCR2B &= ~(1<<FOC1A);
TCCR2B &= ~(1<<FOC1B);
TIMSK2 |= (1<<TOIE2);
sei();
Wire.begin(); // join i2c bus (address optional for
master)
Serial.begin(9600); // start serial communication at 9600bps
}
int reading = 0;
void loop()
{
abfluss=TCNT1;
durchfluss=TCNT0;
}
ISR(TIMER2_OVF_vect)
{ cli();
icCount=ucCount++;
if(icCount==65)
{ TCCR0B &= ~(1<<CS10);
TCCR0B &= ~(1<<CS11);
TCCR0B &= ~(1<<CS12);
Serial.print("Abfluss:");
Serial.print("\t");
Serial.println(abfluss,DEC);
Serial.print("Durchfluss");
Serial.print("\t");
Serial.println(durchfluss,DEC);
range();
TCCR0B |= (1<<CS10);
TCCR0B |= (1<<CS11);
TCCR0B |= (1<<CS12);
ucCount=0;
TCNT1=0;
TCNT0=0;
} // erhöht werden
sei();
}
void range()
{// step 1: instruct sensor to read echoes
Wire.beginTransmission(112); // transmit to device #112 (0x70)
// the address specified in the datasheet
is 224 (0xE0)
// but i2c adressing uses the high 7 bits
so it's 112
Wire.send(0x00); // sets register pointer to the command
register (0x00)
Wire.send(0x50); // command sensor to measure in "inches"
(0x50)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission(); // stop transmitting
// step 2: wait for readings to happen
delay(70); // datasheet suggests at least 65
milliseconds
// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(112); // transmit to device #112
Wire.send(0x02); // sets register pointer to echo #1
register (0x02)
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(112, 2); // request 2 bytes from slave device #112
// step 5: receive reading from sensor
if(2 <= Wire.available()) // if two bytes were received
{
reading = Wire.receive(); // receive high byte (overwrites previous
reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.receive(); // receive low byte as lower 8 bits
Serial.println(reading); // print the reading
} // wait a bit since people have to read the output
:)
}
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.