Hades logoHades applet banner
PIC16F84 mastermind game

applet icon

The image above shows a thumbnail of the interactive Java applet embedded into this page. Unfortunately, your browser is not Java-aware or Java is disabled in the browser preferences. To start the applet, please enable Java and reload this page. (You might have to restart the browser.)

Circuit Description

This applet demonstrates a PIC16 mastermind game. The circuit consists of the microcontroller, a standard ASCII-mode liquid-crystal display, and three switches. The program implements the 'mastermind' game with four places and ten colors, with a four-digit decimal number used instead of colored pegs. Your task is to guess - or rather, deduce - the secret number stored in the C source code (senha array).

(Note: If you prefer to change roles and have the computer deduce your secret number in turn, check out our mastermind solver applet, also based on the PIC16F84 microcontroller.)

Press the up and down switches to change the current digit, and then next to enter a new digit. After you entered your 4 digit guess, the microcontroller checks your answer and the LC display shows the number of correct digits/colors at the correct positions (left) and the number of correct digits/colors at wrong positions (right). For the next round, press the next button again, then repeat entering your next guess via the up- and down-buttons, and press the next-switch to select the next digit. The goal is to reach the 4-0 rating and the corresponding greeting from the program.

Open the PIC user-interface to watch the program execution, setting breakpoints, etc. (I recommend to uncheck the 'update display' checkbox and to click the data and program memories to enforce repainting. This saves a lot of CPU cycles and makes the animation go smoothly). To change the secret number, either edit the source code and recompile, or edit the memory values at addresses 01b,01c,01d,01e (bank0) at runtime. Of course, changing the stored number invalidates the previous answers from the program. Adding a random number generator function to initialize the secret number at program start with a random value would probably make a nice one-evening programming project.

The C source code has been written by Alisson G.D. Garcia, Elizio Rezende, Daniel Rocha, Renato Silva, and Ricardo Ferreira, during the Computer Organization Undergraduate Course, Universidade Federal de Višosa, Brazil.

The following C code (archive with sources and include files) has been compiled by using picclite from Hitech:

// /usr/hitech/bin/picl  -16f84 -C -Zg senha.c lcd8.c delay.c
// /usr/hitech/bin/picl  -16f84 -Osenha.hex senha.obj lcd8.obj delay.obj

#include	"lcd.h"
#include	"delay.h"

#define up RA4
#define down RA3
#define next RA2
// senha
char senha[5];
// tentativa
char tentativa[5];
char s_aux[5];

char up_is_pressed = 1;
char down_is_pressed = 1;
char next_is_pressed = 1;

char position = 0;

char t;
int pos = 0;
int cor = 0;
int i,j;

void change(signed char x)

	if ( x < 0 )


  TRISA = 0x1C; // Output bits porta, xxxSE
  TRISB = 0x00; // Output bits portb 76543210

  senha[0] ='1';
  senha[1] = '2';
  senha[2] = '3';
  senha[3] = '4';
  senha[4] = '\0';

  tentativa[0] = '0';
  tentativa[1] = '0';
  tentativa[2] = '0';
  tentativa[3] = '0';
  tentativa[4] = '\0';

  lcd_write(0x0C); // liga
  lcd_write(0x01); // limpa
  lcd_write(0x80); // coluna 0, linha 0

  	if ( next & next_is_pressed ) {
		next_is_pressed = 0;
		if ( position == 4 ) {
			for (i=0; i<5;++i)
				pos=cor= 0;
				for (i=0; i<4;++i) {
					if (s_aux[i] == tentativa[i]) {
						s_aux[i]= 'x';
				for (i=0; i<4;++i) {
					if (s_aux[i]!='x') {
						for ( j=0; j<4; ++j) {
							if (s_aux[i]==tentativa[j]) {
				lcd_puts(" Pos= ");
				lcd_puts(" Cor= ");
				if (pos==4) lcd_puts(" Parabens, VOCE GANHOU!!");
		if (position==5) {
			tentativa[0] = '0';
  			tentativa[1] = '0';
  			tentativa[2] = '0';
 			tentativa[3] = '0';
  			tentativa[4] = '\0';
			lcd_write(0x01); // limpa
  			lcd_write(0x80); // coluna 0, linha 0

	} else if ( next==0 && !next_is_pressed ) { // release next
		next_is_pressed = !next_is_pressed ;
	} else   if ( up & up_is_pressed ) {
		up_is_pressed = 0;
	} else if ( up==0 && !up_is_pressed ) { // release up
		up_is_pressed = !up_is_pressed ;
	} else   if ( down & down_is_pressed ) {
		down_is_pressed = 0;
	} else if ( down==0 && !down_is_pressed ) { // release down
		down_is_pressed = !down_is_pressed ;


Print version | Run this demo in the Hades editor (via Java WebStart)
Usage | FAQ | About | License | Feedback | Tutorial (PDF) | Referenzkarte (PDF, in German)
Impressum http://tams.informatik.uni-hamburg.de/applets/hades/webdemos/95-dpi/pic16f84-mastermind/senha.html