GenerÃ¥te 2 wÃ¥ves of dÃfferent duty cycles on 2 dÃfferent port pÃns by usÃng both tÃmers in 8051
Discussion in "8051 Discussion Forum" started by Shayaan_Mustafa Apr 11, 2014.
Fri Apr 11 2014, 01:12 am
Hello to all experts!
I want to generate two different waves on two different port pins of different duty cycle in 8051 using assembly language.
one wave is of 10 KHz of 50% duty cycle on pin p1.0
other wave is of 1 KHz of 10% duty cycle on pin p0.0
Here is the code what I get until now.
What changes should I make in order to satisfy above requirements? The above code shown is generating two different wave of 10KHz and 1KHz but both are 50% duty cycle. I want 10KHz as 50% and 1KHz as 10%.
Thank you very much
I want to generate two different waves on two different port pins of different duty cycle in 8051 using assembly language.
one wave is of 10 KHz of 50% duty cycle on pin p1.0
other wave is of 1 KHz of 10% duty cycle on pin p0.0
Here is the code what I get until now.
org 0 mov tmod, #12h ;timer1 in mode=1 and timer0 in mode-2 mov th0, #-50 ;reload value for timer0 setb tr0 ;start timer0 loop: mov th1, #0feh mov tl1, #0ch setb tr1 wait: jnb tf0, next ;check if timer0 is overflowed clr tf0 ;clear flag bit of timer0 is overflowed cpl p1.0 next: jnb tf1, wait ;chekc if timer1 is overflowed clr tr1 ;stop timer1 clr tf1 ;clear flag bit of timer1 is overflowed cpl p0.0 sjmp loop end
What changes should I make in order to satisfy above requirements? The above code shown is generating two different wave of 10KHz and 1KHz but both are 50% duty cycle. I want 10KHz as 50% and 1KHz as 10%.
Thank you very much
[ Edited Fri Apr 11 2014, 11:44 am ]
Fri Apr 11 2014, 12:00 pm
Incase of 1Khz wave, you need to check if high/low cycle has passed and accordingly You should load the timer value. for 1Khz, 1ms is your time period so for 10% duty cycle high cycle should last 0.1ms (100uS) and low time lasts for 900uS. So you need to load -100 for high time and -900 for low time.
Shayaan_Mustafa like this.
Fri Apr 11 2014, 04:08 pm
Thank you for your reply expert.
Here is what I made changes in the above program according to your direction.
This is what I tried to do. But it will not generate wave as I want because when this program enters the low label section then it will jump to wait again and then the code is moving downward from the wait. So I will not able to get wave of high portion. What should I do then?
Should I use the instruction jnb p0.0, low and jb p0.0, high just after the instruction cpl p0.0. Huh???
Here is what I made changes in the above program according to your direction.
org 0 mov tmod, #12h ;timer1 in mode=1 and timer0 in mode-2 mov th0, #-50 ;reload value for timer0 setb tr0 ;start timer0 high: mov th1, #0ffh ;time period for high portion of the wave mov tl1, #0a4h setb tr1 wait: jnb tf0, next ;check if timer0 is overflowed clr tf0 ;clear flag bit of timer0 is overflowed cpl p1.0 next: jnb tf1, wait ;check if timer1 is overflowed clr tr1 ;stop timer1 clr tf1 ;clear flag bit of timer1 is overflowed cpl p0.0 low: mov th1, #0fch ;time for off portion of the wave mov tl1, #0c2h setb tr1 sjmp wait end
This is what I tried to do. But it will not generate wave as I want because when this program enters the low label section then it will jump to wait again and then the code is moving downward from the wait. So I will not able to get wave of high portion. What should I do then?
Should I use the instruction jnb p0.0, low and jb p0.0, high just after the instruction cpl p0.0. Huh???
[ Edited Fri Apr 11 2014, 04:12 pm ]
Sat Apr 12 2014, 11:58 pm
Keep a flag for testing high and low. When high starts set the flag high and when low starts set it low. And check it in your main loop for it.
However to get best result of this requirement. I suggest you use interrupts. You have two timers enable interrupts for both. And individual timer will do its own job in its ISR routine. Do consider it.
However to get best result of this requirement. I suggest you use interrupts. You have two timers enable interrupts for both. And individual timer will do its own job in its ISR routine. Do consider it.
Shayaan_Mustafa like this.
Sun Apr 13 2014, 12:28 am
Keep a flag for testing high and low. When high starts set the flag high and when low starts set it low. And check it in your main loop for it.
Hello sir, I did for testing high and low. But I am not getting what do you mean by set the flag high and set the flag low. Flags are automatically set high when timers are overflowed. Isn't it sir? Am I doing something wrong?
However to get best result of this requirement. I suggest you use interrupts. You have two timers enable interrupts for both. And individual timer will do its own job in its ISR routine. Do consider it.
I know we can use interrupts. But my teacher doesn't allow me to use interrupts because we have not studied interrupts yet in the class. It will take few weeks to get this topic covered in the class. And I have to submit this topic by Wednesday (16th April, 2014). So I need urgent help and with help I want to learn too.
Kindly guide me without using interrupts.
Thank you very much sir.
Mon Apr 14 2014, 02:41 pm
Try this:
flag equ 0 org 0 setb flag ; start with high first mov tmod, #12h ;timer1 in mode=1 and timer0 in mode-2 mov th0, #-50 ;reload value for timer0 setb tr0 ;start timer0 high: ; check if high jnb flag, low setb flag mov th1, #0ffh ;time period for high portion of the wave mov tl1, #0a4h setb tr1 sjmp wait low: clr flag mov th1, #0fch ;time for off portion of the wave mov tl1, #0c2h setb tr1 wait: jnb tf0, next ;check if timer0 is overflowed clr tf0 ;clear flag bit of timer0 is overflowed cpl p1.0 next: jnb tf1, wait ;check if timer1 is overflowed clr tr1 ;stop timer1 clr tf1 ;clear flag bit of timer1 is overflowed cpl p0.0 sjmp high end
Thu Apr 17 2014, 04:47 pm
org 0 mov p2, #0ffh mov tmod, #12h mov th0, #-57 setb tr0 setb p1.0 l_ow: mov th1, #0ffh mov tl1, #0a4h setb tr1 wait: jnb tf0, next clr tf0 cpl p1.1 ;8KHz, p1.1 next: jnb tf1, wait clr tr1 clr tf1 cpl p1.0 ;1KHz, p1.0 jnb p1.0, l_ow jb p1.0, h_igh h_igh: mov th1, #0fch mov tl1, #0c2h setb tr1 sjmp wait end
Hello sir, Thank you for your reply. I need your help in this code sir. This code is generating 8KHz of clock and 1KHz of data pulses. I want to reduce 800Hz of clock and 100Hz of data pulses. What changes should I make in this code?
Thank you.
Fri Apr 18 2014, 04:36 pm
Yes, I tried to increase the time for clock but I am able to get only 4KHz minimum. I want 800Hz. If I use loop i.e. mov r0, #255 then how to use this loop sir?
I hope you have understand my thinking obviously you are experienced one.
I hope you have understand my thinking obviously you are experienced one.
Thu Apr 24 2014, 03:30 pm
org 0 mov p2, #0ffh mov tmod, #12h mov th0, #-57 mov r0, #10 mov r1, #10 setb tr0 setb p1.0 l_ow: mov th1, #0ffh mov tl1, #0a4h setb tr1 wait: jnb tf0, next clr tf0 djnz r0, ? cpl p1.1 ;8KHz, p1.1 next: jnb tf1, wait clr tr1 clr tf1 djnz r1, ? cpl p1.0 ;1KHz, p1.0 jnb p1.0, l_ow jb p1.0, h_igh h_igh: mov th1, #0fch mov tl1, #0c2h setb tr1 sjmp wait end
Hello, This is my code. I have made changes in the program. As discussed above I want to generate 800Hz of clock (p1.1) and 100Hz of data pulses (p1.0). For this I need to create large delay. And I am confuse how to do it. I tried on my own. In capital letters are the recent changes I made. Kindly help me, what label should I use instead of "?", in order to create the delay.
Kindly help me.
[ Edited Thu Apr 24 2014, 03:30 pm ]
Powered by e107 Forum System