k40_watchChecking to see how well the TWR-K40X256 functions as a watch 🙂

For my Embedded Systems Design course we had the opportunity to implement our own designs. ENGG 4560 teaches computer systems design, particularly related to system-on-chip (SOC) design and embedded systems design. An SOC architecture is an ensemble of processors, memories and interconnects customized to an application domain. The target board used for development was the TWR-K40X256 Kit Freescale evaluation board powered by the Kinetis K40 microcontroller as shown below:


TWR-K40X256 Kit Freescale evaluation board

For our final project Jake Nusca and I implemented a simple pattern-matching game using the TWR-K40X256 Kit Freescale evaluation board. We chose the Freescale board as it was the most user friendly and easiest to program and debug. We also had a strict timeline and the other development platforms would have resulted in a much greater development time.

Before we decided on the type of game to implement, we had a few requirements for our design. Our design must:

  • Use features of the TWR-K40X256 that were new to us
  • Must be user friendly
  • Must be interactive

Taking these requirements into consideration we decided on a pattern matching game. The game will work as follows:

  • A random pattern is flashed using the 4 LEDs to the player.
  • The player tries to match the pattern using the capacitive touch sensing inputs (TSI).
  • If the user matches the pattern correctly, their score and pattern difficulty is increased.
  • If the user matches the pattern incorrectly, their score and pattern is reset.
  • New pattern is flashed and the game is repeated.

The game is accompanied by a GUI on the host computer that the Freescale board is attached to. From the GUI the player can see their score, user messages, and the coloured flashing pattern, and hear a sound representing the pattern. A screenshot of the GUI can be seen below:

pattern_game_guiPattern Matching Game GUI

The main functionality of the game runs in a loop in the file main.c. When the program starts, it sends some user-friendly messages to the GUI, and then begins with the game loop. The K40 starts the game by adding a random value of 0-3 (corresponding to the 4 LEDs/buttons on the board) to an array that is used as the program’s pattern sequence that the user must match.

Once the pattern is added to, it is “played” or displayed on both the on-board LEDs and in the GUI program. At this point, the TSI interrupt is enabled, and the program waits indefinitely for the user to try to match the pattern that was just shown.

When the user presses one of the touch buttons on the K40 board, it launches an interrupt handler as previously noted, which adds the pressed button (notated by a value 0-3) to a second array, referred to as the user’s pattern sequence. At the same time, the corresponding LED is flashed on the board, and a corresponding image is flashed on the GUI as well. The code that executes this is looks like the following sample:

if(lValidTouch&((1<<ELECTRODE0))) //If detected a valid touch…
      LED0_TOGGLE; //Toggle LED
      printf(“LED|0\n”); //Command to toggle LED in GUI
      lValidTouch &= ~((1<<ELECTRODE0)); //Clear valid touch
      user_seq[user_index] = 0; //Add ‘0’ (LED 0) to user sequence
      LED0_TOGGLE; //Toggle LED
      printf(“LED|0\n”); //Toggle GUI LED

Once this is complete, control is returned to the main function.

At this point, the main function will notice that a value has been added to the user’s pattern array, and it will check that the user’s array matches the program’s pattern array. If the patterns are the same, then the user’s score is incremented, and another value is added to the computer pattern, which is again displayed for the user and the user matching starts again.

If the user’s pattern does not correctly match the computer’s stored pattern, then the user loses. The score is set back to 0, and both the computer’s pattern and user’s patterns are wiped in order to start the program from the beginning again.

The key to handling the user’s input is in enabling and disabling the TSI interrupt service routine. When it is disabled, touching the buttons on the K40 board will do nothing, allowing the program to flow uninterrupted (used for showing messages, playing back the computer’s pattern, etc.). When the user’s input is required, the TSI ISR is enabled, and the program will wait to detect user input before continuing.