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.

[General] different task performed at every push of one button

Status
Not open for further replies.
hi, i wanted to know if my INITIAL loop is wrong method to check how that how many times the button has been pressed?

- - - Updated - - -

Hi Brian,

I beleive that I am already using timer0 for the delays in blinking. as far as the interrupt check is concerned, I want it to be time independent. therefore, I am not sure why I should use the timer for it

- - - Updated - - -

Hi,

try to set up a timer for an interrupt every about 0.25s
Follow: Timer 1 Mode - Interrupt method
https://exploreembedded.com/wiki/5.8051_Timer_programming

Use one variable/register "LED_mode":
0 = LED OFF
1 = LED blink 1Hz
2 = LED blink 2Hz

***
ISR: (pseudocode)
If LED_mode = 0 then switch OFF LED
if LED_mode = 1 then use a counter/flag and toggle LED every second interrupt
If LED_mode = 2 then toggle LED (every interrupt)
reset interrupt flags
RETI

Klaus


but i dont want it to be time dependent. I want it to work as a counter i.e, for example if I have stored 2 in R0. Each time the button is pressed it should dec the register by one and compare to the given value. i have tried to do the similar thing in my "INITIAL" loop. However, there is some mistake in it due to which it serialy transmits both "ON" & "OFF".
 

Hi,

but i dont want it to be time dependent. I want it to work as a counter i.e, for example if I have stored 2 in R0. Each time the button is pressed it should dec the register by one and compare to the given value. i have tried to do the similar thing in my "INITIAL" loop. However, there is some mistake in it due to which it serialy transmits both "ON" & "OFF".
What code are you talking about?
My pseudo code is not time dependent.
It has nothing to do with key press.
It just handles the LED control (blink 1Hz, blink 2Hz, Led Off in ISR, with about no processing power)
****
You may handle the key press and decide what blink rate to use in main.

Klaus
 

i was talking about my code
 

Despite trying it aint working and i am still stuck at the same interrupt conditional count
 

Hi,

i was talking about my code
Despite trying it aint working and i am still stuck at the same interrupt conditional count
Where is your interrupt code?
We need it to be able to discuss about it.

Klaus
 

Code:
ORG 000H
SJMP CHECK;

org 003H;
	Acall INITIAL;
	reti
ORG 000BH;                     interrupt TIIMER routine
		ajmp timer
		
ORG 0030H

CHECK:
MOV TMOD,#29H;                ;TR1 M2 / TM0-CT0 M1   
MOV IE,#10000011B; 
MOV TH1,#0FDH;	              ;9600BAUD RATE, ,8-BIT,1-STOP BIT;;;;;;;;;;;;;;
MOV SCON,#50H                 ;01010000b
SETB TI;                      ; TRANSMIT BIT HIGH, TRANSMITS WHEN LOW
setb TR1;

loop:        
MOV R0,2                      ;LOADING THE VALUE FOR THE INTERNAL INTERRUPT COUNTER
led4:
		clr p1.4
		setb p1.5;
		mov R3,#10;
		JNB INT0,INITIAL;
		BL:
		MOV R4,#20;             ;LOOP FOR REPEATING COUNT
		LRPT:                   ;MAX DELAY
		MOV TL1,#0H;
        MOV TH1,#0H;
		SETB TR1;
		LAGAIN: 
		JNB TF1, LAGAIN;
		CLR TR1;
		CLR TF1;
		DJNZ R4,LRPT;
		CPL P1.4;
		DJNZ R3,BL;
		ret;

[B][COLOR="#FF0000"]INITIAL:              This is where the interrupt is being checked  
dec R0
cjne @R0,#0,ONloop
MOV A,R0
JNZ OFFloop;
ret[/COLOR][/B]

ONloop:
ACALL DISPLAY_ON
		LOP:
		clr p1.5;
		setb p1.4
		mov R5,#4;
		BLINK:
		MOV R6,#30;              LOOP FOR REPEATING COUNT
		RPT:                   ;2 sec DELAY
		MOV TL0,#0H;
        MOV TH0,#0H;
		SETB TR0;
		AGAIN: 
		JNB TF0, AGAIN;
		CLR TR0;
		CLR TF0;
		DJNZ R6,RPT;
		CPL P1.5;
		DJNZ R5,BLINK;
		reti

TRANS:
MOV SBUF, A;
JNB TI,$
CLR TI
ret

OFFloop:
ACALL DISPLAY_OFF
LOP2:
		clr p1.5;
		setb p1.4
		mov R5,#4;
		BLINK2:
		MOV R6,#30;              LOOP FOR REPEATING COUNT
		RPT2:                   ;2 sec DELAY
		MOV TL0,#0H;
        MOV TH0,#0H;
		SETB TR0;
		AGAIN2: 
		JNB TF0, AGAIN2;
		CLR TR0;
		CLR TF0;
		DJNZ R6,RPT2;
		CPL P1.5;
		DJNZ R5,BLINK2;
		RET                           ; interrupt routine doesnt return to the loop. it turns the led on constantly after blinking it 4 times

DISPLAY_OFF:
setb p1.4
MOV DPTR,#MS1;
MOV R2,4
dis1:
CLR A
MOVC A,@A+DPTR;
acall TRANS
INC DPTR;
DJNZ R2, dis1
ret
		
DISPLAY_ON:                   ;displays ON
setb p1.4
MOV DPTR,#MS;
MOV R7,2
dis:
CLR A
MOVC A,@A+DPTR;
acall TRANS
INC DPTR;
DJNZ R7, dis
ret

timer:
        PUSH ACC;
		SETB P1.4;
		SETB P1.5;
		POP ACC;
		RETI;
		
MS:DB 13,10,"ON",10,13,0
MS1:DB 13,10,"OFF",10,13,0

END
- - - Updated - - -

Code:
ORG 000H
SJMP CHECK;

org 003H;
	Acall INITIAL;
	reti
ORG 000BH;                     interrupt TIIMER routine
		ajmp timer
		
ORG 0030H

CHECK:
MOV TMOD,#29H;                ;TR1 M2 / TM0-CT0 M1   
MOV IE,#10000011B; 
MOV TH1,#0FDH;	              ;9600BAUD RATE, ,8-BIT,1-STOP BIT;;;;;;;;;;;;;;
MOV SCON,#50H                 ;01010000b
SETB TI;                      ; TRANSMIT BIT HIGH, TRANSMITS WHEN LOW
setb TR1;

loop:        
MOV R0,2                      ;LOADING THE VALUE FOR THE INTERNAL INTERRUPT COUNTER
led4:
		clr p1.4
		setb p1.5;
		mov R3,#10;
		JNB INT0,INITIAL;
		BL:
		MOV R4,#20;             ;LOOP FOR REPEATING COUNT
		LRPT:                   ;MAX DELAY
		MOV TL1,#0H;
        MOV TH1,#0H;
		SETB TR1;
		LAGAIN: 
		JNB TF1, LAGAIN;
		CLR TR1;
		CLR TF1;
		DJNZ R4,LRPT;
		CPL P1.4;
		DJNZ R3,BL;
		ret;

[B][COLOR="#FF0000"]INITIAL:              This is where the interrupt is being checked  
dec R0
cjne @R0,#0,ONloop
MOV A,R0
JNZ OFFloop;
ret[/COLOR][/B]

ONloop:
ACALL DISPLAY_ON
		LOP:
		clr p1.5;
		setb p1.4
		mov R5,#4;
		BLINK:
		MOV R6,#30;              LOOP FOR REPEATING COUNT
		RPT:                   ;2 sec DELAY
		MOV TL0,#0H;
        MOV TH0,#0H;
		SETB TR0;
		AGAIN: 
		JNB TF0, AGAIN;
		CLR TR0;
		CLR TF0;
		DJNZ R6,RPT;
		CPL P1.5;
		DJNZ R5,BLINK;
		reti

TRANS:
MOV SBUF, A;
JNB TI,$
CLR TI
ret

OFFloop:
ACALL DISPLAY_OFF
LOP2:
		clr p1.5;
		setb p1.4
		mov R5,#4;
		BLINK2:
		MOV R6,#30;              LOOP FOR REPEATING COUNT
		RPT2:                   ;2 sec DELAY
		MOV TL0,#0H;
        MOV TH0,#0H;
		SETB TR0;
		AGAIN2: 
		JNB TF0, AGAIN2;
		CLR TR0;
		CLR TF0;
		DJNZ R6,RPT2;
		CPL P1.5;
		DJNZ R5,BLINK2;
		RET                           ; interrupt routine doesnt return to the loop. it turns the led on constantly after blinking it 4 times

DISPLAY_OFF:
setb p1.4
MOV DPTR,#MS1;
MOV R2,4
dis1:
CLR A
MOVC A,@A+DPTR;
acall TRANS
INC DPTR;
DJNZ R2, dis1
ret
		
DISPLAY_ON:                   ;displays ON
setb p1.4
MOV DPTR,#MS;
MOV R7,2
dis:
CLR A
MOVC A,@A+DPTR;
acall TRANS
INC DPTR;
DJNZ R7, dis
ret

timer:
        PUSH ACC;
		SETB P1.4;
		SETB P1.5;
		POP ACC;
		RETI;
		
MS:DB 13,10,"ON",10,13,0
MS1:DB 13,10,"OFF",10,13,0

END
 
Last edited by a moderator:

Hi,

I gave you links to informations on how to set up timer for interrupt. With example code.
I told you that you need an ISR for LED control.
I gave you the flow chart for the ISR.
I told you that the ISR has nothing to do with push button.

But it´s all useless as long as you don´t read the posts and don´t follow the recommendations.

Klaus
 

but I am using timer1 for normal led1 blinking and timer0 for the led =2 that should blink when an interrupt is pressed.
 

Hi,

You post your code... foreign people should verify it.
So it´s good habit to use comments in your code to make it easy to understand.
like: "; p1.4: LED1, 1= ON, 0 = OFF" (just as an example, somehwere in the top of your code.)
or like: "; timer0, interval 250ms"
then everybody immediately knows exactely what´s the use of P1.4 anfd timer0
***

I´m not familiar with #2051.
therefore I try to find informations on the internet.

as far as I see:
* "ORG 00BH" is for timer0. (correct me if I´m wrong). In this ISR you just SET two port pins. We don´t know what´s the function of these pins, we can only assume. This is no blinking, because blinking needs to switch OFF and ON in fixed intervals. ...And we don´t know timer0 interval time.
* "ORG = 01BH" is for timer1. You say you use it for LED1 blinking. But there is nothing. No "01BH" in your code. So how can it be blinking of LED1????

******
I´m close to leave this discusion, because I don´t see that you follow our recommendations. So it´s waste of time for me.

Therefore my recommendation:
Quit with this code.
And start completely new.
Draw a flow chart of a new tiny task. (maybe just timer (ISR) controlled blinking one LED with 2 Hz).
Then write the code. Clean, with meaningful lables, and comments.
Then test your code.

In the next post:
* show your flow chart
* show your code
* describe your test conditions
* describe what you expect
* describe what you see instead

Klaus
 

I'm in agreement with Klaus. You code is far better than when you started but its structure is still not good. Adding comments is always a good idea, even to remind yourself if you re-visit the code later.

The other thing that will make it easier to read and write is to use defined names. I'm not sure what assembler you use but most will let you create an 'alias' for a pin name, for example: "setb p1.5;" might be renamed to "LED_ON" (that is only an example, I didn't check if it actually does that) so you can use meaningful statements instead of register and bit numbers that mean nothing to anyone who is not familiar with that MCU.

Golden rules:
An ISR runs independently of the main code then returns from where it was triggered from. It is therefore important that you don't exit via a different route than 'reti' and you do not change any variables that the main program flow was using.

A subroutine must only exit through a 'ret' instruction.

Try to keep ISRs as short as possible, in particular do not use delays in them. If the ISR is called by (for example) a timer, you don't want a situation where it gets stuck in a delay loop while another timer interrupt occurs.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top