8051 is not serving interrupt correctly in a simple square wave generation

Status
Not open for further replies.

tarun0xe

Member level 2
Joined
Jul 25, 2011
Messages
47
Helped
8
Reputation
16
Reaction score
8
Trophy points
1,298
Location
India
Visit site
Activity points
1,812
hello friends. in my assembly program the microcontroller has to do two tasks. it should keep complementing P2.0 and when timer0 interrupt is generated(at delay of approx. 71msec),it should complement P1.3 and then jump back to main program and start complementing P2.0 again. in this way it should generate a square wave of T=142msecs at P1.3 and another square wave of T=2.17microsecs at P2.0.
now the problem is it works correctly with interrupt alone, i.e if I remove the function of complementing P2.0 and replace by an indefinite stay like
here: SJMP here it works fine but soon I put there that complementing function or any other(like sending some data to P2) It only serves that function and doesn't generate square wave at P1.3. code is given below

ORG 0000H
LJMP MAIN

;-------------------------------ISR FOR TIMER0----------------------------------------
ORG 000BH

CPL P1.3; complement P1.3 to generate square wave of T=142ms(approx)
RETI

MAIN: MOV TMOD,#01H
MOV TH0,#00H
MOV TL0,#00H

MOV IE,#82H

SETB TR0

;back:CPL P2.0

stay:SJMP stay

END


i.e it is working fine currently but soon I make it jump to 'back' to serve complementing function of P2.0 too, it only does that and not generate square wave at P1.3 further. I couldn't find any error in code.any help is highly appreciated. thanks in advance
 
Last edited:

MCU can only do one thing at a time. You can't generate two pulses on two different pins simultaneously.You can use two timers to toggle two different ports but as you use the timers to generate delay between toggling pins, even that will not give you continuous freq on the pins.
 
thanks for reply. yes I agree what you said, as MCU performs two tasks by providing different time slots to the tasks but dealing with only one of them at a time. but even then it should, atleast, serve both tasks. one thing I noticed in my program is ...when I produce square wave of higher frequency at port P1.3( i.e using 1 byte timer, timer in its mode 2) it serves both tasks well, but soon I use 2-byte timer mode and produce a higher time-period wave at port P1.3 it produces a very much larger period wave here than what is desired. for eg. for a desired delay of 3 msecs it is producing delay of 40seconds, seems as it is only performing other task(CPL P2.0) well.
note that I have put a semicolon in front of that 'other task' to make it inactive. but soon I make it active, the MCU is responding correctly only to this task and not the other task of producing square wave at P1.3.
 

well no value on TH0 and TL0 means i want the timer to start from 0000H and go to FFFFH i.e to generate a delay of 65536 cycles with each cycle having a delay of 1.085usecs. a total delay of around 71msecs. and no reload is because timer itself rolls over to 0000H from FFFFH each time it completes one go so no reload requirement. and no starting and stopping of timer in interrupt service routine(ISR) because of the same reason as there is only one instruction to be executed 'CPL P1.3', so even if timer is kept running not that much false behavior. and please also note,as i said earlier, the code is working well if the only function performed is 'CPL P1.3' i.e even with no reload of timer. thanks in advance.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…