UART + TIMER

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

UART + TIMER

Postby khairul » Tue May 14, 2013 2:18 am

Hai, i re-post this problem... i have a problem regarding with UART and TIMER, i have a code to transmit a serial data to a module and wait for the module to return a packet of data back if any, else leave the task..

My problem is the coding just wait forever until the packet of data received to the pic due to this coding line

CODE: SELECT_ALL_CODE
while(PIR1bits.RCIF==0); //wait for data to received


and i just want it to wait for a while, if there is no data then leave from the task.

So my solution (if it is possible) is to ON a TIMER to wait for a while and AT THE SAME TIME waiting for the packet of data to be received. If the timer is off, just jump back to the main.

So it is possible to create code like that? or there are other solution that im not aware of.
khairul
Novice
 
Posts: 17
Joined: Wed Feb 02, 2011 2:07 pm

Re: UART + TIMER

Postby shahrul » Tue May 14, 2013 7:55 am

khairul WROTE:Hai, i re-post this problem... i have a problem regarding with UART and TIMER, i have a code to transmit a serial data to a module and wait for the module to return a packet of data back if any, else leave the task..

My problem is the coding just wait forever until the packet of data received to the pic due to this coding line

CODE: SELECT_ALL_CODE
while(PIR1bits.RCIF==0); //wait for data to received


and i just want it to wait for a while, if there is no data then leave from the task.

So my solution (if it is possible) is to ON a TIMER to wait for a while and AT THE SAME TIME waiting for the packet of data to be received. If the timer is off, just jump back to the main.

So it is possible to create code like that? or there are other solution that im not aware of.

In the PIC there have a lot of Interrupt. Some of that UART Interrupt and Timer Interrupt.
So, this case no need Timer, just use UART Interrupt.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: UART + TIMER

Postby khairul » Tue May 14, 2013 8:32 am

i have read the datasheet, RCIF is a interupt flag that set when received buffer is full. so in my coding below,

CODE: SELECT_ALL_CODE
while(PIR1bits.RCIF==0); //wait for data to received


the "while" will wait until the received buffer is full, but i don't want it to wait forever because i know that sometime the module will not give data back although we have send some data to it.

it is exist an interrupt that trigger when there are no data received at the RX?


bit 5 RCIF: EUSART Receive Interrupt Flag bit
1 = The EUSART receive buffer, RCREG, is full (cleared when RCREG is read)
0 = The EUSART receive buffer is empty
khairul
Novice
 
Posts: 17
Joined: Wed Feb 02, 2011 2:07 pm

Re: UART + TIMER

Postby shahrul » Tue May 14, 2013 8:42 am

khairul WROTE:oh, but how do i get out from the code

CODE: SELECT_ALL_CODE
while(PIR1bits.RCIF==0); //wait for data to received


it is exist an interrupt that trigger when there are no data received at the RX?

See this example http://forum.cytron.com.my/viewtopic.php?f=21&t=12129#p22954
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 31 guests