CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

timeout help

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
desertkids



Joined: 30 Nov 2008
Posts: 8

View user's profile Send private message

timeout help
PostPosted: Sun Dec 14, 2008 10:34 am     Reply with quote

why my timeout not function?can help me check my code?i need 2 timeout in my program, the first if not receive char's' in 1 minute then start the program. the second is when cant get the gps signal in 1seconds then jump out from the loop. i use PCH compiler v4.032.
Code:



#define (__PCH__)

//==================include=================================

#include <18F4550.h>

#fuses HS,WDT,NOPROTECT,NOLVP
#device ADC=10                  // define 10 bit resolution
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, parity=n, bits=8, BRGH1OK, stream=UART)
#use rs232(baud=4800, xmit=PIN_D0, rcv=PIN_D1, parity=n, bits=8, BRGH1OK, stream=GPS)

#include <stdlib.h>
#include <input.c>
 
//===============define IO port=============================

#define  sw1      pin_b0

#define temp1_ch 0
#define temp2_ch 1
#define press_ch 4
#define acc_x_ch 5
#define acc_y_ch 6
#define acc_z_ch 7


//=====================variable==========================
int temp_1[40],temp_2[40];
float press[40],acc_x[40],acc_y[40],acc_z[40];
float volt_temp_1,volt_temp_2,volt_press,volt_acc_x,volt_acc_y,volt_acc_z;
int n=0,i=0,k=0;
long timeout;

int1 good_fix;
int len;
char gps_str[60],temp_str[8];
int8 count=0,rcv_chr;
float alt_float[40],lat_float[40],log_float[40];
float utc_float;
unsigned int sat=0;
short timeout_error;
char length[120];
char f1[10]= "*DR=03s/R";
char str_in[10];
int ini=24,wait=26,sta=7,sen=14,c=0,com1_result;

//==============FUNCTION PTOTOTYPE=========================

float analog_read(int channel);
void get_fix();

//=================MAIN PROGRAM================================
void main()
{
      set_tris_a(0xff);                // Let all port A analog channel become input
     
      setup_adc_ports(AN0_TO_AN7);     // All port A is used as analog
      setup_adc(ADC_CLOCK_DIV_64);     // Fosc/Fsam => 20000000/625 000 = 32  => 32
      delay_ms(10);     
      fprintf(UART,"@DT%Xinitialization done.../R\r\n",ini);
      fprintf(UART,"@DT%Xwait for start signal.../R\r\n",wait);//wait for start signal,char 's'
      delay_ms(10);

      timeout=0;
     
      do{
      rcv_chr=fgetc(UART);
      }while(rcv_chr!='s'&&(++timeout<10000));
     
      fprintf(UART,"@DT%Xstart/R\r\n",sta);
      delay_ms(500);
     
   for(n=0;n<40;n++)
   {   
      i++;
      volt_temp_1= analog_read(temp1_ch);
      temp_1[n]=((float)volt_temp_1-2.73)*100;
     
      volt_temp_2= analog_read(temp2_ch);
      temp_2[n]=((float)volt_temp_2-2.73)*100;
     
      volt_press=analog_read(press_ch);
      press[n]=(((float)volt_press-0.204)*21.7865)+15;
             
      volt_acc_x= analog_read(acc_x_ch);
      acc_x[n]=(((float)volt_acc_x-1.5)/0.3);
     
      volt_acc_y= analog_read(acc_y_ch);
      acc_y[n]=(((float)volt_acc_y-1.5)/0.3);
     
      volt_acc_z= analog_read(acc_z_ch);
      acc_z[n]=(((float)volt_acc_z-1.5)/0.3);
     
      good_fix=false;
     
      get_fix();
     
      sprintf(length,",%2d,%1.2f,%1.1f,%1.2f,%1.1f,%1.2f,%1.1f,%1.3f,%3.2f,%1.2f,%d,%1.2f,%d,%6.0f,%4.2f,%5.2f,%3.1f,%2d/R",i,volt_acc_x,acc_x[n],volt_acc_y,acc_y[n],volt_acc_z,acc_z[n],volt_press,press[n],volt_temp_1,temp_1[n],volt_temp_2,temp_2[n],utc_float,lat_float[n],log_float[n],alt_float[n],sat);
      len = strlen(length);   
      fprintf(UART,"@DT%X,%2d,%1.2f,%1.1f,%1.2f,%1.1f,%1.2f,%1.1f,%1.3f,%3.2f,%1.2f,%d,%1.2f,%d,%6.0f,%4.2f,%5.2f,%3.1f,%2d/R\r\n",len,i,volt_acc_x,acc_x[n],volt_acc_y,acc_y[n],volt_acc_z,acc_z[n],volt_press,press[n],volt_temp_1,temp_1[n],volt_temp_2,temp_2[n],utc_float,lat_float[n],log_float[n],alt_float[n],sat);
      delay_ms(1500);
   }
   

   fprintf(UART,"@DT%Xwait for 'r'/R\r\n",sen);
 
      do {
      rcv_chr=fgetc(UART);
      }while(rcv_chr!='r');
     
   for(n=0;n<40;n++)
   { 
   k++;
   fprintf(UART,"\r\nN=%d,TEMP_1=%d,TEMP_2=%d,PRESS=%3.2f,ACC_X=%1.1f,ACC_Y=%1.1f,ACC_Z=%1.1f,LAT=%4.2f,LONG=%5.2f,ALT=%3.1f",k,temp_1[n],temp_2[n],press[n],acc_x[n],acc_y[n],acc_z[n],lat_float[n],log_float[n],alt_float[n]);
   delay_ms(10);   
   }

   fprintf(UART,"\r\nFINISH ALL!!!");
   delay_ms(10);
   }

//===============SUBROUTINE ADC=====================
float analog_read(int channel){
     
      float tmp = 0;
      float tmp_adc = 0;
      int l=0;

      set_adc_channel(channel);
      delay_ms(10);
     
      for(l=0;l<10;l++)
      {
      tmp_adc = read_adc();
      tmp+=tmp_adc;
      }

      return (float)tmp/2046;
}

 
//===================SUBROUTINE GPS=============================

void get_fix(){
timeout=0;
while (!good_fix&&(++timeout<10000)){
   rcv_chr=fgetc(GPS);
   if (rcv_chr=='$'){
      rcv_chr=fgetc(GPS);
      if (rcv_chr=='G'){
         rcv_chr=fgetc(GPS);
         if (rcv_chr=='P'){
            rcv_chr=fgetc(GPS);
            if (rcv_chr=='G'){
               rcv_chr=fgetc(GPS);
               if (rcv_chr=='G'){
                  rcv_chr=fgetc(GPS);
                  if (rcv_chr=='A'){
                     rcv_chr=fgetc(GPS);
                        if (rcv_chr==','){                           
                           for (count=0;count<60;count++){
                              gps_str[count]=fgetc(GPS);
                              }
                                                           
                           for (count=0;count<6;count++){
                              temp_str[count]=gps_str[count];
                              }
                              utc_float=atof(temp_str);
                             
                             
                           for (count=0;count<7;count++){
                              temp_str[count]=gps_str[7+count];
                              }
                              lat_float[n]=atof(temp_str);

                           for (count=0;count<8;count++){
                              temp_str[count]=gps_str[19+count];
                              }
                              log_float[n]=atof(temp_str);
                             
                           for (count=0;count<3;count++){
                              temp_str[count]=gps_str[34+count];
                              }
                              sat=atoi(temp_str);
                             
                           for (count=0;count<7;count++){
                              temp_str[count]=gps_str[42+count];
                              }
                              alt_float[n]=atof(temp_str);
                             
                           good_fix=true;
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }

Ttelmah
Guest







PostPosted: Sun Dec 14, 2008 11:11 am     Reply with quote

Your code, calls 'fgetc'. This will _sit and wait_ for a character. You need to test if a character is available, _before_ calling this. So (for example), your first timeout loop, would need:
Code:

      timeout=0;
      rcv_chr = '\0'; //Make sure the character does _not_ contain
      //a legitimate character when you enter the loop
      do {
          if (kbhit(UART)) rcv_chr=fgetc(UART); //Only get a char if
          //one _is_ available
      }while(rcv_chr!='s'&&(++timeout<10000));

This way you keep looping if a character is not available, and your timeout will count down. You will ned to adjust the timeout value to give the required delay.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group