More on C programming

Similar documents
Embedded programming, AVR intro

Programming refresher and intro to C programming

A brief introduction to C programming for Java programmers

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Beginning C Programming for Engineers

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

The C Programming Language Guide for the Robot Course work Module

Computer Organization & Systems Exam I Example Questions

Bit Manipulation in C

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

CS 241 Data Organization Binary

ME 461 C review Session Fall 2009 S. Keres

EL2310 Scientific Programming

Topic 6: A Quick Intro To C

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

C Language Programming

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Expression and Operator

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Exercise Session 2 Simon Gerber

Chapter 7: Preprocessing Directives

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

EL6483: Brief Overview of C Programming Language

ECEN 449 Microprocessor System Design. Review of C Programming

ELEC 377 C Programming Tutorial. ELEC Operating Systems

A Fast Review of C Essentials Part I

Lecture 03 Bits, Bytes and Data Types

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

LAB A Translating Data to Binary

Computers Programming Course 5. Iulian Năstac

Communication. Serial port programming

Outline. 1 About the course


COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

CSCI 2212: Intermediate Programming / C Chapter 15

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

A Shotgun Introduction to C

Lecture 17 Bit Operations

(T) x. Casts. A cast converts the value held in variable x to type T

Storage class and Scope:

Introduction to C Language

A Shotgun Introduction to C

COMsW Introduction to Computer Programming in C

Lectures 5-6: Introduction to C

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

srl - shift right logical - 0 enters from left, bit drops off right end note: little-endian bit notation msb lsb "b" for bit

A Crash Course in C. Steven Reeves

Fundamental of Programming (C)

Conditional Compilation

Computers Programming Course 6. Iulian Năstac

CSE 333 Midterm Exam 2/12/16. Name UW ID#

Computer Systems Programming. Practice Midterm. Name:

Survey. Motivation 29.5 / 40 class is required

COMP322 - Introduction to C++ Lecture 01 - Introduction

EL2310 Scientific Programming

Variables and literals

Basic Pointers. CSCI 112: Programming in C

Fundamentals of Programming

Lecture 2: C Programm

C introduction: part 1

But first, encode deck of cards. Integer Representation. Two possible representations. Two better representations WELLESLEY CS 240 9/8/15

CIS 2107 Computer Systems and Low-Level Programming Fall 2010 Midterm

Bitwise Operators. Fall Jinkyu Jeong GEBD029: Basis and Practice in Programming Fall 2014 Jinkyu Jeong

High Performance Computing

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

Chapter 11 Introduction to Programming in C

Exercise Session 2 Systems Programming and Computer Architecture

Advanced C Programming Topics

Advanced Pointer & Data Storage

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CS240: Programming in C

C - Basics, Bitwise Operator. Zhaoguo Wang

C Formatting Guidelines

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Chapter 11 Introduction to Programming in C

Why embedded systems?

Have examined process Creating program Have developed program Written in C Source code

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Programming (1.0hour)

Chapter 12 Variables and Operators

Lectures 5-6: Introduction to C

CS Programming In C

Chapter 11 Introduction to Programming in C

Selection from Susta:Computer System Structures & John Loomis: Computer organization & M.Mudawar:Computer Architecture & Assembly Language. Cvičení 1.

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Programming. Elementary Concepts

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

CS61, Fall 2012 Midterm Review Section

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Transcription:

Applied mechatronics More on C programming Sven Gestegård Robertz sven.robertz@cs.lth.se Department of Computer Science, Lund University 2017

Outline 1 Pointers and structs 2 On number representation Hexadecimal and binary representation 3 More on bitwise operators Masking and shifting 4 Macros

Pointers A pointer is a variable containing the address of a variable Syntax in declarations prex * means is pointer to int *x; // x is a pointer -to - int in expressions prex * means contents of int i = *x; // i gets the value x points to prex & means address of int *y = &i; // y points to the variable i Pointers and structs More on C programming 2/26

Function calls call-by-value: the value of the argument is copied void f( int x) { printf ("x = %d\n",x); x += 10; printf ("x = %d\n",x); } x is an int variable: a copy of the argument The change to x does not aect the value in the caller call by reference: a pointer to a variable is passed void f( int *x) { printf ("x = %d\n",*x); *x += 10; printf ("x = %d\n",*x); } x is a pointer to an int variable The change to *x changes the value in the caller Pointers and structs More on C programming 3/26

Structures (structs) Structured data, like Java objects without methods Useful for storing a set of variables that belong together Example: 3D point struct 3 dpoint { int x; int y; int z; }; Suggested idiom: Allocate structs on the stack, typically in main() NB! dierence from Java: structs can be local variables and passed by value Pass a pointer to the struct to other functions (call by reference) Pointers and structs More on C programming 4/26

Struct example struct 3 dpoint { int x; int y; int z; }; void myfunction ( struct 3 dpoint *p) { int px = p->x; int py = p->y; int pz = p->z; int main () { struct 3 dpoint mypoint ; } mypoint.x =10; mypoint.y =20; mypoint.z =30; myfunction (& mypoint ); } printf ("%u,%u, %u\n",px,py,pz ); Pointers and structs More on C programming 5/26

Structs, summary declaration and stack allocation struct 3 dpoint mypoint ; struct member access (when mypoint is a variable) mypoint.x function with call-by-reference (pointer) parameter void myfunction ( struct 3 dpoint *p); struct member access through pointer p->x &: addressof get a pointer to a variable & mypoint ; Pointers and structs More on C programming 6/26

Structures (structs) suggested programming idiom Allocate on the stack, typically in main() declare in a scope with longer lifetime than all uses returning a pointer to a local variable gives undened behaviour Pass a pointer to the struct (call by reference) Use the address of-operator (&) Note how to access elds mystruct.x or mypointer ->x Call by reference can also be used for primitive type parameters to allow a function to change its arguments (but don't overuse it) Pointers and structs More on C programming 7/26

Representation of numbers Positional number systems The decimal system: multiples of 10. (Base 10) Example : 1502 10 = 1 10 3 + 5 10 2 + 0 10 1 + 2 10 0 base 2: binary digits: {0, 1} Example : = 1101 2 = 1 2 3 + 1 2 2 + 1 2 0 = 13 base 16: hexadecimal digits: {0... 9, a... f} Example : 0x73 = 73 16 = 7 16 1 + 3 16 0 = 112 + 3 = 115 On number representation : Hexadecimal and binary representation More on C programming 8/26

Convert to hexadecimal from binary Convert 0101 1100 2 to hex: bits 0... 3 : 1100 2 = 0 2 0 + 0 2 1 + 1 2 2 + 1 2 3 = = 12 10 = c 16 bits 4... 7 : 0101 2 = 1 2 4 + 0 2 5 + 1 2 6 + 0 2 7 = = 16 + 64 = 5 16 = 50 16 which gives the result (in hex) : 50 + c = 5c (= 92 10 ) Observation: Each hexadecimal digit corresponds to 4 bits One byte is two 4 bit nibbles, i.e., hex digits On number representation : Hexadecimal and binary representation More on C programming 9/26

Hexadecimal to binary Convert 5a (0x5a) to binary: the value of each nibble can be calculated independently i.e., you only need to use the values {1, 2, 4, 8} for each hexadecimal digit regardless of how big the number is high nibble: 5 = 4 + 1 = 0101 2 low nibble: a = 8 + 2 = 1010 2 which gives the result: 5a = 0101 1010 On number representation : Hexadecimal and binary representation More on C programming 10/26

Representation of numbers Binary (base 2) and hexadecimal (base 16) representations are more convenient than decimal for doing bit operations In binary, each bit is directly represented In hex, each digit corresponds to 4 bits (a nibble) Converting to/from decimal is tedious; there is no simple way to nd the value(s) of a (set of) bit(s) On number representation : Hexadecimal and binary representation More on C programming 11/26

Constants in C and Java Binary digits: 0 1 constants in gcc and Java 7 are prexed by 0b e.g., 0b0001101011110100 Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 a b c d e f constants in C and Java are prexed by 0x e.g., 0x1af4 Hexadecimal notation is much more readable On number representation : Hexadecimal and binary representation More on C programming 12/26

Bitwise operators Bitwise operators evaluate to a number Works on the binary representation of the operand(s) Example: bitwise not (complement) x - in the binary representation of x, invert each bit unsigned char x,y; // 8 bits long x = 10; y = ~x; x = 10 = 0x0a = 0000 1010 y = 1111 0101 = 0xf5 = 245 More on bitwise operators More on C programming 13/26

Bitwise operators x - not x & y - and the result is x with each bit inverted the result has a 1 (one) in each bit position where the bit value of both x and y ==1 x y - (inclusive) or the result has a 1 (one) in each bit position where the bit value of either x or y ==1 x ˆ y - exclusive or the result has a 1 (one) in each bit position where the bit value of exactly one of x or y ==1 More on bitwise operators More on C programming 14/26

Bitwise operators, examples 1111 0101 = 0000 1010 1110 0111 & 0111 1100 = 0110 0100 0010 1010 1100 0001 = 1110 1011 & is used for bit masking and for clearing bits is used for setting bits ˆ is used for toggling bits More on bitwise operators More on C programming 15/26

Masking and setting bits Example: A register : CTRL_REG A bitmask : FLAGS Named bits : ENABLE_X, ENABLE_Y,... Clear the bits specied by FLAGS and set individual bits. // clear FLAGS using bitmask CTRL_REG = CTRL_REG & ~ FLAGS ; // set individual bits CTRL_REG = CTRL_REG ENABLE_X ENABLE_Y ; Can also be written: CTRL_REG &= ~ FLAGS ; CTRL_REG = ENABLE_X ENABLE_Y ; More on bitwise operators : Masking and shifting More on C programming 16/26

Shift operators In addition to the bitwise logical operators, C and Java has operators for shifting the bits in a number x << n shifts x n steps to the left x >> n shifts x n steps to the right Example: 2 << 2 == 8 (0b0010 << 2 == 0b1000) Shifting can be viewed as multiplication or division by powers of two. More on bitwise operators : Masking and shifting More on C programming 17/26

Masking and shifting Low level drivers often access hardware registers where a single, or a few, bits control something Example: motor servo control word (16 bits) - - - posref[8] velref[4] enable Reading and writing posref: unsigned short cr;// temporary for register value unsigned char pr; // temporary for posref value cr= read_ctrl_reg (); pr = ( cr >> 5) & 0 xff ; // shift and mask cr = cr & ~(0 xff << 5); // clear posref bits pr = pr + 10; cr = cr ( pr << 5); // new value for register write_ctrl_reg ( cr ); More on bitwise operators : Masking and shifting More on C programming 18/26

C preprocessor macros A macro is a fragment of text that has been given a name The C preprocessor replaces every occurrence of a name with its contents Using macros for constants makes it easier (and safer) to change them, compared to writing the same literal at many places Macros can be used to select between dierent variants (#if #ifdef #ifndef #else ) Macros can be given a value on the compiler command line (gcc -DMYMACRO=value...) Macros can have parameters, but it is not always trivial what that means. Useful for simple things. Macros More on C programming 19/26

C preprocessor macros Example: include guards Each declaration must appear exactly once A header le may be included several times (e.g., via other header les) Solution: include guards. Example: in the le example.h # ifndef EXAMPLE_H # define EXAMPLE_H struct example_data { char * buf ; size_t size ; int something ; }; int example_func1 ( struct example_data *); int example_func2 (); # endif Macros More on C programming 20/26

C preprocessor macros Examples: named constant Source code: # define BUFSIZE 80 char buf [ BUFSIZE ];... read (fd, buf, BUFSIZE ); After preprocessor: char buf [80];... read (fd, buf, 80); Macros More on C programming 21/26

C preprocessor macros Examples: debugprint.h # ifdef DEBUG # include <stdio.h> # define debug_s (s) printf (" DEBUG : %s\n", s) # define debug_d (s,d) printf (" DEBUG : %s%d\n", s, d) # define debug_x (s,d) printf (" DEBUG : %s%x\n", s, d) # else # define debug_s (s) # define debug_d (s,d) # define debug_x (s,d) # endif } Macros More on C programming 22/26

# include " debugprint.h" # include <stdio.h> int main () { debug_s (" Testing macros "); int sum = 0; int i; debug_s (" summing multiples of 20"); for (i=0; i <128; i +=20){ debug_x ("i=0x",i); sum += i; debug_d (" sum =",sum ); debug_x (" sum =0x",sum ); } printf (" Sum is: %d\n", sum ); return 0; } Macros More on C programming 23/26

With DEBUG dened, the code becomes int main () { printf (" DEBUG : %s\n", " Testing macros "); } int sum = 0; int i; printf (" DEBUG : %s\n", " summing multiples of 20"); for (i=0; i <128; i +=20){ printf (" DEBUG : %s%d\n", "i=", i); printf (" DEBUG : %s%x\n", "i=0x", i); sum += i; printf (" DEBUG : %s%d\n", " sum =", sum ); printf (" DEBUG : %s%x\n", " sum =0x", sum ); } printf (" Sum is: %d\n", sum ); return 0; Macros More on C programming 24/26

If DEBUG is not dened, the code becomes int main () { ; } int sum = 0; int i; ; for (i=0; i <128; i +=20){ ; ; sum += i; ; ; } printf (" Sum is: %d\n", sum ); return 0; Macros More on C programming 25/26

# ifndef BAR # define BAR 2 # endif int main () { #if BAR == 1 printf (" BAR is one \n"); # elif BAR ==3 printf (" BAR is three \n"); # else printf ("I don t care about bar if it s %d\n", BAR ); # endif return 0; } Compiled with gcc -o macros macros.c, running it prints I don t care about bar if it s 2 Compiled with gcc -o macros -DBAR=3 macros.c, running it prints BAR is three Macros More on C programming 26/26