help using tables

Status
Not open for further replies.

josi

New Member
hi! i'm tring to display numbers on a 7 segment (common anode) using a pic 16f877, the code is not workin as the desplay is showin 1 segment at the time and not the numbers! can an1 help? thanx

Code:
LIST	     p=16F877		                    
#include    <P16F877.inc>		                
__config    _XT_OSC & _PWRTE_ON & _WDT_OFF &_LVP_OFF   
;*****Equates**********************************************
status         equ 03h
PortA          equ 05h
PortD          equ 08h
TRISA         equ 85h
TRISD         equ 88h
count0        equ 25h
count1        equ 26h
count2        equ 27h
digit           equ 28h
pcl             equ 02h
;******Main Program****************************************
org             00h   
bsf             status,RP0        
movlw         B'11111111'       ; set portA as input
movwf        TRISA
movlw         B'00000000'       ; set PortD as outpout
movwf        TRISD
movlw	    D'6'
movwf	    ADCON1
bcf             status,RP0
clrf             digit 

start
call	  Table
movwf	  PortD
call	  delay
incf	  digit,f
movfw	  digit
xorlw	  d'10'
btfsc	  status,Z
clrf	  digit
goto	  start

    
Table
addwf      pcl,f

retlw       b'00000000'     ;display 0      

retlw       b'00000000'
   
retlw       b'11010111'
  
retlw       b'11011101'
  
retlw       b'10111001'
 
retlw       b'01111101'
 
retlw       b'00111111'
 
retlw       b'11001001'
 
retlw       b'11111111'         ;display 8
 
retlw       b'11101111'         ;display 9
 
  


delay
movlw     03h            
Movwf     count2

Loop    
decfsz     count0,1

Goto       Loop
Decfsz    count1,1
Goto       Loop
Decfsz    count2,1
Goto       Loop
Return 
end
 
Last edited:
What will W contain when you call Table? I think maybe you want movfw digit before the call.

Mike.
 
w will have the content of the table! when i use "retlw k" isn't is returning k in w register?
 
w will have the content of the table! when i use "retlw k" isn't is returning k in w register?

You have to load the index value in W before you call the table, or how does it know where to read from?.

If you check my tutorials there are many examples of using tables.
 
hi,
Look at this version of your code, it works in simulation.

I have assumed that the lower 7 bits on PORTD are the segments and the pin is high to light the segment.

76543210
.gfedcba weighting.

OK.

BTW: its not neccesary to define these PIC registers
Code:
status	equ	03h
PortA	equ	05h
PortD	equ	08h
TRISA	equ	85h
TRISD	equ	88h

Code:
	list	p=16F877		                    
#include <P16F877.inc>  
	__config _XT_OSC & _PWRTE_ON & _WDT_OFF &_LVP_OFF   

         errorlevel -302, -207


;NOTE: using PORTD as LED output
;.gfedcba
;76543210 pins on D
;High 1 = segement lit



;*****Equates**********************************************  
status	equ	03h
PortA	equ	05h
PortD	equ	08h
TRISA	equ	85h
TRISD	equ	88h
count0	equ	25h
count1	equ	26h
count2	equ	27h
digit	equ	28h
pcl	equ	02h
;******Main Program****************************************  
	org	00h   
	bsf	status,RP0        
	movlw	B'11111111'	; set portA as input
	movwf	TRISA
	movlw	B'00000000'	; set PortD as outpout
	movwf	TRISD
	movlw	D'6'
	movwf	ADCON1
	bcf	status,RP0
 

main:
	movlw 0x0a
	movwf count1

	movlw 0x00
	movwf digit

start
	movf digit,w

	call	Table
	movwf	PortD
	call	delay
	incf	digit,f
	
	decfsz count1,F
	goto 	start
	goto	main

		 
Table
	addwf	pcl,f

	retlw	b'00111111'		;b'00000000'	;display 0      

	retlw	b'00000110'		;b'00000000'
		 
	retlw	b'01011011'		;b'11010111'
		 
	retlw	b'01001111'		;b'11011101'
		 
	retlw	b'01100110'		;b'10111001'
		 
	retlw	b'01101101'		;b'01111101'
		 
	retlw	b'01111100'		;b'00111111'
		 
	retlw	b'00000111'		;b'11001001'
		 
	retlw	b'01111111'		;b'11111111'	;display 8
		 
	retlw	b'01101111'		;'b'11101111'	;display 9
		 
		 


delay	
	movlw	03h            
	movwf	count2

Loop		 
	decfsz	count0,1

	goto	Loop
	decfsz	count1,1
	goto	Loop
	decfsz	count2,1
	goto	Loop
	return	 
	end
 
Last edited:
thans eric! i'll try to load the code in the pic

The program is modified for a common cathode 7 seg LED, you must wire the same pins to get the correct character pattern.

If you use a common anode 7seg, invert the bit pattern in the Table.
 
The code is still displying one segment at the time! even when i comment the table out! i don't understand......
 
Hi,

Does anyone notice count1 GPR is used twice in this code? This is the problem because count1 will have 0 when returned from the delay sub routine, and in the Start loop, count1 will never reach 0 and digit GPR will not be clear, end up it calls out of the table, crash?
 
hi josi,
Picking up on the 'bananasiong' catch.!

Allocated digcnt as a register for the digit counter.
Added code protect OFF in the CONFIG.

The program drives the 7seg LED in the Oshonsoft Sim OK.
If you still have a problem post the circuit drawing.



Code:
	list	p=16F877, f =inhX32, x=off           
	#include <P16F877.inc>  

 	__CONFIG _CP_OFF & _XT_OSC & _PWRTE_ON  & _WDT_OFF & _LVP_OFF

         errorlevel -302, -207


;NOTE: using PORTD as LED output
;.gfedcba
;76543210 pins on D
;High 1 = segement lit



;*****Equates**********************************************  
count0	equ	25h
count1	equ	26h
count2	equ	27h
digit	equ	28h
digcnt  equ	29h
;******Main Program****************************************  
	org	00h   
	bsf	STATUS,RP0        
	movlw	B'11111111'	; set portA as input
	movwf	TRISA
	movlw	B'00000000'	; set PortD as outpout
	movwf	TRISD
	movlw	D'6'
	movwf	ADCON1
	bcf	STATUS,RP0
 

main:
	movlw 0x0a
	movwf digcnt

	movlw 0x00
	movwf digit

start
	movf digit,w

	call	Table
	movwf	PORTD
	call	delay
	incf	digit,f
	
	decfsz digcnt,F
	goto 	start
	goto	main

		 
Table
	addwf	PCL,f

	retlw	b'00111111'		;b'00000000'	;display 0      

	retlw	b'00000110'		;b'00000000'
		 
	retlw	b'01011011'		;b'11010111'
		 
	retlw	b'01001111'		;b'11011101'
		 
	retlw	b'01100110'		;b'10111001'
		 
	retlw	b'01101101'		;b'01111101'
		 
	retlw	b'01111100'		;b'00111111'
		 
	retlw	b'00000111'		;b'11001001'
		 
	retlw	b'01111111'		;b'11111111'	;display 8
		 
	retlw	b'01101111'		;'b'11101111'	;display 9
		 
		 


delay	 
	movlw	03h            
	movwf	count2

Loop		 
	decfsz	count0,1

	goto	Loop
	decfsz	count1,1
	goto	Loop
	decfsz	count2,1
	goto	Loop
	return	 
	end

bananasiong:
This is one of the 'problems' with Sims, I placed a RETURN at delay call in order to run the Sim faster. Of course count1 reg was never used.
 
Last edited:
Yeah The simulator doesn't run in actual speed, this is the bad part of it.

Normally I use conditional assembly, which tests for a Bit I set/reset in the program header.
If the bit is set, then its Sim, so the long delay routines are dropped.

Have you tried the 30day trial version of the Oshon Software Homepage PIC Sim.

Providing you are aware of the limitations of a Sim its an excellent tool for debugging.
 
hi
i was wondering how i could go about adapting the code to drive a four digit 7 segment display...thanks
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…