0% found this document useful (0 votes)
0 views30 pages

Week2 - Chapter 4 Programming and Compiler

The document provides an overview of microcontroller programming, covering essential concepts such as variables, data types, operators, control statements, looping statements, functions, strings, and delay functions. It explains how compilers translate human-readable code into machine code for microcontrollers and details the use of various programming constructs to control execution flow and manage data. Additionally, it discusses string manipulation and the importance of timing in programming through delay functions.

Uploaded by

ANAS MOHD NOOR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views30 pages

Week2 - Chapter 4 Programming and Compiler

The document provides an overview of microcontroller programming, covering essential concepts such as variables, data types, operators, control statements, looping statements, functions, strings, and delay functions. It explains how compilers translate human-readable code into machine code for microcontrollers and details the use of various programming constructs to control execution flow and manage data. Additionally, it discusses string manipulation and the importance of timing in programming through delay functions.

Uploaded by

ANAS MOHD NOOR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

NMJ21304

-MICROCONTROLLER AND INTERFACES-


BIOMEDICAL ELECTRONIC ENGINEERING PROGRAM
YEAR 2, SEMESTER 2
© Anas Mohd Noor, 2023.

All rights reserved. No part of this document may be reproduced or


transmitted in any forms by any means, electronic, mechanical, or
otherwise, whether now or hereafter devised, including photocopying,
recording, or by any information storage and retrieval system without
express written prior permission from the author
CHAPTER 4: MICROCONTROLLER PROGRAMMING
AND COMPILER

4.1 Introduction
4.2 Variables and Data
4.3 Arithmetic, Logic and Comparison Operators
4.4 Control Statements
4.5 Looping Statements
4.6 Function
4.7 String
4.8 Delay Function
4.1 Introduction

Microcontroller cannot read source-code written by human (C,C++,JAVA,Python,Assembly….)

To translate the source code into a form that the microcontroller can understand, a software tool called a compiler is used.

The compiler performs a series of steps to convert the source code into machine code, which is in binary format (1s and 0s)

The machine code that can be executed by the microcontroller.


4.1 Introduction Compiler

The microcontroller executes each instruction in the machine code


4.2 Variables and Data

A variable is a storage location in a program that holds a value, while a data type is a classification of that value based on the
type of data it represents.

A variable is like a container that can hold a value, and the data type of that variable determines the kind of value it can hold
and the operations that can be performed on it
4.2 Variables and Data

Example data

i. An integer is a whole number (positive or negative) and can be represented by the data types int or long.
( 0,1,2,3,-4,-5,-6)
ii. A floating-point number is a decimal number and can be represented by the data types float or double.
(0.1,1.2,3.4,5.6)
iii. A Boolean variable can hold the values true or false and is used for logical operations. (C++ True/False)
(C 0(false), 1(true)
iv. A character variable can hold a single letter, number, or symbol and is represented by the data type char.
(A,a,B,b,<*&^%)
v. A string variable can hold a sequence of characters and is represented by the data type string. (my name
is anas)
vi. An array is a collection of variables of the same data type and can be represented by the data type array.
vii. A structure is a collection of variables
viii.An enumeration is a data type that consists of a set of named values and can be represented by the data
type enum.
4.3 Arithmetic, Logic and Comparison operators

Arithmetic
4.3 Arithmetic, Logic and Comparison operators

Logic Operator
4.3 Arithmetic, Logic and Comparison operators

Relational Operator
4.3 Arithmetic, Logic and Comparison operators
4.4 Control Statements

Control statements are used in programming to control the flow of execution of a program. They are used to make decisions
and to repeat certain actions based on certain conditions
4.4 Control Statements

In the while loop, we initialize a variable i to 0 and execute the following


code while i is less than 5. We print the value of i and increment it by 1
in each iteration of the loop.

In the do..while loop, we again initialize i to 0 and execute the following


code at least once, and continue to execute the code while i is less than
5. We print the value of i and increment it by 1 in each iteration of the
loop.

In the for loop, we initialize i to 0 and execute the following code for i
from 0 to 4. We print the value of i in each iteration of the loop.
4.4 Control Statements

In the break statement, we have a for loop that executes the following
code for i from 0 to 9. When i is equal to 5, we exit the loop using the
break statement.

In the goto statement, we have a label called loopStart. We initialize i


to 0 and print its value using the Serial library. We increment i by 1 and
check if it's less than 5. If i is less than 5, we jump back to the loopStart
label using the goto statement.

In the continue statement, we have another for loop that executes the
following code for i from 0 to 9. When i is even, we skip that iteration
using the continue statement and move on to the next iteration of the
loop.
4.4 Control Statements

In the if..else statement, we have a variable num set to 2. We check if num is


equal to 1, and if so, we print "Number is 1". If num is not equal to 1, we check
if num is equal to 2, and if so, we print "Number is 2". If num is neither 1 nor 2,
we print "Number is not 1 or 2".

In the switch..case statement, we again have num set to 2. We check the value
of num and execute the corresponding case. If num is equal to 1, we print
"Number is 1". If num is equal to 2, we print "Number is 2". If num is neither 1
nor 2, we execute the default case and print "Number is not 1 or 2".
4.5 Looping Statements
4.5 Looping Statements

A while loop repeatedly executes a block of code as long as a specified condition is true. The code inside the loop will
continue to execute until the condition becomes false.

A for loop allows you to iterate over a range of values, executing a block of code for each value in the range. It has three
parts: an initialization, a condition, and an update. The loop executes as long as the condition is true and updates the loop
variable on each iteration.

A do-while loop is similar to a while loop, but it guarantees that the loop body executes at least once, even if the condition is
initially false. The loop continues to execute as long as the condition is true.

A nested loop is a loop inside another loop. It allows you to execute a block of code multiple times, with the inner loop
executing completely each time the outer loop runs. This can be useful for iterating over multi-dimensional arrays or
performing complex calculations.
4.5 Looping Statements

The while loop will repeat the block of code inside it as long as the value of the
counter variable is less than 10. Inside the loop, we print the current value of counter,
increment it by 1, and wait for 1 second using the delay function. Once the loop
finishes, we print a message and wait for 5 seconds before starting the loop again. We
while loop also reset the counter variable to 0 before starting the loop again.
4.5 Looping Statements

The do-while loop will first execute the block of code inside it, which prints the current value
of the counter variable, increments it by 1, and waits for 1 second using the delay function.
The loop will continue to repeat the block of code as long as the value of counter is less than
10. Once the loop finishes, we print a message and wait for 5 seconds before starting the
loop again. We also reset the counter variable to 0 before starting the loop again.
do-while loop
4.5 Looping Statements

The for loop will repeat the block of code inside it 10 times, starting from 0 and
incrementing the loop variable i by 1 on each iteration, until i is no longer less than 10.
Inside the loop, we print a message indicating the current iteration number and wait for
1 second using the delay function. Once the loop finishes, we print a message and wait
for loop for 5 seconds before starting the loop again.
4.5 Looping Statements

we use a nested loop to create a pattern of 1s and 0s, with the number of rows and
columns specified by the rows and cols variables. Inside the nested loop, we check if the
sum of the loop variables i and j is even, and print a 1 or 0 depending on the result. We
then print a newline after each row is printed. Once the pattern finishes, we print a
nested loop
message and wait for 5 seconds before starting the pattern again.
4.6 Function

A function is a block of code that performs a specific


task.

Used to organize and structure code by breaking it


down into smaller, reusable pieces.

Code easier to understand, maintain, and debug.

One or more input parameters, perform some


operations on them, and return a value or result.

Functions can also be defined to not return any value.

Functions are typically defined at the beginning of a


program, and can be called from anywhere in the code
where they are needed
4.6 Function

function

a=5
b=10
c=3
a+b-c result

In the above code, we have a function called addSubtract that takes in


three integers (a, b, and c). The function calculates the result of a+b-c
and returns the result.

In the loop function, we define three variables (num1, num2, and


num3) and initialize them with values of 5, 10, and 3, respectively. We
then call the addSubtract function and pass in the three variables as
arguments. We store the result in a variable called result and print the
result to the serial monitor using the Serial library.
4.7 String

A string is a sequence of characters that represents text. Strings are used to store and manipulate text data, such as words,
sentences, and paragraphs

There are two main types of strings in Arduino:


char arrays: These are arrays of characters that are used to store strings. They are defined using the char data type
and square brackets, like this: char myString[] = "Hello World!";. The size of the array must be specified or it should be
large enough to store all characters in the string.

String objects: These are objects of the String class that are used to store strings. They are defined using the String
data type, like this: String myString = "Hello World!";. String objects are more flexible and powerful than char arrays,
as they have methods that can be used to manipulate the string, such as toUpperCase(), toLowerCase(), concat() and
many more.
4.8 Delay

The delay(), delayMicroseconds(), millis(), and micros() functions are used to introduce a delay or measure time intervals.
There are four types of delay in Arduino:

delay(ms): This function is used to introduce a delay of a specified number of milliseconds (ms) in the program. For example,
delay(1000) will introduce a delay of 1 second. The delay function stops the execution of the program for the specified time,
and nothing else can happen during that time.

delayMicroseconds(us): This function is used to introduce a delay of a specified number of microseconds (us) in the program.
For example, delayMicroseconds(1000) will introduce a delay of 1 ms. The delayMicroseconds function stops the execution of
the program for the specified time, and nothing else can happen during that time.

millis(): This function returns the number of milliseconds (ms) since the Arduino board began running the current program. It
can be used to measure time intervals or to implement time-based functions.

micros(): This function returns the number of microseconds (us) since the Arduino board began running the current program.
It can be used to measure time intervals or to implement time-based functions.
4.8 Delay

In this example, the built-in LED on the Arduino board is In this example, the built-in LED on the Arduino board is turned
turned on for 1 second, then turned off for 1 second, and on for 500 microseconds, then turned off for 500
the cycle repeats. microseconds, and the cycle repeats.
4.8 Delay

In this example, the built-


in LED on the Arduino
board is turned on after 1
second has passed since
the program started
running.

In this example, the built-


in LED on the Arduino
board is turned on after 1
second has passed since
the program started
running, but this time
we're using micros()
instead of millis()
Summary

The topic covers fundamental concepts of programming, including variables, data types, arithmetic, logic and
comparison operators, control statements, looping statements, functions, strings, and delay functions.

4.1 Introduction: This section provides an overview of compiler used in programming microcontroller. The compiler
analyzes the human readable source code, performs various optimizations, and generates machine code that can be
run on a target microcontroller.

4.2 Variables and Data: This section covers the concept of variables and data types in programming. Variables are
used to store data in memory, and data types define the type of data a variable can store. Some common data types
include integers, floating-point numbers, strings, and Boolean values.

4.3 Arithmetic, Logic, and Comparison Operators: This section covers the various operators used in programming to
perform arithmetic, logical, and comparison operations. Arithmetic operators include addition, subtraction,
multiplication, and division. Logical operators include AND, OR, and NOT, while comparison operators include equals,
not equals, less than, and greater than.
Summary

4.4 Control Statements: This section covers the various control statements used in programming to control the flow of
code execution. Examples of control statements include if-else statements, switch statements, and loops.

4.5 Looping Statements: This section covers looping statements used in programming to execute a block of code
repeatedly. The most common looping statements are the for loop, while loop, and do-while loop.

4.6 Functions: This section covers the concept of functions in programming, which are reusable blocks of code that
perform a specific task. Functions can be defined to take input parameters and return output values.

4.7 Strings: This section covers the concept of strings in programming, which are sequences of characters used to
represent text. The section covers string manipulation techniques such as concatenation, substring extraction, and case
conversion.

4.8 Delay Function: This section covers the concept of the delay function, which is used to pause program execution for a
specified period. The delay function is commonly used in applications such as embedded systems and robotics, where
timing is critical.

You might also like