My Very first code

Discussion in 'Programmer's Corner' started by R!f@@, Feb 9, 2010.

  1. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    Hi there fellow members.Below is the first attempt to write a code alone and it did work but as usual it has bugs.
    This circuit used a 12F629, a standard rotary encoder is connected to two inputs that are held low by pull downs. Two output drives two LED.
    One indicates direction of rotation. (a lit LED for CW and an off LED for CCW rotation).
    The other one pulses as the shaft is rotated in either direction.
    When hard wired these are the results.

    At first I tested with CW rotation, first wrote up that part of code and tested and it works perfectly. the CCW rotation is not tested during this time.
    The CW is lit continuously and the other one pulses as the shaft is rotated.

    Now after writing the CCW part. the code works but now the CW also creates a bit of problem, the CW LED turns off sometimes and same thing happens during CCW and the pulse LED sometimes hardly pulses. :confused:

    Now my observation is contact bounce so I am trying to put some delays in between the direction instructions. But am not sure of how much a delay that I should give.

    Any comments are most welcome and of course if any of you can think of a better way will be most appreciated.

  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    I haven't go through your code yet, so i'll not comment on that.

    The encoder, is it an optical encoder? I think there would be no contact bounce on rotary encoder. It usually doesn't use mechanical switches to generate the pulses.
  3. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    That was my very first hiccup :p
    I assumed that even though it is a mechanical one which just scratches the surface there would be no the bouncing thingy problem.
    But if I carefully veryyyyyyyyyyy slowly rotate, it indicated some kind of bouncing, I guess when the leaf is dragged on at the very edge of the carbon, there is a bouncing at atomic level. :D... u know what I mean.

    WELL! NOT BOUNCING BUT DRAG BOUNCING.. umm some thing like that

    I still working on the delay routine so Ill comment later
  4. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    At a quick look through the code, everything seems to follow a specific program path for a given state on the encoder inputs.

    This will not give directional information, as the two output both cycle regardless of direction.

    Also, you are reading the actual inputs multiple times at different points in the program, so the inputs could change part way through.

    You need to read and store the inputs in ram at the start of a loop, then refer to the RAM image in the main code.

    At the end of the loop, copy the saved values to a 'last pass' store before reading the new set of values.

    By comparing the inputs between the newly fetched data and the last pass data, you can can see how the inputs have *changed*, and it is the sequence of changes that give the directional information.

    Typical encoder quadrature output sequence:

  5. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    Adding Delays to certain points fixed the problem.