What you will need:
PIC18f4685 microcontroller
8x8 LED matrix
8 x 150 ohm resistors
1 x 10k ohm resistor
This is a very simple circuit to make. You just need to connect all the common anodes of the matrix to PORTB of the microcontroller and then all of the common cathodes (through the 150 ohm resistors) to PORTD of the microcontroller. Then the 10k ohm resistor connects between mclr and vcc (to make sure that we are not resetting the microcontroller.
Now here's the code:
Code: Select all
Device = 18F4685 // Tell the compiler what chip we are using
Clock = 8 // Tell the compiler what we will be setting the clock to (Mhz)
Config OSC = IRCIO67 // This tells the microcontroller to use the internal clock
Include "Utils.bas" // Include this file when we compile so that we can use keywords like 'setalldigital'
// Arrays
Const face_data(8) As Byte = (%00111100,%01000010,%10101001,%10000101,%10000101,%10101001,%01000010,%00111100)
Const cathodes(8) As Byte = (%11111110,%11111101,%11111011,%11110111,%11101111,%11011111,%10111111,%01111111)
// variable declaration
Dim x As Byte
// Sub Routines
Sub draw_face()
For x = 0 To 7
PORTD = cathodes(x)
PORTB = face_data(x)
DelayMS(1)
PORTB = %00000000 // I put this here to make sure that we don't get ghosting when moving to the next column
Next
End Sub
// Start Of Program
OSCCON = %01111111 // Sets the internal oscillator for 8Mhz
SetAllDigital // Make all Pins digital I/O's
TRISD = %00000000 // Make PORTD all outputs
TRISB = %00000000 // Make PORTB all outputs
// Main Loop
While True() // This creates an infinite loop
draw_face
Wend // Loop back to the while loop as long as we havent finished.
In this case my main code starts where it says:
Code: Select all
// start of program
If you look up the top of the code, I declare two arrays, each containing eight bytes. An array is great for when dealing with graphics because all of your graphic data can be defined (and even manipulated) quite easily. In this case my two arrays contain my face graphic data and also the data to activate one column of cathodes at a time.
After that, I have just one variable 'x' which I use to cycle through the data in the arrays and also to remain in a loop for a certain amount of time (in this case it will run the loop eight times I.E. from 0 to 7
The next part of the code is a sub routine, here it is again:
Code: Select all
Sub draw_face()
For x = 0 To 7
PORTD = cathodes(x)
PORTB = face_data(x)
DelayMS(1)
PORTB = %00000000 // I put this here to make sure that we don't get ghosting when moving to the next column
Next
End Sub
We run whats called a FOR NEXT loop. the for next loop will remain in the loop while until x = 7. when x does equal 7, it will run the loop statements one last time and will then exit the loop.
So, when we say for x = 0 to 7, it means that we first load the variable x with 0, we then run the code underneath it. We want to send the first byte of data to the LED cathodes (which is PORTD) This is also where the variable x comes in handy because we use this variable to point to the first byte in the array (x = 0 so therefor we grab byte 0 from the array.
We then grab the first byte of data from the face_data variable and send it to PORTB. we then delay for 1mS. Then to prevent ghosting on the screen, we clear PORTB before incrementing to the next cathodes.
We then get to the next statement. This means if x has not reached 7 yet, then go to the next number (from 0 to 7) and run the loop again.
So we go back to the start of the loop and now x = 1. This means we will be grabbing the next byte in the arrays. it keeps doing this until all eight bytes (in each array) have been sent to the LED's. Then thats it. The FOR NEXT loop is done (for now).
The next piece of code is actually where the main program starts.
here we set the oscillator speed (8Mhz) we make sure all ports are set to digital and then we set PORTB and PORTD to outputs. Then comes the main loop.
Here we have a WHILE WEND loop. This is just another type of loop. In this case it will stay in the loop while ever the statement is true.
E.G. we could have a while wend loop that has:
Code: Select all
while PORTA.0 = 1()
run this code....
wend
So in the case of drawing the face, I create an infinite loop where i say "does true equal true?" of course it does! so it will stay in the loop forever.
as you can see, the loop just calls the sub routine to draw the graphics.
and thats it for now!
feel free to ask any questions that you might have.