AnsweredAssumed Answered

TCS3200 color sensor with stm32f4

Question asked by hiep.nguyen on Oct 18, 2014
Latest reply on Oct 18, 2014 by Clive One
Hi 
I'm trying to use TCS3200 color sensor interface with stm32f4
I used input capture of timer 2, channel 3 and Interrupt to calculator pulse from OUT sensor
Of course, i don't  know algorithm good. Because the sensor have 4 mode: blue, red, green and clear. One mode, we have a pulse another.
i wrote as follow

void Color_filter(void)
     {
          /* Reset Sensor value */
          RedMode = 1;
          GreenMode = 0;
          BlueMode = 0;
          
          TimeRed_H = 0;
          TimeRed_L = 0;
          TimeGreen_H=0;
          TimeGreen_L=0;
          TimeBlue_H=0;
          TimeBlue_L=0;     
          
          /* Enable the CC3 Interrupt Request */
       TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE);
          
          /* PB3--> S2, PB4--> S3*/
          
               /* RED Filter */
          GPIO_ResetBits(GPIOB,GPIO_Pin_3);
    GPIO_ResetBits(GPIOB,GPIO_Pin_4);
          Delay(1000);
          while(RedMode == 1);
          
          /* GREEN Filter */
          GPIO_SetBits(GPIOB,GPIO_Pin_3);
    GPIO_SetBits(GPIOB,GPIO_Pin_4);
          GreenMode = 1;
          Delay(1000);
    while(GreenMode == 1);
     
          /* BLUE Filter */
          GPIO_ResetBits(GPIOB,GPIO_Pin_3);
          GPIO_SetBits(GPIOB,GPIO_Pin_4);
          BlueMode = 1;
          Delay(1000);
    while(BlueMode == 1);
                    
          /* Clear */
          GPIO_SetBits(GPIOB,GPIO_Pin_3);
          GPIO_ResetBits(GPIOB,GPIO_Pin_4);


          /* Disable the CC3 Interrupt Request */
       TIM_ITConfig(TIM2, TIM_IT_CC3, DISABLE);


          /* Timimg Calculation avoid for overflow */
          if(TimeRed_H > TimeRed_L)
               TimeRed = (uint32_t)(TimeRed_H - TimeRed_L);
          else if(TimeRed_H < TimeRed_L)
               TimeRed = (uint32_t)((0xFFFF - TimeRed_L) + TimeRed_H);


          if(TimeGreen_H > TimeGreen_L)
               TimeGreen = (uint32_t)(TimeGreen_H - TimeGreen_L);
          else if(TimeGreen_H < TimeGreen_L)
                    TimeGreen = (uint32_t)((0xFFFF - TimeGreen_L) + TimeGreen_H);


          if(TimeBlue_H > TimeBlue_L)
               TimeBlue = (uint32_t)(TimeBlue_H - TimeBlue_L);
          else if(TimeBlue_H < TimeBlue_L)
               TimeBlue = (uint32_t)((0xFFFF - TimeBlue_L) + TimeBlue_H);


     /* Frequency Calculation */
     FreqRed = SystemCoreClock/TimeRed;
     FreqGreen = SystemCoreClock/TimeGreen;
     FreqBlue = SystemCoreClock/TimeBlue;


}


void Delay(__IO uint32_t nCount)
{
  while(nCount--)
  {
  }
}




void TIM2_IRQHandler(void)
{
     if(TIM_GetITStatus(TIM2,TIM_IT_CC3) == SET)
     {     
          if((RedMode == 1) && (Number == 0))
             {
                TimeRed_L = TIM_GetCapture3(TIM2);
                Number = 1; 
             }


             else if((RedMode == 1) && (Number == 1))
             {
                TimeRed_H = TIM_GetCapture3(TIM2);
                RedMode = 0; 
                         Number = 0;
         }
   
            else if((GreenMode == 1)  && (Number == 0))
             {
                TimeGreen_L = TIM_GetCapture3(TIM2);
                         Number = 1;  
             }
             else if((GreenMode == 1) && (Number == 1))
             {
                TimeGreen_H = TIM_GetCapture3(TIM2);
                         GreenMode = 0; 
                         Number = 0;
             }
   
             if((BlueMode == 1) && (Number == 0))
             {
                TimeBlue_L = TIM_GetCapture3(TIM2); 
                         Number = 1;
             }
             else if((BlueMode == 1)  && (Number == 1))
             {
                TimeBlue_H = TIM_GetCapture3(TIM2); 
                         BlueMode = 0;
                         Number = 0;
             }


          TIM_ClearITPendingBit(TIM2,TIM_IT_CC3);      
     }


}

it gets stuck when coming out of interrupt in the first. i really hope you can give me  one solution.


Outcomes