颜色传感器(TCS230)接口不适用于ATMEGA16?

时间:2015-07-28 11:05:36

标签: arduino microcontroller avr atmega arduino-ide

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

  #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;


          }


}    

1 个答案:

答案 0 :(得分:0)

Many issues.

1) don't forget to add break into switch after each case block

2) assignments to PORTA overwrite previous value in this port. I.e. in this construction:

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

you will have a zero (last line) on all the outputs of the PORTA.

3) Even, if something will out to PORTA in chkcolour(), it will be overwritten in next couple of microseconds, because in next iteration it will be assigned:

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

To set or clear bits use construction like this:

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