Freelancer

Showing posts with label Embedded System. Show all posts
Showing posts with label Embedded System. Show all posts

Thursday, March 13, 2014

Simple Counter with PIC16F877A

Config Proteus like this
7 Segments leds counter pic16f877a
And here is the source code
;; -----------------------------------------------------------------------------
;; File name: 7SegmentsLedx4_Counter.asm
;; Email: dmtmd2010@yahoo.com.vn
;; Blog: tdmblogging.blogspot.com
;; Date: March 3rd, 2014
;; Description: a simple counter from 0000 to 9999 using PIC16F877A and 7 segments leds.
;; -----------------------------------------------------------------------------

include P16F877A.inc

; define constants

num_0 equ  20
num_1 equ  21
num_2 equ  22
num_3 equ  23

time_delay  equ  24
time_delay1  equ  25
time_delay2  equ  26

counter  equ  27
rate  equ  28
tmp   equ  29


org 0
goto 100

org 10
table ; look up for 7 segments code
movfw counter
addwf PCL
retlw H'3F'  ; 0
retlw H'06'  ; 1
retlw H'5B'  ; 2
retlw H'4F'  ; 3
retlw H'66'  ; 4
retlw H'6D'  ; 5
retlw H'7D'  ; 6
retlw H'07'  ; 7
retlw H'7F'  ; 8
retlw H'6F'  ; 9

org 100
start
; config and init ports
bsf  STATUS, 5    ; select bank 1

clrw
movwf TRISB    ; mark port B as an output port
movwf TRISC    ; mark port C as an output port

bcf  STATUS, 5    ; select bank 0

clrf PORTB
clrf num_0
clrf num_1
clrf num_2
clrf num_3
clrf rate

movlw H'FF'
movwf PORTC

; main program start here
loop
; control the counting speed
incf rate   ; increase rate
btfsc rate, 6   ; if rate equals 01000000
call incNum0   ; do count
btfsc rate, 6   ; and then clear rate
clrf rate

call display   ; scan the leds
goto loop


display
bsf  PORTC, 0   ; port C equals 11111111

movf num_0, w   ; look for the 7 segments code of num_0 value
movwf counter
call table
movwf PORTB   ; display to port B
bcf  PORTC, 3   ; port C equals 11110111
call delay

bsf  PORTC, 3

movf num_1, w
movwf counter
call table
movwf PORTB
bcf  PORTC, 2
call delay

bsf  PORTC, 2

movf num_2, w
movwf counter
call table
movwf PORTB
bcf  PORTC, 1
call delay

bsf  PORTC, 1

movf num_3, w
movwf counter
call table
movwf PORTB
bcf  PORTC, 0
call delay
return

incNum0
incf num_0   increase num_0
; test if num_0 equals 10 (1010 in binary)
clrf tmp
btfsc num_0, 1   ; xxxxxx1x
bsf  tmp, 0
btfsc num_0, 3   ; xxxx1xxx
incf tmp
btfsc tmp, 1
call incNum1   ; num_0 = 10 -> increase the next
return

incNum1
clrf num_0
incf num_1
clrf tmp
btfsc num_1, 1
bsf  tmp, 0
btfsc num_1, 3
incf tmp
btfsc tmp, 1
call incNum2
return

incNum2
clrf num_1
incf num_2
clrf tmp
btfsc num_2, 1
bsf  tmp, 0
btfsc num_2, 3
incf tmp
btfsc tmp, 1
call incNum3
return

incNum3
clrf num_2
incf num_3
clrf tmp
btfsc num_3, 1
bsf  tmp, 0
btfsc num_3, 3
incf tmp
btfsc tmp, 1
clrf num_3
return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
delay
movlw H'5'
movwf time_delay2
delay3
movlw H'5'
movwf time_delay1
delay2
movlw H'5'
movwf time_delay
delay1
decfsz time_delay
goto delay1
decfsz time_delay1
goto delay2
decfsz time_delay2
goto delay3
return

END