سلام.من یه پروژه دارم که تشخیص رنگ از روی فرکانس هستش اما هرکار می کنم فقط رنگ قرمز و زرد و بنفش رو تشخیص میده.برنامش رو میزارم کسی می تونه برام چک کنه و ایرادم رو بگه؟ممنون
کد:
#include <mega8.h> // Alphanumeric LCD functions #include <alcd.h> #include <delay.h> #include <stdio.h> #define S0 PORTC.3 #define S1 PORTC.2 #define S3 PORTC.1 #define S2 PORTC.0 #define out_f1 S0=0; S1=1 #define out_f2 S0=1; S1=0 #define out_f3 S0=1; S1=1 #define filter_red S2=0; S3=0 #define filter_blue S2=0; S3=1 #define filter_clear S2=1; S3=0 #define filter_green S2=1; S3=1 unsigned int frqun; unsigned int frq[3]; char lcd[16]; unsigned char rang[3][10]={ {"RED"},{"BLUE"},{"GRREN"}, }; void clac(void); // Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isr(void) { // Place your code here frqun=TCNT1; TCNT1=0; } // Declare your global variables here void main(void) { // Declare your local variables here // Input/Output Ports initialization // Port B initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0x00; // Port C initialization // Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00; DDRC=0xFF; // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0x00; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 7.813 kHz TCCR0=0x05; TCNT0=0x00; // Timer/Counter 1 initialization // Clock source: T1 pin Falling Edge // Mode: Normal top=0xFFFF // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge // Timer1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0x00; TCCR1B=0x06; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer2 Stopped // Mode: Normal top=0xFF // OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off MCUCR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x01; // USART initialization // USART disabled UCSRB=0x00; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // ADC initialization // ADC disabled ADCSRA=0x00; // SPI initialization // SPI disabled SPCR=0x00; // TWI initialization // TWI disabled TWCR=0x00; // Alphanumeric LCD initialization // Connections are specified in the // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu: // RS - PORTD Bit 7 // RD - PORTB Bit 0 // EN - PORTB Bit 1 // D4 - PORTB Bit 2 // D5 - PORTB Bit 3 // D6 - PORTB Bit 4 // D7 - PORTB Bit 5 // Characters/line: 8 lcd_init(16); // Global enable interrupts #asm("sei") lcd_putsf("HI"); out_f2; while (1) { filter_red; delay_ms(200); frq[0]=frqun; filter_blue; delay_ms(200); frq[1]=frqun; filter_green; delay_ms(200); frq[2]=frqun; clac(); } } void clac(void){ unsigned int max; char num[3]; char T,I; int s1,s2; num[0]=0; max=frq[num[0]]; if(max<frq[1]){max=frq[1];num[0]=1;} if(max<frq[2]){max=frq[2];num[0]=2;} T=1; for(I=0;I<3;I++){ if(I!=num[0]){num[T]=I;T++;} } if(frq[num[1]]<frq[num[2]]){ max=num[2]; num[2]=num[1]; num[1]=max; } lcd_clear(); s1=frq[num[0]]-frq[num[1]]; s2=frq[num[0]]-frq[num[2]]; if(s1>s2)max=s1-s2;else max=s2-s1; lcd_gotoxy(0,0); sprintf (lcd,"R=%d B=%d G=%d",frq[0]/10,frq[1]/10,frq[2]/10); lcd_puts(lcd); lcd_gotoxy(0,1); if(max<50&s1>300&s2>300)lcd_puts(rang[num[0]]); else { sprintf(lcd,"%s/%s ",rang[num[0]],rang[num[1]]); lcd_puts(lcd); if(num[0]==0 & num[1]==1)lcd_putsf("Violet"); if(num[0]==0 & num[1]==2)lcd_putsf("Yellow"); if(num[0]==1 & num[1]==0)lcd_putsf("Violet"); if(num[0]==1 & num[1]==2)lcd_putsf("Indigo"); if(num[0]==2 & num[1]==0)lcd_putsf("Yellow"); if(num[0]==2 & num[1]==1)lcd_putsf("Indigo"); } }
دیدگاه