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 calls itself?

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



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

kbhit calls itself?
PostPosted: Mon Jun 12, 2017 9:05 am     Reply with quote

Hello everyone

I am trying to write a communication code using kbhit function.

First i tried this code
Code:

#include <18F2550.h>
#DEVICE ADC=10
#fuses INTRC_IO,NOWDT,NOMCLR,NOPROTECT,NOLVP,NODEBUG,NOBROWNOUT,CPUDIV1,VREGEN
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_A4,rcv = PIN_A5,stream = bt)

int command_array[20] = {};
byte baglantikuruldu = 0;

void komutal();
void baglan();
void main()
{
delay_ms(2000);
   while(true)
   {
   delay_ms(1000); 
   baglan();
   komutal();
   }
}

void baglan()
{
int basla = 0;
while(!baglantikuruldu)
{
   while(!kbhit(bt))
   delay_us(10);
   
   if(kbhit(bt))
   {
      basla = getc(bt);
      if(basla == 'o')
      {
         delay_ms(100);
         printf("k");   
         baglantikuruldu = 1;
      }
      else
         printf("!");
   }
}
}

void komutal()
{
int i = 0;

   while(!kbhit(bt))
   delay_us(10);
   
   
   if(kbhit(bt))
   {
      command_array[0] = getc(bt);
      printf("%d",command_array[0]);
   }
   if (command_array[0] == 'b')  //if begin id is correct, get commands on array
   {   
      for(i=1;i<11;i++)
      {
         command_array[i] = getc(bt);
         printf("%d\n\r",command_array[i]);
         if(command_array[i] == 'f')  //if one of the command is 'F', wait for finish id
         {
            break;
         }
         
      }
   printf("bitti");
   command_array[i+1] = getc();
   printf("%d",command_array[i+1]);
      if(command_array[i+1] != 'p') 
      {
         delay_ms(100);
         printf("NO FINISH ID");
      }
      else
      {
         delay_ms(100);
         printf("READY!");
      }
   }
   else
   {
      printf("NO START ID");
   }
}

This works succesfully.

I copied the code another source file and started to add some pieces. After a that i saw an error and deleted the other parts.

The last version of my code is below. The interesting part is, the code gets out automatically in second if.

I press 'o' and get the 'k'.

If i press other than 'b' (for example 'n') it still goes into the second if. I see "the command is 110", "you are in of first if" and "you are out of first if" on the screen.

If i press 'b' i see "the command is :98", "you are in of first if", "you are out of first if" and "you are out of second if".

After that i have getc function but it returns to first if again. Whatever i press, i see the "you are in first if" "you are out of first if".

So i assume kbhit interrupt calls itself. I am not sure especially after seeing the first code works. Does kbhit() call itself since it is an interrupt? If so how do i get different inputs on different kbhits ?

Code:

#include <18F2550.h>
#DEVICE ADC=10
#fuses INTRC_IO,NOWDT,NOMCLR,NOPROTECT,NOLVP,NODEBUG,NOBROWNOUT,CPUDIV1,VREGEN
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_B1,stream = motormodul)
#use rs232(baud=9600, xmit=PIN_A4,rcv = PIN_A5,stream = bt)

int command_array[20] = {};
byte baglantikuruldu = 0;

void baglan();
void komutal();
void komutgonder();
void command_list(int sayi);

void uyan(int time3);
void acilis();


void main()
{
   setup_timer_0(T0_INTERNAL | T0_DIV_16 | T0_8_BIT); 
   
//!   acilis();   
   baglan();
   while(true)
   {
      komutal();
      delay_ms(10);
   }
   
}


//!void acilis()
//!{
//!   uyan(100);
//!   gozkirp(25);
//!   notacal(dizi2,sizeof(dizi2),bosluk2,notauzunluk2);     
//!}
//!
//!void uyan(int time3)
//!{
//!   enable_interrupts(INT_TIMER0);
//!   enable_interrupts(GLOBAL);
//!   int i = 0;
//!   widths.R = 0;
//!   widths.G = 0;
//!   widths.B = 0;
//!   widths.R2 = 0;
//!   widths.G2 = 0;
//!   widths.B2 = 0;
//!   for(i=0;i<LOOPCNT;i++)
//!      {
//!         widths.R++;
//!         widths.R2++;
//!         widths.B++;
//!         widths.B2++;
//!         widths.G++;
//!         widths.G2++;
//!         delay_ms(time3);
//!      }
//!   delay_ms(50);
//!   disable_interrupts(INT_TIMER0);
//!   disable_interrupts(GLOBAL);   
//!}

void baglan()
{
int basla = 0;
while(!baglantikuruldu)
{
   while(!kbhit(bt))
   delay_us(10);
   
   if(kbhit(bt))
   {
      basla = getc(bt);
      if(basla == 'o')
      {
         delay_ms(100);
         printf("k");   
         baglantikuruldu = 1;
      }
      else
         printf("!");
   }
}
}

void komutal()
{
int i = 0;

   while(!kbhit(bt))
   delay_us(10);
     
   if(kbhit(bt))
   {
      command_array[0] = getc(bt);
      printf("the command is:%d\n\r",command_array[0]);
      printf("you are in first if\n\r");
   }
   printf("you are out of first if\n\r");
   if(command_array[0] == 'b') 
   {
   printf("you are in second if\n\r");
       
//!      for(i=1;i<11;i++)
//!      {
//!         command_array[i] = getc(bt);
//!         printf("%d\n\r",command_array[i]);
//!         if(command_array[i] == 'f')  //if one of the command is 'F', wait for finish id
//!         {
//!            break;
//!         }
//!         
//!      }
//!   printf("bitti");
      command_array[i+1] = getc();     
      printf("you are still in second if\n\r");
      if(command_array[i+1] != 'p') 
      {
         delay_ms(10);
         printf("NO FINISH ID");
      }
      else
      {
         delay_ms(10);
         printf("READY!");
      }
   }
   else
   {
      printf("NO START ID");
   }
}


Thank you so much!
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Jun 12, 2017 9:54 am     Reply with quote

Kbhit() doesn't call itself, it simply checks for a character, and is not an "interrupt". There are no interrupts in your code.

Think about what happens in your code. Work through it line by line.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Jun 12, 2017 10:19 am     Reply with quote

Code:
command_array[i] = getc(bt);


your code makes it quite easy for this line to execute when
kbit() is FALSE

you might have a close look at the .LST code for getc()
to see further implications to your time domain performance.
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Mon Jun 12, 2017 10:51 am     Reply with quote

Thank you both for your answers. Asmboy, i did not understand your answer because the line you showed me is commented. RF_Developer, i did what you said, in order to understand where i am, i wrote the lines that says in which part of function i am.

So one more time... with feeling and line by line.

I am skipping the baglan() function it works fine.

Code:
void komutal()
{
int i = 0;

   while(!kbhit(bt))
   delay_us(10); //wait until kbhit



Code:
if(kbhit(bt))
   {
      command_array[0] = getc(bt);
      printf("the command is:%d\n\r",command_array[0]);
      printf("you are in first if\n\r");
   }
printf("you are out of first if\n\r");  //i am out of first if


I have kbhit. I get value and put it on array[0]. For example 'n' is my value.
I print the value and info of that i am in second if, even though i should not be, because my value is not 'b'.



If my value is 'b' i have to get into this if

Code:
if(command_array[0] == 'b')
   {
      printf("you are in second if\n\r");
      command_array[i+1] = getc();   
      printf("you are still in second if\n\r");
      if(command_array[i+1] != 'p') 
      {
         delay_ms(10);
         printf("NO FINISH ID");
      }
      else
      {
         delay_ms(10);
         printf("READY!");
      }
   }

otherwise this one

Code:

   else
   {
      printf("NO START ID");
   }
}


I can't get to the else either. There is no "NO START ID" and yet i still can't see why the code goes in the second if.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 12, 2017 11:36 am     Reply with quote

#use rs232(baud=9600, xmit=PIN_A4,rcv = PIN_A5,stream = bt)
You are using a software UART. A software UART, in the way that CCS
implements it, requires special care when writing receiver code.

1. My advice, at your stage of programmer knowledge, you should only
use a hardware UART. This is on pins C6 and C7 in your PIC.
You can configure it like this:
Code:
#use rs232(baud=9600, UART1, ERRORS)

You can add a stream if you want to.

2. I also suggest that you always use an #int_rda software receive fifo
with the hardware UART. Use it with a large buffer, at least 32 or 64
bytes in size. The CCS file ex_sisr.c shows how to do this. Instead
of using kbhit() and getc(), you use bkbhit() and bgetc(). If you use
this receive fifo, you have more time available to get the received bytes.
Your main() program can do other things for many milliseconds and then
get a byte from the buffer.

I think if you do these two things, most of your problems will disappear.
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Tue Jun 13, 2017 3:03 am     Reply with quote

PCM programmer wrote:
#use rs232(baud=9600, xmit=PIN_A4,rcv = PIN_A5,stream = bt)
You are using a software UART. A software UART, in the way that CCS
implements it, requires special care when writing receiver code.

1. My advice, at your stage of programmer knowledge, you should only
use a hardware UART. This is on pins C6 and C7 in your PIC.
You can configure it like this:
Code:
#use rs232(baud=9600, UART1, ERRORS)

You can add a stream if you want to.

2. I also suggest that you always use an #int_rda software receive fifo
with the hardware UART. Use it with a large buffer, at least 32 or 64
bytes in size. The CCS file ex_sisr.c shows how to do this. Instead
of using kbhit() and getc(), you use bkbhit() and bgetc(). If you use
this receive fifo, you have more time available to get the received bytes.
Your main() program can do other things for many milliseconds and then
get a byte from the buffer.

I think if you do these two things, most of your problems will disappear.


Hello PCM Programmer

Thank you for your reply.

Your suggestion about use hardware UART, i will do that. I need stream because the project needs to get data from pc and send it to a module also.

About using buffer. I should research it and get more info before trying it because i have no idea about it.

Thank you again.

Best Wishes

Doguhan
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