Microcontroller Programming: LED Matrix Control with I2C and UART - Prof. Lippold Haken, Papers of Electrical and Electronics Engineering

A c code example for programming a microcontroller to control an led matrix using i2c communication and uart interface. The code includes functions for initializing the led matrix, sending data to it, and handling serial communication. The document also defines constants and variables used in the code. This example can be useful for students and researchers in the field of microcontroller programming, embedded systems, and digital electronics.

Typology: Papers

Pre 2010

Uploaded on 03/13/2009

koofers-user-nlp-1
koofers-user-nlp-1 🇺🇸

10 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Guitar Chord Trainer
May 4, 2005
Tom Panfil
Amy Slater
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download Microcontroller Programming: LED Matrix Control with I2C and UART - Prof. Lippold Haken and more Papers Electrical and Electronics Engineering in PDF only on Docsity!

Guitar Chord Trainer

May 4, 2005

Tom Panfil

Amy Slater

Abstract:

Guitar Chord Trainer is an interactive guitar instructor. The project consists of a

customized guitar and computer software. The guitar is fitted with a 12x6 array of LEDs.

Each LED represents a possible place for the player to hold down a string. By lighting up

specific LEDs, the guitar provides the player a hands-on approach to learning guitar

chords. The software is responsible for telling the guitar which LEDs to light up. This

communication is achieved via a serial cable. Basic functionality of the software includes

chord memorization.

Operating Instructions:

Setting up the guitar:

Items needed:

 9V battery

 serial cable

 software (Guitar Chord Trainer.exe)

 project guitar

Insert the 9V battery into the black box mounted on the guitar. Plug in the male end

of the serial cable into the black box. Plug the female end of the serial cable into the

COM1 port on the CPU. Turn on the guitar by flipping the switch on the top of the

black box. Load the software.

Software instructions:

Double click on Guitar Chord Trainer.exe to open the program. The top three boxes

labeled First Chord, Second Chord, and Third Chord allow you to tell the program

which chords you would like to learn. You have the option to learn up to three chords

at a time, displayed randomly on the guitar, or one at a time by pressing the “Display

first chord only” button. If you would like to learn multiple chords at one time,

choose a speed at which the chords will be displayed and the number of times you

would like the chords to change and press the “Start” button. If at any time you want

the chords to stop cycling through on the guitar, press the “Stop” button. The “Close”

button will close the program.

Project Details:

Basic Block Diagram

GUI Details:

GUI Flowchart

Functions:

This group of functions initializes the string picture data based on the correct strings to

strum for a given chord.

Private Sub init1TO5()

-X

Private Sub init1TO4()

-X0000X

Private Sub init2TO5()

-XX

Private Sub init0TO3()

-0000XX

Private Sub init0TO5()

Private Sub initNone()

-This function is used to initialize all of the guitar display information, including chord

finger placement data, string strumming data, and the guitar picture background.

Private Sub initTimer()

-This function is used to set the speed variable.

This group of functions gathers the string data and stores the correct data in the specific

variables.

Private Sub initString1(), initString2(), initString3()

This group of functions prints the given chord data and calls the function pickChord.

Private Sub printString1(), printString2(), printString3()

Private Function showChord(c0 As Integer, c1 As Integer, c2 As Integer, c3 As Integer,

c4 As Integer, c5 As Integer)

-This function uses the passed data to determine which finger placement circles to display

for a given chord.

Private Function pickChord(chrNum As Integer)

-This function calls send13byte and showChord. It is used to send the correct bytes to the

guitar for LEDs and to display the chord data in the GUI. It uses case statements to pick

the correct chord data.

Private Function send13Byte(b0 As Byte, b1 As Byte, b2 As Byte, b3 As Byte, b4 As

Byte, b5 As Byte, b6 As Byte, b7 As Byte, b8 As Byte, b9 As Byte, b10 As Byte, b11 As

Byte, b12 As Byte)

-This function sends 13 bytes of data serially.

0 - 0x80 0 - 0x04 0 - 0x02 0 - 0x01 6 - 0x40 6 - 0x20 6 - 0x10 6 - 0x08 6 - 0x04 6 - 0x02 6 - 0x01 9 - 0x

1 - 0x80 1 - 0x04 1 - 0x02 1 - 0x01 7 - 0x40 7 - 0x20 7 - 0x10 7 - 0x08 7 - 0x04 7 - 0x02 7 - 0x01 7 - 0x

2 - 0x80 2 - 0x04 2 - 0x02 2 - 0x01 8 - 0x40 8 - 0x20 8 - 0x10 8 - 0x08 8 - 0x04 8 - 0x02 8 - 0x01 8 - 0x

3 - 0x80 3 - 0x04 3 - 0x02 3 - 0x01 9 - 0x40 9 - 0x20 9 - 0x10 9 - 0x08 9 - 0x04 9 - 0x02 9 - 0x01 9 - 0x

4 - 0x80 4 - 0x04 4 - 0x02 4 - 0x01 10 - 0x40 10 - 0x20 10 - 0x10 10 - 0x08 10 - 0x04 10 - 0x02 10 - 0x01 10 - 0x

5 - 0x80 5 - 0x04 5 - 0x02 5 - 0x01 11 - 0x40 11 - 0x20 11 - 0x10 11 - 0x08 11 - 0x04 11 - 0x02 11 - 0x01 11 - 0x

This is the chart used for determining which bytes to send for any given set of LEDs representing a chord on the guitar. The first

number represents which place in the 13 byte data vector the number should be written. The second number is the HEX value that

should take the byte place.

Ex/ If the guitar should show this placement of LEDs, the 13 byte vector would be 1F 00 00 00 00 00 00 00 02 04 02 80 00. The most

significant byte represents the string strumming LEDs.

Circuit Block Diagram

RS-

Serial

TTL I^2 C

Serial Bus

Start

Initialize

LED Driver

Initialize

Serial

Interrupts

Wait for

incoming

serial data

Light up

LEDs

Microcontroller Block Diagram

13 bytes received

Initialize LED driver:

The MAX6955 is initialized by setting configuration registers. The PIC16F877 sets these

registers by communicating with MAX6955 through an I^2 C bus. The configuration

registers are set to the following:

Global LED Intensity: 27 mA

Decode Mode: Direct Control

Scan Limit: All

Configuration: Normal Operation and Blink Disabled

Digit Type: 16 7-segment registers

27 mA will flow to all turned on LEDs. Direct Control enables all the LEDs to be

controlled discretely (not decoded as in alphanumeric displays). The scan limit is set to

enable all possible LEDs to be illuminated. The Configuration register is set to enable

normal operation (not testing) and NOT to blink the LEDs. The digit type, 16 7-segment

registers, is equivalent to saying 128 discrete LEDs.

Initialize Serial Interrupts:

This simply initializes the serial interrupt to fire every time a byte of serial data is

received. The serial interrupt handler simply inputs received serial data into a buffer for

later use.

Wait for incoming serial data:

The main program simply loops forever waiting for serial data. When 13 bytes have been

received (i.e. a new chord is to be displayed), the correct LEDs are turned on.

Code:

PC software code: '''''''''''''''''''''''' ''variables'' '''''''''''''''''''''''' Dim timercount As Integer Dim temp As Integer Dim chord1 As String Dim chord2 As String Dim chord3 As String Dim counter As Integer Dim chordNum1 As Integer Dim chordNum2 As Integer Dim chordNum3 As Integer Dim tempy As Integer Private Sub cmdDsp1_Click() Timer1.Enabled = False picChord.Cls Call initNone Call initString Call printString End Sub Private Sub cmdDsp2_Click() Timer1.Enabled = False picChord.Cls Call initNone Call initString Call printString End Sub Private Sub cmdDsp3_Click() Timer1.Enabled = False picChord.Cls Call initNone Call initString Call printString End Sub Private Sub Form_Load() MSComm1.PortOpen = True Call initNone End Sub Private Sub cmdStart_Click() Call initTimer Timer1.Enabled = True ''''''''''''''''' ''initial'' ''''''''''''''''' picChord.Cls timercount = 0 Call initNone Call initTimer '''''''''''''''''''''''''''''''''''''''''''''''''''' ''setting count variable'' '''''''''''''''''''''''''''''''''''''''''''''''''''' If comboCount.Text = "5" Then counter = 4 ElseIf comboCount.Text = "10" Then counter = 9 ElseIf comboCount.Text = "15" Then counter = 14 ElseIf comboCount.Text = "20" Then

counter = 19 End If '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''setting chords and serial data'' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Call initString Call initString Call initString ''''''''''''''''''''''''''''''''' ''first iteration'' ''''''''''''''''''''''''''''''''' tempy = Int(Rnd * 3) + 1 Select Case tempy Case Is = 1 Call printString Case Is = 2 Call printString Case Is = 3 Call printString Case Else picChord.Cls End Select End Sub Private Sub Timer1_Timer() timercount = timercount + 1 picChord.Cls Call initNone tempy = Int(Rnd * 3) + 1 Select Case tempy Case Is = 1 Call printString Case Is = 2 Call printString Case Is = 3 Call printString Case Else picChord.Cls End Select If timercount = counter Then Timer1.Enabled = False End If End Sub Private Sub cmdStop_Click() Timer1.Enabled = False End Sub Private Sub cmdClose_Click() End End Sub ''''''''''''''''''''''''' ''functions'' ''''''''''''''''''''''''' Private Sub init1TO5() 'X***** ImageX(0).Visible = True ImageX(0).Refresh For temp = 1 To 5 ImgStr(temp).Visible = True ImgStr(temp).Refresh

Img1to5(2).Visible = False Img1to5(2).Refresh Img1to5(3).Visible = False Img1to5(3).Refresh Img1to5(5).Visible = False Img1to5(5).Refresh Img3to5(0).Visible = False Img3to5(0).Refresh Img0to5(0).Visible = False Img0to5(0).Refresh Img0to5(1).Visible = False Img0to5(1).Refresh Img0to5(2).Visible = False Img0to5(2).Refresh Img0to5(3).Visible = False Img0to5(3).Refresh 'strings For temp = 0 To 5 ImgStr(temp).Visible = False ImgStr(temp).Refresh ImageX(temp).Visible = False ImageX(temp).Refresh Next 'main guitar pic ImgGuitar.Visible = True ImgGuitar.Refresh End Sub Private Sub initTimer() '''''''''''''''''''''''''''''''''''''''''''''''''''' ''setting speed variable'' '''''''''''''''''''''''''''''''''''''''''''''''''''' If comboSpeed.Text = "Fast" Then Timer1.Interval = 2000 ElseIf comboSpeed.Text = "Medium" Then Timer1.Interval = 4000 ElseIf comboSpeed.Text = "Slow" Then Timer1.Interval = 6000 End If End Sub Private Sub initString1() If optMaj1.Value Then chord1 = Combo1.Text & " Major" chordNum1 = Combo1.ListIndex ElseIf optMin1.Value Then chord1 = Combo1.Text & " Minor" chordNum1 = Combo1.ListIndex + 12 ElseIf optDim1.Value Then chord1 = Combo1.Text & " Dim" chordNum1 = Combo1.ListIndex + 24 Else 'opt71.Value Then chord1 = Combo1.Text & " 7" chordNum1 = Combo1.ListIndex + 36 End If End Sub Private Sub initString2() If optMaj2.Value Then chord2 = Combo2.Text & " Major" chordNum2 = Combo2.ListIndex ElseIf optMin2.Value Then chord2 = Combo2.Text & " Minor" chordNum2 = Combo2.ListIndex + 12 ElseIf optDim2.Value Then chord2 = Combo2.Text & " Dim" chordNum2 = Combo2.ListIndex + 24 Else 'opt72.Value Then chord2 = Combo2.Text & " 7"

chordNum2 = Combo2.ListIndex + 36 End If End Sub Private Sub initString3() If optMaj3.Value Then chord3 = Combo3.Text & " Major" chordNum3 = Combo3.ListIndex ElseIf optMin3.Value Then chord3 = Combo3.Text & " Minor" chordNum3 = Combo3.ListIndex + 12 ElseIf optDim3.Value Then chord3 = Combo3.Text & " Dim" chordNum3 = Combo3.ListIndex + 24 Else 'opt73.Value Then chord3 = Combo3.Text & " 7" chordNum3 = Combo3.ListIndex + 36 End If End Sub Private Sub printString1() picChord.Print chord Call pickChord(chordNum1) End Sub Private Sub printString2() picChord.Print chord Call pickChord(chordNum2) End Sub Private Sub printString3() picChord.Print chord Call pickChord(chordNum3) End Sub Private Function send13Byte(b0 As Byte, b1 As Byte, b2 As Byte, b3 As Byte, b4 As Byte, b As Byte, b6 As Byte, b7 As Byte, b8 As Byte, b9 As Byte, b10 As Byte, b11 As Byte, b12 As Byte) MSComm1.Output = Chr(b12) MSComm1.Output = Chr(b11) MSComm1.Output = Chr(b10) MSComm1.Output = Chr(b9) MSComm1.Output = Chr(b8) MSComm1.Output = Chr(b7) MSComm1.Output = Chr(b6) MSComm1.Output = Chr(b5) MSComm1.Output = Chr(b4) MSComm1.Output = Chr(b3) MSComm1.Output = Chr(b2) MSComm1.Output = Chr(b1) MSComm1.Output = Chr(b0) End Function Private Function showChord(c0 As Integer, c1 As Integer, c2 As Integer, c3 As Integer, c As Integer, c5 As Integer) ImgChr0(c0).Visible = True ImgChr0(c0).Refresh ImgChr1(c1).Visible = True ImgChr1(c1).Refresh ImgChr2(c2).Visible = True ImgChr2(c2).Refresh ImgChr3(c3).Visible = True ImgChr3(c3).Refresh ImgChr4(c4).Visible = True ImgChr4(c4).Refresh ImgChr5(c5).Visible = True ImgChr5(c5).Refresh End Function Private Function pickChord(chrNum As Integer) Select Case chrNum

Call send13Byte(30, 0, 32, 16, 8, 16, 0, 0, 0, 0, 0, 0, 0) Call init1TO Call showChord(12, 5, 6, 7, 6, 12) Case Is = 39 'd# Call send13Byte(15, 0, 0, 0, 0, 0, 0, 0, 0, 128, 2, 4, 2) Call init2TO Call showChord(12, 12, 0, 2, 1, 2) Case Is = 4 'emajor Call send13Byte(63, 0, 0, 0, 0, 0, 0, 0, 4, 4, 128, 0, 0) Call init0TO Call showChord(12, 1, 1, 0, 12, 12) Case Is = 16 'eminor Call send13Byte(63, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 0) Call init0TO Call showChord(12, 1, 1, 12, 12, 12) Case Is = 28 'edim Call send13Byte(30, 0, 16, 8, 4, 8, 0, 0, 0, 0, 0, 0, 0) Call init1TO Call showChord(12, 6, 7, 8, 7, 12) Case Is = 40 'e Call send13Byte(63, 0, 0, 0, 0, 0, 0, 0, 4, 0, 128, 0, 0) Call init0TO Call showChord(12, 1, 12, 0, 12, 12) Case Is = 5 'fmajor Call send13Byte(63, 0, 0, 0, 0, 0, 0, 128, 130, 130, 132, 128, 128) Call init0TO Call showChord(12, 2, 2, 1, 12, 12) Img0to5(0).Visible = True Img0to5(0).Refresh Case Is = 17 'fminor Call send13Byte(63, 0, 0, 0, 0, 0, 0, 128, 130, 130, 128, 128, 128) Call init0TO Call showChord(12, 2, 2, 12, 12, 12) Img0to5(0).Visible = True Img0to5(0).Refresh Case Is = 29 'fdim Call send13Byte(30, 0, 8, 4, 2, 4, 0, 0, 0, 0, 0, 0, 0) Call init1TO Call showChord(12, 7, 8, 9, 8, 12) Case Is = 41 'f Call send13Byte(63, 0, 0, 0, 0, 0, 0, 128, 130, 128, 132, 128, 128) Call init0TO Call showChord(12, 2, 12, 1, 12, 12) Img0to5(0).Visible = True Img0to5(0).Refresh Case Is = 6 'f#major Call send13Byte(63, 0, 0, 0, 0, 0, 0, 4, 5, 5, 6, 4, 4) Call init0TO Call showChord(12, 3, 3, 2, 12, 12) Img0to5(1).Visible = True Img0to5(1).Refresh Case Is = 18 'f#minor Call send13Byte(63, 0, 0, 0, 0, 0, 0, 4, 5, 5, 4, 4, 4) Call init0TO Call showChord(12, 3, 3, 12, 12, 12) Img0to5(1).Visible = True Img0to5(1).Refresh Case Is = 30 'f#dim Call send13Byte(30, 0, 4, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0) Call init1TO Call showChord(12, 8, 9, 10, 9, 12) Case Is = 42 'f# Call send13Byte(63, 0, 0, 0, 0, 0, 0, 4, 5, 4, 6, 4, 4) Call init0TO Call showChord(12, 3, 12, 2, 12, 12) Img0to5(1).Visible = True Img0to5(1).Refresh Case Is = 7 'gmajor Call send13Byte(63, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 2) Call init0TO Call showChord(2, 1, 12, 12, 12, 2)

Case Is = 19 'gminor Call send13Byte(63, 0, 64, 64, 0, 0, 0, 2, 2, 2, 2, 2, 2) Call init0TO Call showChord(12, 4, 4, 12, 12, 12) Img0to5(2).Visible = True Img0to5(2).Refresh Case Is = 31 'gdim Call send13Byte(30, 0, 2, 1, 128, 1, 0, 0, 0, 0, 0, 0, 0) Call init1TO Call showChord(12, 9, 10, 11, 10, 12) Case Is = 43 'g Call send13Byte(63, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 128) Call init0TO Call showChord(2, 1, 12, 12, 12, 0) Case Is = 8 'g#major Call send13Byte(63, 0, 32, 32, 64, 0, 0, 1, 1, 1, 1, 1, 1) Call init0TO Call showChord(12, 5, 5, 4, 12, 12) Img0to5(3).Visible = True Img0to5(3).Refresh Case Is = 20 'g#minor Call send13Byte(63, 0, 32, 32, 0, 0, 0, 1, 1, 1, 1, 1, 1) Call init0TO Call showChord(12, 5, 5, 12, 12, 12) Img0to5(3).Visible = True Img0to5(3).Refresh Case Is = 32 'g#dim Call send13Byte(60, 0, 64, 32, 0, 0, 0, 1, 0, 0, 1, 0, 0) Call init0TO Call showChord(3, 4, 5, 3, 12, 12) Case Is = 44 'g# Call send13Byte(63, 0, 32, 0, 64, 0, 0, 1, 1, 1, 1, 1, 1) Call init0TO Call showChord(12, 5, 12, 4, 12, 12) Img0to5(3).Visible = True Img0to5(3).Refresh Case Is = 9 'amajor Call send13Byte(31, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 0) Call init1TO Call showChord(12, 12, 1, 1, 1, 12) Case Is = 21 'aminor Call send13Byte(31, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 128, 0) Call init1TO Call showChord(12, 12, 1, 1, 0, 12) Case Is = 33 'adim Call send13Byte(30, 64, 32, 16, 64, 0, 0, 0, 0, 0, 0, 0, 0) Call init0TO Call showChord(4, 5, 6, 4, 12, 12) Case Is = 45 'a Call send13Byte(31, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 4, 0) Call init1TO Call showChord(12, 12, 1, 12, 1, 12) Case Is = 10 'a#major Call send13Byte(31, 0, 0, 0, 0, 0, 0, 0, 128, 130, 130, 130, 128) Call init1TO Call showChord(12, 12, 2, 2, 2, 12) Img1to5(0).Visible = True Img1to5(0).Refresh Case Is = 22 'a#minor Call send13Byte(31, 0, 0, 0, 0, 0, 0, 0, 128, 130, 130, 132, 128) Call init1TO Call showChord(12, 12, 2, 2, 1, 12) Img1to5(0).Visible = True Img1to5(0).Refresh Case Is = 34 'a#dim Call send13Byte(60, 32, 16, 8, 32, 0, 0, 0, 0, 0, 0, 0, 0) Call init0TO Call showChord(5, 6, 7, 5, 12, 12) Case Is = 46 'a# Call send13Byte(31, 0, 0, 0, 0, 0, 0, 0, 128, 130, 128, 130, 128) Call init1TO