HOW TO CALCULATE DELAY IN SEC at89s52

Status
Not open for further replies.
Joined
May 20, 2017
Messages
54
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
0
Here is code for blinking led
how to calculate time in sec when osc is at 11.0592

org 0000h
main:
mov P0,0xff
call delay
mov p0,0x00
call delay

jmp main
delay:
mov R0,#200
l3: mov R1,#200
l2:mov R2,#10
l1:djnz R2,l1
djnz R1,l2
djnz R0,l3
ret

END
 

Your frequency is 11.0592 mhz. The time is 1/frequency. Each machine cycle takes 12 pulses. Hence your frequency is decreased by 12 and your time will increase by 12 for each machine cycle. You need to identify the number of machine cycles your instruction takes, based on that you have to calculate how long you should be loop.
 

That will be available in the micro controller datasheet.
 

Hi,

For blinking a LED I recommend to use a timer, too.
* the timer solution is more flexible and useful
* the timer solution is more exact
* the timer solution needs about no processing power (your solution needs 100% of processing power, it makes it difficult for the microcontroller to react on any other events, like key press)

But back to your solution. A busy wait.

In general...
* any instruction needs exactely n crystal clock cycles (it depends on the instruction and the clock system)to process.
Read datasheet.. how many clock cycle each instruction takes.

Now let's say you want the LED to blink 400ms ON, 400ms OFF. Clock is 11.0592 MHz.
Then multiply the 11,509,200 Hz with 0.4s and get 4,423,680 clock ticks.

You need to program a loop that takes 4.4 million clock cycles to be processed.
In assembly you need nested loops.
To me more useful I recommend to use a one byte parameter to select the delay time.
In your case maybe 10ms steps. So you are able to flexiby generate delay times from 10ms up to 2560ms.
A 400ms delay then acts as a 40 x 10ms delay.
Then the outer loop counts (parameter: 40) how often the 10ms inner loop is beeing processed.
The inner loop = 10ms = 110592 clock ticks.
Because the "110592" is that big an 1 byte loop is not sufficient. I assume you need two counters for thd inner loop.
A loop consists of
* a variable that is incremented (lets say 4 clock cycles)
* a compare (lets say 4 clock cycles)
* a conditional jump (lets say 12clock cycles)
So one loop takes a total of 4 + 4 + 12 = 20 clock cycles
Now use one variable as 256 loop counter. 256 x 20 clock cycles = 5120 clock cycles (463 us)
110592 / 5120 = 21.6 ..... use 22
--> Use one variable to count 22 loops (of 463us each) to get about 10ms.
--> 40 x 22 x 256 x 20 = about 4.5 million clock ticks = about 407.4ms.
(It's possible to get more precise 400ms .... but for blinking a LED....)

Klaus
 
For blinking a LED I recommend to use a timer, too.

how to do with timer?
 

Hi.

Timer method:
* configure a timer that starts an interrupt every xx ms.
* use a flag to enabld LED blinking. Write it in main loop. Read it in ISR. 0= LED OFF, 1 = LED blinking.
* in ISR: if flag = 0 then switch LED OFF.
...else run a counter to get the desired blink time. On timeout toggle LED status.

***
Maybe it's possible to configure the timer interrupt to 400 ms, then toggle the LED every ISR run.
Maybe you need to configure for a smaller time, let's say 10 ms, then count the ISR runs and toggle LED status every 40th run.

Such an ISR run every 10ms won't take more than 1% processing power. Therefore you won't recognize it in main loop.
Don't worry about blinking at all in main loop. Just set/clear flag, thats all.

Klaus
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…