Page 1 of 1

Drawing Graphics "PacMan" on 24x24 Led Matrix

Posted: Fri Sep 03, 2010 4:20 am
by bitfogav
Heres a PacMan animation that I made for my 24x24 led matrix.

The link for the video is here -
http://www.youtube.com/watch?v=S2ARe1bBN-0

I can't post the whole code as theres alot of animation data but I will try my best.


Code: Select all

// Name of Program
Program PacMan_animation  // useful for documentation purposes

// Device and Clock 
Device = 18F4685         // Tell the compiler what chip we are using 
Clock = 20               // Tell the compiler what we will be setting the clock to (Mhz)            

Config MCLRE = ON
    
    // default fuses...
Config
   OSC = HS,
   FCMEN = OFF,
   IESO = OFF,
   PWRT = ON,
   BOREN = BOACTIVE,
   WDT = OFF,
   WDTPS = 128,
   PBADEN = OFF,
   DEBUG = OFF,
   XINST = OFF,
   LVP = OFF,
   STVREN = ON

// Modules 
Include "Utils.bas"      // Include this file when we compile so that we can use keywords like "SetAllDigital"

// Const Arrays 

// Anodes if we are using ULN2803 Chips (common-cathode Darlington transistor array.)  
Const Anode_data(8) As Byte = (%10000000,%01000000,%00100000,%00010000,%00001000,%00000100,%00000010,%00000001)



// Setup ALL our PORTS
Dim E_ALL_74573 As PORTC.6               ' setup ALL 74573 enable pins

Dim L_RedB0_74573 As PORTC.4              ' setup our RED CATHODE 74573 latch pin
Dim L_RedB1_74573 As PORTC.0              ' setup our RED CATHODE 74573 latch pin
Dim L_RedB2_74573 As PORTC.1              ' setup our RED CATHODE 74573 latch pin

Dim L_GreenB0_74573 As PORTC.2            ' setup our GREEN CATHODE 74573 latch pin
Dim L_GreenB1_74573 As PORTC.3            ' setup our GREEN CATHODE 74573 latch pin
Dim L_GreenB2_74573 As PORTC.5            ' setup our GREEN CATHODE 74573 latch pin

Dim L_AnodeR0_74573 As PORTE.0            ' setup our ANODE 74573 latch pin
Dim L_AnodeR1_74573 As PORTE.1            ' setup our ANODE 74573 latch pin
Dim L_AnodeR2_74573 As PORTE.2            ' setup our ANODE 74573 latch pin

Dim Anode_port As PORTB                   ' Anode DATA PORT
Dim Graphic_port As PORTD                 ' Graphic DATA PORT (green and red)

' when latch pin = high then data can be latched, when latch pin = low data cannot be stored
' when enable pin = high outputs OFF, when enable pin = low outputs ON
The code above: starts with a "Program PacMan_animation" this is used only for documentation purposes, and which means we need to use "END" at the end of our program which you will see at the end of the code. We then declare our microchip and clock speed, I am using an external 20mhz clock, next we have Config MCLRE = ON as I am using a RESET switch on my hardware, then we include swordfish module "Utils" so we can set I/0 ports to digital ports, and then we have a constant array "Anode_data" this array is called in code to set the Anodes of the matrix.

Then lastly I define all my PORT BITS which control all my 'D' type Latch 74573's. Im using nine 74573's in total, 3 to control all the GREEN cathodes and 3 to control all the RED cathodes and 3 to control all the Anodes, and then set PORTB as my Anode control bus and PORTD as my graphic (cathode) bus, I could have used just one port for all my data bus.

Code: Select all

// Variable Declarations
     
Dim x As Byte
Dim t As Word                
Dim counter As Word 
        
                
Dim block_0_green(8) As Byte
Dim block_0_red(8) As Byte
Dim block_1_green(8) As Byte
Dim block_1_red(8) As Byte
Dim block_2_green(8) As Byte
Dim block_2_red(8) As Byte
Dim block_3_green(8) As Byte
Dim block_3_red(8) As Byte
Dim block_4_green(8) As Byte
Dim block_4_red(8) As Byte
Dim block_5_green(8) As Byte
Dim block_5_red(8) As Byte
Dim block_6_green(8) As Byte
Dim block_6_red(8) As Byte
Dim block_7_green(8) As Byte
Dim block_7_red(8) As Byte
Dim block_8_green(8) As Byte
Dim block_8_red(8) As Byte


Dim display_step As Word
In the code above I just declare my Variables

Code: Select all

// Subroutines ==== 

Sub update_ram_data()
         
    Select display_step
    
        Case 0
block_0_red(0) = $00
block_0_red(1) = $00
block_0_red(2) = $00
block_0_red(3) = $00
block_0_red(4) = $00
block_0_red(5) = $00
block_0_red(6) = $00
block_0_red(7) = $00

block_0_green(0) = $00
block_0_green(1) = $00
block_0_green(2) = $00
block_0_green(3) = $00
block_0_green(4) = $00
block_0_green(5) = $00
block_0_green(6) = $00
block_0_green(7) = $00

block_1_red(0) = $00
block_1_red(1) = $00
block_1_red(2) = $00
block_1_red(3) = $00
block_1_red(4) = $00
block_1_red(5) = $00
block_1_red(6) = $00
block_1_red(7) = $00

block_1_green(0) = $00
block_1_green(1) = $00
block_1_green(2) = $00
block_1_green(3) = $00
block_1_green(4) = $00
block_1_green(5) = $00
block_1_green(6) = $00
block_1_green(7) = $00

block_2_red(0) = $00
block_2_red(1) = $00
block_2_red(2) = $00
block_2_red(3) = $00
block_2_red(4) = $00
block_2_red(5) = $00
block_2_red(6) = $00
block_2_red(7) = $00

block_2_green(0) = $00
block_2_green(1) = $00
block_2_green(2) = $00
block_2_green(3) = $00
block_2_green(4) = $00
block_2_green(5) = $00
block_2_green(6) = $00
block_2_green(7) = $00

block_3_red(0) = $00
block_3_red(1) = $00
block_3_red(2) = $00
block_3_red(3) = $00
block_3_red(4) = $00
block_3_red(5) = $00
block_3_red(6) = $00
block_3_red(7) = $00

block_3_green(0) = $00
block_3_green(1) = $00
block_3_green(2) = $00
block_3_green(3) = $00
block_3_green(4) = $00
block_3_green(5) = $00
block_3_green(6) = $00
block_3_green(7) = $00

block_4_red(0) = $00
block_4_red(1) = $00
block_4_red(2) = $00
block_4_red(3) = $00
block_4_red(4) = $00
block_4_red(5) = $00
block_4_red(6) = $00
block_4_red(7) = $00

block_4_green(0) = $00
block_4_green(1) = $00
block_4_green(2) = $00
block_4_green(3) = $01
block_4_green(4) = $01
block_4_green(5) = $00
block_4_green(6) = $00
block_4_green(7) = $00

block_5_red(0) = $00
block_5_red(1) = $00
block_5_red(2) = $00
block_5_red(3) = $00
block_5_red(4) = $00
block_5_red(5) = $00
block_5_red(6) = $00
block_5_red(7) = $00

block_5_green(0) = $00
block_5_green(1) = $00
block_5_green(2) = $00
block_5_green(3) = $b6
block_5_green(4) = $b6
block_5_green(5) = $00
block_5_green(6) = $00
block_5_green(7) = $00

block_6_red(0) = $00
block_6_red(1) = $00
block_6_red(2) = $00
block_6_red(3) = $00
block_6_red(4) = $00
block_6_red(5) = $00
block_6_red(6) = $00
block_6_red(7) = $00

block_6_green(0) = $00
block_6_green(1) = $00
block_6_green(2) = $00
block_6_green(3) = $00
block_6_green(4) = $00
block_6_green(5) = $00
block_6_green(6) = $00
block_6_green(7) = $00

block_7_red(0) = $00
block_7_red(1) = $00
block_7_red(2) = $00
block_7_red(3) = $00
block_7_red(4) = $00
block_7_red(5) = $00
block_7_red(6) = $00
block_7_red(7) = $00

block_7_green(0) = $00
block_7_green(1) = $00
block_7_green(2) = $00
block_7_green(3) = $00
block_7_green(4) = $00
block_7_green(5) = $00
block_7_green(6) = $00
block_7_green(7) = $00

block_8_red(0) = $00
block_8_red(1) = $00
block_8_red(2) = $00
block_8_red(3) = $00
block_8_red(4) = $00
block_8_red(5) = $00
block_8_red(6) = $00
block_8_red(7) = $00

block_8_green(0) = $00
block_8_green(1) = $00
block_8_green(2) = $00
block_8_green(3) = $00
block_8_green(4) = $00
block_8_green(5) = $00
block_8_green(6) = $00
block_8_green(7) = $00
The Code above is my Subroutine, this is the first animation data which sets the nine 8x8 matrix's, I am using a swordfish "SELECT" and then just calling aload of CASE commands. to update my RAM with new animation data.

Code: Select all

        Case 1
        
block_0_red(3) = $80
block_0_red(4) = $80

block_0_green(3) = $80
block_0_green(4) = $80

block_6_red(3) = $80
block_6_red(4) = $80

block_6_green(3) = $80
block_6_green(4) = $80

        Case 2
        
block_0_red(2) = $80
block_0_red(3) = $e0
block_0_red(4) = $e0
block_0_red(5) = $c0
block_0_red(6) = $80

block_0_green(2) = $80
block_0_green(3) = $e0
block_0_green(4) = $e0
block_0_green(5) = $c0
block_0_green(6) = $80

block_6_red(1) = $80
block_6_red(2) = $c0
block_6_red(3) = $e0
block_6_red(4) = $e0
block_6_red(5) = $80

block_6_green(1) = $80
block_6_green(2) = $c0
block_6_green(3) = $e0
block_6_green(4) = $e0
block_6_green(5) = $80
the code above is the next two CASE commands, as you can see theres not has much data needed for the next load of data, as I am only updating the blocks of data that is needed to display the next Frame, and this is how the pacman code goes, I use a total of 64 CASE commands, each one is a Frame of data and only updates the RAM with the bits that are needed for the next frame.

Code: Select all

        Case 64
        
block_2_green(5) = %00000000
block_2_green(6) = %00000000
block_2_green(7) = %00000000

block_5_green(0) = %00000000
block_5_green(1) = %00000001
block_5_green(2) = %00000001
block_5_green(3) = %00000001
block_5_green(4) = %00000001
block_5_green(5) = %00000001
block_5_green(6) = %00000001
block_5_green(7) = %00000001

block_8_green(0) = %00000001
block_8_green(1) = %00000001
block_8_green(2) = %00000001
block_8_green(3) = %00000001
block_8_green(4) = %00000001
block_8_green(5) = %00000001
block_8_green(6) = %00000001

       EndSelect
    
            // this is our count of frames
            // increment by 1
        display_step = display_step + 1
    
            // if we get to end, reset to 0
    If display_step = 64 Then    ' change the number of steps
        display_step = 0
    EndIf
    
    
    High (E_ALL_74573)  ' makes enable pin high (all outputs OFF)
the code above is our last CASE 64 (last frame) so we END SELECT. and then increase the display_step, this is our counter for which frame to display. and then we check display_step and if it equals 64 then it is reset to 0. and lastly make sure all the 74573's enable pins are in a HIGH state to turn off all outputs.

Code: Select all

        For t = 0 To 150  // t = time delay
            
            For x = 0 To 7 
  

        //===================================================================================                
                
        ' red data

             High   (L_RedB0_74573)               ' setup our RED CATHODE 74573 latch pin              
                Graphic_port = block_0_red(x)             
             Low    (L_RedB0_74573)               ' setup our RED CATHODE 74573 latch pin
                
             High   (L_RedB1_74573)               ' setup our RED CATHODE 74573 latch pin             
                Graphic_port = block_3_red(x)             
             Low    (L_RedB1_74573)               ' setup our RED CATHODE 74573 latch pin
                                          
             High   (L_RedB2_74573)               ' setup our RED CATHODE 74573 latch pin                
                Graphic_port = block_6_red(x)             
             Low    (L_RedB2_74573)               ' setup our RED CATHODE 74573 latch pin 
             
        //=================================================================================== 
       
        ' green data
 
             High   (L_GreenB0_74573)             ' setup our GREEN CATHODE 74573 latch pin                    
                Graphic_port = block_0_green(x)             
             Low    (L_GreenB0_74573)             ' setup our GREEN CATHODE 74573 latch pin
             
             High   (L_GreenB1_74573)             ' setup our GREEN CATHODE 74573 latch pin             
                Graphic_port = block_3_green(x)             
             Low    (L_GreenB1_74573)             ' setup our GREEN CATHODE 74573 latch pin                          
                          
             High   (L_GreenB2_74573)             ' setup our GREEN CATHODE 74573 latch pin
                Graphic_port = block_6_green(x)             
             Low    (L_GreenB2_74573)             ' setup our GREEN CATHODE 74573 latch pin                           
                
        //===================================================================================                
             
        ' anodes data

             High   (L_AnodeR0_74573)             ' setup our ANODE 74573 latch pin
                Anode_port = Anode_data (x)             
             Low    (L_AnodeR0_74573)             ' setup our ANODE 74573 latch pin                
 
 
             High   (L_AnodeR1_74573)             ' setup our ANODE 74573 latch pin
             High   (L_AnodeR2_74573)             ' setup our ANODE 74573 latch pin
                Anode_port = $00             
             Low    (L_AnodeR1_74573)             ' setup our ANODE 74573 latch pin
             Low    (L_AnodeR2_74573)             ' setup our ANODE 74573 latch pin 

                
        //===================================================================================                 
             
        ' Enable our Matrix
        
             Low    (E_ALL_74573)       ' makes enable pin low (all outputs ON)
  
                DelayUS(400)
                
             High   (E_ALL_74573)       ' makes enable pin high (all outputs OFF)

             ' we now clear our Anode data to stop graphic ghosting
             High   (L_AnodeR0_74573)             ' setup our ANODE 74573 latch pin
                Anode_port = $00             
             Low    (L_AnodeR0_74573)             ' setup our ANODE 74573 latch pin             
  
        //===================================================================================                
                
        ' red data

             High   (L_RedB0_74573)               ' setup our RED CATHODE 74573 latch pin              
                Graphic_port = block_1_red(x)             
             Low    (L_RedB0_74573)               ' setup our RED CATHODE 74573 latch pin
             
             High   (L_RedB1_74573)               ' setup our RED CATHODE 74573 latch pin             
                Graphic_port = block_4_red(x)             
             Low    (L_RedB1_74573)               ' setup our RED CATHODE 74573 latch pin
                                          
             High   (L_RedB2_74573)               ' setup our RED CATHODE 74573 latch pin                
                Graphic_port = block_7_red(x)             
             Low    (L_RedB2_74573)               ' setup our RED CATHODE 74573 latch pin 
             
        //=================================================================================== 
       
        ' green data

             High   (L_GreenB0_74573)             ' setup our GREEN CATHODE 74573 latch pin                    
                Graphic_port = block_1_green(x)             
             Low    (L_GreenB0_74573)             ' setup our GREEN CATHODE 74573 latch pin

             High   (L_GreenB1_74573)             ' setup our GREEN CATHODE 74573 latch pin             
                Graphic_port = block_4_green(x)             
             Low    (L_GreenB1_74573)             ' setup our GREEN CATHODE 74573 latch pin                          
                          
             High   (L_GreenB2_74573)             ' setup our GREEN CATHODE 74573 latch pin
                Graphic_port = block_7_green(x)             
             Low    (L_GreenB2_74573)             ' setup our GREEN CATHODE 74573 latch pin                           
                
        //===================================================================================                
             
        ' anodes data

             High   (L_AnodeR0_74573)             ' setup our ANODE 74573 latch pin
             High   (L_AnodeR2_74573)             ' setup our ANODE 74573 latch pin                
                Anode_port = $00             
             Low    (L_AnodeR0_74573)             ' setup our ANODE 74573 latch pin
             Low    (L_AnodeR2_74573)             ' setup our ANODE 74573 latch pin
             
 
             High   (L_AnodeR1_74573)             ' setup our ANODE 74573 latch pin  
                Anode_port = Anode_data (x)             
             Low    (L_AnodeR1_74573)             ' setup our ANODE 74573 latch pin
             
                
        //===================================================================================                 
             
        ' Enable our Matrix
        
             Low    (E_ALL_74573)       ' makes enable pin low (all outputs ON)
  
                DelayUS(400)
                
             High   (E_ALL_74573)       ' makes enable pin high (all outputs OFF)

             ' we now clear our Anode data to stop graphic ghosting
             High   (L_AnodeR1_74573)             ' setup our ANODE 74573 latch pin
                Anode_port = $00             
             Low    (L_AnodeR1_74573)             ' setup our ANODE 74573 latch pin 
                             
        //===================================================================================                
                
        ' red data

             High   (L_RedB0_74573)               ' setup our RED CATHODE 74573 latch pin              
                Graphic_port = block_2_red(x)             
             Low    (L_RedB0_74573)               ' setup our RED CATHODE 74573 latch pin
     
             High   (L_RedB1_74573)               ' setup our RED CATHODE 74573 latch pin             
                Graphic_port = block_5_red(x)             
             Low    (L_RedB1_74573)               ' setup our RED CATHODE 74573 latch pin
                                          
             High   (L_RedB2_74573)               ' setup our RED CATHODE 74573 latch pin                
                 Graphic_port = block_8_red(x)             
             Low    (L_RedB2_74573)               ' setup our RED CATHODE 74573 latch pin 
             
       //=================================================================================== 
       
       ' green data

             High   (L_GreenB0_74573)             ' setup our GREEN CATHODE 74573 latch pin                    
                Graphic_port = block_2_green(x)             
             Low    (L_GreenB0_74573)             ' setup our GREEN CATHODE 74573 latch pin

             High   (L_GreenB1_74573)             ' setup our GREEN CATHODE 74573 latch pin             
                Graphic_port = block_5_green(x)             
             Low    (L_GreenB1_74573)             ' setup our GREEN CATHODE 74573 latch pin                          
             
             High   (L_GreenB2_74573)             ' setup our GREEN CATHODE 74573 latch pin
                Graphic_port = block_8_green(x)
             Low    (L_GreenB2_74573)             ' setup our GREEN CATHODE 74573 latch pin                           
                
       //===================================================================================                
             
       ' anodes data
       
                Anode_port = Anode_data (x)
             High   (L_AnodeR2_74573)             ' setup our ANODE 74573 latch pin
             Low    (L_AnodeR2_74573)             ' setup our ANODE 74573 latch pin             
          
                Anode_port = $00 
             High   (L_AnodeR0_74573)             ' setup our ANODE 74573 latch pin
             High   (L_AnodeR1_74573)             ' setup our ANODE 74573 latch pin   
             Low    (L_AnodeR0_74573)             ' setup our ANODE 74573 latch pin
             Low    (L_AnodeR1_74573)             ' setup our ANODE 74573 latch pin
             
       //===================================================================================                 
             
       ' Enable our Matrix
        
             Low    (E_ALL_74573)       ' makes enable pin low (all outputs ON)
  
                DelayUS(400)
                
             High   (E_ALL_74573)       ' makes enable pin high (all outputs OFF)

             ' we now clear our Anode data to stop graphic ghosting
             High   (L_AnodeR2_74573)             ' setup our ANODE 74573 latch pin
                Anode_port = $00             
             Low    (L_AnodeR2_74573)             ' setup our ANODE 74573 latch pin 
                                  
            Next
              Inc(t)
        Next
End Sub
 
the code above setups all the data lines and even sets a delay so each frame is set to display a certain amount of time, this is done with the following line of code "For t = 0 To 150 // t = time delay".

Code: Select all

// Set Ports 
SetAllDigital       // Make all Pins digital I/O's 
TRISA = %11111111   // Make PORTA all inputs 
TRISB = %00000000   // Make PORTB all outputs 
TRISC = %00000000   // Make PORTC all outputs          
TRISD = %00000000   // Make PORTD all outputs 
TRISE = %00000000   // Make PORTE all outputs

    PORTB = $00      ' clear all ports
    PORTC = $00
    PORTD = $00
    PORTE = $00

    // this clears all data in all 74573 data latches
             High    (L_RedB0_74573)               ' setup our RED CATHODE 74573 latch pin
             High    (L_RedB1_74573)               ' setup our RED CATHODE 74573 latch pin
             High    (L_RedB2_74573)               ' setup our RED CATHODE 74573 latch pin

             High    (L_GreenB0_74573)             ' setup our GREEN CATHODE 74573 latch pin
             High    (L_GreenB1_74573)             ' setup our GREEN CATHODE 74573 latch pin
             High    (L_GreenB2_74573)             ' setup our GREEN CATHODE 74573 latch pin

             High    (L_AnodeR0_74573)             ' setup our ANODE 74573 latch pin
             High    (L_AnodeR1_74573)             ' setup our ANODE 74573 latch pin
             High    (L_AnodeR2_74573)             ' setup our ANODE 74573 latch pin
        
        // and this makes all outputs off     
        High (E_ALL_74573)  ' makes enable pin high (all outputs OFF)
        
        ' make sure all our data latches are closed
             Low    (L_RedB0_74573)               ' setup our RED CATHODE 74573 latch pin
             Low    (L_RedB1_74573)               ' setup our RED CATHODE 74573 latch pin
             Low    (L_RedB2_74573)               ' setup our RED CATHODE 74573 latch pin

             Low    (L_GreenB0_74573)             ' setup our GREEN CATHODE 74573 latch pin
             Low    (L_GreenB1_74573)             ' setup our GREEN CATHODE 74573 latch pin
             Low    (L_GreenB2_74573)             ' setup our GREEN CATHODE 74573 latch pin

             Low    (L_AnodeR0_74573)             ' setup our ANODE 74573 latch pin
             Low    (L_AnodeR1_74573)             ' setup our ANODE 74573 latch pin
             Low    (L_AnodeR2_74573)             ' setup our ANODE 74573 latch pin

// Initialize Variables  
counter = 0 

display_step = 0
 

// Main Program 

While True()
      
          update_ram_data

Wend    

// end of program.
End
And then the code above is the last part of the code: we start by setting all the PORTS of the microchip and then I make sure that all the data on all of the 74573's is cleared. I also then initialize variables and then make a WHILE LOOP which only calls one subroutine "update_ram_data" this gives us a Infinite loop so the animation PacMan will go on forever! :)

If you have any questions then just ask.

Posted: Fri Sep 03, 2010 10:01 pm
by brad
Very cool man :)

You have actually inspired me to get into these types or larger displays. I am also looking into using PWM to give more shades / colours.

Ontop of that I have been reading up on using standard LED's as light sensors (photodiodes) They work unbelievably well! I will go and start a new thread on the topic...

Just imagine your 24 x 24 matrix where you can touch it and make pixel's light up!

Posted: Sat Sep 04, 2010 3:26 am
by bitfogav
brad wrote:Very cool man :)

You have actually inspired me to get into these types or larger displays. I am also looking into using PWM to give more shades / colours.

Ontop of that I have been reading up on using standard LED's as light sensors (photodiodes) They work unbelievably well! I will go and start a new thread on the topic...

Just imagine your 24 x 24 matrix where you can touch it and make pixel's light up!
Yeah I have read that thread, thats well cool and yeah that would be something if my 24x24 matrix did that :)

Ive looked into PWM too, I know quite abit about it, what it is and how it works because it is used in many motor vehicle systems, eg: fan motors etc, and I have made a project which controls 8 leds using PWM, but it would be something if these 8x8 red and green used PWM..