Instruction Set for PIC16F628
PIC16F628 data

Chip Programmer - PICkit2 from Modtronix
(MPASM and MPLAB come with PICkit2)

No kits are available for this project, however all the
components are available from Talking Electronics.

See more projects using micros:
Elektor,EPE,Silicon Chip

Notepad2.zip     Notepad2.exe 
Library of Sub-routines "Cut and Paste
Library of routines:   A-E   E-P    P-Z 

The files for Lift Counter
LiftCounter-asm.txt (.asm)
LiftCounter-hex.txt (.hex)


This project has been developed due to a request from Mr Moshweunyane (dmoshweunyane8@gmail.com). He asked for a circuit that would count up when someone entered a lift and count down when someone exited, using two infra-red sensors.
All we had to do was take the 2-digit up/down counter and add two optical sensors.
These sensors could be any type of detector and we have shown two LDR's (Light Dependent Resistors) and two amplifying transistors mounted on a sub-board. You can use infrared or photo-transistors as the sensors to get equal results.
All the "detection" is done via the software and the program "polls" the detectors and works out if a person is entering or leaving the lift.
The reason for polling the sensors is clever. It prevents the micro being caught in a loop and allows the program to display numbers at the same time.
The same design can be used for a shop or any activity where you need to know if a room is getting too crowded.
This arrangement has been requested for bathrooms in an attempt to control and avoid unsavory behavior.
The circuit is designed around a PIC16F628A. It has been presented on an experimental PC board using surface-mount components and was built in less than 1 hour, with about 2 hours to write and finalise the program.
It uses "In Circuit Programming" via PICkit-2 or Talking Electronics Multi-Chip Programmer, plus the adapter (specific to each programmer) shown below.
You can add an alarm feature if the lift gets overcrowded or if someone is in the bathroom when the shop is closing.

The Lift Counter Circuit

This project has been created as an add-on for the 2-Digit Counter. We placed the two transistors, LDR's and pots on a small PC board and connected it to the 2-Digit Counter via a plug and socket.

The light detectors have to be set up for the application. The best is to use infra-red detectors as they are not upset by ambient light.
Each detector has to be set up so that light falling on the detector makes the input line LOW.
When the beam is broken, the line goes HIGH.
We have directly coupled the output of the detector to the micro however you could use capacitor coupling  and breaking the beam will produce a pulse.

The files for Lift Counter
LiftCounter-asm.txt (.asm)
LiftCounter-hex.txt (.hex)

; Lift Counter    Started 8/8/2009	
;Count the number entering and leaving a lift
;Port B drives 7 segment display
;enter on RA2   exit on RA3
;Units drive on RA0   Tens drive on RA1		*
;* 						*

	list P = 16F628		;microcontroller 
	include 	;registers for F628

	__Config 	_cp_off & _lvp_off & _pwrte_on 
		& _wdt_off & _intRC_osc_noclkout & _mclre_off
;code protection - off
;low-voltage programming - off
;power-up timer -  on
;watchdog timer - off
;use internal RC for 4MHz - all pins for in-out

; variables - names and files

		;Files for F628 start at 20h 
temp1		equ 20h	;for delay
temp2		equ 21h	;for delay
SwUp		equ 22h	;
SwDwn		equ	23h	;
units		equ	24h	;
tens		equ	25h	;
Sw_Flag		equ	26h	;
counter		equ	27h	;max count is 2Hz

status		equ	0x03
cmcon		equ	0x1F
rp1			equ	0x06
rp0			equ	0x05
portA		equ 0x05
portB		equ 0x06

;Beginning of program
reset	org	00	;reset vector address	
	goto	SetUp	;goto SetUp

table	addwf   PCL,F           ;02h,1  add W to program counter 
        retlw   b'00111111'     ; "0"   -|F|E|D|C|B|A
        retlw   b'00000110'     ; "1"   -|-|-|-|C|B|-
        retlw   b'01011011'     ; "2"   G|-|E|D|-|B|A
        retlw   b'01001111'     ; "3"   G|-|-|D|C|B|A 
        retlw   b'01100110'     ; "4"   G|F|-|-|C|B|-
        retlw   b'01101101'     ; "5"   G|F|-|D|C|-|A
        retlw   b'01111101'     ; "6"   G|F|E|D|C|-|A
        retlw   b'00000111'     ; "7"   -|-|-|-|C|B|A
        retlw   b'01111111'     ; "8"   G|F|E|D|C|B|A
        retlw   b'01101111'     ; "9"   G|F|-|D|C|B|A

;* port A and B initialisation					*

SetUp	bsf	status,rp0	
	movlw	b'00001100'	;Make RA0,1 out   RA2,3 in
	movwf	05h			
	clrf	06h		;Make all RB output
	bcf	status,rp0	;select programming area - bank0 
	movlw	b'10000000'	;Turn off T0CKI, prescale for TMR0 = 1:
	movwf	option_reg		
	clrf 	portB		;Clear Port B of junk 
	clrf	units		;zero the units file		
	clrf	tens		;zero the tens file		
	clrf	Sw_Flag
	movlw	07h		;turn comparators off and enable
	movwf	cmcon		;    pins for I/O functions	
	goto 	Main						

;* Delay 10mS 		10 x 1,000uS	

D_10mS	movlw	0Ah
	movwf	temp2
D_a	nop
	decfsz temp1,1
	goto 	D_a
	decfsz temp2,1
	goto 	D_a	
	retlw 	00
Up	btfsc	Sw_Flag,2
	retlw	00
	bsf	Sw_Flag,2
	incf	units,1
	movlw	0Ah	  ;put 10 into w
	xorwf	units,0	  ;compare units file with 10
	btfss	status,2  ;zero flag in status file. Will be set if units is 10
	retlw	00
	clrf	units
	incf	tens,1
	movlw	0Ah	  ;put 10 into w
	xorwf	tens,0	  ;compare units file with 10
	btfsc	status,2  ;zero flag in status file. Will be set if tens is 10
	clrf	tens			
	retlw	00	  ;display passes 99 but not below 0		
Dwn	btfsc	Sw_Flag,3
	retlw	00
	bsf	Sw_Flag,3
	decf	units,1
	movlw	0FFh	  ;put FFh into w
	xorwf	units,0	  ;compare units file with FFh
	btfss	status,2  ;zero flag in status file. Will be set if units is 10
	retlw	00
	movlw	09
	movwf	units	  ;put 9 into units file
	decf	tens,1
	movlw	0FFh	  ;put 0FFh into w
	xorwf	tens,0	  ;compare tens file with 0FFh
	btfsc	status,2  ;zero flag in status file. Will be set if tens is 0FFh
	goto	$+2	  ;tens file is 0FFh
	retlw	00						
	clrf	tens
	clrf	units
	retlw	00	  ;display  not below 0		
;* Main 							*

Main	movlw	040h		;maximum count is 2Hz
	movwf	counter		;

Main1	btfss	portA,2		;test for enter (will go HIGH on enter)
	goto	$+3		;entry not detected
	call	Up
	goto	Main2
	btfss	portA,3	;test for exit (will go HIGH on exit)
	goto	$+3		;exit not detected
	call 	Dwn
	goto	Main2
	clrf	counter
	incf	counter		;effectively bypass counter
Main2	movlw	b'00000001'	;Make RA0 HIGH for units drive 	
	movwf	portA	
	movf	units,0		;copy unit value into w
	call	table		;unit display value will return in w
	movwf	portB		;output units value
	call	D_10mS		;call delay
	clrf	portB		;clear display
	movlw	b'00000010'	;Make RA1 HIGH for tens drive 	
	movwf	portA			
	movf	tens,0		;copy tens value into w		
	call	table		;tens display value will return in w
	movwf	portB		;output tens value
	call	D_10mS		;call delay
	clrf	portB		;clear display
	btfsc	portA,2		;bit 2 is zero when enter LDR illuminated
	bcf	Sw_Flag,2
	btfsc	portA,3		;bit 3 is zero when exit LDR illuminated
	bcf	Sw_Flag,3	
	decfsz 	counter					
	goto	Main2
	goto	Main				

Going Further
This program can be modified to suit your particular application.
You can add a relay, buzzer, set of LEDs, globes to the project to create a valuable piece of industrial equipment.