Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:examples:timer:software_delay [2010/03/04 10:19] priitjen:examples:timer:software_delay [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +<pagebreak>
 ====== Software delay ====== ====== Software delay ======
  
Line 5: Line 6:
 ===== Theory ===== ===== Theory =====
  
-There is quite often a need to create delays in programs of microcontrollers, it is necessary to time the actions or wait them to end. By idea one of the easiest of the methods of creating a break in the work of microcontroller is overloading its processor with some marginal action – for example order it to count big numbers. From the frequency of the processor can be calculated up to what number it should count to get certain time delay. Counting some number from zero up to the value of the frequency of the processor in hertz-s, should theoretically create a delay for one second. In practice it is not so simple because of different reasons.+There is often a need to create delays in programs of microcontrollers, it is necessary to time the actions or wait them to end. By its concept one of the easiest methods for creating a break in the work of microcontroller is overloading its processor with some alternative action – for example order it to count big numbers. From the stroke frequency of the processor can be calculated to which number it should count in order to get certain time delay. Counting number from zero up to the value of the stroke frequency of the processor in hertz-s, should theoretically create a delay for one second. For number of reasons this is not as simple in practice.
          
-If the processor of the microcontroller calculates using numbers which’s binary form is as wide as its inner bus (AVR has 8 bits), then it takes one töötakt?`? of the processor to perform one arithmetical operation for example adding 1 to some value. In order to operate with thousands or millions the number has to be 16 bit or 32 bit and for an 8 bit processor it takes more than one töötakt?? to calculate them. Hence when dealing with large numbers, one has to be familiar with the inside of the processor – exactly its command.   +If the processor of the microcontroller calculates using numbers which’s binary form is as wide as its inner bus (AVR has 8 bits), then it takes one firing stroke of the processor to perform one arithmetical operation for example adding 1 to any value. In order to operate with thousands or millions the number has to be 16 or 32 bit and for an 8 bit processor it takes more than one firing stroke to calculate them. Hence when dealing with large numbers, one has to be familiar with the inside of the processor – exactly its commands.   
  
-Since when programming in advanced languages (C-language for example), the programs are not written directly on the basis of the command, in order to create a software delay,  one needs to know also its compiler which converts the program to the machine code. Exactly form this depends how many instructions (and phases) it takes to perform one arithmetical operation. Complexity is added by the fact that the compiler may convert the program into the machine code in several ways – for example it can make the machine code as memory saving as possible or as easy to execute. Such compiler’s action is called optimization. Different modes of optimization mean different software delay’s machine codes and different duration of them.    +When programming in advanced languages (C-language for example), the programs are not written directly on the basis of the command, in order to create a software delay, one needs to know also its compiler which converts the program to the machine code. From this depends how many instructions (and phases therefore) it takes to perform one arithmetical operation. Complexity is added by compilers ability to convert the program into the machine code in several ways – for example, by making the machine code as memory saving as possible or easily executable. Such compiler’s actions are called optimization. With different modes of optimization the software delay’s machine codes and their duration come out different.    
  
  
 ===== Practice ====== ===== Practice ======
  
-The following is an example of generating software delay with AVR microcontroller. There is a part of a program in C-language, which counts the variable x of //for// cycle from 0 to 100. Inside each cycle a nothing doing empty instruction is completed. It is needed there, because if the content of the cycle is empty, the compiler optimizes it out of the program because it thinks that this is useless. +The following is an example of generating software delay with AVR microcontroller. part of a program in C-language is written, which counts the variable x of for-cycle from 0 to 100. Inside each cycle a no-action empty instruction is completed. It is needed, because if the content of the cycle is empty, the compiler optimizes it out of the program as it considers this to be useless.
  
 <code c> <code c>
Line 27: Line 28:
 </code> </code>
  
-This is the same part of a program after compiling. 2 hexadecimal numbers on the left is machine code and on the right is the command with operand(s) in assembler language.  The machine code and the assembler language are one to one related, the assembler is just for presenting the machine code for human in a readable form. In compiling, the optimization of the length of the program is used (compiler’s parameter –Os).+This is the same part of a program after compiling. 2 hexadecimal numbers on the left is machine code and on the right is the command with operand(s) in assembler language. The machine code and the assembler language are conformal; the assembler is just for presenting the machine code for humans in a readable form. In compiling, the optimization of the length of the program is used (compiler’s parameter –Os).
  
 <code asm> <code asm>
-80 e0     ldi  r24, 0x00   ; r24 loading number 0 to the index +80 e0     ldi  r24, 0x00 ; r24 loading number 0 to the index 
-00 00     nop              ; Empty operation +00 00     nop            ; Empty operation 
-8f 5f     subi r24, 0xFF   ; subtracting 255 form the r24 index, that means adding +1 +8f 5f     subi r24, 0xFF ; subtracting 255 form the r24 index, that means adding +1 
-84 36     cpi  r24, 0x64   ; comparing r24 index with number 100 +84 36     cpi  r24, 0x64 ; comparing r24 index with number 100 
-e1 f7     brne .-8         ; If the comparison was wrong, then transfer 8-baits back+e1 f7     brne .-8       ; If the comparison was wrong, then transfer 8-baits back
 </code> </code>
  
  
-In the compiled form can be seen, what is actually happening with the cycle of the C-language and it can be used to calculate how many clock cycles does it take to complete one period. The information about the effect of the instructions and operating time can be found from AVR’s instruction set datasheet. In the given example, it takes 4 clock cycles to complete 4 instructions in one cycle period, because all instructions demand one clock rate. In addition, one clock rate is used before the cycle for loading instruction and afterwards one extra clock rate for exiting the cycle. Let’s assume, that the working clock rate of the controller is 14,7456 MHz, it can be calculated the whole delay produced by the program. +In the compiled form can be seen, what is actually happening with the cycle of the C-language and it can be used to calculate how many clock sycles is needed to complete a cycle of one period. The information about the effect of the instructions and operating time can be found from AVR’s instructions datasheet. In the given example, it takes 4 clock cycles to complete 4 instructions in one cycle period, because all instructions demand one clock rate. In addition, one clock rate is used before the cycle for loading instruction and afterwards one extra clock rate for exiting the cycle. By assuming, that the working clock rate of the controller is 14,7456 MHz, the whole delay produced by the program can be calculated
  
 (1 + 100 ⋅ 4 + 1) / 14745600 = 27,26 μs (1 + 100 ⋅ 4 + 1) / 14745600 = 27,26 μs
  
-The delay produced in the example, is in microseconds and the variable used is 8-bit so the machine code is fairly simple. In order to produce a break in millisecond, we need to count much larger numbers and then the machine code gets longer. Cycles working inside each other may also be used, but with this method the delay is not in linear relation with the number of the cycles because with each level of cycle small extra delays occur.+The delay produced in the example, is in microseconds and the variable used is 8-bit so the machine code is fairly simple. In order to produce a break in millisecond, we need to count much larger numbers and thus the machine code gets longer. Cycles working inside each other may also be used, but with this method the delay is not in linear relation with the number of the cycles because with each level of cycle small extra delay occurs.
              
-The goal of this exercise is not creating precise software delay on the level of machine code, because it is quite accurate work and we already have the functions necessary to produce delays in the avr-libc and in the library of the HomeLab. Those are used also in the following examples.+The goal of this exercise is not creating precise software delay on the level of machine code, because it is quite an accurate work and we already have the functions necessary to produce delays in the avr-libc and in the library of the HomeLab. Those are used also in the following examples.
  
-It is necessary to know when dealing with the software delay, that regardless its basic simplicity; it is extremely inefficient method from point of view of the power consumption. During all of these clock rates when the microcontroller is counting the useless, energy is consumed. So if using applications using batteries, it is not wise to write long software delays. Wiser is to use hardware timers, which are working independently and wake the processor from hibernating when it is necessary to continue the work.    +When dealing with the software delay it is important to know, that regardless its basic simplicity; it is extremely inefficient method from the power consumption point of view.  During all of these clock rates when the microcontroller is counting the useless, energy is consumed. So if using applications operating on batteries, it is not wise to write long software delays. Wiser is to use hardware timers, which are working independently and wake the processor from hibernating when it is time to continue the work.    
  
  
 ===== Practice ====== ===== Practice ======
  
-The following code of a program is about software delay function //sw_delay_ms// , which makes a given delay in milliseconds using the parameter //count//. The function uses avr-libc library’s function //_delay_ms// which is partly written in assembler language. The reason why the //_delay_ms// is not used in the exercise immediately is that when using the //_delay_ms// there may occur some problems when the delays are long. The //sw_delay_ms// enables creating 65535 ms long delays without problems+The following code of a program is about software delay function //sw_delay_ms// , which makes a given delay in milliseconds using the parameter //count//. The function uses avr-libc library’s function //_delay_ms// which is partly written in assembler language. The reason why the //_delay_ms// is not used in this exercise immediately is that when using the //_delay_ms// problems may occur when the delays are long. The //sw_delay_ms// enables creating 65535 ms long delays without any complications
  
 <code c> <code c>
Line 68: Line 69:
 </code> </code>
  
-The following program is for using the function shown. It creates two delays in the endless loop: 100 ms and 900 ms. During the shorter delay LED is lit and during the longer it is switched off, the result – the LED is blinking periodically+The following program is for using the given function, this creates two delays in the endless loop: 100 ms and 900 ms. During the shorter delay LED is lit and during the longer it is switched off, the result – the LED is blinking periodically
  
 <code c> <code c>
 // //
-// The demonstration program of the software delay of the Homelab.+// The demonstration program of the software delay of the HomeLab.
 // The program is blinking a LED for a moment after ~1 second. // The program is blinking a LED for a moment after ~1 second.
 // //
en/examples/timer/software_delay.1267697948.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0