1 / 19

Introduction to basic programming

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?

ormand
Télécharger la présentation

Introduction to basic programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to basic programming Kai Zang Jul 2nd, 2012

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

  3. What’s inside a PC? Central processing unit Input device (keyboard, mouse, gamepads) Output device (head phones, monitor) Memory(ROM) Memory(RAM)

  4. What about our Teensy??? Central processing unit Output device (head phones, monitor) Input device (sensors, buttons, knobs) Memory(ROM) Memory(RAM) What if we want to design TAIKO DRUM MASTER?!

  5. 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 2 3 4 5 1 2 3 4 5 6 A typical 6*5 memory array

  6. 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 2nnumber in total.

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

  8. To represent character ‘a’ in memory 1 2 3 4 5 1 2 3 4 5 6 (97)10=(61)16=(0110 0001)2 Syntax: Char var_name = ‘a’; Constint var_name_2 = 12; ‘Var_name’ will occupy address From (1,1) to (2,3) in our 5*6 Memory.

  9. Will these make any sense? • Now let’s see in contra, we use ‘Hack’ to make our hero undead.

  10. Syntax • The following are examples: • Float x; • Int y=10; • Char letter=‘a’; • char letter=97; • Intnum[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 • */

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

  12. Now comes logic commands • Sequence • Commands are run in sequence • Intcounter=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;}

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

  14. 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_typefunction_name(argument1, argument2…) • Int addition(int a, int b) { Int result=a+b; Return result; } Int result = addition(2, 3); // then result=5

  15. 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. a b result result

  16. Assembly code Assembler Object code One step back: machine language • Machine language • That machines can directly read, mianly0/1 • Assembly language • That becomes easier to work with • Higher-level language • C , C++, Java, Pascal, BASIC • Fourth generation language • Visual basic(VB)

  17. Phases of C Programs: 1. Program is created in the editor and stored on disk Editor Disk 2. Preprocessor program processes the code Preprocessor Disk 3. Compiler creates object code and stores it on disk. Compiler Disk Linker Disk 4. Linker links the object code with the libraries Primary Memory Loader 5. Loader puts program in memory. Disk Primary Memory CPU 6. CPU takes each instruction and executes it, possibly storing new data values as the program executes A Typical C Program Development Environment 1. Edit 2. Preprocess 3. Compile 4. Link 5. Load 6. Execute

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

  19. Thanx for your time!!! XDAny Qs?

More Related