P1   P2   P3
Home


Buying PICkit-2
PIC16F628 Data Sheet (.pdf  4,926KB)
Instruction Set for PIC16F628
blank16F628.asm template
Notepad2.zip     Notepad2.exe 
Library of Sub-routines "Cut and Paste
"
Library of routines:   A-E   E-P    P-Z 
 

 

This project is a mini "GAMES CONSOLE."
It comes with two games and the idea is to create more of your own games by looking at the programs we have provided and work from there.
The games have been
written in "assembly" and this is the "lowest" or "simplest" way to write a program. It may not be the "easiest," but it is the only way to teach PIC programming. No other form of programming uses the set of 35 instructions that come with the PIC chip. 
Hand-coding in assembly uses theses instructions and they are all you need to get it to perform all its operations (capabilities).
We could write the program in "C" or PICbasic" or some other form of programming language but this would put you at "arms length" with PIC chips as the instructions would not be PIC instructions.
Debate rages as to which form of coding is the best, but let me tell you this: When something doesn't work, you will appreciate the fact that we have used the simplest way to do everything and it's the only way to eliminate frustration.
Writing in assembly is the starting-point to learning to create a program.
It will only take a few days to learn the 36 instructions as they are not difficult to remember.
They are called mnemonics and this means they consist of letters of a word or the first letter of a number of words.
Examples are: btf (bit test file) decfsz (decrement file and skip the next instruction if not zero), and you can see what they mean.
We have also made programming easy by providing a library of routines so you can "cut and paste." 
If you follow our suggestion of adding a few instructions at a time or one sub-routine at a time, you cannot go wrong. This is the way we create programs and it saves a lot of frustration. If something does not work, the last few lines are at fault. 
There are a number of debugging tools and single-stepping routines, but they all take skill to use and they can finish up more frustrating than following our method.
We have tried them all and come up with the ANSWER.


BEGINNERS PROJECT
This project is designed for BEGINNERS. This might seem to be an unusual thing to say, but it's true. We have used the simplest microcontroller, the simplest circuit and the simplest way to create a program - to produce a really interesting project. There are lots of circuits that use a microcontroller to flash a LED and once you have done this, you will want to go further.
Talking Electronics has produced a project PIC LAB-1 that uses a PIC16F84A to drive a 7-segment display, convert the rotation of a 10k pot to display the numbers 0-9 and detect the temperature of a sensor such as reverse-biased diode.
Once you have completed a demonstration project such as this, you will want to expand to something really interesting.
This is what we have done.
We have opted to write the programs in ASSEMBLY LANGUAGE as this uses the 35 mnemonic codes that come with the PIC microcontroller and when you look at the program, you can see and read exactly what the micro is doing.
It is a little bit like Charles Dickens - writing a novel by hand. It might be a slow at first but it's the simplest way to get into microcontroller programming.
If you use one of the programs we have included in this project, called xxxxxx, you will be able to add your own lines of code and produce very simple animations.


 
THE CIRCUIT
The basic circuit:


7x5 GAMES CONSOLE - basic circuit

The circuit consists of a PIC16F628 microcontroller and a CD4017 chip (that turns on one output at a time). There are lots of names for this chip, (Decade Counter, Divider etc) but it is simply clocked by the micro to turn on a set of transistors (one at a time) to create scanning of the display.
The display contains 35 green LEDs and 35 red LEDs and we cannot turn them all on at the same time with a simple microcontroller such as the PIC16F628. The answer is to connect port A to the green LEDs and port B to the red LEDs and sink each column via a transistor.
This means a column of red LEDs is illuminated at a time when port A is active and a column of red LEDs when port B is active.
When both ports are active, the result is orange LEDs.
In this project the display is turned 90° anti-clockwise and the rows become columns etc.

  




7x5 GAMES CONSOLE - pinouts


 

The PIC micro is constantly executing 1 million instructions per second and you can put it in a loop and do nothing or create lots of interesting effects on a 5x7 Display.
There are two ways to get "things'" to appear on the display

SCREEN DUMP  
The first method is to "dump" or "illuminate" one or more pixels (LEDs) on the screen by accessing the associated row and column connections.
It is not possible to illuminate some combinations of LEDs as other LEDs will also illuminate at the same time.
So, this method is limited to a few combinations.
The transistors driving the LEDs have current limiting resistors (not fully shown in the diagram above) and these are designed to deliver a high current for the scan routine. If we turn on a single LED or just a few LEDs, they will appear extra bright - via the DUMP method. This can be avoided by pulsing the LEDs, and will be discussed later.


SCANNING

The solution to illuminating ANY combination of LEDs (there are 70 LEDs) is to turn on one column at a time and if this is done very fast, we can illuminate the whole screen. This is called a "Persistence Of Vision" trick. It's the same as the frames of a movie film producing movement on a cinema screen. The eye holds or retains an image for a short period of time after the image has extinguished and this allows us to illuminate the display, one column at a time, and the eye thinks the whole display is illuminated at the one time.
A complete column of LEDs are illuminated at the same time and this can be up to 14 LEDs. As can be seen from the diagram above, if the red line is taken HIGH and the 4017 is reset, then clocked so that the first output is HIGH, the transistor will connect the cathode of the red and green LEDs to the 0v rail and the red LED will illuminate.
If both red and green LEDs are activated, the result will be yellow (orange).
At the same time, any or all the red and green LEDs in the first column can be activated. 
The required LEDs are illuminated for a very short period of time and then turned off.
The 4017 is clocked so that the next output connects the second column to the 0v rail and the required LEDs are activated via the 14 lines from the PIC chip.
This process is repeated very quickly so that all the LEDs can be activated.
If we produce a sub-routine to do this, we can illuminate any combination of LEDs.
Even if we are illuminating one LED, the sub-routine can be called and it will produce a blank (black) screen in which only one LED is illuminated.
This sub-routine is called the SCAN routine. In our case the scan routine is called a RASTER routine.
A raster is the white screen produced on a TV when no channel is selected. It may be speckled due to "snow" or noise being picked up.
In our case the RASTER is produced by a sub-routine taking data from files 60h to 66h to produce the red pixels and files 70h to 76h for the green pixels.
To make it easy to illuminate pixels on the screen, we have only used the 5 lower bits of each file.  If the 7 files for each colour are laid out as shown in the diagram below, the bits correspond directly to the pixels on the display. By making a bit = 1, the corresponding LED is illuminated.
Each output line of the PIC chip is capable of delivering 25mA. To illuminate the display in multiplex or "scan" mode, we need to deliver slightly more than 25mA to each LED as we are only illuminating it for 20% of the time.
If we deliver 40mA for 20% duration, this works out as 5mA on a constant basis. But LEDs do not work on a linear scale and neither does your eye.
A LED delivered 40mA for 20% duration will appear brighter than one being delivered 5mA on a constant basis and this is why multiplexing is so successful.
But a PIC chip cannot deliver 40mA so we need buffer transistors. The same applies to the CD 4017. It can only deliver about 10mA per output and it also needs a set of buffer transistors.

RASTER ROUTINE
The Raster sub-routine does only one thing. It takes the data from the 14 files and puts it on the screen. This is done very quickly so that you cannot see any flickering. In fact only one column is displayed at a time but by scanning very quickly (faster than 100 times per second) it appears that all LEDs are illuminated at the same time.
It's the same with the raster on a TV set. The screen is illuminated by a single dot moving across the screen very quickly and producing more than 625 lines to fill the screen.
The direction of scan has been chosen so that bit 0 can be sent to the display and then the files are shifted right so bit1 is in bit0 location and can be sent to the display.
After 8 shifts, the files are back to their original value for the next scan of the display.

The pixel(s) we want to display are loaded into files 50h to 56h and/or 57h to 5Dh. For instance, if bit0 in file 50h is SET, the display will show red.
If bit0 in file 60h is SET, the display will show green.
If both bits are SET, the display will show orange.
We could directly transfer this data to the display but since one bit from 6 files will be sent to
portA and one bit from 6 files will be sent to portB 



7x5 Display


 
ANIMATION
At the beginning of each scan, bits in each file are "1" to represent an illuminated LED. Before the scan is started, we can manipulate the bits by moving or shifting them within the file or to another file in the set of Ghost Files and when the scan sub-routine is performed, the pixels will appear to move.

SYNC
There is a requirement called SYNCHRONISATION. The output of the CD 4017 must be known by the micro so that the correct LEDs will be illuminated on the display.
The CD4017 is clocked by the PIC chip but it must know which output is active at the beginning of the scan cycle.
We do not have sufficient lines from the micro for a reset line so an ingenious alternative is to connect one of the outputs of the CD 4017 to the input line of the PIC and clock the CD 4017 until this output is detected.
The micro now knows the state of the CD 4017 and every 10 clock pulses will bring it to the same state.
 
TWO PIC LINES
You will notice the PIC16F628 has two lines that are not fully input/output. These are RA4 and RA5. RA4 only pulls down and must be pulled high via a resistor. RA5 is input only.

6-BUTTONS
The board has 6 buttons. Four control the direction of the "cursor" and the centre button has been called FIRE. The extra button can be programmed for any other application.

DETECTING BUTTONS
The circuit diagram above does not show all the componentry and does not show how the buttons are detected.
The buttons are "polled" (looked-at) during the scan routine where the in-out lines of the PIC are converted to input and if one of the buttons is pushed, it will be detected as a LOW.
The software can detect two buttons at the same time and a sub-routine can be written to detect the press of a button before the project is turned on. It actually detects the press of a button at the absolute instant of turn-on. You can also detect repeat pressing or a code made up of a sequence.
 

IN-CIRCUIT PROGRAMMING
The PIC micro used in this project has the capability of being programmed while it is connected to a project. This means you don't have to remove it every time you want to update the program. This is very handy when developing a program as it may take 100 or more "burns" to get a game fully working.
But there is one thing you have to remember.
The Programmer (PICkit-2) is not capable of programming the chip if any of the programming lines are connected to a low-impedance load.
The output socket of the PICkit-2 requires a 6-pin plug and Talking Electronics has produced a connector that connects to the PICkit-2 via 6 pins and has 5 pins at the other end to connect to the five programming pins on the 7x5 Games Module project.
This is called In-Circuit Programming.
You will notice we have not connected low impedance loads to any of the pins used for In-Circuit programming.
Also, pin 14 must be able to rise higher than rail voltage during programming so we have added a 4k7 as this pin is connected to the clock on the 4017 and all inputs of a CMOS chip are protected from rising above rail voltage  by input diodes.

See the following circuit for the In-Circuit Programming connections:



 
 

THE POSSIBILITIES ARE ENDLESS
The possibilities with this simple project are endless. The display will produce a number of colours including red, green and orange and you can illuminate any combination of LEDs via the scanning routine.
This will give you the capability of producing simple attack games, strategy games or something of your own invention.
When we thought everything in the toy world had been invented, along came the Rubik's cube and Tetris.
But one area that has not been addressed is the birthday present from an Auntie. Auntie's will generally want to post the present and it needs to be small and thin for easy posting.
This game is ideal. There is also a demand for games for children in hospital and as presents for those who want a challenging game. 
The secret is to produce something that grabs the players attention and nothing is better than "playing against the computer." 
Games like "Reversi," "Simon," "Tic Tac Toe," "21 Pegs," are all possible.
Talking Electronics has been producing simple games for over 30 years and one of our readers Cameron Shepherd, started by producing a "space Invaders" game for our Z-80 computer. He then went on to produce games for Nintendo and now has his own software company based in the UK.
There is a market for games that can be easily posted and nothing fills this "niche" at the moment.
Be the first.

STRATEGY GAMES
Programming a game of strategy requires the highest level of thinking as you need to create sub-routines that take all possibilities into account. It's only by covering all possibilities, that a computer appears to have "intelligence."
Writing a program for a game is like writing a book. You need to get all your facts together and work out what you want to do.
Obviously the limitation of the game is decided by the 5x7 set of "pixels" but since these can be 1-of-3 colours or flashing, the end-result can be quite impressive.
Before you do anything, read through the programs we have developed and see how the sub-routines perform particular functions.
You will be using many of the pre-written sub-routines and it's just a matter of starting with the scan sub-routine and loading the Ghost Files with data for the start of a game.
To create movement or "animation" you will be calling the scan sub-routine a number of times before the display is updated.
This will give you the speed of the fastest object. Slow objects will call the scan sub-routine less frequently.


MINEFIELD-1
Minefield-1 is a simple memory game.
The display is set up with 4 mines and these are turned off after 3 seconds. Your object is to traverse the whole display without touching any of the mines. You can cheat by pressing the "Fire" button and see where the mines are located.
You can go back over any location more than once to fulfill your task.
To get MINEFIELD-1 you must push the left button while turning the Game Console on. This will initiate MINEFIELD-1.

Once you have played MINEFIELD-1a number of times you will want to know how the game is written.
This is what we will cover now:

THE DISPLAY

The display is used "on its side" for this project. This simply means the transistors are sinking the "rows" instead of the "columns." None of the circuitry is altered, just the terminology. The external markings on the sides of the display show how it is placed on the PC board.

Before writing a program you need to understand how the program creates "layers" by using a set of files to correspond to each pixel on the display.
The following diagram will help you understand how this is done.
Files from 70h to 7Dh are used to hold the red and green pixel data. This is called the SCANNING AREA.
The red data is outputted via port A and the green data is outputted via port B. 
When a pixel is present in both areas, at the same relative location, the result is orange on the display.
If we need to create pixels for "Bombs" or other obstacles, these are created in files 60h to 6Dh.
The third area is the CURSOR AREA.
This is where the cursor pixel is located and by pressing the Up, Down, Left, Right buttons, the cursor pixel is moved. The final location is moved to the scanning area for display.

 

 

THE CURSOR
The cursor is a flashing green LED that can be moved over the entire screen via the Left, Right, Up and Down buttons.
The cursor can be placed anywhere on the screen at the beginning of any game and the buttons will control its movement.
We will take the example of the cursor starting in the centre of the screen.
The Up button shifts all the files as a "rotate right file" operation but if the cursor is in bit0 the operation is not carried out.
The Down button shifts all the files as a "rotate left file" operation but if the cursor is in bit4 the operation is not carried out.
The Left operation does not operate on file 50h, but file 51h is transferred to file 50h, file 52h is transferred to file 51h etc.
The Right operation does not operate on file 56h, but file 55h is transferred to file 56h, file 54h is transferred to file 55h etc.
You can visualise the "cursor files" creates an area and this is shifted up to down or left or right and it contains the cursor pixel. We do not keep track of the location of the cursor but merely output the whole area to the display. This is just a simple way of adding the "cursor layer" to the display.
 

FLASHING CURSOR
The flashing cursor is created by producing two loops. The micro executes the first loop 10 times in which the data in the files is output to the display. The micro then executes the second loop 10 times in which the data is cleared before outputting. The number of loops determines the flash-rate.  
The program does not keep track of the actual position of the cursor, it simply outputs all the cursor files to the display.

However it is very easy to locate the cursor by checking for zero in a file and if it not zero, we locate the bit by xxxxxxxxxxxxxxxx
This is necessary so the cursor pixel can be aligned with a "bomb" or other "device" to create a "hit."



MINEFIELD-2
This is the reverse of Minefield-1. You are required to explode each mine by landing on top and pushing "fire" button.

P2   P3

 

 



  4/7/09