An Introduction to C Adam Gleitman 6.270 – IAP 2014.

Slides:



Advertisements
Similar presentations
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
ARDUINO CLUB Session 1: C & An Introduction to Linux.
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Basic Input/Output and Variables Ethan Cerami New York
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
How to use the CCS compiler with the serial port.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
Numeric Types, Expressions, and Output ROBERT REAVES.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Introduction to Programming
Copyright © – Curt Hill Types What they do.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
ME 120: Arduino Programming Arduino Programming Part II ME 120 Mechanical and Materials Engineering Portland State University
Introduction to Programming the Arduino Dr Gaw 3/21/14.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
ECE Application Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Arduino Programming Part II
Floating Point Math & Representation
Chapter 2 - Introduction to C Programming
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 2 - Introduction to C Programming
Formatting Output.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Arithmetic operations, decisions and looping
Chapter 2 - Introduction to C Programming
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Conversion Check your class notes and given examples at class.
C Programming Getting started Variables Basic C operators Conditionals
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Chapter 2 - Introduction to C Programming
EECE.2160 ECE Application Programming
DATA TYPES There are four basic data types associated with variables:
Setting up a basic program with Arduino
Introduction to C Programming
Presentation transcript:

An Introduction to C Adam Gleitman 6.270 – IAP 2014

What a C Program Looks Like #include <joyos.h> int usetup(void) { return 0; } int umain(void) { // Your code here... functions statements comments preprocessor

A More Interesting Program int umain(void) { // Turn motor 0 on motor_set_vel(0, 200); // Wait 3 seconds pause(3000); // Turn motor 0 off motor_set_vel(0, 0); return 0; }

The Obligatory “Hello World” The printf function writes a particular string to the USB serial port of the HappyBoard. int umain(void) { printf("Hello world!\n"); return 0; } '\n' denotes the end of a line To view the output on your computer: Windows users: Termite or PuTTY Mac/Linux users: $ screen <portname> <baudrate>

Variables int umain(void) { uint8_t x = 12; uint8_t y = 15; uint8_t z = 19; z = x + y; x = 41; x = x - 4; y *= 7; // y = y * 7; z++; // z += 1; x = (y - 6) / (x - z); return 0; } x 41 12 11 37 y 15 105 z 19 28 27

Data Types uint8_t x = 12; This means that x is: unsigned an integer 8 bits wide In other words: 0 ≤ x ≤ 28 – 1

Data Types: Integers Number of bits Signed Unsigned 8 int8_t −27 ≤ x ≤ 27 − 1 −128 ≤ x ≤ 127 uint8_t 0 ≤ x ≤ 28 − 1 0 ≤ x ≤ 255 16 int16_t −215 ≤ x ≤ 215 − 1 −32,768 ≤ x ≤ 32,767 uint16_t 0 ≤ x ≤ 216 − 1 0 ≤ x ≤ 65,535 32 int32_t −231 ≤ x ≤ 231 − 1 −2.15 × 109 ≤ x ≤ 2.15 × 109 uint32_t 0 ≤ x ≤ 232 − 1 0 ≤ x ≤ 4.3 × 109 64 int64_t −263 ≤ x ≤ 263 − 1 −9.22 × 1018 ≤ x ≤ 9.22 × 1018 uint64_t 0 ≤ x ≤ 264 − 1 0 ≤ x ≤ 18.4 × 1018

Data Types: Real Numbers float (32-bit) −3.4 × 1038 ≤ x ≤ 3.4 × 1038 smallest positive value is approximately 1.18 × 10−38 always signed around 7 significant figures of accuracy For the compiler (avr-gcc) we’re using, double is the same as float Examples: float g = -9.80665; float avogadro = 6.022e23; float charge = 1.6e-19;

Printing Values of Variables The special formatters, indicated by %d, are replaced by the values of these variables. int umain(void) { uint8_t x = 32; uint8_t y = 11; uint8_t z = x + y; printf("%d plus %d equals %d\n", x, y, z); printf("%d minus %d equals %d\n", x, y, x-y); return 0; } 32 plus 11 equals 43 32 minus 11 equals 21

Other printf Formatters Description of what will be printed %d A 16-bit integer %03d A 16-bit integer, padded with zeros to occupy 3 digits (e.g., 017) %4d A 16-bit integer, padded with spaces to occupy 4 characters %u A 16-bit unsigned integer %f A floating-point number with six digits of precision (the default) %.3f A floating-point number with three digits of precision %x A hexadecimal number %ld A 32-bit (signed) integer %% A percent sign (%) – this does not require an additional argument A more detailed list of formatters can be found here: http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#gaa3b98c0d17b35642c0f3e4649092b9f1

Conditionals Heading > 90°? Left wheel forwards YES Right wheel backwards YES NO Left wheel backwards Right wheel forwards

Conditionals if (heading > 90.0) { left_wheel_vel = 75; right_wheel_vel = -75; } else { left_wheel_vel = -75; right_wheel_vel = 75; } motor_set_vel(0, left_wheel_vel); motor_set_vel(1, right_wheel_vel);

Conditionals if (heading > 135.0) { left_wheel_vel = 150; right_wheel_vel = -150; } else if (heading > 90.0) { left_wheel_vel = 75; right_wheel_vel = -75; } else { left_wheel_vel = -75; right_wheel_vel = 75; } motor_set_vel(0, left_wheel_vel); motor_set_vel(1, right_wheel_vel); You can run multiple mutually exclusive tests by using else if. You can have as many tests as you want.

You don’t need to include an else statement if you don’t need it. Conditionals if (heading > 88.0 && heading < 92.0) { printf("Close enough."); } Comparators: Boolean operators: x == y equals x && y AND x != y not equals x || y OR x < y less than !x NOT x > y greater than x <= y less than or equal to x >= y greater than or equal to You don’t need to include an else statement if you don’t need it.

Loops: while General form: while (<condition>) { <actions> } Here’s a neat little trick: while (1) { // loop forever int i = frob_read_range(0, 100); printf("The frob is at: %d\n", i); pause(200);

Loops: for General form: for (<initialization>; <condition>; <increment>) { <actions> } This will print out the numbers from 1 through 10: int n; for (n = 1; n <= 10; n++) { printf("%d\n", n);

Example 1: Drive Straight int usetup(void) { gyro_init(11, 1400000L, 1000); return 0; } int umain(void) { while (1) { float deg = gyro_get_degrees(); if (deg < 0) { motor_set_vel(0, 40); motor_set_vel(1, 90); } else { motor_set_vel(0, 90); motor_set_vel(1, 40);

Example 2: Ball Dispenser uint8_t last_bump = false; while (1) { uint8_t cur_bump = (analog_read(8) < 500); if (cur_bump && !last_bump) { servo_set_pos(0, 341); pause(300); servo_set_pos(0, 220); pause(400); } last_bump = cur_bump;

Making Your Own Functions This seems useful. Can we find a way to make this code more reusable? int umain(void) { // ... float d2, d; d2 = (myX - mouseX) * (myX - mouseX) + (myY - mouseY) * (myY - mouseY); d = sqrt(d2); if (d < 10.0) { // mouse within 10 cm? }

Making Your Own Functions return type arguments uint8_t point_near(float x1, float y1, float x2, float y2) { float d2; d2 = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1); return sqrt(d2) < 10.0; } int umain(void) { // ... if (point_near(myX, myY, mouseX, mouseY)) { This must be placed above* any calls we make to it. Now we can call point_near wherever we want without copying and pasting large blocks of code!

Making Your Own Functions void set_drive_speed(int16_t left, int16_t right) { motor_set_vel(0, left); motor_set_vel(1, -right); } void drive_forward() { set_drive_speed(100, 100); void stop(void) { set_drive_speed(0, 0); A function doesn’t have to return a value. In this case, the return type should be void. A function doesn’t have to contain any arguments. In this case, place the word void in between the parentheses or don’t put anything there. You would call these functions as drive_forward() and stop().

Organizing Your Code Better // Declare functions uint8_t point_near(float, float, float, float); int umain(void) { // body of umain } uint8_t point_near(float x1, float y1, float x2, float y2) { // body of point_near Alternative strategy: Declare a function first, and define it later!

Organizing Your Code Better umain.c point_near.c point_near.h #include <joyos.h> #include "point_near.h" int usetup(void) { // ... } int umain(void) { #include "point_near.h" uint8_t point_near( float x1, float y1, float x2, float y2) { // ... } #ifndef __POINT_NEAR_H__ #define __POINT_NEAR_H__ uint8_t point_near( float, float, float, float); #endif Define these new functions in this file. Declare new functions in this header file. To use these functions, #include the header file at the top and pass the corresponding C file into the compiler.

The Makefile # User source files USERSRC = user/robot/umain.c user/robot/point_near.c #AVRDUDE_PORT = /dev/tty.usbserial-0000113D AVRDUDE_PORT ?= com7 #AVRDUDE_USERPORT = /dev/tty.usbserial-A20e1uZB AVRDUDE_USERPORT ?= com7 CC = avr-gcc MCU = atmega128 OBJCOPY = avr-objcopy AVRDUDE = avrdude FTDI_EEPROM = ftdi_eeprom ...

Including Libraries We have provided you with several libraries that may be useful for performing computations. http://www.nongnu.org/avr-libc/user-manual/modules.html For example, <math.h> contains sqrt, trig functions, mathematical constants, etc. <stdlib.h> contains abs, random number generation, etc. To include one of these libraries, put the following line at the top of your code with the appropriate library name: #include <math.h>

Defining Constants You can also define constants like this: #define SQRT_3 1.73205080757 #define GYRO_PIN 11 #define LEGO_STUD_WIDTH 0.8 #define LEGO_BRICK_HEIGHT (1.2 * LEGO_STUD_WIDTH) #define LEGO_PLATE_HEIGHT (LEGO_STUD_WIDTH / 3.0)

x = 5 assigns a new value; it does not check if x equals 5. Common Mistakes x = 5 assigns a new value; it does not check if x equals 5. Instead, use x == 5. int x = 4; if (x = 5) { printf("WTF?!\n"); } float a = 0.3; float b = 0.4; if (a + b != 0.7) { printf("MATH FAIL!\n"); uint8_t n; for (n = 0; n < 300; n++) { printf("%d\n", n); Floating-point arithmetic is subject to rounding error. Instead, check if a + b and 0.7 differ by at most a fixed constant epsilon. uint8_ts have a maximum value of 255. Incrementing n at this value will cause an overflow, and the value will reset to 0. This for loop will never terminate.

Common Mistakes, Fixed int x = 4; if (x == 5) { printf("WTF?!\n"); } float a = 0.3; float b = 0.4; if (abs(a + b - 0.7) >= 1e-6) { printf("MATH FAIL!\n"); uint16_t n; for (n = 0; n < 300; n++) { printf("%d\n", n);

Another Common Mistake int x = 5; int y = 2; if (abs(x / y - 2.5) >= 1e-6) { printf("WRONG!\n"); } if (abs((float) x / y - 2.5) < 1e-6) { printf("Much better!\n"); When dividing two integers, the remainder is dropped. You need to explicitly cast one of the operands of the division to a float in order to get a decimal answer.

Partial Function Reference Description digital_read(pin) Reads the input on a pin. Returns 0 or 1. analog_read(pin) Reads the analog voltage on a pin. Returns a number between 0 and 1023, which correspond to 0 V and 5 V respectively. motor_set_vel(motor, vel) Sets the velocity on the specified motor. vel ranges from −255 to 255. motor_brake(motor) “Brakes” the specified motor. servo_set_pos(servo, pos) Sets the specified servo to a specified position. pos ranges from 0 to 511. servo_disable(servo) Turns off control signals to a servo. This is useful for stopping continuous rotation servos. frob_read_range(low, high) Reads the frob knob. Returns an integer between low and high. pause(millis) Pauses the program for a certain number of milliseconds.

Partial Function Reference Description printf(str, params...) Prints text to the USB port. go_click() Pause the program until the GO button is pressed. stop_click() Pause the program until the STOP button is pressed. go_press() Returns 1 if the GO button is pressed, 0 otherwise. stop_press() Returns 1 if the STOP button is pressed, 0 otherwise. encoder_read(pin) Returns the number of encoder clicks that have happened on a particular encoder since it was last reset. encoder_reset(pin) Resets the counter for the given encoder to 0. get_time() Returns the number of milliseconds that have passed since the HappyBoard was turned on. get_time_us() Returns the number of microseconds that have passed since the HappyBoard was turned on.