Вы находитесь на странице: 1из 25

Brno University of Technology

Programming in C Language

Microprocessor Techniques and Embedded Systems Lecture 5

Dr. Tomas Fryza


11-Nov-2011

Contents

Microcontrollers Programming in C Language Application Compilation Programming in C Language

Combination of Assembly and C Language

Source Code Example for ATmega16 (C Language)

Contents

Microcontrollers Programming in C Language Application Compilation Programming in C Language

Combination of Assembly and C Language

Source Code Example for ATmega16 (C Language)

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.

Compilation and linking


source code counter.c

Compiler

object code

counter.o

library code start-up code Linker

executable code

counter.out/.exe

Figure: Compilation of C language source code.

Software development tools


Windows general:
Microsoft Visual Studio http://www.microsoft.com/cze/msdn/vstudio/, C++ Builder http://www.embarcadero.com/products/application-development, Dev-C++; using free tool GCC GNU Compiler Collection http://www.bloodshed.net/devcpp.html, ...

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/, ...

Source Code Compilation

1 2 3 4 5 6 7 8 9 10 11 12

... DDRB = 0 xFF ; temp = 0 x03 ; PORTB = temp ; while ( 1 ) PORTB ;

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

// ( ! ) OUT // ( ! ) DEC R24 RJMP PC 0x0002

Rem.: STS k, Rr - Store Direct to Data Space (0 r 31, 0 k 65 535).


Figure: Compilation of C language source code.

Assembling the application

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

Table: Machine language in Intel HEX format.

... :10009000D4E0DEBFCDBF8FEF87BB83E080936000ED :0800A00088BB88B38150FCCF3E

Intel HEX Format Description

Table: Machine language in Intel HEX format.

... :10 :08

0090 00A0

00 00

D4E0 DEBF CDBF 8FEF 87BB 83E0 8093 6000 88BB 88B3 8150 FCCF 3E

ED

Meaning of control bytes in Intel HEX format:


: 10 0090 00 ... ED Beginning of line. Number of data bytes in one line (in hexadecimal). Address of rst data byte (0x00482=0x0090). Code type (for AVR, 00 coresponds with 64 kB memory page). Data in machine language (LSB rst). Control checksum of one line without :. All bytes are added. From results, the twos complement of LSB is calculated.

Checksum: 08 + 00 + A0 + 00 + 88 + BB + 88 + B3 + 81 + 50 + FC + CF = $5C2 C2 3E.

Basic Principles in C Language Programming

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

int main( void )

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 ...

Figure: Anatomy of a C code.

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 }

c a l l setup function f o r e v e r loop { body o f c o n d i t i o n return value = 1

// 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

Tips on Making Programs Readable

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 ; }

i n t main ( v o i d ) { i n t four ; four = 4 ; printf ( %d \ n , four ) ; r e t u r n 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

According to accessibility, each variable could be:


Local: Allocated in stack or heap memory part while the function is executed. Global: Allocated during compiling process, accessible for all functions.

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

...

Arithmetic and Binary Operations

Table: Arithmetic operations Table: Binary operations

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 ;

Operand ~ << >> & | ^

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

Operand == != < <= > >=

Loops

Forever loop: for Loop: for for( par0; par1; par2 ) { ; } Loop: while while( condition ) { ; } for( ;; ) { ; } Forever loop: while (a) while( 1 ) { ; } (b) while( 1 ) ;

Special Functions of GCC and AVR Libc

Bit value testing in control register:


if( bit is set( register, bit )) { ; } if( bit is clear( register, bit )) { ; }

Loop with bit value testing:


loop until bit is set( register, bit ) { ; } loop until bit is clear( register, bit ) { ; }

Contents

Microcontrollers Programming in C Language Application Compilation Programming in C Language

Combination of Assembly and C Language

Source Code Example for ATmega16 (C Language)

Combination of Assembly and C Language

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" ) ;

Parameters Transfer Between Assembly and C Language

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

Microcontrollers Programming in C Language Application Compilation Programming in C Language

Combination of Assembly and C Language

Source Code Example for ATmega16 (C Language)

Interrupt Service Routine in C Language

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.

ISR Parameters for ATmega16


Vector No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Program Address 0x0000 0x0002 0x0004 0x0006 0x0008 0x000A 0x000C 0x000E 0x0010 0x0012 0x0014 0x0016 0x0018 0x001A 0x001C 0x001E 0x0020 0x0022 0x0024 0x0026 0x0028 ISR Input Parameter INT0 vect INT1 vect TIMER2 COMP vect TIMER2 OVF vect TIMER1 CAPT vect TIMER0 COMPA vect TIMER0 COMPB vect TIMER1 OVF vect TIMER0 OVF vect SPI STC vect USART RXC vect USATRT UDRE vect USART TXC vect ADC vect EE RDY vect ANA COMP vect TWI vect INT2 vect TIMER0 COMP vect SPM RDY vect

Interrupt Service Routine in C Language

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

// i n t e r r u p t s e r v i c e r o u t i n e f o r INT0 // body o f ISR

// i n t e r r u p t s e r v i c e r o u t i n e f o r ADC // body o f ISR

main a p p l i c a t i o n body o f main f u n c t i o n global interrupt enable f o r e v e r loop

// r e t u r n v a l u e = 1

Вам также может понравиться