Академический Документы
Профессиональный Документы
Культура Документы
Programming in C Language
Contents
Contents
C language
Figure: Dennis MacAlistair Ritchie (19412011); D.M.Ritchie, Brian Kernighan. The C Programming Language. 1978.
C89
C90
C99
C1x
First edition of The C Programming Language by Brian Kernighan and Dennis Ritchie (1978, called K&R C) dened the C language, but not the C library. ANSI (American National Standards Institute) developed a new standard in 1989 (called ANSI C). This new standard denes both the language and a standard C library. The International Organization for Standardization adopted a C standard (ISO C) in 1990. ISO C and ANSI C are essentially the same standard. The nal version of the ANSI/ISO standard is often referred to as C89 or C90. In 1994, work began on revising the standard, an eort that resulted in the C99 standard. The actual version of standard is ISO/IEC 9899:1999; last correction (Cor 3) was published 2007-11-15. Working group WG14, responsible for C code standardization, preparing a new standard titled ISO/IEC 9899:201x.
Compiler
object code
counter.o
executable code
counter.out/.exe
Windows AVR:
AVR Studio + WinAVR www.atmel.com/microsite/avr_studio_5/ + http://winavr.sourceforge.net/, CodeVisionAVR http://www.hpinfotech.ro/html/cvavr.htm, IAR Embedded Workbench http://www.iar.com/, Keil http://www.keil.com/, ...
Linux:
gcc-avr, binutils-avr, avr-libc, avrdude, . . . Eclipse (IDE), eclipse-cdt (C/C++ plugin) http://www.eclipse.org/cdt/, ...
1 2 3 4 5 6 7 8 9 10 11 12
LDI LDI OUT OUT SER OUT LDI STS OUT IN SUBI RJMP
R28 , 0 x5F R29 , 0 x04 0 x3E , R29 0 x3D , R28 R24 0 x17 , R24 R24 , 0 x03 0 x0060 , R24 0 x18 , R24 R24 , 0 x18 R24 , 0 x01 PC 0x0003
// // // //
(!) (!) (!) (!) SER OUT LDI R24 0 x17 , R24 R24 , 0 x03 0 x18 , R24
1 2 3 4 5 6 7 8 9 10 11 12 13 14
; ;
A d d r e s s Machine l a n g . Instruction DEscription ====================================================================== 0 x0047 E5CF LDI R28 , 0 x5F ; load immediate 0 x0048 E0D4 LDI R29 , 0 x04 ; load immediate 0 x0049 BFDE OUT 0 x3E , R29 ; o u t t o I /O l o c a t i o n 0 x004A BFCD OUT 0 x3D , R28 ; o u t t o I /O l o c a t i o n 0 x004B EF8F SER R24 ; set register 0 X004C BB87 OUT 0 x17 , R24 ; o u t t o I /O l o c a t i o n 0 x004D E083 LDI R24 , 0 x03 ; load immediate 0 x004E 93800060 STS 0 x0060 , R24 ; s t o r e d i r e c t to data space 0 x0050 BB88 OUT 0 x18 , R24 ; o u t t o I /O l o c a t i o n 0 x0051 B388 IN R24 , 0 x18 ; i n from I /O l o c a t i o n 0 x0052 5081 SUBI R24 , 0 x01 ; s u b t r a c t immediate 0 x0053 CFFC RJMP PC 0x0003 ; r e l a t i v e jump
0090 00A0
00 00
D4E0 DEBF CDBF 8FEF 87BB 83E0 8093 6000 88BB 88B3 8150 FCCF 3E
ED
All commands end by semicolon ;. Bodies of functions, conditions, cycles, etc. are grouped inside the braces {}. Strings are enclosed by quotation marks "". Comments are introduce by double slash //, or are enclosed by /* . . . */. Using of reserved words is strictly observed: for, return, switch, case, if , else, char, int, oat, unsigned, void, . . . Function prototype declares type of return address, number and types od input parameters.
E.g. int imax( int, int ) ;
Function declaration:
return value function name( par0, par1, . . . ) { // body of function }
Structure of C Code
Typical C program
#include
preprocessor instructions
main() is always the rst function called statements functions are made up of statements
function a( ) statements function b( ) statements functions are the building blocks of C declaration assignment function control ...
Function Prototype
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/ H e a d e r f i l e s / #i n c l u d e <a v r \ i o . h>
// h e a d e r
file
f o r MCU
/ F u n c t i o n p r o t o t y p e s / v o i d setup ( v o i d ) ; // s e t u p f u n c t i o n p r o t o t y p e i n t key_pressed ( v o i d ) ; // k e y p r e s s e d f u n c t . p r o t o t y p e / G l o b a l v a r i a b l e s / c h a r temp ; // d e c l a r a t i o n o f 8 b i t v a r . temp i n t main ( v o i d ) { setup ( ) ; // for ( ; ; ) { // i f ( key_pressed ( ) ) ... // } return ( 1 ) ; // } v o i d setup ( v o i d ) { ... } i n t key_pressed ( v o i d ) { // r e t u r n c o d e o f p r e s s e d k e y ... // body o f f u n c t i o n }
// s e t i n p u t / o u t p u t p o r t s // body o f f u n c t i o n
Choose meaningful variable names and use comments. Note that these two techniques complement each other. Using blank lines to separate one conceptual section of a function from another. (C does not require the blank line, but it enhances readability.) Use one line per statement. C has a free-form format. (1)
1 2 3 4 5 6 7
(2)
i n t main ( v o i d ) { i n t four = 4 ; printf ( %d\n , four ) ; return 0 ; }
Variables
All variables are identied by its name and type and must be declared before using:
char temp ; // 8-bit signed number unsigned int ii ; // 16-bit unsigned number oat f = 3.14 ; // 32-bit floating point number // with assignment
Type
char unsigned char signed char int unsigned int oat
Size [b] 8 8 8 16 16 32
Range 128 127 0 255 128 127 32 768 32 767 0 65 535 1,1751038 3,4021038
...
Operation Multiplication Division Modulo division Addition Subtraction Incrementation Decrementation Simplied entry:
a += 3 a++ ; b -= 2 c *= 5 d /= a ; ; ; ; // // // // //
Operand * / % + ++ -Operation Ones complement Left shift Right shift Logical AND Logical OR Logical EX-OR Simplied entry:
a=a+3 a=a+1 b=b-2 c=c*5 d=d/a a b c d |= 3 ; &= 2 ; ^= 5 ; <<= 2 ;
Condition
Table: Condition operators Condition: if if( condition ) { ; } else { ; } Condition switch switch( variable ) { case value0: ; case value1: ; default: ; }
Operation Equal to Not equal to Less than Less than or equal Greater than Greater than or equal
Loops
Forever loop: for Loop: for for( par0; par1; par2 ) { ; } Loop: while while( condition ) { ; } for( ;; ) { ; } Forever loop: while (a) while( 1 ) { ; } (b) while( 1 ) ;
Contents
Programming in C language is simpler, faster, and code could be portable between diferent processor platforms. Compiled code allocated more space in program memory, thus its execution is slower. Common practice is to program entire application in C language, and only critical parts should be reprogrammed in assembly language. In these parts we obtain absolute control of instruction number and program velocity. Assembly language inserting into C language by asm() function:
asm( asm( Compiled: SEI LDI INC "SEI" ) ; "LDI R16, 0x20\nINC R16, 0x20 R16 R16\n" ) ;
For parameters transfer between ASM and C code, the predetermined registers are used:
Input parameter 0: R25:R24, Input parameter 1: R23:R22, ... Input parameter 8: R9:R8, Function return value: R25:R24.
Ex.: extern int add compl( int,int ) ; Calling of ASM function for complex addition from C code: c = add compl( a,b ) ;
First complex operand: a @ R25:R24 Second complex operand: b @ R23:R22 Returned complex addition value: c @ R25:R24
Contents
While interrupt routines are used, the header le interrupt.h have to be included in C source code. For programmers there is no need to:
Stack denition in source code the compiler do it automatically. Know the concrete addresses of interrupt vectors.
ISR (Interrupt Service Routine) is programmed by macro ISR( parameter ), while parameter stands for an interrupt source identication:
INT0 vect External Interrupt Request 0, ADC vect ADC Conversion Complete. ...
All informations and code examples are valid only for GCC with AVR Libc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#i n c l u d e <a v r \ i o . h> // h e a d e r f i l e f o r MCU #i n c l u d e <a v r \ i n t e r r u p t . h> // h e a d e r f i l e f o r ISR ( INT0_vect ) { ... } ISR ( ADC_vect ) { ... } i n t main ( v o i d ) { ... sei ( ) ; while ( 1 ) ; return ( 1 ) ; } // // // //
all
interrupts
// r e t u r n v a l u e = 1