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.

I want to use timer0 for a lot of timer

Status
Not open for further replies.

bragas

Newbie level 5
Joined
Sep 29, 2010
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,354
hello.At firstly I'm sorry for my terrible english:cry:.I design a circuit for car body control unit(BSI).I will control windsheld wipers(with rain sensor),car ceiling lamp,headlights,central locking etc. automatically with PIC16F877A.But ı have some problems about lightening system.I can use timer0 or timer2.For example,when you open the car's door,ceiling lamp will ON continuous.then you close the door,lamp will OFF 12 seconds later. I wrote asm code for 12 seconds.It works.NO problem.but I want that:After central locking is ON ,ceiling lamp will ON during 5 seconds.then It will OFF.And headlights will ON 20 seconds.How can I design algoritm and write asm code for this scenario.

I opened the door =>> ceiling lamp:ON
I closed the door =>> ceiling lamp:in normally will OFF 12 seconds later.But After 4 seconds from I closed door,I locked car.the ceiling lamp will OFF 5 seconds later.In totally,lamp will be OFF ,after 9 seconds.Also headlights will ON during 20 seconds.then it will OFF.

my asm code for 12 seconds: (ı must add 5 seconds and 20 seconds for central locking).

Code:
LIST	P=16F877A
INCLUDE	"P16F877A.INC"
__CONFIG    H'3F31'

COUNT_60mS	EQU	h'20' 
	ORG	h'000'	
	GOTO	START	
	ORG	h'004'		
	GOTO	INTERRUPT	


START
	CLRF PORTB
	BANKSEL	TRISB		 
	CLRF TRISB  ;B0 is ceiling lamp,B1 is head lights
	MOVLW b'11111111'
	MOVWF  TRISD	;D0 is door and D1 is central locking	
	MOVLW	b'11010111'	; T0CS=0, PSA=0, PRSC=1/256
	MOVWF	OPTION_REG	; 
	BANKSEL	PORTB		; BANK0
	BSF	INTCON,7	

;CEILING LAMP CONTROL
START1
    BTFSS PORTD,0 
	CALL TIMER
	BTFSS PORTD,0
	GOTO START1
	BCF INTCON,5
	BSF PORTB,0
	BCF INTCON,5
	GOTO START1



TIMER
BSF INTCON,5
RETURN

; after 12 seconds , ceiling lamp will turn off
INTERRUPT		
 
	BCF	INTCON,2	
	MOVLW	D'10'	
        MOVWF	TMR0	
	INCF	COUNT_60mS,F	
	MOVLW   D'200'   ; 60mS*200=12000mS=12 seconds
   	SUBWF	COUNT_60mS,W	
	BTFSS	STATUS,Z	
	GOTO	INT_J1	
	CLRF	COUNT_60mS	
	CLRF PORTB

INT_J1
RETFIE

END
 

The method I use when several timers are needed is to use TMR0 or TMR1 to generate an interrupt at the period needed. If necessary, use a register to count interrupts if a longer period is needed. Then do this at each period. I leave it to you to convert it to assembler because it is so easy:

check if timer_a = 0. If it is, skip the next step
decrement timer_a
check if timer_b = 0. If it is, skip the next step
decrement timer_b
.
.
check if timer_z = 0. If it is return from the interrupt
decrement timer_z.
return from the interrupt.

If you do it this way you can have as many timers as you like and they all count down to zero and stop there. Supposing the period was 1 second, you could load timer_a with 6 and timer_b with 22 to get that many seconds delay. Then in your main loop all you do is check if they are zero to know if the period has elapsed. Timer_a will reach zero after 6 seconds and timer_b in 22 seconds. Remember that in PIC assembler, it is very easy to check if a register is zero or not, just use (example) "movf timer_a,f" and check if the Z flag has set or not.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top