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

kbhit() Problem

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



Joined: 25 Feb 2013
Posts: 6

View user's profile Send private message

kbhit() Problem
PostPosted: Sat Mar 02, 2013 7:43 pm     Reply with quote

Hi, I have a gps and two PIC 16F877.
The GPS sends the data infinitely to pic # 2 and I want to take the data to send to pic 1.
This is the code:
Code:

   while (1)
    {
       if (kbhit ())
       {
         valor_ready = getc ();
 
       if ((cont_ready == 18) && (valor_ready == ','))
          {
             if (input_state (pin_a3) == 0)
                {
                 output_d (valor_ready);
                 output_bit (pin_a3, 1);
                 permiso1 = 0;
                }
             if (input (pin_a4) == 1)
               output_bit (pin_a3, 0);
           
           }
       cont_ready + +;
       }
    }
}

The problem I have is entering the if (kbhit ()) only 3 times and the program stops.
The GPS data is sending endlessly, but does not take the data.

Please help!
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Sun Mar 03, 2013 2:44 am     Reply with quote

Nothing ever resets cont_ready. Once it passes 18 nothing more will happen.
Remember also the entire operation will depend on what actual signal levels the pins go to. If the load on A3 is too much for it to actually be seen as a logic 0, the input_state test won't go true, etc. etc..

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Mar 03, 2013 5:45 am     Reply with quote

The sample code is not complete so it is hard to tell, but there might be another problem too: make sure you have added 'ERRORS' to the '#USE RS232' line.
Without the ERRORS keyword you are responsible for clearing buffer overrun errors in the hardware UART. 99% of the programmers want the compiler to handle this, so always add ERRORS unless you have a very good reason to leave it out.
On buffer overflow the UART will stop receiving after 2 (or 3?) characters which sounds similar to your problem.
leojma



Joined: 25 Feb 2013
Posts: 6

View user's profile Send private message

PostPosted: Sun Mar 03, 2013 1:24 pm     Reply with quote

ok, fix the code, and discovered that if inside the if (kgbit ()) write any line containing the variable to get the gps (valor_ready) the program does not work, just go in if (kgbit ()) 2 or 3 times. why this happens? if I put a: printf ("% u", variable_ready) or if (variable_ready == 1) the program crashes and does not continue and if not put "variable_ready" works!

Code:
 #include <16f877a.h>
#use delay (clock = 4 M)  //Reloj de 4 Megahertz
#include <LCD.h>
#include <stdlib.h> 
#use RS232 (baud=9600, bits =8, parity= N, xmit= pin_c6, rcv=pin_c7)
#fuses HS,NOWDT,NOPUT,NOPROTECT

int8 ceros=0;
int cont_ready=0;
char valor_ready;

void Gps_ready()
{

   while(1)
   {       
      if(kbhit())
      {
        valor_ready=fgetc();       
       
        if ((cont_ready==18)) //the condition should be: if ((cont_ready==18)&&(valor_ready==','))
        {
            if (input_state(pin_a3) == 0)
               {
                output_d(ceros);             
                output_bit(pin_a3,1);       
               }
            if (input(pin_a4)==1)
              output_bit(pin_a3,0); 
           
           cont_ready=-1;
         
          printf("valor: %u",valor_ready);//this does not work
          }
      cont_ready++;
      }     
   }
}


void main()
{     
  set_tris_a(0x15);
  set_tris_e(0);
  set_tris_b(0);
  set_tris_d(0);
  set_tris_c(0xbf);
 
  output_bit(pin_a3,0);
 
Gps_ready();
}
leojma



Joined: 25 Feb 2013
Posts: 6

View user's profile Send private message

PostPosted: Sun Mar 03, 2013 1:33 pm     Reply with quote

Ttelmah wrote:
Nothing ever resets cont_ready. Once it passes 18 nothing more will happen.
Remember also the entire operation will depend on what actual signal levels the pins go to. If the load on A3 is too much for it to actually be seen as a logic 0, the input_state test won't go true, etc. etc..

Best Wishes


Ok, fix the code, and discovered that if inside the if (kbhit ()) write any line containing the variable to get the gps (valor_ready) the program does not work, just go in if (kbhit ()) 2 or 3 times. Why this happens? If I put a: printf ("% u", variable_ready) or if (variable_ready == 1) the program crashes and does not continue and if not put "variable_ready" works!

Code:

 #include <16f877a.h>
#use delay (clock = 4 M)  //Reloj de 4 Megahertz
#include <LCD.h>
#include <stdlib.h> 
#use RS232 (baud=9600, bits =8, parity= N, xmit= pin_c6, rcv=pin_c7)
#fuses HS,NOWDT,NOPUT,NOPROTECT

int8 ceros=0;
int cont_ready=0;
char valor_ready;

void Gps_ready()
{

   while(1)
   {       
      if(kbhit())
      {
        valor_ready=fgetc();       
       
        if ((cont_ready==18)) //the condition should be: if ((cont_ready==18)&&(valor_ready==','))
        {
            if (input_state(pin_a3) == 0)
               {
                output_d(ceros);             
                output_bit(pin_a3,1);       
               }
            if (input(pin_a4)==1)
              output_bit(pin_a3,0); 
           
           cont_ready=-1;
         
          printf("valor: %u",valor_ready);//this does not work
          }
      cont_ready++;
      }     
   }
}


void main()
{     
  set_tris_a(0x15);
  set_tris_e(0);
  set_tris_b(0);
  set_tris_d(0);
  set_tris_c(0xbf);
 
  output_bit(pin_a3,0);
 
Gps_ready();
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Sun Mar 03, 2013 1:44 pm     Reply with quote

You haven't added ERRORS.
Your print will take up to about 10mSec to execute. All this time your loop will not be reading the serial port, so the hardware buffer (less than 2 characters), _will_ overflow. This will then hang the UART...

Add ERRORS, and add the serial buffer code from ex_sisr. Call bkbhit, instead of kbhit, and bgetc, instead of getc. This way the serial will be handled while you are printing, and the UART won't get hung.

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