#include #include #include #include #ifndef F_CPU #define F_CPU 16000000L #include #endif #define UART_UBRR_CALC(BAUD_,FREQ_) ((FREQ_)/((BAUD_)*16L)-1) #define UART_BAUD_RATE 115200 #define UART_BUFFER 64 #define FREQUENCY 20000L #define PI 3.14159 #define AMPLIFIER 2 ///////////////////////////////////////////////////////////////////////////////// //Init all variables const float cosinus[] PROGMEM= {1.00000,0.99985,0.99939,0.99863,0.99756,0.99619,0.99452,0.99255,0.99027, 0.98769,0.98481,0.98163,0.97815,0.97437,0.97030,0.96593,0.96126, 0.95630,0.95106,0.94552,0.93969,0.93358,0.92718,0.92050,0.91355, 0.90631,0.89879,0.89101,0.88295,0.87462,0.86603,0.85717,0.84805, 0.83867,0.82904,0.81915,0.80902,0.79864,0.78801,0.77715,0.76604, 0.75471,0.74314,0.73135,0.71934,0.70711,0.69466,0.68200,0.66913, 0.65606,0.64279,0.62932,0.61566,0.60182,0.58779,0.57358,0.55919, 0.54464,0.52992,0.51504,0.50000,0.48481,0.46947,0.45399,0.43837, 0.42262,0.40674,0.39073,0.37461,0.35837,0.34202,0.32557,0.30902, 0.29237,0.27564,0.25882,0.24192,0.22495,0.20791,0.19081,0.17365, 0.15643,0.13917,0.12187,0.10453,0.08716,0.06976,0.05234,0.03490, 0.01745,0.00000,-0.01745,-0.03490,-0.05234,-0.06976,-0.08716,-0.10453, -0.12187,-0.13917,-0.15643,-0.17365,-0.19081,-0.20791,-0.22495,-0.24192, -0.25882,-0.27564,-0.29237,-0.30902,-0.32557,-0.34202,-0.35837,-0.37461, -0.39073,-0.40674,-0.42262,-0.43837,-0.45399,-0.46947,-0.48481,-0.50000, -0.51504,-0.52992,-0.54464,-0.55919,-0.57358,-0.58779,-0.60182,-0.61566, -0.62932,-0.64279,-0.65606,-0.66913,-0.68200,-0.69466,-0.70711,-0.71934, -0.73135,-0.74314,-0.75471,-0.76604,-0.77715,-0.78801,-0.79864,-0.80902, -0.81915,-0.82904,-0.83867,-0.84805,-0.85717,-0.86603,-0.87462,-0.88295, -0.89101,-0.89879,-0.90631,-0.91355,-0.92050,-0.92718,-0.93358,-0.93969, -0.94552,-0.95106,-0.95630,-0.96126,-0.96593,-0.97030,-0.97437,-0.97815, -0.98163,-0.98481,-0.98769,-0.99027,-0.99255,-0.99452,-0.99619,-0.99756, -0.99863,-0.99939,-0.99985,-1.00000,-0.99985,-0.99939,-0.99863,-0.99756, -0.99619,-0.99452,-0.99255,-0.99027,-0.98769,-0.98481,-0.98163,-0.97815, -0.97437,-0.97030,-0.96593,-0.96126,-0.95630,-0.95106,-0.94552,-0.93969, -0.93358,-0.92718,-0.92050,-0.91355,-0.90631,-0.89879,-0.89101,-0.88295, -0.87462,-0.86603,-0.85717,-0.84805,-0.83867,-0.82904,-0.81915,-0.80902, -0.79864,-0.78801,-0.77715,-0.76604,-0.75471,-0.74314,-0.73135,-0.71934, -0.70711,-0.69466,-0.68200,-0.66913,-0.65606,-0.64279,-0.62932,-0.61566, -0.60182,-0.58779,-0.57358,-0.55919,-0.54464,-0.52992,-0.51504,-0.50000, -0.48481,-0.46947,-0.45399,-0.43837,-0.42262,-0.40674,-0.39073,-0.37461, -0.35837,-0.34202,-0.32557,-0.30902,-0.29237,-0.27564,-0.25882,-0.24192, -0.22495,-0.20791,-0.19081,-0.17365,-0.15643,-0.13917,-0.12187,-0.10453, -0.08716,-0.06976,-0.05234,-0.03490,-0.01745,0.00000,0.01745,0.03490, 0.05234,0.06976,0.08716,0.10453,0.12187,0.13917,0.15643,0.17365, 0.19081,0.20791,0.22495,0.24192,0.25882,0.27564,0.29237,0.30902, 0.32557,0.34202,0.35837,0.37461,0.39073,0.40674,0.42262,0.43837, 0.45399,0.46947,0.48481,0.50000,0.51504,0.52992,0.54464,0.55919, 0.57358,0.58779,0.60182,0.61566,0.62932,0.64279,0.65606,0.66913, 0.68200,0.69466,0.70711,0.71934,0.73135,0.74314,0.75471,0.76604, 0.77715,0.78801,0.79864,0.80902,0.81915,0.82904,0.83867,0.84805, 0.85717,0.86603,0.87462,0.88295,0.89101,0.89879,0.90631,0.91355, 0.92050,0.92718,0.93358,0.93969,0.94552,0.95106,0.95630,0.96126, 0.96593,0.97030,0.97437,0.97815,0.98163,0.98481,0.98769,0.99027, 0.99255,0.99452,0.99619,0.99756,0.99863,0.99939,0.99985}; const float sinus[] PROGMEM= {0.00000,0.01745,0.03490,0.05234,0.06976,0.08716,0.10453,0.12187,0.13917, 0.15643,0.17365,0.19081,0.20791,0.22495,0.24192,0.25882,0.27564, 0.29237,0.30902,0.32557,0.34202,0.35837,0.37461,0.39073,0.40674, 0.42262,0.43837,0.45399,0.46947,0.48481,0.50000,0.51504,0.52992, 0.54464,0.55919,0.57358,0.58779,0.60182,0.61566,0.62932,0.64279, 0.65606,0.66913,0.68200,0.69466,0.70711,0.71934,0.73135,0.74314, 0.75471,0.76604,0.77715,0.78801,0.79864,0.80902,0.81915,0.82904, 0.83867,0.84805,0.85717,0.86603,0.87462,0.88295,0.89101,0.89879, 0.90631,0.91355,0.92050,0.92718,0.93358,0.93969,0.94552,0.95106, 0.95630,0.96126,0.96593,0.97030,0.97437,0.97815,0.98163,0.98481, 0.98769,0.99027,0.99255,0.99452,0.99619,0.99756,0.99863,0.99939, 0.99985,1.00000,0.99985,0.99939,0.99863,0.99756,0.99619,0.99452, 0.99255,0.99027,0.98769,0.98481,0.98163,0.97815,0.97437,0.97030, 0.96593,0.96126,0.95630,0.95106,0.94552,0.93969,0.93358,0.92718, 0.92050,0.91355,0.90631,0.89879,0.89101,0.88295,0.87462,0.86603, 0.85717,0.84805,0.83867,0.82904,0.81915,0.80902,0.79864,0.78801, 0.77715,0.76604,0.75471,0.74314,0.73135,0.71934,0.70711,0.69466, 0.68200,0.66913,0.65606,0.64279,0.62932,0.61566,0.60182,0.58779, 0.57358,0.55919,0.54464,0.52992,0.51504,0.50000,0.48481,0.46947, 0.45399,0.43837,0.42262,0.40674,0.39073,0.37461,0.35837,0.34202, 0.32557,0.30902,0.29237,0.27564,0.25882,0.24192,0.22495,0.20791, 0.19081,0.17365,0.15643,0.13917,0.12187,0.10453,0.08716,0.06976, 0.05234,0.03490,0.01745,0.00000,-0.01745,-0.03490,-0.05234,-0.06976, -0.08716,-0.10453,-0.12187,-0.13917,-0.15643,-0.17365,-0.19081,-0.20791, -0.22495,-0.24192,-0.25882,-0.27564,-0.29237,-0.30902,-0.32557,-0.34202, -0.35837,-0.37461,-0.39073,-0.40674,-0.42262,-0.43837,-0.45399,-0.46947, -0.48481,-0.50000,-0.51504,-0.52992,-0.54464,-0.55919,-0.57358,-0.58779, -0.60182,-0.61566,-0.62932,-0.64279,-0.65606,-0.66913,-0.68200,-0.69466, -0.70711,-0.71934,-0.73135,-0.74314,-0.75471,-0.76604,-0.77715,-0.78801, -0.79864,-0.80902,-0.81915,-0.82904,-0.83867,-0.84805,-0.85717,-0.86603, -0.87462,-0.88295,-0.89101,-0.89879,-0.90631,-0.91355,-0.92050,-0.92718, -0.93358,-0.93969,-0.94552,-0.95106,-0.95630,-0.96126,-0.96593,-0.97030, -0.97437,-0.97815,-0.98163,-0.98481,-0.98769,-0.99027,-0.99255,-0.99452, -0.99619,-0.99756,-0.99863,-0.99939,-0.99985,-1.00000,-0.99985,-0.99939, -0.99863,-0.99756,-0.99619,-0.99452,-0.99255,-0.99027,-0.98769,-0.98481, -0.98163,-0.97815,-0.97437,-0.97030,-0.96593,-0.96126,-0.95630,-0.95106, -0.94552,-0.93969,-0.93358,-0.92718,-0.92050,-0.91355,-0.90631,-0.89879, -0.89101,-0.88295,-0.87462,-0.86603,-0.85717,-0.84805,-0.83867,-0.82904, -0.81915,-0.80902,-0.79864,-0.78801,-0.77715,-0.76604,-0.75471,-0.74314, -0.73135,-0.71934,-0.70711,-0.69466,-0.68200,-0.66913,-0.65606,-0.64279, -0.62932,-0.61566,-0.60182,-0.58779,-0.57358,-0.55919,-0.54464,-0.52992, -0.51504,-0.50000,-0.48481,-0.46947,-0.45399,-0.43837,-0.42262,-0.40674, -0.39073,-0.37461,-0.35837,-0.34202,-0.32557,-0.30902,-0.29237,-0.27564, -0.25882,-0.24192,-0.22495,-0.20791,-0.19081,-0.17365,-0.15643,-0.13917, -0.12187,-0.10453,-0.08716,-0.06976,-0.05234,-0.03490,-0.01745}; float volatile factoramp = 4883; float volatile amplitude =1.0; float volatile real=0.0; float volatile imag=0.0; float volatile factor=0.0; float volatile phase1 =0; uint8_t volatile haveValue; uint8_t uart_buffer[UART_BUFFER]; int8_t uart_count = 0; uint16_t phase0 =0; uint16_t phase2 =0; uint16_t phase3 =0; uint16_t phase4 =0; uint16_t adc_value =0; uint16_t prevCount; uint16_t lowDuty; uint16_t highDuty; ///////////////////////////////////////////////////////////////////////////////// //Interrupts ISR(SIG_INPUT_CAPTURE1) { uint16_t nowCount = ICR1; if( TCCR1B & ( 1 << ICES1 ) ) { lowDuty = nowCount - prevCount; // wir sind auf einer steigenden Flanke -> // d.h. der low Duty wurde gemessen TCCR1B &= ~( 1 << ICES1 ); // die nächste Flanke soll eine fallende sein } else { TCNT1 = 0; highDuty = nowCount - prevCount; TCCR1B |= ( 1 << ICES1 ); // die nächste Flanke soll eine steigende sein } haveValue = 1; TIFR &= ~(1<>8); UBRRL = (uint8_t) UART_UBRR_CALC(baudrate,F_CPU); UCSRA |= (1<