10 Replies Latest reply on Sep 16, 2015 1:32 PM by user_343349849

    Trouble with Timer code for PSOC 4200

    user_343349849

      Hi,

         

      I need to implement a synchronous receiver that receives 4 Data Bytes. The bytes are clocked by a hardware clock pin. I have written a routine that works well when data is coming in but if the transmitter stops at certain points part way through, my code hangs. My code should be receiving bits based on clock state but have an overall timeout so if data stops then the routine exits after timeout period but it is not working.   If I call the "sampleLF" routine when no data being received, it hangs in the loop and never sees Timer_Flag set.

         

      Any help much appreciated, thanks.

         

      Here is my code that uses a 32 bit timer component

         

      #define LFSAMPLETIME  24000000/1000*80

         

      void StartTimer(volatile uint32 tdelay){
          Timer_WritePeriod(tdelay);
          Timer_WriteCounter(tdelay);
          Timer_Enable(); 
          Timer_Flag = 0;    
      }

         

      void StopTimer(void){
         Timer_Stop();
         Timer_Flag = 0;   
      }

         

      void SampleLFData(uint8 * buf){
          volatile uint8  Sample;
          volatile uint8  nBytes;
          volatile uint8 SamplePtr;

         

          Sample = 0;
          nBytes = 0;
          SamplePtr = 0;
       
          StartTimer(LFSAMPLETIME);  //12ms Data 4 Bytes should take 8ms to receive
          while ((!Timer_Flag)&&(nBytes < 4)){   //get 4 bytes or timeout
               while(!CL_DAT_Read() & !Timer_Flag){} //wait for clock high
               Sample<<=1;
               if(DAT_Read()) Sample |= 1;   //read data bit
               SamplePtr++;
               if(SamplePtr == 8){            //save every 8 bits as byte
                if(nBytes < 4) buf[nBytes] = Sample;
                Sample= 0;
                nBytes++;
                SamplePtr = 0;
               }
               while(CL_DAT_Read() & !Timer_Flag){}  //wait for clock low
               CyDelayUs(50);  //clock settle time
          }
          StopTimer();
       }