AVR Microcontroller Programming with C ~ 17th Dimension

Sunday, May 22, 2011

AVR Microcontroller Programming with C

Programming Language C for AVR widely used C language for programming a variety of devices, including microcontrollers. This language is already a high level language, which allows programmers pouring algorithm. To know basic C language can be studied as follows.

1. Coding Structure
# Include <[library1.h]> / / Optional
# Include <[library2.h]> / / Optional
# Define [name1] [value]; / / Optional
# Define [name2] [value]; / / Optional
[Global variables] / / Optional
[Functions] / / Optional
void main (void) / / Main Program
{[Declaration of local variable / constant] [Contents Main Program]}

2. Data type
char: 1 byte (-128 s / d 127)
unsigned char: 1 byte (0 to 255)
int: 2 bytes (-32 768 s / d 32 767)
unsigned int: 2 bytes (0 s / d 65 535)
Long: 4 bytes (-2,147,483,648 s / d 2147483647)
unsigned long: 4 bytes (0 to 4,294,967,295)
float: decimal
array: a collection of the same data type.

3. Declaration of variables and constants
The variable is a data storage memory whose value can be changed.
Writing: [Data type] [name] = [value];
Constant is data storage memory whose value can not be changed.
Writing: const [name] = [value];
Supplement: Global variables / constants that can be accessed throughout the program.
Local variables / constants that can only be accessed by the function of the declaration.

4. Statement
Statement means any operation in programming, should end with [;] or [}]. Statement will not be executed if preceded by a [/ /] for a single line. More than 1 line to use the couple [/ *] and [* /]. Statement is not executed is also called comments / commentary.
Example: temperature = adc/255 * 100; / / example of formula calculation temperature

5. Function Function
is part of a program that can be called by the main program.
 Writing: [type data] [function name] ([type of data input 1], [type of data input 2]) {[statement];}

6. Conditional if else statements and loops
used for the selection condition
if ([requirements]) {[statement1]; [statement2];} else {[statement3]; [statement4];}
for: used for loops with a known amount
for ([initial value]; [requirements]: [the operation value]) {[statement1]; [statement2];}
while loop: used to loop if and flesh, certain eligible
while ([requirements]) {[statement1]; [statement2];}
do while loop: used to loop if and flesh meet certain requirements, but min 1 time
do {[statement1]; [statement2];} while ([requirement])
switch case: used for selection with many conditions
switch ([variable name]) {case [value1]: [statement]; break; case [value2]: [statement]; break;}

7. Operation logic and binary logic
AND: & &
NOT:!
OR: | |
Binary AND: &
OR: |
XOR: ^
Shift right:>>
Shift left: <<
Complement: ~

8. Relational operations (comparisons)
Equal to: ==
Not equal:! =
Bigger:>
Bigger equals:> =
Smaller: <
Smaller equal to: <=

9. Arithmetic operations
+, -, *, /: Add, less, times, for
+ =, -=, *=, / =: Value to the left of the operator on the plus / less / multiply / divide by the value of the right of the operator
%: Remainder for
+ +, -: Plus one (increment), less one (decrement)
Example:
a = 5 * 6 + 2 / 2 -1;
then the value of a is 30 a *= 5;
if the initial value of a is 30, then the value of a = 30x5 = 150. a + = 3;
if the initial value of a is 30, then the value of a = 30 +5 ​​= 33. a + +;
if the initial value of a is 5 then the value of a = a +1 = 6. a -;
if the initial value of a is 5 then the value of a = a-1 = 4.

AVR Microcontroller Programming with C

0 comments:

Post a Comment