Continue to Site

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.

how can I create 100 counter

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Visit site
Activity points
9,442
Guys,

I have a code like this :
Code:
	;....... 1ms  timer counter here.....
		                 clrf TMR0 ;Clear TMR0 and prescaler
				 banksel TMR0
				 movlw 0xB2 ;Count=B2 
				 movwf TMR0
		;=======================================

		
       	

		;....................................

	         incf COUNT, f
		movlw  .10		;Compare count to 10 decimal
		subwf COUNT, W		;Store the result back in 
		btfss STATUS, C		;Is the result 0, skip if no
		goto exittimer

How can I create a counter so I will have 1ms timer ?
thanks
 

I don't know PIC assembly but i know 8051 assembly in which we assign register say R0 = 10 then is ISR we decrement this R0 each time till Zero when it zero then set flag = 1 for 10ms like this u can do it.
 

First take the clk speed, find number of machine cycles per instruction, write conditional loop for n times...
Now calculate the time taken for executing loop for one time. this is T1
now find number of times to execute..
put the value as decrementable register. Thats all.
 

First take the clk speed, find number of machine cycles per instruction, write conditional loop for n times...
Now calculate the time taken for executing loop for one time. this is T1
now find number of times to execute..
put the value as decrementable register. Thats all.
can you show me the code ?

- - - Updated - - -

I don't know PIC assembly but i know 8051 assembly in which we assign register say R0 = 10 then is ISR we decrement this R0 each time till Zero when it zero then set flag = 1 for 10ms like this u can do it.
that's allright, can you show me the code ?
thanks
 

HERE1: MOV R3,#100
HERE2: MOV R4,#100
HERE3: DJNZ R4,HERE3
DJNZ R3,HERE2
DJNZ R2,HERE1
RET

You can understand how its works.... Just put the values according to your need and that is from 8051...

DJNZ - Decrement jump if not zero..
until zero it continues.... so 100 * 100 times of 3 instructions having 3 cycles each...

- - - Updated - - -

https://www.edaboard.com/attachments/79894d1347137887-delay-loop.png
 

HERE1: MOV R3,#100
HERE2: MOV R4,#100
HERE3: DJNZ R4,HERE3
DJNZ R3,HERE2
DJNZ R2,HERE1
RET


You can understand how its works.... Just put the values according to your need and that is from 8051...

DJNZ - Decrement jump if not zero..
until zero it continues.... so 100 * 100 times of 3 instructions having 3 cycles each...

- - - Updated - - -

how about using decrement ?

- - - Updated - - -

HERE1: MOV R3,#100
HERE2: MOV R4,#100
HERE3: DJNZ R4,HERE3
DJNZ R3,HERE2
DJNZ R2,HERE1
RET

You can understand how its works.... Just put the values according to your need and that is from 8051...

DJNZ - Decrement jump if not zero..
until zero it continues.... so 100 * 100 times of 3 instructions having 3 cycles each...

- - - Updated - - -

https://www.edaboard.com/attachments/79894d1347137887-delay-loop.png

how about using decrement ? and put the starting counter in memory ?
 

can I get the counter like this ?
Code:
	timer100ms
              movlw 0x64
			  movwf counter100ms
			  decfsz counter100ms
              return

- - - Updated - - -

here's the code, my question is, how can I create a counter for 10ms ?
Code:
;------------------------------------------------------------------------------
BANK0	UDATA
;------------------------------------------------------------------------------
COUNT		RES	1	;COUNT OF INTERRUPTS
COUNT1		RES	1	;COUNT FOR  1 SECOND TIMER



;------------------------------------------------------------------------------
PROG0		CODE			; program code section
;------------------------------------------------------------------------------
TimerInt 	; Interrupt routine

		global TimerInt		; Define as global 

intsOff		bcf INTCON, T0IE	;Disable timer interrupts
	
;-------------- Insert no new code above here ---------------------------------
		CBLOCK 0x30 ; RAM AREA for USE at address 20h
			; Variables used in delay routine
			
			counter1ms    ; counter for 1ms delay
			counter10ms	; counter for 10ms delay
			counter100ms  ; counter for 100ms delay
			counter1000ms ; counter for 1000ms delay
		    ;Variables
		    
		ENDC	
		;*********************************************************************
		; 1 ms timer function calls
		;*********************************************************************
		bcf  STATUS, RP0		;Select Bank 0
		bcf  STATUS, RP1	

		;....... 1ms  timer counter here.....		
		
				 clrf TMR0 ;Clear TMR0 and prescaler
				 banksel TMR0
				 movlw 0xB2 ;Count=B2 
				 movwf TMR0
			
       	

		;....................................

	    incf COUNT, f
		movlw  .10		;Compare count to 10 decimal
		subwf COUNT, W		;Store the result back in 
		btfss STATUS, C		;Is the result 0, skip if no
		goto exittimer

		clrf COUNT		;Clear count register
        call timer100ms
		;**********************************************************************
		; 10ms timer function calls
		;**********************************************************************
		;....... 10ms  timer counter here.....	
        timer10ms
              movlw 0x0A
			  movwf counter10ms
			  decfsz counter10ms
              return 
              	
        		
		;....................................

		incf COUNT1, f
		movlw  .10		;Compare count to 100 decimal
		subwf COUNT1, W		;Store the result back in 
		btfss STATUS, C		;Is the result 0, skip if no
		goto exittimer
		clrf 	COUNT1
		;**********************************************************************
		; 100ms timer function calls
		;**********************************************************************
		;....... 100ms  timer counter here.....		
		timer100ms
              movlw 0x64
			  movwf counter100ms
			  decfsz counter100ms
              return 
		;....................................
        

exittimer       
		movlw 	.217		;count 14
		movwf 	TMR0
		
		banksel TMR0
		movlw 0xB2 ;Count=B2 
		movwf TMR0

        
;-------------- Insert no new code below here ---------------------------------
		bcf INTCON, T0IF	;Clear timer overflow flag
;		bsf INTCON, T0IE	;Enable interrupts again
		return
;----------------------------------------------------------------------------------
;**********************************************************************************
initTimer
 Global	initTimer
	GLOBAL 	COUNT
	clrf	COUNT
	clrf	COUNT1
	
	return



	end

- - - Updated - - -

If u don't know logic as well as programming then don't ask all ready u must try something then ask show me ?
is my question clear for you now ?
 

10 ms Delay for 4 MHz Clock.


Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
; Delay = 0.01 seconds
; Clock frequency = 4 MHz
 
; Actual delay = 0.01 seconds = 10ms = 10000 cycles
; Error = 0 %
 
        cblock
        d1
        d2
        endc
 
                        ;9998 cycles
        movlw   0xCF
        movwf   d1
        movlw   0x08
        movwf   d2
Delay_0
        decfsz  d1, f
        goto    L1
        decfsz  d2, f
L1      goto    Delay_0
 
                        ;2 cycles
        
        return

 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top