diff --git a/es/blatt3/uebung3-1-test/uebung3-1-test.ino b/es/blatt3/uebung3-1-test/uebung3-1-test.ino new file mode 100644 index 0000000..277ed83 --- /dev/null +++ b/es/blatt3/uebung3-1-test/uebung3-1-test.ino @@ -0,0 +1,77 @@ +// these variables describe the used hardware pins +// adjust them when you use other pins +// hardware pins +int az = 50; + +int xout = A1; +int zout = A3; +int vref = A0; + +// used to achieve a 10 Hz frequency +// don't touch them +int rc = 1049999; + +bool volatile read_ready = false; + +/** + * Setup function for initial setup code + */ +void setup() { + pmc_set_writeprotect(false); + pmc_enable_periph_clk(ID_TC0); + + // configure hardware timer + TC_Configure(TC0, 0, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2) ; + TC_SetRC(TC0, 0, rc); + + TC0->TC_CHANNEL[0].TC_IER=TC_IER_CPCS; // IER = interrupt enable register + TC0->TC_CHANNEL[0].TC_IDR=~TC_IER_CPCS; + + NVIC_ClearPendingIRQ(TC0_IRQn); + NVIC_EnableIRQ(TC0_IRQn); + + // start hardware timer + TC_Start(TC0, 0); + + // Configure axis pins for input mode + pinMode(az, OUTPUT); + digitalWrite(az, HIGH); + + // initialize serial port + Serial.begin(9600); +} + +/** + * Loop function for main code + */ +void loop() { + Serial.println("NOTHING HAPPENS"); + /*if (read_ready) { + int xAxis = analogRead(xout); + int zAxis = analogRead(zout); + int ref = analogRead(vref); + + int differenceXRef = xAxis - ref; + int differenceZRef = zAxis - ref; + + Serial.print("x:"); + Serial.println(xAxis); + Serial.print("z: "); + Serial.println(zAxis); + Serial.print("x - ref: "); + Serial.println(differenceXRef); + Serial.print("z - ref: "); + Serial.println(differenceZRef); + read_ready = false; + }*/ +} + +/** + * Used to handle the timer. + */ +void TC0_Handler() +{ + // request static for some magic behind the curtain + TC_GetStatus(TC0, 0); + read_ready = true; +} diff --git a/es/blatt3/uebung3-1/uebung3-1.ino b/es/blatt3/uebung3-1/uebung3-1.ino new file mode 100644 index 0000000..3894d74 --- /dev/null +++ b/es/blatt3/uebung3-1/uebung3-1.ino @@ -0,0 +1,87 @@ +// these variables describe the used hardware pins +// adjust them when you use other pins +// hardware pins +int az = 50; + +int xout = A1; +int zout = A3; +int vref = A0; + +// used to achieve a 10 Hz frequency +// don't touch them +int rc = 1049999; + +bool volatile read_ready = false; + +/** + * Setup function for initial setup code + */ +void setup() { + pmc_set_writeprotect(false); + pmc_enable_periph_clk(ID_TC0); + + // configure hardware timer + TC_Configure(TC0, 0, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2) ; + TC_SetRC(TC0, 0, rc); + + TC0->TC_CHANNEL[0].TC_IER=TC_IER_CPCS; // IER = interrupt enable register + TC0->TC_CHANNEL[0].TC_IDR=~TC_IER_CPCS; + + NVIC_ClearPendingIRQ(TC0_IRQn); + NVIC_EnableIRQ(TC0_IRQn); + + // start hardware timer + TC_Start(TC0, 0); + + // Configure axis pins for input mode + + pinMode(az, OUTPUT); + digitalWrite(az, HIGH); + + // initialize serial port + Serial.begin(9600); + delay(100); +} + +/** + * Loop function for main code + */ +void loop() { + if (read_ready) { + int xAxis = analogRead(xout); + int zAxis = analogRead(zout); + int ref = analogRead(vref); + + double differenceXRef = xAxis - ref; + double differenceZRef = zAxis - ref; + + double drehrateX = differenceXRef / 9.1; + double drehrateZ = differenceZRef / 9.1; + + Serial.print("x:"); + Serial.println(xAxis); + Serial.print("z: "); + Serial.println(zAxis); + Serial.print("ref: "); + Serial.println(ref); + Serial.print("x - ref: "); + Serial.println(differenceXRef); + Serial.print("z - ref: "); + Serial.println(differenceZRef); + Serial.print("Drehrate X: "); + Serial.println(drehrateX); + Serial.print("Drehrate Z: "); + Serial.println(drehrateZ); + read_ready = false; + } +} + +/** + * Used to handle the timer. + */ +void TC0_Handler() +{ + // request static for some magic behind the curtain + TC_GetStatus(TC0, 0); + read_ready = true; +} diff --git a/es/blatt3/uebung3-2/uebung3-2.ino b/es/blatt3/uebung3-2/uebung3-2.ino new file mode 100644 index 0000000..47aeb58 --- /dev/null +++ b/es/blatt3/uebung3-2/uebung3-2.ino @@ -0,0 +1,73 @@ +#include + +// these variables describe the used hardware pins +// adjust them when you use other pins +// hardware pins +int az = 50; + +int xout = A1; +int zout = A3; +int vref = A0; + +// servo +int servoPin = 11; +Servo ourServo; + +// used to achieve a 10 Hz frequency +// don't touch them +int rc = 1049999; + +bool volatile read_ready = false; + +/** + * Setup function for initial setup code + */ +void setup() { + pmc_set_writeprotect(false); + pmc_enable_periph_clk(ID_TC0); + + // configure hardware timer + TC_Configure(TC1, 0, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK2) ; + TC_SetRC(TC1, 0, rc); + + TC1->TC_CHANNEL[0].TC_IER=TC_IER_CPCS; // IER = interrupt enable register + TC1->TC_CHANNEL[0].TC_IDR=~TC_IER_CPCS; + + NVIC_ClearPendingIRQ(TC1_IRQn); + NVIC_EnableIRQ(TC1_IRQn); + + // start hardware timer + //TC_Start(TC0, 0); + + // Configure servo + ourServo.attach(servoPin); + ourServo.write(90); + + pinMode(az, OUTPUT); + pinMode(servoPin, OUTPUT); + + digitalWrite(az, HIGH); + + // initialize serial port + Serial.begin(9600); +} + +/** + * Loop function for main code + */ +void loop() { + for (int i = 0; i < 90; i++) { + ourServo.write(90 + i); + delay(10); + } + Serial.println(ourServo.read()); +} + +/** + * Used to handle the timer. + */ +void TC1_Handler() +{ + // request static for some magic behind the curtain + TC_GetStatus(TC1, 0); +}