Need help with timer interrupt

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 Hi,

   

I am trying to set up an interrupt driven RS232 receiver that uses a simple state machine to gather bytes.

   

Essentially, Each time the interrupt fires and I capture the byte into my buffer, I want to set up a 50ms timeout so I can detect end of transmission (Unless the UART has some sort of idle time interrupt? that would be great).

   

The data is being received ok. I can run it for a while, pasue and look at the redceive buffer and correct data is in it.

   

The timeout does not seem to be working too well. The timer should be reinitialized to 50ms timeout each time a byte is received and when no byte within 50ms then the Timer-Int should fire and flag that data is ready in my buffer.

   

I also need a method to timeout the overall process (ie. Send a command and wait for certain time for response and then timeout if none). I suppose this would be a non-interrupt driven timer that I start and then poll for EITHER TC or RX Data in my buffer?

   

Here is my little snippet of code. Thanks, I appreciate all the help you folks have given me as I climb up the learning curve with PSOC and the components in creator.

   

 

   

CY_ISR(ServiceTimer1)   //timer 1 int - This routine should only fire when no bytes for 50ms and flag packet if receiving

   

{

   

    Timer1_Stop();  //50ms timeout since last byte received

   

    numSoundRXbytes = AudioRX_Ptr;  //get number of bytes received

   

    if(SoundRXmode == RECEIVING) DatainBuf = true;  //flag packet ready

   

}

   

 

   

CY_ISR(Service_SoundRX)  //this shuld fire evry received byte (it does) and should restart the timer to prevent it timing out if more bytes are arriving.

   

{

   

    AUDIO_RXBuf[AudioRX_Ptr++] = SOUND_RXDATA_REG;

   

    SoundRXmode = RECEIVING;

   

    Timer1_Stop();

   

    Timer1_WritePeriod(50000);  //50ms

   

    Timer1_Start();

   

    Audio_Int_StartEx(ServiceTimer1);  //Enable timer 1 int

   

}

   

 

   

void ResetAudio(void)

   

{

   

    DatainBuf = false;

   

    SoundRXmode = IDLE;

   

    AudioRX_Ptr = 0x00;

   

}

   
        
0 Likes
4 Replies
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 I can answer my own post. I managed to figure it out and use a sofware counter instead of trying to start and stop timer. Also needed to read status to reset timer. Code below if it is useful to anyone.

   

CY_ISR(ServiceTimer1)   //timer 1 int

   

{

   

    Timer1_STATUS;

   

    msTick++;

   

    if((SoundRXmode == RECEIVING)&&(msTick == RxTimeout))

   

    {

   

       DatainBuf = true;  //flag packet ready

   

    }

   

}

   

CY_ISR(Service_SoundRX)

   

{

   

    if(SoundRXmode == IDLE)

   

    {

   

        AudioRX_Ptr = 0x00;

   

        SoundRXmode = RECEIVING;

   

    }

   

    RxTimeout = msTick + 50;

   

    AUDIO_RXBuf[AudioRX_Ptr++] = SOUND_RXDATA_REG;

   

}

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

You could also have considered a LUT and a 50 mS clock as one of the LUT

   

qualifiers to yield a more HW aoproach. Just a thought.

   

 

   

Also when you do interrupts generally do most of the work outside interrupt

   

by setting a flag and exiting in ISR. Additionally declare globals used inside

   

ISR as volatile.

   

 

   

    

   

          http://www.barrgroup.com/Embedded-Systems/How-To/C-Volatile-Keyword

   

 

   

Regards, Dana.

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 Thanks for the advice Dana,

   

I went to the link and ended up purchasing the C Coding Standard book.

   

What is LUT?

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Look Up Table, can be registered (feedback) or non registered.

   

Basically create a state machine or simple combinatorial logic.

   

 

   

    

   

          http://www.cypress.com/?rid=46472

   

 

   

Attached a good C book.

   

 

   

Regards, Dana.

0 Likes