Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Chapter 12 : Instant C Programming

Introducing you into a brand new world using C language

sheng fei chik

on 6 October 2012

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Chapter 12 : Instant C Programming

Introducing you into a brand new world using C language chapter 13 : Instant C Programming What do you know about C? 1.C is a general-purpose, structured programming language.

2.It is a high-level language that allows the programmer to write a program.

3.The program using C language can be compiled to become executable machine code. This is it Tell you more :) Digital Logic 1.Use number with base 2(binary).
2.Represented by 2 different levels: 0 and 1 or low and high or false and true. Basic Data Types in C Operators 1.Arithmetic Operators 2.Relational Operators 3. Logical Operators 4.Bitwise Operators Not only these! 5.Hierarchy of Operators Variable and Assignment A variable :

1. has a name
2. has an associated type (for example, integer)
3. is used to hold data which can be modified The general form of the declaration is

type variable_name;

1. Type (size): what kind of data will be stored in that location (integer? character? floating point?)
2. variable_name : what is the name of the variable?
3. semi-colon : indicates this is a statement! Examples of variable declaration:

unsigned int score;
float amount;
unsigned char a;

Types of variable:
char, int, short, double, long, float Variable is categorized into local and global variable

A local variable is a variable declared inside a function.
This variable can only be used within the function.

A global variable is a variable declared outside of any functions.
This variable can be used anywhere in the program. What is Assignment?

1. To assign a number to a variable or from a variable to a variable.
2. This assigning works from right to left. Example: *Want some example? This is it: *Variables must be declared before use.But how? Do you know that? Flow Chart Flowcharts are used in designing and documenting complex processes or programs. Like other types of diagram, they help visualize what is going on and thereby help the viewer to understand a process, and perhaps also find flaws, bottlenecks, and other less-obvious features within it. Objective:

1.Workflow Management and Continuous Improvement
To analyze your processes for waste and inefficiency
Identified areas for process improvement

To flesh out their programming logic
Represents an algorithm in graphical symbols

3.Troubleshooting Guides
Narrow the range of possible solutions based on a series of criteria via decision tree
To check/trace the algorithm TO LET YOU KNOW! Different shape represents different function Let me show you an example! There is another important thing! #include <stdio.h>

int main()
float X, Y, Z, S, A, P;

printf("Type in 3 number and press return > ");
scanf("%f%f%f",&X, &Y, &Z);
S = X + Y + Z;
A = S/3;
P = X * Y * Z;
printf("Sum is %.2f\nAverage is %.2f\nProductis %.2f\n ", S, A, P);

return 0;
} In C The Pseudocode This is how you implement Gonna learn this later Before going to the next title, let me show you one last example: Selection Structure Selection Structure allows the program to decide an action based on the user’s input or other processes

if-else if
switch case 1. if-else construct:

statement 1;
statement 2;

If the condition (expression) is true, statement1 is executed and if it is false, statement2 is executed. Example:

if(total_price > 200) {
discount = 5;
} else {
discount = 0;
} 2. if-else if construct:

if (condition _1){
} else if (condition _2){

} else if (condition_n){
} else{
} If condition_1 is true, statement_1 is executed and if it is false, condition_2 is checked. If condition_2 is also false, the condition is checked until it is true. If all the conditions are false, last_statement will be executed. Example:

if(mode == 1) {
LED1 = 1;
} else if (mode == 2) {
LED2 = 1;
} else {
LED1 = 0;
LED2 = 0;
} 3. Switch case:
switch (variable){
case expression_1:
case expression_2:

} Example:
switch (mode){
case 1:
LED1 = 1;
case 2:
LED2 = 1;
LED1 = 0;
LED2 = 0;
A sequence of instructions to be executed repeatedly until a certain condition is reached. There are 2 keywords in repetition structure :
1. while
2. for There are 3 important steps:
1. Variables initialization
2. Testing condition
3. Updating data General form of: 1. while construct : initialization;
} 2. for construct :

for (initialization, condition, updating) {
} Example: to print numbers 1 up to 4 using 1. while construct:
a = 1;
while(a < 5)
printf(“ %d\n“, a); “% a++;
} 2. for construct :
for ( a = 1; a < 5; a++) {
printf(“ %d\n“, a);
} 'while' and 'for' construction both having the same output:
4 Infinite Loop :
When the condition is always true.
Example :
a = 0;
while ( 1) {
a = a + 1;
next instruction; The break Statement:
A break terminates the nearest enclosing switch or loop. Example:
while(1) {
led = 1;
led = 0; Try these Questions : Q1:
while(1) {

n = 0;
while( n < 2) {
printf(“ to UTM”);

for (n = 0; n < 3; n++) {
} Functions Take a 1st look: consist of main function & sub-functions
1. Every program must have one (1) main function. This is the point where the program starts to run.
2. You may call sub-functions from there. Main function Let's see Example: Sub functions 1. You may have several sub-functions in your code.
2. Used to simplify your main functions, and make your code more organized.
3. One sub-function can be called several times from different places within the code – reduce code redundancy. 3 steps in using functions Step 1: Declare the function:

1.The general form of a function prototype declaration is:
return type function_name(type parameter);
void addition(void);
3.Usually at the top of program. Step 2: Define the function:
1.Known as the function definition or function implementation.
2.Write the block of statements (body) of the function to define the procedure that should be done by the function. To define a function, do the steps like this Step 3: Call the function:

1. Known as function call or function invocation.
2. Call the name of the function in order to execute it. Example (main function)

How to write a program for addition, subtraction and multiplication of variable x and variable y?

The general equation is:
sum = x + y
balance = x - y
product = x * y

Given that x = 3 and y = 5.
Sample code:
int x, y, sum, balance, product;
void main(void)
x = 3;
y = 5;
sum = x + y;
balance = x – y;
product = x * y;
} Example for sub-functions How to Program An Autocar? Do you have any idea?
1.A line-following robot is a common type of robot. Robotics competitions usually have a line-following event. The line is usually a black tape on a white surface.

2.The robots sense the line by measuring light reflected off the ground. Introduction We are using MPLAB IDE 1.Preprocessor Directory
To let the microcontroller know the register name that is used in code. If the PIC we use is PIC18f4520, then the directory will be p18f452.h
Format to write:
#include<p18f452.h> 2.Input and output pins declaration

Inputs are the signals or data received by the system.
Ex : Sensors, buttons, mouse, keyboard.
Output are the signals or data sent from it.
Ex: LEDs, motors, printer, monitor, LCD

Format to define pin
Input : PORTAbits.RA0
#define “any name you want” “name of PIC pin (set it input or output)” This is how you should do:

Input : #define button PORTCbits.RC4
Output: #define led LATDbits.LATD0 Your turn now! :) Function Declaration 1. Create function prototype for the function that is being created below the main function. Usually declare at top of program.

2. Example of function prototype:
void forward(void);
void turn(unsigned char direction); Variables Declaration Declare variables to be used in the program. Usually declare at top program.
char mode;
unsigned int speed;
float value; Main Function The code must have ONLY ONE main function, this is where the program start to run. Call sub function at here;
void main (void)
“call sub function”;
} TRIS Register Setting 1. TRIS SETTING is a setting to tell microcontroller that the pin is used as INPUT OR OUTPUT PURPOSE.
2. If TRIS setting is SET WRONGLY, the PROGRAM will UNSTABLE.
3. Usually this setting is written inside the main function.
4. Basically, logic ‘1’ is to set input and logic ‘0’ is to set output. Remember 1 as in Input and 0 as in Output . Just to revise before going into sample code 1.Other function beside main function, declared and defined by user.
2.To make the code organized and tidy.
3.Simplify main function.

//declaration of subfunction
void name_of_your_subfunction(void);

//definition of subfunction
void name_of_your_subfunction(void)
“sub function code goes here”;
} Sample Program Code

Program main function: Use buttons(RA0,RA1) to test LEDs(RA2,RA3)
Components: 2 push buttons, 2 LEDs and related circuit THE END Subfunction What are the output of these questions?
Full transcript