#include #include #include #include const char out_sign[10] = {0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111}; const float capture_time = 0.008192, a1=-202.7755, b1=10464.162, c1=1384729.4, d1=179769.39; const float a2=-8006.5664, b2=79823.682, c2=1918082.9, d2=287765.3; int N = 0; INTERRUPT(SIG_OUTPUT_COMPARE0) { N = 80; } float _get_opt_from_frequ (float frequ) { float light; float a=a1, b=b1, c=c1, d=d1; if (frequ>1349000) { a=a2; b=b2; c=c2; d=d2; } light=a+b/(1+square((frequ-c)/d)); return light; } void LED_OUT_4 (float out) { char dot=0, dig=0; float whole, fract; if (out>9999) { dot = 7; out/=10; } fract=modf(out,&whole); if (whole<10) { dot=3; } else if (whole<100) { dot=2; out=out/10; } else if (whole<1000) { dot=1; out=out/100; } else { out=out/1000; }; fract=modf(out,&whole); dig=out_sign[(int)whole]; if (dot==3) dig=dig|0x80; PORTC=0x01; PORTA=dig; whole=fract*10; fract=modf(whole,&whole); dig=out_sign[(int)whole]; if (dot==2) dig=dig|0x80; PORTC<<=1; PORTA=dig; whole=fract*10; fract=modf(whole,&whole); dig=out_sign[(int)whole]; if (dot==1) dig=dig|0x80; PORTC<<=1; PORTA=dig; whole=fract*10; fract=modf(whole,&whole); if (fract>=0.5) whole++; dig=out_sign[(int)whole]; if (dot==7) dig=dig|0x80; PORTC<<=1; PORTA=dig; } int main (void) { float frequ = 0; float light = 0; DDRA = 0xFF;//Set I/O ports DDRC = 0x0F; TCCR0=0x05;//Set timer0 OCR0=0x60; TIMSK=0x01; TCCR1B=0x46;//set timer1 to capture a rising edge sei();//set global interrupt while(1) { if (N!=7) { frequ=N/capture_time;//computing frequency light=_get_opt_from_frequ(frequ);//computing light N=7; } light=TCNT1L; if (frequ==0) light=0; LED_OUT_4(light);//output light PORTA=0x00; } }