Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ful progrm

Status
Not open for further replies.
LIST P = PIC16F877A

#INCLUDE "P16F877A.INC"


__CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _LVP_OFF

cblock 20h
hibyte
lowbyte
temp
IRWaitCnt
msb,lsb,ones,tens,hund,thus,ss,sp,rr,tmp,tmp1,STORE,blink,hunds,tmp2,timer
endc


ORG 0X00

MOVLW 00H
MOVWF PCLATH
CLRF STATUS
GOTO main
;********************************************************************************
org 04h
bcf INTCON,T0IF
bcf PIR1,TMR1IF
movwf temp
swapf STATUS,W
movwf STORE
clrf STATUS
GOTO ISR
retfie
;********************************************************************************
lookup

addwf PCL,F ;digit to be displayed
retlw b'00111111' ;'0'
retlw b'00000110' ;'1'
retlw b'01011011' ;'2'
retlw b'01001111' ;'3'
retlw b'01100110' ;'4'
retlw b'01101101' ;'5'
retlw b'01111101' ;'6'
retlw b'00000111' ;'7'
retlw b'01111111' ;'8'
retlw b'01101111' ;'9'
retlw b'01110111' ;'a'
retlw b'01111100' ;'b'
retlw b'00111001' ;'c'
retlw b'01011110' ;'d'
retlw b'01111001' ;'e'
retlw b'01110001' ;'f'
retlw b'00000000'


;*****************************************************************************************




initport
bsf STATUS,RP0
bcf TRISC,1
bcf TRISC,0
movlw 0x06
movwf OPTION_REG
movlw b'00000000'
movwf TRISD
bcf TRISB,5
movlw b'11010000'
movwf TRISB
bsf PIE1,TMR1IE
bcf STATUS,RP0
bcf PIR1,TMR1IF
bsf INTCON,GIE
bsf INTCON,PEIE
bsf T1CON,0
bcf T1CON,2
bcf T1CON,1
clrf TMR0
clrf TMR1L
clrf TMR1H
movlw 0xf0
movwf TMR1L
movlw 0xee
movwf TMR1H
bcf PORTC,0
bcf PORTC,1
movlw b'11110000'
andwf PORTB,F
clrf PORTD
bsf PORTC,0
bcf PORTC,0
movlw 0x5f
movwf ss
bsf tmp,0
movlw 0x01
movwf ones
movlw 0x02
movwf tens
movlw 0x03
movwf hunds
movlw 0x04
movwf thus
clrf blink
return





mainline




nop
btfsc PORTB,4
goto mainline
call start
xorlw 0FFh
btfss STATUS,Z
goto mainline
movlw 0FFh
movwf PORTD
bsf PORTC,1
call delay
bcf PORTC,1
movfw lowbyte
movfw hibyte
movwf tmp1


; movfw lowbyte
; movwf tmp
; xorlw 0x0c
; btfss STATUS,Z
; goto ISR
; bsf tmp2,0
; goto next

;next
movfw lowbyte
movwf tmp
xorlw .1
btfss STATUS,Z
goto next1
goto chkOvr

next1 movfw lowbyte
xorlw .2
btfss STATUS,Z
goto next2
goto chkOvr

next2 movfw lowbyte
xorlw .3
btfss STATUS,Z
goto next3
goto chkOvr

next3 movfw lowbyte
xorlw .4
btfss STATUS,Z
goto next4
goto chkOvr

next4 movfw lowbyte
xorlw .5
btfss STATUS,Z
goto next5
goto chkOvr

next5 movfw lowbyte
xorlw .6
btfss STATUS,Z
goto next6
goto chkOvr

next6 movfw lowbyte
xorlw .7
btfss STATUS,Z
goto next7
goto chkOvr

next7 movfw lowbyte
xorlw .8
btfss STATUS,Z
goto chkOvr
goto chkOvr

chkOvr


; rlf tmp2,1
call convert
clrf lowbyte
clrf hibyte
goto mainline

chk clrf tmp1
retlw 0Fh

start

nop
incf tmp1,f
movlw .45
subwf tmp1,w
skpc
goto start
clrf tmp1
clrf TMR0
movlw 0x06
movwf TMR0
bcf INTCON,T0IF
call Wlow
call Whigh
btfsc INTCON,T0IF
retlw 0x0F

movfw TMR0
clrf TMR0
movwf timer
movwf temp
clrc
rrf temp,f
clrc
rrf temp,f
movf temp,w
subwf timer,
movlw 0x0c
movwf temp
nextBit movfw timer
subwf TMR0,w
skpz
goto nextBit
btfss PORTB,4
goto waitlow
call Whigh
setc
goto clock



waitlow call Wlow
clrc
clock clrf TMR0
btfsc INTCON,T0IF
retlw 0x00
rlf lowbyte,F
rlf hibyte,F
decfsz temp,f
goto nextBit
movfw lowbyte
movwf temp
rlf temp,f
rlf hibyte,F
rlf temp,f
rlf hibyte,F
movlw 0x3f
andwf hibyte,F
movlw 0x3f
andwf lowbyte,F
retlw 0xff



Wlow btfsc INTCON,T0IF
return
btfss PORTB,4
goto Wlow
return



Whigh btfsc INTCON,T0IF
return
btfsc PORTB,4
goto Whigh
return


;----------------------------------------------------------------------------------------------------------------------
ISR



btfss tmp,0
goto one
bcf PORTB,0
movfw ones
call lookup
movwf PORTD
btfsc blink,0
btfsc ss,5
bsf PORTB,0
rlf tmp,F
GOTO finish

one
btfss tmp,1
goto ten
bcf PORTB,0
movfw tens
call lookup
movwf PORTD
btfsc blink,1
btfsc ss,5
bsf PORTB,1
rlf tmp,F
goto finish

ten btfss tmp,2
goto hund1
bcf PORTB,1
movfw hunds
call lookup
movwf PORTD
btfsc blink,2
btfsc ss,5
bsf PORTB,2
rlf tmp,F
goto finish

hund1 btfss tmp,3
goto thuds
bcf PORTB,2
movfw thus
call lookup
movwf PORTD
btfsc blink,3
btfsc ss,5
bsf PORTB,3
rlf tmp,1
goto finish

thuds btfss tmp,4
goto finish
bcf PORTB,3
clrf tmp
bsf tmp,0
goto finish

;-----------------------------------------------------------------------------
ffinish

swapf temp,W

movlw 0xff
movwf TMR1L
movlw 0xef
movwf TMR1H

; *** Restore Values ***
swapf STORE,W
movwf STATUS
swapf temp,F
swapf temp,w
bcf INTCON,T0IF
bcf PIR1,TMR1IF
return









convert

movf tmp,0
movwf lsb
; btfsc tmp2,0
; goto aa
; btfsc tmp2,1
; goto bb
; btfsc tmp2,2
; goto cc
; btfsc tmp2,3
; goto dd
; clrf tmp2
; goto mainline
; return

aa
movf lsb,0
movwf ones
bcf ones,7
bcf ones,6
bcf ones,5
bcf ones,4
call ISR
return

bb
movf lsb,0
movwf tens
bcf tens,7
bcf tens,6
bcf tens,5
bcf tens,4
call ISR
return


cc
movf lsb,0
movwf hunds
bcf hunds,7
bcf hunds,6
bcf hunds,5
bcf hunds,4
call ISR
return



dd
movf lsb,0
movwf thus
bcf thus,7
bcf thus,6
bcf thus,5
bcf thus,4
call ISR
return


delay

movlw 0x05
movwf rr
back2: movlw 0xff
movwf sp
back decfsz sp,1
goto back
decfsz rr,1
goto back2
return


main
call initport
goto mainline
end
 
Last edited:
LIST P = PIC16F877A

#INCLUDE "P16F877A.INC"


__CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _LVP_OFF

cblock 20h
hibyte
lowbyte
temp
IRWaitCnt
msb,lsb,ones,tens,hund,thus,ss,sp,rr,tmp,tmp1,STOR E,blink,hunds,tmp2,timer
endc


ORG 0X00

MOVLW 00H
MOVWF PCLATH
CLRF STATUS
GOTO main
;************************************************* *******************************
org 04h
bcf INTCON,T0IF
bcf PIR1,TMR1IF
movwf temp
swapf STATUS,W
movwf STORE
clrf STATUS
GOTO ISR
retfie
;************************************************* *******************************
lookup

addwf PCL,F ;digit to be displayed
retlw b'00111111' ;'0'
retlw b'00000110' ;'1'
retlw b'01011011' ;'2'
retlw b'01001111' ;'3'
retlw b'01100110' ;'4'
retlw b'01101101' ;'5'
retlw b'01111101' ;'6'
retlw b'00000111' ;'7'
retlw b'01111111' ;'8'
retlw b'01101111' ;'9'
retlw b'01110111' ;'a'
retlw b'01111100' ;'b'
retlw b'00111001' ;'c'
retlw b'01011110' ;'d'
retlw b'01111001' ;'e'
retlw b'01110001' ;'f'
retlw b'00000000'


;************************************************* ****************************************




initport
bsf STATUS,RP0
bcf TRISC,1
bcf TRISC,0
movlw 0x06
movwf OPTION_REG
movlw b'00000000'
movwf TRISD
bcf TRISB,5
movlw b'11010000'
movwf TRISB
bsf PIE1,TMR1IE
bcf STATUS,RP0
bcf PIR1,TMR1IF
bsf INTCON,GIE
bsf INTCON,PEIE
bsf T1CON,0
bcf T1CON,2
bcf T1CON,1
clrf TMR0
clrf TMR1L
clrf TMR1H
movlw 0xf0
movwf TMR1L
movlw 0xee
movwf TMR1H
bcf PORTC,0
bcf PORTC,1
movlw b'11110000'
andwf PORTB,F
clrf PORTD
bsf PORTC,0
bcf PORTC,0
movlw 0x5f
movwf ss
bsf tmp,0
movlw 0x01
movwf ones
movlw 0x02
movwf tens
movlw 0x03
movwf hunds
movlw 0x04
movwf thus
clrf blink
return





mainline



bcf INTCON,T0IF
bcf PIR1,TMR1IF
nop
btfsc PORTB,4
goto mainline
call start
xorlw 0FFh
btfss STATUS,Z
goto mainline
movlw 0FFh
movwf PORTD
bsf PORTC,1
call delay
bcf PORTC,1
movfw lowbyte
movfw hibyte
movwf tmp1


; movfw lowbyte
; movwf tmp
; xorlw 0x0c
; btfss STATUS,Z
; goto ISR
; bsf tmp2,0
; goto next

;next
movfw lowbyte
movwf tmp
xorlw .1
btfss STATUS,Z
goto next1
goto chkOvr

next1 movfw lowbyte
xorlw .2
btfss STATUS,Z
goto next2
goto chkOvr

next2 movfw lowbyte
xorlw .3
btfss STATUS,Z
goto next3
goto chkOvr

next3 movfw lowbyte
xorlw .4
btfss STATUS,Z
goto next4
goto chkOvr

next4 movfw lowbyte
xorlw .5
btfss STATUS,Z
goto next5
goto chkOvr

next5 movfw lowbyte
xorlw .6
btfss STATUS,Z
goto next6
goto chkOvr

next6 movfw lowbyte
xorlw .7
btfss STATUS,Z
goto next7
goto chkOvr

next7 movfw lowbyte
xorlw .8
btfss STATUS,Z
goto chkOvr
goto chkOvr

chkOvr


; rlf tmp2,1
call convert
clrf lowbyte
clrf hibyte
goto mainline

chk clrf tmp1
retlw 0Fh

start

nop
incf tmp1,f
movlw .45
subwf tmp1,w
skpc
goto start
clrf tmp1
clrf TMR0
movlw 0x06
movwf TMR0
bcf INTCON,T0IF
call Wlow
call Whigh
btfsc INTCON,T0IF
retlw 0x0F

movfw TMR0
clrf TMR0
movwf timer
movwf temp
clrc
rrf temp,f
clrc
rrf temp,f
movf temp,w
subwf timer,
movlw 0x0c
movwf temp
nextBit movfw timer
subwf TMR0,w
skpz
goto nextBit
btfss PORTB,4
goto waitlow
call Whigh
setc
goto clock



waitlow call Wlow
clrc
clock clrf TMR0
btfsc INTCON,T0IF
retlw 0x00
rlf lowbyte,F
rlf hibyte,F
decfsz temp,f
goto nextBit
movfw lowbyte
movwf temp
rlf temp,f
rlf hibyte,F
rlf temp,f
rlf hibyte,F
movlw 0x3f
andwf hibyte,F
movlw 0x3f
andwf lowbyte,F
retlw 0xff



Wlow btfsc INTCON,T0IF
return
btfss PORTB,4
goto Wlow
return



Whigh btfsc INTCON,T0IF
return
btfsc PORTB,4
goto Whigh
return


;----------------------------------------------------------------------------------------------------------------------
ISR

btfsc INTCON,T0IF
goto mainline

btfss tmp,0
goto one
bcf PORTB,0
movfw ones
call lookup
movwf PORTD
btfsc blink,0
btfsc ss,5
bsf PORTB,0
rlf tmp,F
GOTO finish

one
btfss tmp,1
goto ten
bcf PORTB,0
movfw tens
call lookup
movwf PORTD
btfsc blink,1
btfsc ss,5
bsf PORTB,1
rlf tmp,F
goto finish

ten btfss tmp,2
goto hund1
bcf PORTB,1
movfw hunds
call lookup
movwf PORTD
btfsc blink,2
btfsc ss,5
bsf PORTB,2
rlf tmp,F
goto finish

hund1 btfss tmp,3
goto thuds
bcf PORTB,2
movfw thus
call lookup
movwf PORTD
btfsc blink,3
btfsc ss,5
bsf PORTB,3
rlf tmp,1
goto finish

thuds btfss tmp,4
goto finish
bcf PORTB,3
clrf tmp
bsf tmp,0
goto finish

;-----------------------------------------------------------------------------
finish


swapf STORE,W
movwf STATUS
swapf temp,F
swapf temp,W
bcf INTCON,T0IF
bcf PIR1,TMR1IF
movlw 0x0c
movwf TMR1L
movlw 0xff
movwf TMR1H
retfie
return


return


convert

movf tmp,0
movwf lsb
; btfsc tmp2,0
; goto aa
; btfsc tmp2,1
; goto bb
; btfsc tmp2,2
; goto cc
; btfsc tmp2,3
; goto dd
; clrf tmp2
; goto mainline
; return

aa
movf lsb,0
movwf ones
bcf ones,7
bcf ones,6
bcf ones,5
bcf ones,4
call ISR
return

bb
movf lsb,0
movwf tens
bcf tens,7
bcf tens,6
bcf tens,5
bcf tens,4
call ISR
return


cc
movf lsb,0
movwf hunds
bcf hunds,7
bcf hunds,6
bcf hunds,5
bcf hunds,4
call ISR
return



dd
movf lsb,0
movwf thus
bcf thus,7
bcf thus,6
bcf thus,5
bcf thus,4
call ISR
return


delay

movlw 0x05
movwf rr
back2: movlw 0xff
movwf sp
back decfsz sp,1
goto back
decfsz rr,1
goto back2
return


main
call initport
goto mainline
end
 
This is just careless and wrong:

Code:
swapf STORE,W
movwf STATUS
swapf temp,F
swapf temp,W
bcf INTCON,T0IF
bcf PIR1,TMR1IF
movlw 0x0c
movwf TMR1L
movlw 0xff
movwf TMR1H
retfie
return


return


Should be

Code:
swapf temp,W
movlw 0x0c
movwf TMR1L
movlw 0xff
movwf TMR1H

; *** Restore Values ***
swapf STORE,W
movwf STATUS
swapf temp,F

retfie
 
A little advice on writing code, please use ample comments so other people can decode your program. Also a schematic would be helpful.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top