# PIC18F4580 - Keyboard start

#### frahman3

##### New Member
Hi,

I'm trying to start my program which is to ON/OFF LEDs when a character is pressed from the keyboard. In this case the character would be an 'a'. It is simple but the truth is I still could not figure it out what is wrong with the program. I found when I pressed any keys (including the 's') on the keyboard the TX
indicator (UIC008 - usb icsp pic programmer) is blinking which means the data is being transmitted but led connected at RB7 (led2) is still OFF and the program executes the code to ON/OFF the led connected to RB6(led1) & RB7(led2). Is there any file(s) which I need to include? I'm using CCS C compiler ver. 5.010. Please advice.

TQ

Regards
FR

#### frahman3

##### New Member
Sorry i forgot to upload the program code.

FR

CSS:
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600,UART)
#include <stdlib.h>
#include<string.h>

void main(void)
{

set_TRIS_B(0x0F); //PORT B as OUTPUT/INPUT
set_TRIS_C(0xFF); //PORT C as INPUT

while(true)
{
wait:
while(!kbhit())
if(getc()!='s')

{

output_b(10000000);
delay_ms(1000);
output_b(00000000);
delay_ms(1000);
goto wait;

}
output_b(00000000);
delay_ms(500);
output_b(11000000);
delay_ms(800);

}

}

#### Pommie

##### Well-Known Member
What is kbhit ()?

Mike.

#### frahman3

##### New Member
HI,

kbhit() determines if i the key has been pressed. If pressed, it will pass non-zero value.

FR

#### rjenkinsgb

##### Active Member
You do not have a CPU type include line?

If it's a DSPIC or PIC24 (PCD compiler), the output_x instruction is defined as needing a 16 bit value.

The output_bit() instruction may be more appropriate, that controls individual pins.
eg. to set an output high:
output_bit(PIN_B7, 1);

Have you tried setting a breakpoint after the if getc line, to see what is actually happening there?

I'd change to using a variable for the getc() result, then the IF on a separate line.
That allows you to see what was received by looking at the variable.

However --
Looking again, the programs structure seems odd...

The while statement needs something after it, another statement or {} block, which is executed while kbhit returns false.
At the moment that is the getc line...

Try something more like this:

C:
char c;

while(true)
{
wait:
while(!kbhit())
;

c = getc();

if(c !='s')

{
...

#### frahman3

##### New Member
Hi,

Finally with some help the code below meets the requirement.

Regards
FR

CSS:
void main(void)
{

set_TRIS_B(0x0F); //PORT B as OUTPUT/INPUT
set_TRIS_C(0xFF); //PORT C as INPUT

while(true)
{
wait:
if(!kbhit())
if(getc()!='s')

{

output_b(10000000);
delay_ms(1000);
output_b(00000000);
delay_ms(1000);
goto wait;

}
output_b(00000000);
delay_ms(500);
output_b(11000000);
delay_ms(800);

}

}