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:hardware_delay [2010/02/08 12:48] mikk.leinien:examples:timer:hardware_delay [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Hardware delay ====== ====== Hardware delay ======
  
-//Vajalikud teadmised: [HW] [[en:hardware:homelab:controller]], [AVR] [[en:avr:timers]], [LIB] [[en:software:homelab:library:pin]], [LIB] [[en:software:homelab:library:delay]], [LIB] [[en:software:homelab:library:timer]], [PRT] [[en:examples:timer:software_delay]]//+//Necessary knowledge: [HW] [[en:hardware:homelab:controller]], [AVR] [[en:avr:timers]], [LIB] [[en:software:homelab:library:pin]], [LIB] [[en:software:homelab:library:delay]], [LIB] [[en:software:homelab:library:timer]], [PRT] [[en:examples:timer:software_delay]]//
  
-===== Teooria =====+===== Theory =====
  
-Tarkvaraline viide pole ainus meetod pausi tekitamiseksSama asja saab teha ka taimerigaTaimer on riistvaraline kindla sagedusega suurenev või vähenev loendurLoenduri taktsignaali saab enamasti tekitada mikrokontrolleri töötaktist või mingist välisest taktistTaktsignaali sagedust saab üldjuhul ka läbi jagada, et väiksem taktsagedus saavutada seda tehakse taktijaguriga, mida inglise keeles nimetatakse kui //prescaler//Oluline on siinkohal siiski fakt, et fikseeritud taktsagedusega loenduri väärtus on lineaarses sõltuvuses ajastAja saab välja arvutada, korrutades loenduri taktisignaali perioodi loenduri väärtusega.+The software delay is not the only method for creating breaksThe same can be done using timerTimer is a hardware which counts up or down with a certain frequencyThe clock frequency of the timer can be generated from microcontroller’s frequency or from some kind of other outside paceIn general the clock frequency can  be divided with a multiplier to reach a smaller frequency this is done with prescaler. Important fact is that the fixed clock frequency timer’s value is linearly related to the timeThe time can be calculated by multiplying the period of the clock frequency of the timer with the value of the timer.
  
-[{{  :examples:timer:timer_counter.png?300|AVR loenduri väärtuse muutusega kaasnevad sündmused}}]+[{{  :examples:timer:timer_counter.png?300|The events which come with the changes of AVR timer.}}]
  
-AVR loendurid saab panna loenduri ületäitumisest (inglise keeles //overflow//) või kindla väärtuse saavutamisest (inglise keeles //compare match//) teavitamaÜletäitumine tekib hetkelkui loendur on omistanud maksimaalse võimaliku väärtuse ja alustab uuesti nullist loendamistVäärtuse saavutamise kontroll toimub loenduri suurendamise hetkel selle uut väärtust kasutaja poolt määratud väärtusega võrreldesSündmuse tekkimise korral seatakse AVR olekuregistrites vastavad bitid automaatselt kõrgeks.+AVR counters can be made to inform about overflow of the counter or achieving compare machOverflow happens when the counter has the maximal possible value and the cycle starts all over again form 0. With reaching a pre set value during the moment of growth of the counter’s value it is compared to the value given by the user. On the occurrence of the eventthe bits in the status indexes of the AVR are automatically set as high  
 +  
 +For generating a delay using a timer, it is only necessary to set the timer and waiting for the status bit to go highDifferent from the software delay, the work of the timer is not depending on the compiler, which makes them more reliable. At the same time the diversity (or complexity) of the set-up of the AVR counter can be considered fairly troublesome. Depending on the microcontroller’s timing signal, may happen that it will not divide exactly with the desired delay period and the delay will not be accurate 
  
-Sellekset taimeriga viide tekitadapiisabki vaid loenduri seadistamisest ja olekubiti kõrgeks minemise ootamisestErinevalt tarkvaralisest viitest ei sõltu taimerite töö kompilaatoristmis teeb nende kasutamise töökindlamaksSamas võib AVR loendurite mitmekesisuse (või ka segasuse) tõttu  nende seadistamine üsna tülikas tundudaOlenevalt mikrokontrolleri taktsignaalist võib ka juhtudaet see ei jagu täpselt soovitud viite perioodiga ja viide ei ole täpne.+===== Practice ===== 
 +The program code below is a delay function based on a timerwhich is simplified a little bit. The principle of counting is the same as it is at software delay function – a desired amount of 1 ms long delays are produced. The delay is produced with an 8-bit ATmega 128 counter 0. It is calculated previously that at clock frequency 14,7456 Mhz the timing signal has to be divided at least 64 times, so that the counter would not reach to overflow in 1 msThe value which the counter must have so that the overflow occurs after 1 ms is presented in the form of an expression and the variable is //timer_start//. //F_CPU// which is a constant in macro-languagethat shows clock frequency in HzThe clock frequency should be 25,6 at the moment but since fractions can not be used, the initial value will be set 26Unfortunately here arises a mistake in delay timehowever it is fairly small (-1,7 μs)
  
-===== Praktika =====+In the cycle takes place initialing of the counter and zeroing the flag of the overflow (by writing 1 into that). Then is waited until the counter counts to 256 from the initial value, i.e. to the overflow. At the moment of the overflow the flag goes high and the delay of 1 ms has taken place. In the end of the function the timer is stopped.       
  
-Allpool olev programmikood on aga taimeril põhinev viitefunktsioon, mida on natuke lihtsustatud. Loendamise põhimõte on sama mis tarkvaralise viite funktsioonilgi - tekitatakse soovitud arv 1 ms pikkuseid viiteid. Viite tekitamiseks on kasutusel ATmega128 8-bitine loendur 0. Eelnevalt on juba välja arvutatud, et 14,7456 Mhz taktsageduse puhul peab loenduri taktsignaal olema vähemalt 64 korda jagatud, et 1 ms jooksul 8-bitine loendur üle ei täituks. See, mis väärtust loendur omama peab, et ületäitumine toimuks 1 ms järel, on esitatud avaldise kujul ja omistatud muutujale //timer_start//. //F_CPU// on makro-keele konstant mis näitab taktsagedust hertsides. Nimetatud taktsageduse puhul peaks loenduri väärtus 25,6 olema, kuid kuna murdarve kasutada ei saa, siis loenduri algväärtuseks saab 26. Siin tekib paraku ka viga viite ajas, kuid see on üsna väike (-1,7 μs). 
- 
-Tsüklis toimub loenduri algväärtustamine ja ületäitumise lipukese nullimine (sellesse 1 kirjutades). Seejärel oodatakse, kuni loendur loendab algväärtusest 256-ni, ehk ületäitumiseni. Ületäitumise hetkel läheb ületäitumise lipuke kõrgeks ja 1 ms viide ongi toimunud. Funktsiooni lõpus taimer peatatakse. 
  
 <code c> <code c>
 // //
-// Riistvaraline viide millisekundites+// Hardware delay in milliseconds.
 // //
 void hw_delay_ms(unsigned short count) void hw_delay_ms(unsigned short count)
 {  {
- // Taimeri algväärtuse arvutamine+ // Calculating the initial value of the timer.
  register unsigned char timer_start = 256 - F_CPU / 1000 / 64;  register unsigned char timer_start = 256 - F_CPU / 1000 / 64;
   
- // Taimeri käivitamine+ // Starting the timer.
  timer0_init_normal(TIMER0_PRESCALE_64);  timer0_init_normal(TIMER0_PRESCALE_64);
  
- // Viite muutuja nullini loendamine+ // Counting the variable of the delay to the 0.
  while (count-- > 0)  while (count-- > 0)
  {  {
- // Taimeri algväärtustamine+ // Initializing the timer.
  timer0_set_value(timer_start);  timer0_set_value(timer_start);
  
- // Ületäitumise lipukese nullimine+ // Zeroing the overflow flag.
  timer0_overflow_flag_clear();   timer0_overflow_flag_clear();
   
- // Ületäitumise ootamine+ // Waiting for overflow.
  while (!timer0_overflow_flag_is_set())  while (!timer0_overflow_flag_is_set())
  {  {
Line 47: Line 47:
  }  }
  
- // Ületäitumise lipukese nullimine+ // Zeroing the overflow flag.
  timer0_overflow_flag_clear();   timer0_overflow_flag_clear();
  
- // Taimeri peatamine+ // Stoping the timer.
  timer0_stop();   timer0_stop();
 } }
 </code> </code>
  
-Järgnevalt on toodud samasugune programm nagu tarkvaralise viite näiteskiLühemal 100 ms poolperioodil LED süüdatakse, pikemal 900 ms poolperioodil kustutatakseTulemusena vilgatab LED  iga sekundi järelParaku pole ka selles näites periood täpselt sekundsest programmi muude funktsioonide täitmine igas tsüklis võtab samuti aegaTäpseks ajastuseks tuleb kasutada 16-bitist taimerit koos katkestustega.+The following is a similar program to the example of the software delayIn the shorter 100 ms half-period the LED is lit and on the longer 900 ms half-period it is switched offAs the result the LED is blinking after every secondUnfortunately, in this example the period isn't precisely second eitherbecause executing other functions of the program takes also timeFor exact timing 16-bit timer with interruptions must be used
  
 <code c> <code c>
 // //
-// Kodulabori raudvaralise viite demonstratsioonprogramm+// Demonstration program of hardware delay of the HomeLab
-// Programm vilgutab ~1 sekundi järel hetkeks LED-i.+// The Program blinks LED for a moment after every ~1 second.
 // //
 #include <homelab/pin.h> #include <homelab/pin.h>
Line 66: Line 66:
  
 // //
-// Test LED viigu määramine+// Determining the pin of the Test LED.
 // //
 pin debug_led = PIN(B, 7); pin debug_led = PIN(B, 7);
  
 // //
-// Põhiprogramm+// Main program.
 // //
 int main(void) int main(void)
 { {
- // LED-i viigu väljundiks seadmine+ // Setting the pin of the  LED as output.
  pin_setup_output(debug_led);  pin_setup_output(debug_led);
  
- // Lõputu tsükkel + // Endless loop.
  while (true)  while (true)
  {  {
- // LED-i süütamine+ // Lighting the LED.
  pin_clear(debug_led);  pin_clear(debug_led);
   
- // Riistvaraline paus 100 millisekundit+ // Hardware delay for 100 milliseconds.
  hw_delay_ms(100);  hw_delay_ms(100);
  
- // LED-i kustutamine+ // Switch off of the LED.
  pin_set(debug_led);  pin_set(debug_led);
   
- // Riistvaraline paus 900 millisekundit+ // Hardware delay for 900 milliseconds.
  hw_delay_ms(900);  hw_delay_ms(900);
  }  }
en/examples/timer/hardware_delay.1265633280.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