Need help with my program

D

DarkKobold

I am writting a keyboard emulator for the pic microcontroller.
Unfortunately, after watching it on the oscilliscope, it is not
transmitting the right data. The clock signal on B1 is working correctly,
however, the actual data part is not. I've included this short snipet,
and the entire program, so anyone can peruse my work. Thanks for any and
all help.

for (i = 0; i<= 7; i++)
{
if ((buffer[curr] & y) == y)
// ^^ this line is the main problem I think.
{
parity = not(parity);
output_high(PIN_B0);
}
else
{
output_low(PIN_B0);
}
delay_us(24);
output_low(PIN_B1);
delay_us(40);
output_high(PIN_B1);
delay_us(15);
y << 1;
}


Kyle


#include <16F874.H>

#use delay(clock=20000000)

#define ALLIN 0xFF
#define LiftC 0xF0
#define DirKey 0xE0
#define KeyTm 0x03
//ENTER UP LEFT DOWN RIGHT Q W 1
const char TBLP1[] = {0x5A, 0x75, 0x6B, 0x72, 0x74, 0x15, 0x1D, 0x16};

//5 A S D F G H 2
const char TBLP2[] = {0x36, 0x1C, 0x1B, 0x23, 0x2B, 0x34, 0x33, 0x1E};


int x;
int8 y;
int8 i;
int8 buffer[25];
int1 parity;
int8 buffsize;
int8 curr;
int8 next;
int8 load;
int8 P1;
int8 P2;
int8 P1Stat;
int8 P2Stat;
int1 Shift1;
int1 Shift2;

#byte PORTC = 6
#byte PORTD = 8


void buff_load()
{
buffsize++;
buffer[next] = load;
next++;
if (next == 25) next = 0;
}


//CLOCK on PIN B1
//DATA on PIN B0
void main()
{
curr = 0;
next = 0;
P1 = 0;
P2 = 0;
Shift1 = 0;
Shift2 = 0;
x = 0;
while(1)
{
set_tris_b(ALLIN);
set)tris_c(ALLIN); //Start Keyboard
//Test Key Values
P1Stat = PORTC;
/* if (!Input(PIN_C0) != bit_test(P1, 1))
{

}*/
x = 2;
for(i = 1; i <=7; i++)
{
if((not(P1Stat) & x) != (P1 & x))
{
if (i < 5)
{
load = DirKey;
buff_load();
}
if (bit_test(P1,x))
{
bit_set(P1,x);
}
else
{
bit_clear(P1,x);
load = LiftC;
buff_load();
}
load = TBLP1;
buff_load();
}
x << 1;
}

/* if (x == 1000 && P1 != 1)
{
P1 = 1;
load = TBLP1[7];
buff_load();
}
x++;*/
output_low(PIN_A0);
if (buffsize > 0)
{
//Test if keyboard is ready to transmit
while( !INPUT(PIN_B0) || !INPUT(PIN_B1))
{

}

delay_us(40);
//BEGIN TRANSMISSION
output_high(PIN_A0);
delay_ms(500);
parity = TRUE;
set_tris_b(KeyTm);
output_low(pin_B0);
delay_us(15);
output_low(PIN_B1);
delay_us(40);
output_high(PIN_B1);
delay_us(15);
y = 1;
for (i = 0; i<= 7; i++)
{
if ((buffer[curr] & y) == y)
{
parity = not(parity);
output_high(PIN_B0);
}
else
{
output_low(PIN_B0);
}
delay_us(24);
output_low(PIN_B1);
delay_us(40);
output_high(PIN_B1);
delay_us(15);
y << 1;
}
if (parity)
{
output_high(PIN_B0);
}
else
{
output_low(PIN_B0);
}
delay_us(24);
output_low(PIN_B1);
delay_us(40);
output_high(PIN_B1);
delay_us(15);
output_high(PIN_B0);
delay_us(24);
output_low(PIN_B1);
delay_us(40);
output_high(PIN_B1);
buffsize = buffsize - 1;
if (curr != 25)
{
curr++;
}
else
{
curr = 0;
}
} //if (buffsize > 0)
} //while(1)
} //void main()
 
J

Joona I Palaste

DarkKobold <[email protected]> scribbled the following
I am writting a keyboard emulator for the pic microcontroller.
Unfortunately, after watching it on the oscilliscope, it is not
transmitting the right data. The clock signal on B1 is working correctly,
however, the actual data part is not. I've included this short snipet,
and the entire program, so anyone can peruse my work. Thanks for any and
all help.
for (i = 0; i<= 7; i++)
{
if ((buffer[curr] & y) == y)
// ^^ this line is the main problem I think.
{
parity = not(parity);
output_high(PIN_B0);
}
else
{
output_low(PIN_B0);
}
delay_us(24);
output_low(PIN_B1);
delay_us(40);
output_high(PIN_B1);
delay_us(15);
y << 1;

I suspect the culprit is this line. It is calculating y << 1, i.e.
y * 2, and then throwing the result away. y is left unchanged. I
suppose you want y <<= 1.

(snip loads and loads of off-topic code)
 
E

Ed Morton

DarkKobold <[email protected]> scribbled the following
on comp.lang.c:

for (i = 0; i<= 7; i++)
{
if ((buffer[curr] & y) == y)
// ^^ this line is the main problem I think.


Also, it appears that y is uninitialized when it is first used. It needs to
have a value before you use it the first time. You can't assume that it
starts at zero.

Yes you can, it's static. See http://www.eskimo.com/~scs/C-faq/q1.30.html.

Ed.
 
P

Peter Bennett

DarkKobold <[email protected]> scribbled the following
on comp.lang.c:
<snip>
for (i = 0; i<= 7; i++)
{
if ((buffer[curr] & y) == y)
// ^^ this line is the main problem I think.
<snip>
Also, it appears that y is uninitialized when it is first used. It needs to
have a value before you use it the first time. You can't assume that it
starts at zero.


However, you want y to start at 1, so you still need to initialize it
immediately before entering the for() loop.
 
E

Eric Sosman

Not said:
Joona I Palaste said:
DarkKobold <[email protected]> scribbled the following
on comp.lang.c:
for (i = 0; i<= 7; i++)
{
if ((buffer[curr] & y) == y)
// ^^ this line is the main problem I think.

Also, it appears that y is uninitialized when it is first used. It needs to
have a value before you use it the first time. You can't assume that it
starts at zero.

The initialization did not appear in the extract at
the top, but you'll find it right there in plain sight
if you read the "complete" code later in the message.
 
N

Not Really Me

Eric Sosman said:
Not Really Me wrote:
The initialization did not appear in the extract at
the top, but you'll find it right there in plain sight
if you read the "complete" code later in the message.

You got me there. I did not read the full code. I see enough awful code
all day long doing software testing for safety critical apps. I just
couldn't bring myself to continue. Sadly it looks like all too much of the
"production" code that customers submit for validation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top