
Programming PIC16f886. The game "Stone, scissors, paper"

This article is aimed at newcomers to the programming of a family of pic controllers based on the assembler language. I took the pic16f886 microcontroller as a basis. For programming and modeling, the MPlab IDE (Microchip) and Proteus (Labcenter) programs were used, respectively.
Briefly about the essence of the project
This project allows people with disabilities to play Rock, paper, scissors, paper (Partial paralysis, lack of one or more fingers or hands). The idea belongs to meequz , my implementation
Project structure
Each player has 3 buttons at their disposal, with the corresponding inscriptions (stone, scissors, paper). The result of the selection and the score is displayed on the seven-segment board. The score goes up to 9 points. Start a new game by pressing the "Reset" button
Pic controller programming
Once again, I’ll repeat that for programming we will use a low-level machine-oriented language - assembly language.
First we’ll declare which microcontroller we use and reserve general purpose registers that we will need in the future:
LIST P=16f886
#include
CBLOCK 0x20
IGROK_1
IGROK_2
KAMEN
NOJNITSI
BUMAGA
SCHET_1
SCHET_2
Reg_1
Reg_2
Reg_3
NINE
ENDC
Next, the actual start of the program and the configuration of input / output ports:
start ;Подпрограмма
bsf STATUS, RP1 ;Включаем банк памяти 3
bsf STATUS, RP0
clrf ANSEL ;Устанавливаем порты ввода\выводы как цифровые
clrf ANSELH
BCF STATUS, RP1 ;Включаем банк памяти 1
BSF STATUS, RP0
movlw b'11100111' ;Выводы RA0,RA1,RA2,RA7,RA6,RA5 настраиваем на вход, остальные на выход
movwf TRISA
movlw b'00000000' ;Все выводы настраиваем на выход
movwf TRISB
movlw b'00000001' ;Вывод RC0 настраиваем на вход, остальные на выход
movwf TRISC
bcf STATUS,RP0 ;Включаем банк памяти 0
bcf STATUS,RP1
CLRF PORTA ; Инициализируем порты
CLRF PORTB
CLRF PORTC
movlw b'00000001'
movwf KAMEN
movlw b'00000010'
movwf NOJNITSI
movlw b'00000100'
movwf BUMAGA
movlw .9
movwf NINE
clrf IGROK_1
clrf IGROK_2
clrf SCHET_1
clrf SCHET_2
clrf W
bcf INTCON,6 ;Запрещаем прерывания
bcf INTCON,7
Teams:
movlw - Sends a constant to the battery.
movwf - sends the contents of the battery to the
bcf register - clears the bit in the
bsf register - sets the bit in the
clrf register - clears the
btfss register - checks the bit in the register, skips the next command if the result is 1
btfsc - checks the bit in the register, skips the next command if the result 0
The main cycle of the program. It checks the click of a button on both controllers:
GAME ;Подпрограмма
BTFSS PORTA,0
CALL PRE_KANMEN_1
BTFSS PORTA,5
CALL PRE_KANMEN_2
BTFSS PORTA,1
CALL PRE_NOJNITSI_1
BTFSS PORTA,6
CALL PRE_NOJNITSI_2
BTFSS PORTA,2
CALL PRE_BUMAGA_1
BTFSS PORTA,7
CALL PRE_BUMAGA_2
GOTO PROVERKA
VOZVRAT ;Подпрограмма
GOTO GAME
The PROVERKA subroutine checks whether an element has been selected by player 1:
PROVERKA ;Подпрограмма
BTFSC IGROK_1,0 ;камень
GOTO ONE
BTFSC IGROK_1,1 ;ножницы
GOTO TWO
BTFSC IGROK_1,2 ; бумага
GOTO THREE
GOTO VOZVRAT
When a player presses a button, there is a transition to a specific subprogram (for example, imagine that player 1 selected a stone):
PRE_KAMEN_1 ;Подпрограмма
MOVLW .1
CALL KOD
MOVWF PORTB
BSF PORTA,3
MOVFW KAMEN
MOVWF IGROK_1
CLRF W
call delay
BCF PORTA,3
RETURN
The selected item is recorded in the register IGROK_1
After selecting player 1, a check is made to see whether the selection was made by player 2 and at the same time compares the selection results. After comparison, it goes to 1 of 3 subprograms:
1) The first player won
2) The second player won
3) Draw
ONE ;Подпрограмма
BTFSC IGROK_2,0
GOTO NICHA
BTFSC IGROK_2,1
GOTO FIRST_WIN
BTFSC IGROK_2,2
GOTO SECOND_WIN
GOTO VOZVRAT
TWO ;Подпрограмма
BTFSC IGROK_2,0
GOTO SECOND_WIN
BTFSC IGROK_2,1
GOTO NICHA
BTFSC IGROK_2,2
GOTO FIRST_WIN
GOTO VOZVRAT
THREE ;Подпрограмма
BTFSC IGROK_2,0
GOTO FIRST_WIN
BTFSC IGROK_2,1
GOTO SECOND_WIN
BTFSC IGROK_2,2
GOTO NICHA
GOTO VOZVRAT
In order not to paint all 3 subprograms, imagine that the first player won:
FIRST_WIN ; Подпрограмма
incf SCHET_1 ;Происходит увеличении счета на 1
FIRST_W ;Подпрограмма
MOVFW SCHET_1
CALL KOD2
MOVWF PORTB
BSF PORTC,1
NOP ;Пустая команда
NOP
NOP
NOP
NOP
BCF PORTC,1
MOVFW SCHET_2
CALL KOD2
MOVWF PORTB
BSF PORTC,2
NOP
NOP
NOP
NOP
NOP
BCF PORTC,2
BTFSC PORTC,0
GOTO FIRST_W
PROV ;Подпрограмма
BTFSC PORTC,0 ;Проверка была ли нажата кнопка "сброс"
GOTO PRED_GAME
GOTO PROV
Elements are displayed using the KOD routine:
KOD
addwf PCL,1;
retlw b'11111111';
retlw b'10100011'; 'КАМЕНЬ'
retlw b'11001001'; 'НОЖНИЦЫ'
retlw b'11000000'; 'БУМАГА'
Stone -

Scissors -

Paper -

Digits are displayed using the KOD2 routine:
KOD2
addwf PCL,1;
retlw b'11000000'; '0'
retlw b'11111001'; '1'
retlw b'10100100'; '2'
retlw b'10110000'; '3'
retlw b'10011001'; '4'
retlw b'10010010'; '5'
retlw b'10000010'; '6'
retlw b'11111000'; '7'
retlw b'10000000'; '8'
retlw b'10010000'; '9'
The final step of the program is to clear the registers IGROK_1 and IGROK_2, and check the account overflow:
PRED_GAME ;Подпрограмма
CLRF IGROK_1
CLRF IGROK_2
BCF STATUS,2
MOVFW SCHET_1
SUBWF NINE,0
BTFSC STATUS,2
GOTO CLEAR
BCF STATUS,2
MOVFW SCHET_2
SUBWF NINE,0
BTFSC STATUS,2
GOTO CLEAR
GOTO GAME
CLEAR ;Подпрограмма
CLRF SCHET_1
CLRF SCHET_2
GOTO GAME
Conclusion
The scheme of this project compiled in the Proteus program:

******************************************* **********************************
For convenience, an .asm file , .hex file and a schematic in proteus are applied to this article