How to draw a smiley face on an 8x8 LED matrix

Post here to discuss programming with swordfish basic

Moderators: Chuckt, Garth, bitfogav

Post Reply [phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

How to draw a smiley face on an 8x8 LED matrix

Post by brad » Thu Jul 15, 2010 9:35 am

Here's a simple little tutorial on how you can draw a simple graphic on an 8x8 LED matrix display.

What you will need:

:arrow: PIC18f4685 microcontroller
:arrow: 8x8 LED matrix
:arrow: 8 x 150 ohm resistors
:arrow: 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.
The code is quite straight forward. The interesting thing is that the sub routines MUST be above your main code.

In this case my main code starts where it says:

Code: Select all

 // start of program
Whenever you see two forward slashes // it means the compiler will ignore the code coming after it. Great for making comments in your code.

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
This sub routine takes care of drawing the graphics (from the arrays) on the 8x8 matrix.

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
this simple loop will run this piece of code over and over again until PORTA pin 0 no longer equals 1.

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.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Post by bitfogav » Sun Aug 15, 2010 7:33 pm

Brad got a question for you, so you are using "Const face_data( 8 ) As Byte" as your data to draw the face, what if you used "DIM face_data( 8 )" and saved it to RAM instead?, could you then update each byte of data within your program?

because what I am aware of is that if you use "Const" then this data is stored to your program memory and it is unchangable.

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by brad » Sun Aug 15, 2010 9:45 pm

bitfogav wrote:Brad got a question for you, so you are using "Const face_data( 8 ) As Byte" as your data to draw the face, what if you used "DIM face_data( 8 )" and saved it to RAM instead?, could you then update each byte of data within your program?

because what I am aware of is that if you use "Const" then this data is stored to your program memory and it is unchangable.
Yep, you are exactly right. variables stored in program ram via const are not able to be changed. But storing it in ram via dim can be changed.

In what way did you want to update the data?

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Post by bitfogav » Sun Aug 15, 2010 9:49 pm

Well what if you wanted to change the face to a LETTER? instead of calling another "const" just use a Array (DIM) and then just update each byte of data? if that makes sense? :)

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Post by bitfogav » Sun Aug 15, 2010 9:50 pm

I think you just answered that question in my 24x24 project post :)

http://www.bradsprojects.com/phpBB2/vie ... =1083#1083

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by brad » Sun Aug 15, 2010 9:54 pm

bitfogav wrote:I think you just answered that question in my 24x24 project post :)

http://www.bradsprojects.com/phpBB2/vie ... =1083#1083
But wouldn't that data have to be somewhere in the first place in order to update it? :wink:

Post Reply
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Who is online

Users browsing this forum: No registered users and 4 guests