ARCADE GAME WITH PIC16F877AFollow article
Nowadays, who does not remember those arcade games with which everything started? Those times when you had to go to the arcade machines to play and you spent the weekly pay to have a good time surrounded by friends.
The project is based on microcontroller PIC16F877A and consists of a three-level arcade game, in which you can select your difficulty and see your final score. Therefore, the game can be divided into 3 different main stages:
- Level selection menu: Selection of the 3 different levels of difficulty.
- Game: Game screen with music in which a pause is included.
- Score: Show your score after seeing a loser face with music.
The project can be divided into three main parts:
- Circuit connections: the electronic control circuit is based on a Microchip® PIC16F877A microcontroller connected to different sensors and actuators described below.
- Microcontroller programming: the microcontroller has been programmed in C using MPLAB integrated development environment.
- Video: Video making the connections with the microcontroller and showing the functioning of the arcade game.
In this section, the circuit connections are explained, as well as the operation modules employed. The project consists of some modules such as a joystick and a led matrix with a Johnson counter.
The joystick counts with 5 terminals:
- GND, +5V
- VRx, VRy: they provide an analogical signal between 0 V and 5 V proportional to the joystick’s position in axis X and axis Y respectively. These voltages are converted into a number between 0 and 1023 using the microcontroller’s 10-bit ADC (Analog-to-Digital Converter).
- Switch: it is the output terminal for the joystick’s pushbutton and requires a pull-up resistor. When it is pressed, the laser on top of the servomotors is activated.
The connections with the microcontroller have been the following:
2. Led matrix with Johnson counter
The LED matrix is formed by LED diodes arranged in the form of a matrix. In addition, through its 16 pins, it allows you to select both the column and the LED of the row to be illuminated.
In this case, the matrix used is a common anode, which means that if in column 1, for example, we set a HIGH, all the cathodes of the LEDs that are in LOW of that column will light up. This allows you to create figures, letters, numbers ...
For greater simplicity of the control of the matrix, a Johnson counter has been used, which allows illumination of the different columns one by one by means of a pulse sweep. The time that passes between the pulses from one column to another is defined by RE0, it will works like a clock at a fixed frequency that we define. RE1 works as the reset of the Johnson counter pulses. Because only 8 pins of the 10 available are used, this reset will occur upon reaching the eighth column.
The connections with both the microcontroller and the matrix are described in the following image:
3. Piezo Buzzer
This component reproduces the music programmed in the microcontroller. The CCP module operates in PWM mode with a 50% duty cycle. The duration of the pulse depends on the musical note because each one of them has a different frequency and therefore a period. The music code is included in the "Arcade game" program.
The image below shows the connections:
The arcade game program and the inputs and outputs are explained in this section. As it was mentioned before, the microcontroller has been programmed in C using MPLAB integrated development environment. The program code is attached at the end of the article.
The inputs and outputs used are:
- RE0: As digital output. Pulse sweep frequency of the Johnson counter
- RE1: As digital output. Reset frequency of the Johnson counter.
- RD0-RD7: As digital outputs. Control of the LED diodes of the rows of the matrix.
- RC2: As analogic output. Generates the melodies of the buzzer (CCP1)
- RB0: As digital input. Causes the interruption of the joystick button (SW)
- RA1: As analogic input. Y position of the joystick (VRy)
- RA3: As analogic input. X position of the joystick (VRx)
The model used in the laboratory is shown in the following image. All connections can be seen on it.
For the development of the arcade game, different modules of the PIC have been configured:
- Timer0 module. It is configured as a timer module every 500us, which causes an interruption when it overflows. In order to configure it, a pre-divider of 4 was used and the value 131 was loaded in the TMR0.
- CCP1 module. It has been set in PWM mode to control the different frequencies of the piezo buzzer notes. To achieve this, the Timer 2 adjusts the period of the notes and the CCPR1L the work cycle to 50%
- Timer2 module. It is used to adjust the period of the PWM signal of the previously mentioned CCP1 module. The post divider of timer 2, when used in PWM mode, is irrelevant
- 10-bit A/D converter module. It converts the analogical signal of the joystick into different digital samples with a resolution of 4,8876 mV/bit. The conversions are released every 2ms in the different channels (AN1 and AN3)
To explain the operation of the program, a simplified main program block diagram is shown. This contains the three main stages of the Arcade game: level selection, game, score.
The block diagrams of the interruptions of RB0 and the A/D converter best explained are also shown.
- Main program block diagram:
- A/D converter interrupt block diagram:
- RB0(SW) interrupt block diagram:
The interruption of tmr0 has not been included in the block diagrams because it is only used as a 500us timer module. With this timer, you control both the music, the game, the sweep of the LED matrix, that is, everything that needs to be timed.