This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:programming:c:crashcourse [2012/06/05 09:38] – eero.valjaots | en:programming:c:crashcourse [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Crash course ====== | + | ====== |
- | <note important> | + | |
===== Program structure ===== | ===== Program structure ===== | ||
Line 43: | Line 43: | ||
/* | /* | ||
- | | + | 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. |
*/ | */ | ||
</ | </ | ||
- | ~~PB~~ | ||
===== Data ===== | ===== Data ===== | ||
Line 141: | Line 140: | ||
</ | </ | ||
- | ~~PB~~ | + | < |
===== 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~~ | + | < |
===== 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~~ | + | < |
===== Statements ===== | ===== Statements ===== | ||
Line 318: | Line 317: | ||
</ | </ | ||
- | ~~PB~~ | + | < |
==== Switch statement ==== | ==== Switch statement ==== | ||
Line 346: | Line 345: | ||
// Random action | // Random action | ||
functionX(); | functionX(); | ||
- | // break operation not needed | + | // break operation not needed, |
- | // kuna võrdlemine lõppeb nagunii | + | // because the comparison ends anyway |
} | } | ||
</ | </ | ||
- | ===== Tsüklid | + | ===== Loops ===== |
- | Tsüklitega saab programmilõiku täita mitmeid kordi. | + | Loops can be used for executing code several times. |
- | ==== while ==== | + | ==== while loop ==== |
- | // | + | Code marked with // |
<code c> | <code c> | ||
int x = 0; | int x = 0; | ||
- | // Tsükkel kestab seni, kuni x on väiksem kui 5 | + | // Loop will execute until x is smaller than 5 |
while (x < 5) | while (x < 5) | ||
{ | { | ||
- | // x suurendamine ühe võrra | + | // x incrementation |
x++; | x++; | ||
} | } | ||
</ | </ | ||
- | ~~PB~~ | + | < |
- | ==== for ==== | + | ==== for loop ==== |
- | // | + | // |
- | Näide: | ||
<code c> | <code c> | ||
int i, x = 0; | int i, x = 0; | ||
- | // Algul määratakse | + | // i is equal to 1 at the beginning of the loop. |
- | // i on vähem kui 5. Iga tsükli lõpus suurendatakse | + | // Loop will be executed until i is smaller than 5. |
+ | // i will be incremented in the end of every loop cycle. | ||
for (i = 0; i < 5; i++) | for (i = 0; i < 5; i++) | ||
{ | { | ||
- | // x suurendamine | + | // x addition by 2 |
x += 2; | x += 2; | ||
} | } | ||
- | // Siinkohal tuleb x väärtuseks | + | // here x value is 10 |
</ | </ | ||
- | ==== Tsüklis liikumine | + | ==== Halting loops ==== |
- | // | + | As and exception exit from // |
<code c> | <code c> | ||
int x = 0, y = 0; | int x = 0, y = 0; | ||
- | // Lõputu tsükkel, kuna 1 on loogiline tõesus | + | // Infinite loop because |
while (1) | while (1) | ||
{ | { | ||
- | // Tsüklist väljutakse, | + | // Exit the the loop cycle if x becomes |
if (x >= 100) break; | if (x >= 100) break; | ||
- | // x suurendamine, | + | // x incrementation to end loop at some time |
x++; | x++; | ||
- | // Kui x on 10 või vähem, siis alustatakse järgmist tsüklit | + | // If x 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 y väärtus | + | // Here y value is 90 |
</ | </ | ||
- | ==== Tekstitöötlus | + | ==== Text operations |
- | Tekstitöötlusfunktsioone | + | Text operations are needed for microcontrollers foremost for displaying characters and text on LCD. |
==== sprintf ==== | ==== sprintf ==== | ||
- | sprintf | + | //sprintf// function is similar to ordinary //printf// function commonly used in C-language. The difference is that the result of this function is loaded into variable not standard |
- | tagastus | + | return |
- | + | Example: | |
- | Näide: | + | |
<code c> | <code c> | ||
int r = sprintf(buffer, | int r = sprintf(buffer, | ||
</ | </ | ||
- | 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, " | + | sprintf(x, " |
- | // sama tulemuse saaksime ka nii: | + | // the same result can be achieved also: |
- | x = " | + | x = " |
- | sprintf(x, " | + | sprintf(x, " |
- | // sama tulemuse saaksime ka nii: | + | // the same result can be achieved also: |
- | x = " | + | x = " |
</ | </ | ||
- | %s ja %d on antud juhul parameetrid, | + | %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, | + | In the first example, the 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 |
- | parameetreid, peab olema ka muutujaid. | + | |
- | Esimese näite puhul oli meil parameetriks | + | |
- | väärtusega | + | |
- | vastavalt muutuja väärtustega | + | |
- | Just nimelt sellises järjekorras, | + | |
- | %d numbrilist väärtust. Erinevate andmetüüpide jaoks on olemas vastavad | + | |
- | muutujate kirjeldused: | + | |
- | ^ Parameeter | + | ^ Parameter |
- | | %c | Tähemärk | + | | %c | Char | a | |
- | | %i või %d | Täisarv| 123 | | + | | %i or %d | Integer| 123 | |
- | | %f | Murdarv | + | | %f | Real number |
- | | %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, " | + | n=sprintf (buffer, " |
- | printf (" | + | printf (" |
return 0; | return 0; | ||
} | } | ||
</ | </ | ||
- | ==== Üldised utiliidid | + | ==== General utilities |
- | Teegi standard | + | Standard functions |
- | ==== 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 generation. The array based on one number is always the same. To make the result more random, the function can be inputed with a values taken from free floating |
- | Sama numbri järgi genereeritakse alati sama jada. Suvalisema tulemuse saamiseks võib seemendamiseks kasutada näiteks tühjast | + | |
- | Näide: | + | Example: |
<code c> | <code c> | ||
srand(100); | srand(100); | ||
Line 495: | Line 483: | ||
</ | </ | ||
- | Näide, juhuarvu genereerimiseks vahemikus | + | Example about generating a random number in range of 16: |
<code c> | <code c> | ||
#include < | #include < | ||
Line 504: | Line 492: | ||
- | Põhjalikuma inglisekeelse kirjelduse | + | The more in-depth english description about C-language functions is in: |
[[http:// | [[http:// | ||
- | ~~DISCUSSION~~ |