In my project, I want to do a work for 'n' sec. I am using MikroC and PIC16F877.
For example,
Code C - [expand]
1
UART1_Write_Text('String')
After executing this, I want to wait up to 'n' sec. If there no reply even after 'n' sec, function Timeout() will execute. Please help me by giving code for the same.
Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// this loop waits for the answerdo{if(UART1_Data_Ready()){
response[x]= UART1_Read();
x++;// check if the desired answer is in the response of the moduleif(strstr(response, expected_answer1)!= NULL){
answer =1;}}// Waits for the asnwer with time out}while("not timeout");
//...inside function
while(1)
{
while(!UART1_Data_Ready()) //while no serial data is receive
{
DelayMs(1); //... wait until timeout
timer++;
if(timer > time_out)
{
return ERR_TIMEOUT;
}
}
response[x] = UART1_Read(); // read serial data
x++;
//More codes here
// to process byte
break; //Exit from function
}
return OK;
then call the function like this:
Code:
result = GetSerialByte(2000); //Get byte, wait for 2 sec
if(result == ERR_TIMEOUT)
{
printf("Your dead");
}