2015-07-28 56 views
0

先生,我不知道为什么我的颜色传感器的C代码无法正常工作。我使用的是ATMEGA16微控制器,我使用的传感器是连接到微控制器的TO引脚PB0(PORTB0)的TCS230传感器。请帮助我,我在这里附上我的C代码 - 请记住,我使用20%的缩放比例,并且已经将S0和S1引脚连接到1(VCC)和0(Gnd)。颜色传感器(TCS230)接口不适用于ATMEGA16?

#define S2 PA0 
    #define S3 PA1 
    #define F_CPU 11.0592 
    #include <util/delay.h> 
    #include<avr/io.h> 
    #include<avr/interrupt.h> 
    //Variable declarations 
    unsigned char state; 
    unsigned int counter_r,counter_g,counter_b,counter_no; 
    unsigned char i=0; //to store value of counter 
    unsigned char flag=0; 
void chkcolour(); 

int main() 
    { 
    DDRB=0x00; //PB0 and T0(counter pin) Input 
    DDRA=0xFF; //PA2(R),PA3(G) & PA4(B) for RGB LED ,PA0(S2) & PA1(S3) for RGB selection ,Output Pins 
    TCNT0=0x00; 
    TCCR0=0x07; 
    state=0; //start from 0 then 1,2,then again same 

    sei(); 

    while(1) 
    { flag=0; 
switch(state) 
{ 
    case 0: 

     PORTA=0b00000000; //For Red 
     _delay_ms(1000); 
     counter_r=TCNT0; 
     TCNT0=0x00; 
     state=1; 

    case 1:  

     PORTA=0b00000010; //For blue 
     _delay_ms(1000); 
     counter_b=TCNT0; 
     TCNT0=0x00; 
     state=2; 

    case 2: 
     PORTA=0b00000011; //For Green 
     _delay_ms(1000); 
     counter_g=TCNT0; 
     TCNT0=0x00; 
     state=3; 

    case 3: 
     PORTA=0b00000001; //No Filter 
     _delay_ms(1000); 
     counter_no=TCNT0; 
     TCNT0=0x00; 
     state=0; 
     break; 

} 


chkcolour(); 

} 

return 0; 
} 



void chkcolour() 
{ 


     if((counter_r > counter_b) && (counter_r > counter_g)) 
      { 
      PORTA=0b00000100; //Glow RED LED,Off Green LED,Off Blue LED 
      flag=1; 


      }  

     else if((counter_g > counter_r) && (counter_g > counter_b)) 
     { 
     PORTA=0b00001000; //Glow GEREEN LED,Off RED LED,Off Blue LED 
     flag=1; 


      } 

     else if((counter_b > counter_r) && (counter_b > counter_g) ) 
     { 
     PORTA=0b00010000; //Glow BLUE LED,Off RED LED,Off GREEN LED 
     flag=1; 


      } 


    else 
      { 
     PORTA=0b00000000; //0ff GEREEN LED,Off RED LED,Off Blue LED 
     flag=1; 


      } 


}  
+0

请编辑您的问题,以解释您的意思是“不工作”。 – Blackwood

回答

0

很多问题。

1)不要忘记每个情况

2)分配到PORTA在此端口覆盖以前的值后添加断裂开关。即在此构造中:

 PORTA=(0<<PORTA2); //Off RED LED 
    PORTA=(1<<PORTA3); //Glow Green LED 
    PORTA=(0<<PORTA4); //off Blue LED 

您将在PORTA的所有输出上都有一个零(最后一行)。

3)甚至,如果事情会出PORTA在chkcolour(),它将在接下来的几微秒的覆盖,因为下一次迭代将被分配:

PORTA=(0<<S3) | (0<<S2) ; //For Red 

设置或清除位使用像这样的结构:

PORTA |= (1 << bitnum); // to set a bit 
PORTA &= ~(1 << bitnum); // to clear (note bitwise negation ~ symbol) 
+0

先生,我不是在每种情况下使用中断,因为我想将我的'控制'发送到下一个状态..我根据您的建议对我的代码进行了一些更改,但它仍然无效...请帮助 –

+0

然后,为什么你需要使用“开关”呢?再看一遍:无论在chkcolour()中将输出到PORTA中,它将立即被PORTA = 0b00000000覆盖;在你的交换机中。使用PORTA =(PORTA&0b11111100)| Z Z;代替。其中zz - 0b00 0b01 0b10 0b11(每个“case”的最后两位) – AterLux

+0

先生,请检查这个问题,并给你的sugesstion ------- http://stackoverflow.com/questions/31730103/counting-without-反式 - 微控制器是,可能 –