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:programming:c:crashcourse [2012/06/05 09:38] eero.valjaotsen:programming:c:crashcourse [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Crash course ====== +====== Crash course ====== 
-<note important>Translation needed</note>+
 ===== Program structure ===== ===== Program structure =====
  
Line 43: Line 43:
  
 /* /*
-  Block comment can be used to include more than one line. + Block comment can be used to include more than one line. 
-  The beginning and the end of a comment is assigned with slash and asterisk signs.+ The beginning and the end of a comment is assigned with slash and asterisk signs.
 */ */
 </code> </code>
  
-~~PB~~ 
  
 ===== Data ===== ===== Data =====
Line 141: Line 140:
 </code> </code>
  
-~~PB~~+<pagebreak>
  
 ===== Operations ===== ===== Operations =====
Line 234: Line 233:
 Bit operations are essential when using the registers of microcontroller. These are described in AVR register chapter. Bit operations are essential when using the registers of microcontroller. These are described in AVR register chapter.
  
-~~PB~~+<pagebreak>
  
 ===== Functions ===== ===== Functions =====
Line 273: Line 272:
 The execution of a C-language program is started from //main// function which makes it compulsory function. The execution of a C-language program is started from //main// function which makes it compulsory function.
  
-~~PB~~+<pagebreak>
  
 ===== Statements ===== ===== Statements =====
Line 318: Line 317:
 </code> </code>
  
-~~PB~~+<pagebreak>
  
 ==== Switch statement ==== ==== Switch statement ====
Line 346: Line 345:
  // Random action  // Random action
  functionX();  functionX();
- // break operation not needed because the comparisonbreak lauset pole vaja+ // break operation not needed, 
- // kuna võrdlemine lõppeb nagunii+ // because the comparison ends anyway
 } }
 </code> </code>
  
-===== Tsüklid =====+===== Loops =====
  
-Tsüklitega saab programmilõiku täita mitmeid kordi.+Loops can be used for executing code several times.
  
-==== while ====+==== while loop ====
  
-//while// võtmesõnaga tähistatud programmilõiku täidetakse seni, kuni sulgudes olev avaldis on tõene.+Code marked with //while// keyword is executed until condition in brackets is trueExample:
  
 <code c> <code c>
 int x = 0; int x = 0;
  
-// Tsükkel kestab seni, kuni on väiksem kui 5+// Loop will execute until is smaller than 5
 while (x < 5) while (x < 5)
 { {
- // x suurendamine ühe võrra+ // x incrementation
  x++;  x++;
 } }
 </code> </code>
  
-~~PB~~+<pagebreak>
  
-==== for ====+==== for loop ====
  
-//for// võtmesõnaga tsükkel sarnaneb //while// tsüklile, kuid lisaks on sulgudes ära määratud enne tsüklit täidetav lause ja iga tsükli ajal täidetav lause.+//for// keyword loop is similar to //while// loop exept there are described operation executed before the loop and operation executed in the end of every loop cycleExample:
  
-Näide: 
 <code c> <code c>
 int i, x = 0; int i, x = 0;
  
-// Algul määratakse nulliksTsüklit täidetaks seni, kuni +// i is equal to 1 at the beginning of the loop
-// i on vähem kui 5. Iga tsükli lõpus suurendatakse ühe võrra+// Loop will be executed until is smaller than 5. 
 +// will be incremented in the end of every loop cycle.
 for (i = 0; i < 5; i++) for (i = 0; i < 5; i++)
 { {
- // x suurendamine võrra+ // x addition by 2
  x += 2;  x += 2;
 } }
  
-// Siinkohal tuleb väärtuseks 10+// here value is 10
 </code> </code>
  
-==== Tsüklis liikumine ====+==== Halting loops ====
  
-//while// ja //for// tsüklitest saab erandkorras väljuda //break// võtmesõnaga. //continue// võtmesõnaga saab alustada järgmist tsüklit ilma järgnevat koodi täitmata.+As and exception exit from //while// and //for// loops can be made with keyword //break//To start the next loop cycle without executing the remaining code in loop, //continue// keyword can be usedFor example:
  
 <code c> <code c>
 int x = 0, y = 0; int x = 0, y = 0;
    
-// Lõputu tsükkel, kuna on loogiline tõesus+// Infinite loop because is logic true
 while (1) while (1)
 {     {    
- // Tsüklist väljutakse, kui on saavutanud väärtuse 100+ // Exit the the loop cycle if becomes 100
  if (x >= 100) break;  if (x >= 100) break;
   
- // x suurendamine, et tsükkel kunagi lõppeks ka+ // x incrementation to end loop at some time
  x++;  x++;
    
- // Kui on 10 või vähem, siis alustatakse järgmist tsüklit+ // If is 10 or less then the next cycle is started
  if (x <= 10) continue;  if (x <= 10) continue;
    
- // y suurendamine+ // y incrementation
  y++;  y++;
 } }
  
-// Siinkohal on väärtus 90+// Here value is 90
 </code> </code>
  
-==== Tekstitöötlus ====+==== Text operations ====
  
-Tekstitöötlusfunktsioone on mikrokontrolleri puhul vaja eelkõige teksti kuvamiseks LCD ekraanile.+Text operations are needed for microcontrollers foremost for displaying characters and text on LCD.
  
 ==== sprintf ==== ==== sprintf ====
  
-sprintf funktsioon toimib sarnaselt C-keeles üldlevinud printf funktsioonigaErinevuseks on funktsiooni tulemuse väljastamine puhvrisse (muutujasse), mitte standard väljundisse.+//sprintf// function is similar to ordinary //printf// function commonly used in C-languageThe difference is that the result of this function is loaded into variable not standard output.
  
-tagastus = sprintf(muutujaparameetritega_tekstparameetrid);+return = sprintf(variableparameter_textparameters);
  
-  +Example:
-Näide:+
 <code c> <code c>
 int r = sprintf(buffer, "%d pluss %d on %d", a, b, a+b); int r = sprintf(buffer, "%d pluss %d on %d", a, b, a+b);
 </code> </code>
-Väärtustab muutuja vormindatud tekstiga, mis on antud funktsiooni 
-teisest kuni n parameetrini. Sprintf funktsioon lihtsustab keerulisemate 
-fraaside või lausete koostamist. Mugavam on kasutada tekstis muutujaid, mis 
-asendatakse väärtustega. Funktsioon tagastab muutujasse salvestatud teksti pikkuse. Vea korral tagastatakse negatiivne arv. 
  
-Näide:+It will load formated text into variable which is given from the function second to n parameter. //sprintf// will simplify composing more sophisticated statements. Easier is to use variables in text that will be replaced by values. Function returns the length of text loaded into variable. In case of error occurrence, negative value is returned. 
 + 
 +Example:
 <code c> <code c>
-  sprintf(x, "%d. on esimene", 1); +  sprintf(x, "%d. is first", 1); 
-  // sama tulemuse saaksime ka nii+  // the same result can be achieved also
-  x = "1. on esimene";+  x = "1. is first";
  
-  sprintf(x, "%s on %d aastat vana", "Juku", 10); +  sprintf(x, "%s is %d years old", "Juku", 10); 
-  // sama tulemuse saaksime ka nii+  // the same result can be achieved also
-  x = "Juku on 10 aastat vana";+  x = "Juku is 10 years old";
 </code> </code>
  
-%s ja %d on antud juhul parameetrid, mis asendatakse vastavalt muutjate +%s and %d are in this case parameters that will be repalaced accordingly by variable values which are the last parameters to function. The number of parameters must be the same as variables
-väärtustega, mis on funktsiooni viimasteks parameetriteksNiipalju, kui on +In the first examplethe parameter was %d which was replaced by variable value 1. In the second example parameters were %s and %d which were replaced by variable values "Juku" and 10. It was strictly in this order because %s was waiting value in form of a text and %d value of a numberThere are special variable descriptions for different data types:
-parameetreidpeab olema ka muutujaid. +
-Esimese näite puhul oli meil parameetriks %d, mis asendati muutuja +
-väärtusega 1. Teise näite puhul olid parameetriteks %s ja %d, mis asendati +
-vastavalt muutuja väärtustega "Juku" ja 10. +
-Just nimelt sellises järjekorras, sest %s ootab väärtust teksti kujul ja +
-%d numbrilist väärtustErinevate andmetüüpide jaoks on olemas vastavad +
-muutujate kirjeldused:+
  
-Parameeter      ^ Kirjeldus       ^ Näide     ^ +Parameter       ^ Description     ^ Example    
-| %c | Tähemärk     | a | +| %c | Char     | a | 
-| %i või %d    | Täisarv| 123 | +| %i or %d    | Integer| 123 | 
-| %f | Murdarv | 3,14 | +| %f | Real number | 3,14 | 
-| %s | Tekst   | näide+| %s | Text   | example
-| %X | Heksadetsimaalarv| 3F |+| %X | Hexadecimal number| 3F |
  
 <code c> <code c>
Line 472: Line 461:
   char buffer [50];   char buffer [50];
   int n, a=5, b=3;   int n, a=5, b=3;
-  n=sprintf (buffer, "%d pluss %d on %d", a, b, a+b); +  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b); 
-  printf ("\"%s\" on %d markki pikk\n",buffer,n);+  printf ("\"%s\" is %d digits long\n",buffer,n);
   return 0;   return 0;
 } }
 </code> </code>
  
-==== Üldised utiliidid ====+==== General utilities ====
  
-Teegi standard library (stdlib.h) funktsioonide erinevate operatsioonide ja konverteerimiste lihtsustamiseks+Standard functions library (stdlib.h) includes functions to simplify different common operations and conversions.
  
-==== random ====+==== Random function ====
  
-Juhuarvude genereerimine ei olegi AVR kontrolleril väga lihtne.+Generating a random number is not so simple for AVR microcontroller.
  
-Esmalt tuleb juhunumbrigeneraator seemendada arvuga, mille järgi genereeritakse suvaliste numbrite jada. +At first the random number generator must be inputed with a number to be the basis of random number array generationThe array based on one number is always the sameTo make the result more random, the function can be inputed with a values taken from free floating ADC.
-Sama numbri järgi genereeritakse alati sama jadaSuvalisema tulemuse saamiseks võib seemendamiseks kasutada näiteks tühjast ADC-st loetud ujuvaid väärtusi.+
  
-Näide:+Example:
 <code c> <code c>
 srand(100); srand(100);
Line 495: Line 483:
 </code> </code>
  
-Näide, juhuarvu genereerimiseks vahemikus 16+Example about generating a random number in range of 16:
 <code c> <code c>
 #include <stdlib.h> #include <stdlib.h>
Line 504: Line 492:
  
  
-Põhjalikuma inglisekeelse kirjelduse keele funktsioonide kohta leiad aadressilt:+The more in-depth english description about C-language functions is in:
  
 [[http://www.cplusplus.com/reference/clibrary/|http://www.cplusplus.com/reference/clibrary/]] [[http://www.cplusplus.com/reference/clibrary/|http://www.cplusplus.com/reference/clibrary/]]
  
-~~DISCUSSION~~ 
en/programming/c/crashcourse.1338889102.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