introduction to basic programming

Post on 23-Feb-2016

32 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

Introduction to basic programming

Kai ZangJul 2nd, 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?

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

What’s inside a PC?

Central processing

unit

Memory(ROM) Memory(RAM)

Input device(keyboard,

mouse, gamepads)

Output device(head phones,

monitor)

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?!

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

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.

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)

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;

Will these make any sense?

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

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

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

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

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;

}

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

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

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

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

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.

Thanx for your time!!! XDAny Qs?

top related