A little bit of help with the lighthouse circuit.

I had an email from a friend who has built my lighthouse circuit but whose LEDs were lighting up in the wrong order.  This is exactly what happened to me when I built it the first time too.  But no worries!

The great thing about charlieplexing and this circuit is that it doesn’t matter which order you place the leds on your board as you can change the code to match the correct sequence for your final layout.  This means that you can layout your circuit for ease of wiring rather than having to keep the LEDs in any particular order.  As long as the circuit is correct and that you have an LED everywhere you should have one the ordering can be sorted out later.

Of course if you are just using my compiled HEX file and don’t want to touch the code then you need to be a bit more careful and place the LEDs correctly as the ordering code is fixed into the HEX and can’t be changed without a re-compile.

If you want to change the code the bit you need to edit is from line 41.

// pattern for all 12 LEDs in sequential order.
const char charlieBits[] = {
0b10000010
,    0b10100000
,    0b10000010
,    0b10000100
,    0b10000100
,    0b10100000
,    0b10000100
,    0b10000001
,    0b10000001
,    0b10100000
,    0b10000001
,    0b10000010
};

const char trisBits[] = {
0b10011101
,    0b10011101
,    0b10111001
,    0b10111001
,    0b10011011
,    0b10011011
,    0b10111010
,    0b10111010
,    0b10011110
,    0b10011110
,    0b10111100
,    0b10111100
};

These two arrays store the two status bytes of the PIC output pins (GPIO) for each LED in the sequence.  The first row or the array is for LED 1 the 2 is for LED 2 and so on.  We are using GPIO’s 0,1,2 and 5 for the LED connections so these are the only bits that are relevant in these two arrays, the other bits refer to the button input on GPIO4 and GPIO3 which is defined as the MCLR (reset), bits 6 and 7 are irrelevant.  So for each row only bits 0,1,2 and 5 should change. Eg. 0b--X--XXX.

There are two arrays, the first (charlieBits) is for the GPIO output hi/lo status, the second (trisBits) is for the GPIO TRIS status which sets if the pin is an input or an output.  By combining these two array we have three states for each GPIO pin. When TRIS is 0 the pin is an output and so is either hi or lo depending on the charlieBit status.  When TRIS bit is 1 then pin is set as an input and so the charlieBit doesn’t matter.  When the pin is an input it is effectively non existent as far as our LED’s are concerned.  In actual fact it’s still connected but with a high impedance that doesn’t affect our circuit.

So to match the arrays up to how you have connected you LEDs it’s a bit of trial and error.  When I did  it I started by resetting the circuit and noting which LED lit up first.  There is a test pattern that cycles through each LED individually before starting the main routine which gives you a nice change to note the order.  You can alter the delay used here to make it slower or faster by changing the value on lines 160 and 161.
delay_ms(200);
delay_s(1);

The first delay is in milliseconds the second is whole seconds so we have a total delay of 1.2 seconds here.  You could make that slower by making the delay_s(1) into a delay_s(2) then recompiling and uploading to the PIC.

By repeatedly resetting the PIC you will be able to see in which order the LEDs light up in the test phase.  Once you have them noted you can go back into the code and re-order the rows in the two arrays.  Watch the LED that you want to be the first one, eg the one at the 12’oclock position. And note in which order it lights up – just count them off. Eg. if your 1st physical LED is lighting up 5th then you would need to move the 5th row of the array to the top of the array, remember to do that for both arrays to keep them in sync.  Re-compile, re-upload and check that the first physical LED is lighting up first.  Now do same for the 2nd physical LED (2o’clock).  You could try to do them all in one go but I found it easier to re-compile and upload after each change to check I was on the right track.

Sounds a bit tedious, but it’s not too bad really.
Hope that helps to get things up and running smoothly.

Advertisements

One thought on “A little bit of help with the lighthouse circuit.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s