Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] how to make counter from outer signsal on pic 16f877a assembly

Status
Not open for further replies.

imnimn

Junior Member level 3
Joined
Jul 19, 2011
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
1,481
i wrote this code and i want you to help me if there somehting wrong in it

Code:
include "p16f877.inc"
org 0x00
goto Initialize








T0_delay: movlw 0x28;0x21
banksel TRISC
movwf OPTION_REG
banksel TMR0

;movlw 0x6a
;movwf TMR0
clrf TMR0
banksel INTCON
chIF:btfss INTCON,2
goto chIF
return

display: call Table
 banksel PORTC
movwf PORTC
return 

sec_delay: movlw 0xc9; 
 banksel temp
movwf temp
calling: call T0_delay 
clrf INTCON 
decfsz temp
goto calling
return 

Table 
 MOVF digits,W ; Get count
ADDWF PCL ;
RETLW B'00111111' ; Code for '0'
RETLW B'00000110' ; Code for '1'
RETLW B'01011011' ; Code for '2'
RETLW B'01001111' ; Code for '3'
RETLW B'01100110' ; Code for '4'
RETLW B'01101101' ; Code for '5'
RETLW B'01111101' ; Code for '6'
RETLW B'00000111' ; Code for '7'
RETLW B'01111111' ; Code for '8'
RETLW B'01101111' ; Code for '9'
RETLW B'01110111' ; Codefor 'A'
RETLW B'01111100' ; Code for'B'
RETLW B'00111001' ; Code for C'
RETLW B'01011110' ; Code for D'
RETLW B'01111001' ; Code for E'
RETLW B'01110001' ; Code for 'F'




org 0x300

Initialize:clrf digits 
banksel TRISC 
movlw 0x00
movwf TRISC

banksel TRISA
movlw 0x1f
Movwf TRISA	


temp equ 0x19
Digits equ 0x20
#DEFINE rst PORTD,'1'
#DEFINE step PORTD,'0'
temp equ 0x19
digits equ 0x20


banksel PORTC
call display 

chrst: btfss rst
goto ckstep
goto Initialize

ckstep:btfss step 
goto chrst

incf Digits
call display 
call sec_delay
 banksel INTCON
clrf INTCON
banksel PORTC
goto chrst

end
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
Hi,

Have corrected your code so it builds ok, have added some comments about things you need to correct.

Have not looked at your logic flow as need more info on what you are trying to do, a circuit diagram would also help.



Code:
	include "p16f877.inc"    ; USUALLY FIND THE 877A CHIP IS THE ONE TO BUY

;							 NORMALLY HAVE A 'CONFIGURE' STATEMENT HERE
;						     MOST IMPORTANTLY DEPENDS ON WHAT OSCILLATOR / FREQUENCY YOU ARE USING
	org 0x00
	goto Initialize
				
	
	
	
	
	
	
	
	
T0_delay: movlw 0x28;0x21
	banksel TRISC
	movwf OPTION_REG
	banksel TMR0
	
	;movlw 0x6a
	;movwf TMR0
	clrf TMR0
	banksel INTCON
chIF:btfss INTCON,2
	goto chIF
	return
	
display: call Table
	 banksel PORTC
	movwf PORTC
	return 
	
sec_delay: movlw 0xc9; 
	 banksel temp
	movwf temp
calling: call T0_delay 
	clrf INTCON 
	decfsz temp
	goto calling
	return 
	
Table 
	 MOVF digits,W ; Get count
	ADDWF PCL ;
	RETLW B'00111111' ; Code for '0'
	RETLW B'00000110' ; Code for '1'
	RETLW B'01011011' ; Code for '2'
	RETLW B'01001111' ; Code for '3'
	RETLW B'01100110' ; Code for '4'
	RETLW B'01101101' ; Code for '5'
	RETLW B'01111101' ; Code for '6'
	RETLW B'00000111' ; Code for '7'
	RETLW B'01111111' ; Code for '8'
	RETLW B'01101111' ; Code for '9'
	RETLW B'01110111' ; Codefor 'A'
	RETLW B'01111100' ; Code for'B'
	RETLW B'00111001' ; Code for C'
	RETLW B'01011110' ; Code for D'
	RETLW B'01111001' ; Code for E'
	RETLW B'01110001' ; Code for 'F'
	
	
	
	
	org 0x300
	
Initialize:clrf digits 		; WHAT ABOUT PORT D??

;                           SET ALL PORTS UP CORRECTLY  HERE
;							ENSURE THEY ARE SET TO DIGITAL, NOT THE DEFAULT ANALOGUE
;							SEE THE DATA SHEET SECTION - PORTA
	banksel TRISC 
	movlw 0x00
	movwf TRISC
	
	banksel TRISA
	movlw 0x1f
	Movwf TRISA	
	
	
temp equ 0x19				; YOU ARE USING HEX VALUES HERE  19 IS FOLLOWED BY 1A NOT 20 AS IN DECIMAL
							; ALSO USER MEMORY, RAM, STARTS AT HEX 20, YOU HAVE OVERWRITTEN A SYSTEM REGISTER AT LOCATION H 19
Digits equ 0x20
#DEFINE rst PORTD,1			; DEFINE THE PORT BIT LIKE THIS
#DEFINE step PORTD,0
temp equ 0x19
digits equ 0x20
	
	
	banksel PORTC
	call display 
	
chrst: btfss rst
	goto ckstep
	goto Initialize
	
ckstep:btfss step 
	goto chrst
	
	incf Digits
	call display 
	call sec_delay
	 banksel INTCON
	clrf INTCON
	banksel PORTC
	goto chrst
	
	end
 
  • Like
Reactions: imnimn

    imnimn

    Points: 2
    Helpful Answer Positive Rating

imnimn

Junior Member level 3
Joined
Jul 19, 2011
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
1,481
I use p16f877a but I forgot to write that in include "p16f877.inc"
I'll use 100khz
"ENSURE THEY ARE SET TO DIGITAL, NOT THE DEFAULT ANALOGUE"
I don't understand what do you mean in this statement .
I'm trying to make a counter count the secounds pass
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
I use p16f877a but I forgot to write that in include "p16f877.inc"
I'll use 100khz
"ENSURE THEY ARE SET TO DIGITAL, NOT THE DEFAULT ANALOGUE"
I don't understand what do you mean in this statement .
I'm trying to make a counter count the secounds pass

You need to set your configuratation bits for the 100khz signal your are using, is it a crystal or r/c ?
See the datasheet section - Special Features, Configuation and Oscillator sub sections for an explanation then choose the appropriate bit in the config option - see example code .

When the chip powers on certain ports, mainly PortA are set to Analogue Inputs, to use them as Digital ports you need to turn off the Analogue function - the datasheet section I/O Ports A explains this.
It does not affect you actual port usage at the moment.
Similary you have not configured PortD which you are using, fortunately that defaults to Digital Input which you are using them as - but its better to set them all up properly.

You use of TM0 for a little delay, that is one way, but it can be a little confusing to start with - the code example below details how to use it.
A much simpler was is to use a software delay , you can get it calculated and coded for you at this site.
https://www.golovchenko.org/cgi-bin/delay

So all you do is Call Delay1Sec


Code:
; Delay = 1 seconds
; Clock frequency = 0.1 MHz

; Actual delay = 1 seconds = 25000 cycles
; Error = 0 %

	cblock 0x20b    ; another way of specifying your registers
	d1
	d2
	endc


Delay1Sec:
			;24998 cycles
	movlw	0x87
	movwf	d1
	movlw	0x14
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay_0

			;2 cycles
	goto	$+1
        RETURN


Finally be aware that there is a setting in MPlab that makes your code Capitals Case sensitive - noticed that you have coded ' digit' and also 'Digit'

Timer0 delay routine with a simple Config line that you can use in your 877A code, if you select the right type of OSC XT or RC

Code:
		list p=16f84a
		#include p16f84a.inc


		__CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC

		; OSCILLATOR  RC set to 800khz





		cblock	0x0C		; start location of user Registers

		D1,D2,D3			; Delay work files
		COUNTER				; to count Timer0

		endc
	

		org 	0x00			; first location of program memory
		goto	start

		org		0x04			; location of where the program jumps to when an Interrupt occurs
		retfie					; not used here
	

start 	bsf 	STATUS, 5		; set up the I/O Ports etc.

		movlw 	B'00000000' 	; PortA as Outputs
		movwf	TRISA
		movlw	B'00001000'		; PortB, bit 3 as input, rest as output
		movwf	TRISB

		movlw	B'00000111'  	; set up timer0 , bits 0-2 prescaler 1:256,  bit3 prescaler to Timer 0, bit5  using main internal clock 800khz/4 = 200khz !
		movwf	OPTION_REG
		bcf  	STATUS, 5		; Timer 0 is now running !

		clrf	PORTA			; turn off all ports
		clrf	PORTB
		clrf	COUNTER	

								; this routine counts on TMR0 then turns on a LED, 
								; it then does another TMR0 count and turns off the LED
								; it then loops back to the begining.

								; the timing formula for TMR0
								; oscillator 800k / 4 = 200k processor cycle.
								; timer0 prescaler 200k / 256 = 781 hz
								; timer0 counter   781 / 256  = 3 hz  or  .33 sec 
	

again	clrf	TMR0			; clear Timer0 register so it starts counting from 0
	
		bcf		INTCON,T0IF		; Clear the TNR0 full Flag, When its full it SETS its Flag to let the system know the count is up.

tm0wait1 						; just wait for the Timer to count up
		btfss	INTCON,T0IF		; test to see if Tmr0 is full
		goto	tm0wait1		; no loop around
		
		bsf		PORTB,4			; turn on led

		clrf	TMR0			; clear Timer0 register so it starts counting from 0
		bcf		INTCON,T0IF		; Clear the TNR0 full Flag, When its full it SETS its Flag to let the system know the count is up.
tm0wait2						; just wait for the Timer to count up
		btfss	INTCON,T0IF		; test to see if Tmr0 is full
		goto	tm0wait2		; no loop around

		bcf		PORTB,4			; turn off led
 
  • Like
Reactions: imnimn

    imnimn

    Points: 2
    Helpful Answer Positive Rating

imnimn

Junior Member level 3
Joined
Jul 19, 2011
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
1,481
thank you very much
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top