introduction to basic programming

19
Introduction to basic programming Kai Zang Jul 2 nd , 2012

Upload: ormand

Post on 23-Feb-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Introduction to basic programming. Kai Zang Jul 2 nd , 2012. Introduction. Programming: one of the tools that you can change lives around you Want to find if a picture has been photoshoped ? Robot, mindrover Game development? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to basic programming

Introduction to basic programming

Kai ZangJul 2nd, 2012

Page 2: Introduction to basic programming

Introduction

• Programming: one of the tools that you can change lives around you– Want to find if a picture has been photoshoped?– Robot, mindrover– Game development?

• For this lecture, the grammar will be based upon C. The contents will ONLY cover our basic use for Arduino.

Page 3: Introduction to basic programming

What’s inside a PC?

Central processing

unit

Memory(ROM) Memory(RAM)

Input device(keyboard,

mouse, gamepads)

Output device(head phones,

monitor)

Page 4: Introduction to basic programming

What about our Teensy???

Central processing

unit

Memory(ROM) Memory(RAM)

Input device (sensors, buttons,

knobs)

Output device(head phones,

monitor)

What if we want to design TAIKO DRUM MASTER?!

Page 5: Introduction to basic programming

So let’s go in further…

• What’s inside a memory?

• Bit: smallest unit, 1 bit reprensent 0 or 1

• Byte = 8 bits• 1kb = 1024 bytes• 1Mb=1024 kb• 1Gb=1024Mb

1 0 0 1 01 1 1 1 00 1 0 0 10 0 1 0 11 1 0 1 10 0 1 1 1

123456

1 2 3 4 5

A typical 6*5 memory array

Page 6: Introduction to basic programming

How do we use bits to represent variable & constant?

• Variable & constant both name a place in memory where you store a Value of a certain Type.

• Their difference: the Value of Variable can be changed, but constant cannot.

• How to represent value with bits?

• Use binary or hexadecimal: (0010 1100)2 = (2c)16=(44)10

• For a series of n bits, we may represent 2n

number in total.

Page 7: Introduction to basic programming

Then what about data type?

• Int (integer, 4 bytes)– Signed: -2147483648 to 2147483647– Unsigned: 0 to 4294967295

• Double (double precision floating point number, 8 bytes)

• Char (character, 1 bytes, ACSII)• bool (boolean value, 1 bytes, true/false)

Page 8: Introduction to basic programming

To represent character ‘a’ in memory

0 1 1 0 01 1 1 1 00 1 0 0 10 0 1 0 11 1 0 1 10 0 1 1 1

123456

1 2 3 4 5

‘Var_name’ will occupy addressFrom (1,1) to (2,3) in our 5*6Memory.

(97)10=(61)16=(0110 0001)2

Syntax:Char var_name = ‘a’;Const int var_name_2 = 12;

Page 9: Introduction to basic programming

Will these make any sense?

• Now let’s see in contra, we use ‘Hack’ to make our hero undead.

Page 10: Introduction to basic programming

Syntax• The following are

examples:• Float x;• Int y=10;• Char letter=‘a’; • char letter=97;• Int num[4]={1,2,3,4};• Char s[5]={‘a’,’b’,’c’,’d’,’e’};• // the size of array is fixed

since its creation

• Definition of ‘=‘• y=y+1; //now y has

become 11• letter = letter + 2; //now

letter has become ‘c’• S[1]= s[1]+2;• // can comment a line• /*• Can comment a paragrah• */

Page 11: Introduction to basic programming

Syntax for operator

• Introduce some of the most basic:– Int x=5, y=2; – Printf(x/y); //output is 2, larger integer < 2.5– Printf(x%y); //% can only be used with integer

module integer, output is 1– Int x=5;– Double y=2;– Printf(x/y); //output is 2.5

Page 12: Introduction to basic programming

Now comes logic commands

• Sequence– Commands are run in

sequence• Int counter=1;• Counter = counter + 1;• Counter = counter + 2;

• Selection– check to see if condition

is met. If yes, run. If no, run otherwise.

• Int counter = 1;• If (counter != 1)

{counter=counter+1;}Else

{counter = counter+2;}

Page 13: Introduction to basic programming

Logic commands (cont.)

• Loop (for)for(initial condition; end condition; loop commands)

• Example:• For(i=1; i<5; i=i+1)

{result=a+b;// i=i+2;

}

• Loop (while)While(ending condition)

• i=1;• While(i<5)

{result=a+b;i=i+1;

}

Page 14: Introduction to basic programming

We plan to make modules for a program and so comes function

• A Function is a series of instructions to run. You pass Arguments to a function and it returns a Value.

• Syntax:• Return_type function_name(argument1, argument2…)• Int addition(int a, int b)

{Int result=a+b;Return result;}

Int result = addition(2, 3); // then result=5

Page 15: Introduction to basic programming

Memory use for function

• Let’s see what happened when run?

Int addition(int a, int b){Int result=a+b;Return result;}

Int result = addition(2, 3); // then result=5

We will come about the definition of global variable, local variable when introducing Arduino later.

ab

result

result

Page 16: Introduction to basic programming

One step back: machine language

• Machine language– That machines can directly read, mianly 0/1

• Assembly language– That becomes easier to work with

• Higher-level language– C , C++, Java, Pascal, BASIC

• Fourth generation language– Visual basic(VB)

Assembler

Assemblycode

Object code

Page 17: Introduction to basic programming

1. Edit2. Preprocess3. Compile4. Link5. Load6. Execute

Disk

Disk

Loader

Linker

Compiler

Preprocessor

Editor

Disk

Disk

Disk

Primary Memory

CPU

Primary Memory

1. Program is created in the editor and stored on disk

2. Preprocessor program processes the code

3. Compiler creates object code and stores it on disk.

5. Loader puts program in memory.

4. Linker links the object code with the libraries

6. CPU takes each instruction and executes it, possibly storing new data values as the program executes

•Phases of C Programs:

A Typical C Program Development Environment

Page 18: Introduction to basic programming

A powerful tool: flow chart

• I do not make ppt for this part but download an introductory ppt from Internet.

• Just search ‘programming flow chart’ in Google.

Page 19: Introduction to basic programming

Thanx for your time!!! XDAny Qs?