Need help with my program

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
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(clock20%000000)

#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[i];
        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()

Re: Need help with my program
on comp.lang.c:
Quoted text here. Click to load it


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)

--
/-- Joona Palaste ( snipped-for-privacy@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
We've slightly trimmed the long signature. Click to see the full one.
Re: Need help with my program

Quoted text here. Click to load it

    <snip>
Quoted text here. Click to load it

    <snip>

Quoted text here. Click to load it

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.



Re: Need help with my program


Quoted text here. Click to load it

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

    Ed.


Re: Need help with my program
On Thu, 16 Oct 2003 15:20:52 -0500, Ed Morton


Quoted text here. Click to load it
 
Quoted text here. Click to load it
 
Quoted text here. Click to load it
 
Quoted text here. Click to load it

 
Quoted text here. Click to load it
 
Quoted text here. Click to load it



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









--
Peter Bennett VE7CEI        
GPS and NMEA info and programs: http://vancouver-webpages.com/peter/index.html
We've slightly trimmed the long signature. Click to see the full one.
Re: Need help with my program
   I'm taking out the crosspost to comp.lang.c -- I've never even READ
that group -- from what I've heard about it, I don't even want to take
the chance...

In comp.lang.c,comp.arch.embedded, Ed Morton

Quoted text here. Click to load it

   I don't trust compilers to initialize variables, even if they all
do. This code snippet is one good reason not to rely on the "but it's
initialized!" feature.
   The variable y gets initialized to the wrong value and the program
still won't work. Variable y should be explicitely initialized to 1
for the program [or at least this code snippet] to work properly. If
it's 0 (as the compiler sets it), it will stay at 0 even with the y
=<< 1 correction.

Quoted text here. Click to load it


Re: Need help with my program
Quoted text here. Click to load it

    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.

--
snipped-for-privacy@sun.com

Re: Need help with my program
Quoted text here. Click to load it
<snip>

Quoted text here. Click to load it

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.

--
Scott
Validated Software Corp.



Site Timeline