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.

(PIC16f877a) 00-99 seven segment Counter ,Assembly language

Status
Not open for further replies.
Hi,

I changed the lookup table e.g.

yours: ;RETLW b'10001000' ;0
;RETLW b'10111011' ;1

mine: retlw b'00111111' ; 0
retlw b'00000110' ; 1
 
hi sniper,

LED Segment pattern is: x,g,f,e,d,c,b,a, so b'00000110 lights the c & b segments which is seen as a '1'.

E
 

Attachments

  • AAesp05.gif
    AAesp05.gif
    13 KB · Views: 179
Last edited:
Another way to get a code box is to type [code] before it and [/code] after.

Mike.
 
Last edited by a moderator:
This is still giving me a hard time
So I load the code to the chip and all i get is 00 on 7-seg screen its not running. but when i check it with the watch in mplab it shows that it runs fine the numbers come up in the watch so can anyone see what the problem is. I have added all the parts into the code from this forum depending on the display you are using.

Code:
;7 segment LED display - count up from zero

	LIST	p=16F877a		;tell assembler what chip we are using
	include "P16F877a.inc"		;include the defaults for the chip
	__config	 h'3f73'
	



		cblock	0x20			;start of general purpose registers
			count			;used in looping routines
			count1			;used in delay routine
			counta			;used in delay routine
			countb			;used in delay routine
			tmp1			;temporary storage
			tmp2
			p_temp			;save register
			s_temp			;save register
			w_temp			;save register
			tens			;tens storage
			ones			;ones storage
		endc

SEG_PORT	Equ	PORTB			;7 segment port
SEG_TRIS	Equ	TRISB


		org	0x0000
		goto	Initialise

;**************************************************************************	
;			     	Interrupt routine
;**************************************************************************



; 	Interrupt routine handles TMR2 which generates a 1ms tick

;	Interrupt vector

	ORG	0x0004


INT
		movwf	w_temp		; Save W register
		swapf	STATUS,W	; Swap status to be saved into W
		movwf	s_temp		; Save STATUS register
		movfw	PCLATH
		movwf	p_temp		; Save PCLATH 
	
		btfss	PIR1,TMR2IF	; Flag set if TMR2 interrupt
		goto	INTX		; Jump if not timed out

;common cathode set up ,use whats below

	; Timer (TMR2) timeout every 1 milli second
	

		bcf	PIR1,TMR2IF	; Clear the calling flag


		btfss	SEG_PORT, 7	;check which LED was last
		goto	Do_tens
		movfw	ones
		andlw	0x0F		;make sure in range of table
		call	LED_Table
 		andlw	0x7F		;set to correct LED
		movwf	SEG_PORT
		goto	INTX

Do_tens		movfw	tens
		andlw	0x0F		;make sure in range of table
		call	LED_Table
		iorlw	0x80		;set to correct LED
		movwf	SEG_PORT

; Timer (TMR2) timeout every 1 milli second  
 
 ;This is so that common anode LED. As segment to be ON is at logic "0".


	;;	bcf	PIR1,TMR2IF	; Clear the calling flag
 
 
	;;	btfss	SEG_PORT,7	;check which LED was last
	;;	goto	Do_tens
	;;	movfw	ones
	;;	andlw	0x0F		;make sure in range of table
	;;	call	LED_Table
     ;;   XORLW     0x7F        ;ADD INSTRUCTION here		
     ;;   andlw	0x7F		;set to correct LED
	;;	movwf	SEG_PORT
	;;	goto	INTX
 
;;Do_tens		movfw	tens
;;		andlw	0x0F		;make sure in range of table
;;		call	LED_Table
;;		XORLW     0x7F         ;ADD INSTRUCTION also here
;;        iorlw	0x80		;set to correct LED
;;		movwf	SEG_PORT




INTX
		movfw	p_temp
		movwf	PCLATH		; Restore PCLATH
		swapf	s_temp,W
		movwf	STATUS		; Restore STATUS register - restores bank
		swapf	w_temp,F
		swapf	w_temp,W	; Restore W register
		;
LED_Table  	ADDWF   PCL       , f
            ;	retlw   b'10001000'		;0
            ;	retlw   b'10111011'		;1
            ;	retlw   b'11000001'		;2
            ;	retlw   b'10010001'		;3
            ;	retlw   b'10110010'		;4
            ;	retlw   b'10010100'		;5
            ;	retlw   b'10000100'		;6
            ;	retlw   b'10111001'		;7
            ;	retlw   b'10000000'		;8
            ;	retlw   b'10110000'		;9
			;	retlw	b'11111111'	;blank
			;	retlw	b'11111111'	;blank
			;	retlw	b'11111111'	;blank
			;	retlw	b'11111111'	;blank
			;	retlw	b'11111111'	;blank
			;	retlw	b'11111111'	;blank
            ;use this
				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'01111100'		;6
            	retlw   b'00001111'		;7
            	retlw   b'00111111'		;8
            	retlw   b'01100111'		;9
				retlw	b'11111111'	;blank
				retlw	b'11111111'	;blank
				retlw	b'11111111'	;blank
				retlw	b'11111111'	;blank
				retlw	b'11111111'	;blank
				retlw	b'11111111'	;blank



Initialise	movlw	0x07
		movwf	CMCON			;turn comparators off (make it like a 16F84)
		bsf 	STATUS,		RP0	;select bank 1
		movlw	b'00000000'		;Set port data directions, data output
		movwf	SEG_TRIS
		bcf 	STATUS,		RP0	;select bank 0
	

		;	Set up Timer 2.
	
		;movlw	b'01111110'		; Post scale /16, pre scale /16, TMR2 ON
		;uncomment previous line, and comment next line, to slow multiplexing speed
		;so you can see the multiplexing happening
		movlw	b'00010100'		; Post scale /4, pre scale /16, TMR2 ON
		movwf	T2CON

		bsf 	STATUS,		RP0	;select bank 1

		movlw	.249			; Set up comparator
		movwf	PR2

		bsf	PIE1,TMR2IE		; Enable TMR2 interrupt

		bcf 	STATUS,		RP0	;select bank 0

		; Global interrupt enable

		bsf	INTCON,PEIE		; Enable all peripheral interrupts
		bsf	INTCON,GIE		; Global interrupt enable

		bcf 	STATUS,		RP0	;select bank 0
	
		clrf	tens
		clrf	ones

	

Main
		call	Delay255
		call	Delay255
		call	Delay255
		call	Delay255
		incf	ones,	f
		movf	ones, 	w		;test first digit
		sublw	0x0A
		btfss	STATUS, Z
		goto	Main
		clrf	ones
		incf	tens,	f
		movf	tens, 	w		;test second digit
		sublw	0x0A
		btfss	STATUS, Z
		goto	Main
		clrf	tens
		goto	Main			;loop for ever

Delay300	movlw	0x12c			;delay 300mS
		goto	d0
Delay255	movlw	0xff			;delay 255 mS
		goto	d0
Delay100	movlw	d'100'			;delay 100mS
		goto	d0
Delay50		movlw	d'50'			;delay 50mS
		goto	d0
Delay20		movlw	d'20'			;delay 20mS
		goto	d0
Delay5		movlw	0x05			;delay 5.000 ms (4 MHz clock)
d0		movwf	count1
d1		movlw	0xC7			;delay 1mS
		movwf	counta
		movlw	0x01
		movwf	countb
Delay_0
		decfsz	counta, f
		goto	$+2
		decfsz	countb, f
		goto	Delay_0

		decfsz	count1	,f
		goto	d1
		retlw	0x00

	END
 
Hi Sniper,

The code you posted in your very first post works ok for me. The only thing that has been changed is the lookup table. It works with a circuit as shown in the schematic posted earlier...

Try this original code again (assuming you are using common cathode displays).

Code:
;7 segment LED display - count up from zero
 
	LIST	p=16F877a		;tell assembler what chip we are using
	include "P16F877a.inc"		;include the defaults for the chip
	ERRORLEVEL	0,	-302	;suppress bank selection messages
 
 
 
		cblock	0x20			;start of general purpose registers
			count			;used in looping routines
			count1			;used in delay routine
			counta			;used in delay routine
			countb			;used in delay routine
			tmp1			;temporary storage
			tmp2
			p_temp			;save register
			s_temp			;save register
			w_temp			;save register
			tens			;tens storage
			ones			;ones storage
		endc
 
SEG_PORT	Equ	PORTB			;7 segment port
SEG_TRIS	Equ	TRISB
 
 
		org	0x0000
		goto	Initialise
 
;**************************************************************************	
;			     	Interrupt routine
;**************************************************************************
 
 
 
; 	Interrupt routine handles TMR2 which generates a 1ms tick
 
;	Interrupt vector
 
	ORG	0x0004
 
 
INT
		movwf	w_temp		; Save W register
		swapf	STATUS,W	; Swap status to be saved into W
		movwf	s_temp		; Save STATUS register
		movfw	PCLATH
		movwf	p_temp		; Save PCLATH 
 
		btfss	PIR1,TMR2IF	; Flag set if TMR2 interrupt
		goto	INTX		; Jump if not timed out
 
		; Timer (TMR2) timeout every 1 milli second
 
 
		bcf	PIR1,TMR2IF	; Clear the calling flag
 
 
		btfss	SEG_PORT, 7	;check which LED was last
		goto	Do_tens
		movfw	ones
		andlw	0x0F		;make sure in range of table
		call	LED_Table
		andlw	0x7F		;set to correct LED
		movwf	SEG_PORT
		goto	INTX
 
Do_tens		movfw	tens
		andlw	0x0F		;make sure in range of table
		call	LED_Table
		iorlw	0x80		;set to correct LED
		movwf	SEG_PORT
 
 
 
INTX
		movfw	p_temp
		movwf	PCLATH		; Restore PCLATH
		swapf	s_temp,W
		movwf	STATUS		; Restore STATUS register - restores bank
		swapf	w_temp,F
		swapf	w_temp,W	; Restore W register
		retfie
 
;THIS CODE WORKS OK! :-)
;THIS LOOKUP TABLE IS ALL THAT HAS BEEN CHANGED!
LED_Table  	ADDWF   PCL       , f
		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'01111100'	; 6
		retlw	b'00000111'	; 7
		retlw	b'01111111'	; 8
		retlw	b'01100111'	; 9
 
 
Initialise	movlw	0x07
		movwf	CMCON			;turn comparators off (make it like a 16F84)
		bsf 	STATUS,		RP0	;select bank 1
		movlw	b'00000000'		;Set port data directions, data output
		movwf	SEG_TRIS
		bcf 	STATUS,		RP0	;select bank 0
 
 
		;	Set up Timer 2.
 
		;movlw	b'01111110'		; Post scale /16, pre scale /16, TMR2 ON
		;uncomment previous line, and comment next line, to slow multiplexing speed
		;so you can see the multiplexing happening
		movlw	b'00010110'		; Post scale /4, pre scale /16, TMR2 ON
		movwf	T2CON
 
		bsf 	STATUS,		RP0	;select bank 1
 
		movlw	.249			; Set up comparator
		movwf	PR2
 
		bsf	PIE1,TMR2IE		; Enable TMR2 interrupt
 
		bcf 	STATUS,		RP0	;select bank 0
 
		; Global interrupt enable
 
		bsf	INTCON,PEIE		; Enable all peripheral interrupts
		bsf	INTCON,GIE		; Global interrupt enable
 
		bcf 	STATUS,		RP0	;select bank 0
 
		clrf	tens
		clrf	ones
 
 
 
Main
		call	Delay255
		call	Delay255
		call	Delay255
		call	Delay255
		incf	ones,	f
		movf	ones, 	w		;test first digit
		sublw	0x0A
		btfss	STATUS, Z
		goto	Main
		clrf	ones
		incf	tens,	f
		movf	tens, 	w		;test second digit
		sublw	0x0A
		btfss	STATUS, Z
		goto	Main
		clrf	tens
		goto	Main			;loop for ever
 
 
Delay255	movlw	0xff			;delay 255 mS
		goto	d0
Delay100	movlw	d'100'			;delay 100mS
		goto	d0
Delay50		movlw	d'50'			;delay 50mS
		goto	d0
Delay20		movlw	d'20'			;delay 20mS
		goto	d0
Delay5		movlw	0x05			;delay 5.000 ms (4 MHz clock)
d0		movwf	count1
d1		movlw	0xC7			;delay 1mS
		movwf	counta
		movlw	0x01
		movwf	countb
Delay_0
		decfsz	counta, f
		goto	$+2
		decfsz	countb, f
		goto	Delay_0
 
		decfsz	count1	,f
		goto	d1
		retlw	0x00
 
	END
 
Are you using a Pickit to program your chip? Are you removing the pickit from the circuit after programming? You will not be able to run your hardware properly in debug mode i.e. using the pickit as a debug tool.

Edit: Just to clarify. Because the in-circuit-debugging pins (39 & 40) are connected to the led segments and the base of one of the transistors.

Also, although not related to the problems you are having, it's not necessary to set up the CMCON register. The comparators are turned off by default upon a reset.

**broken link removed**
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top