Need help with my program

Discussion in 'C Programming' started by DarkKobold, Oct 16, 2003.

  1. DarkKobold

    DarkKobold Guest

    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()
     
    DarkKobold, Oct 16, 2003
    #1
    1. Advertising

  2. DarkKobold <> scribbled the following
    on comp.lang.c:
    > 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)

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "To err is human. To really louse things up takes a computer."
    - Anon
     
    Joona I Palaste, Oct 16, 2003
    #2
    1. Advertising

  3. "Joona I Palaste" <> wrote in message
    news:bmmn46$rj3$...
    > DarkKobold <> 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>

    > > y << 1;

    >


    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.
     
    Not Really Me, Oct 16, 2003
    #3
  4. DarkKobold

    Ed Morton Guest

    On 10/16/2003 3:18 PM, Not Really Me wrote:
    > "Joona I Palaste" <> wrote in message
    > news:bmmn46$rj3$...
    >
    >>DarkKobold <> 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>
    >
    >>> y << 1;

    >>

    >
    > 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.
     
    Ed Morton, Oct 16, 2003
    #4
  5. On Thu, 16 Oct 2003 15:20:52 -0500, Ed Morton
    <> wrote:


    >On 10/16/2003 3:18 PM, Not Really Me wrote:
    >> "Joona I Palaste" <> wrote in message
    >> news:bmmn46$rj3$...


    >>>DarkKobold <> 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>


    >>>> y << 1;


    >> 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.



    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
    Newsgroup new user info: http://vancouver-webpages.com/nnq
     
    Peter Bennett, Oct 16, 2003
    #5
  6. DarkKobold

    Eric Sosman Guest

    Not Really Me wrote:
    >
    > "Joona I Palaste" <> wrote in message
    > news:bmmn46$rj3$...
    > > DarkKobold <> 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>
    >
    > > > y << 1;

    > >

    >
    > 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.

    --
     
    Eric Sosman, Oct 16, 2003
    #6
  7. "Eric Sosman" <> wrote in message
    news:...
    > Not Really Me wrote:
    >

    <snip>

    > 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.

    --
    Scott
    Validated Software Corp.
     
    Not Really Me, Oct 17, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wrecker
    Replies:
    3
    Views:
    402
    Kevin Spencer
    Jun 21, 2005
  2. pipi
    Replies:
    1
    Views:
    638
    Joseph Millar
    Jul 21, 2003
  3. Replies:
    1
    Views:
    490
    Walter Roberson
    Aug 12, 2005
  4. Replies:
    66
    Views:
    1,193
    Kenny McCormack
    Jun 18, 2006
  5. Kris Stark
    Replies:
    1
    Views:
    114
    John Bokma
    Sep 8, 2005
Loading...

Share This Page