4 - Chapter 3 C Programming - 1 - 2024

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 44

‫بسم هللا الرحمن الرحيم‬

‫والصالة والسالم على سيدنا محمد‬


‫صلى هللا عليه وسلم‬
Chapter 3: C programming
How to develop software for an embedded system
Know declarations: simple data types,
( char, short, long (unsigned and signed)
• Know the form of the mandatory main subroutine for a
program to be executable
• Know the basic assignment statement: variable = expression;
• Know how to use printf and scanf for I/O.
• Know the basic form of the if-statement and
the while(1) statement and how they use the conditional
Boolean expression.
• Know the importance of functions and understand the use
with inputs and outputs.
• C is a structured language
• Structured programs in C are built from three basic
templates: the sequence, the conditional(decision), and
the while-loop (iterative- It has a decision, but if the
decision is true, then we'll perform some function).
• . the “block” contains simple and well-defined commands,
Area = Height*Width;
I/O functions are also low-level building blocks.
• C is a structured language
• Structured means that there are templates.
• The first template is sequential, which means we
do 1, and then we do 2.
• The second template is a decision.
• the third is an iterative template. It has a decision,
but if the decision is true, then we'll perform some
function.
• C
Z = X + Y;
• Assembly
ADD R2,R1,R0 R2=R1+R0
• Machine Code
0xEB010200 32 bit,16 bit code
• Integrated Development Environments
IDE
Software development on Keil
// 1.Documentation Section
// main.c
// Authors: ----------
// Date: --------------

// 2. Pre-processor Directives Section


// INCLUDES: Lists of all libraries you will refer to using
// symbolic names instead of addresses
// 3. Subroutines Section
// MAIN: Mandatory for a C Program to be executable
int main(void)
{

}
return no input

// when void is used as a function return type, it indicates that the


function does not return a value.
Section 1.1: C Data types
• Char
The char data type is a byte size data whose bits are
designated as D7-D0. It can be signed or unsigned.
In the signed format the D7 bit is used for the + or -
sign and takes values between -128 to +127.
In the unsigned char we have values between 0x00
to 0xFF in hex or 0 to 255 in decimal since there is no
sign and the entire 8 bits are used for the magnitude.
The ARM microcontrollers use 4 bytes of memory
space for 8-bit peripheral I/O ports.
Section 1.1: C Data types for Embedded systems
• Char
The char data type is a byte size data whose bits are
designated as D7-D0.
• In the signed format the D7 bit is used for the + or -
sign and takes values between -128 to +127.
• In the unsigned char we have values between 0x00
to 0xFF in hex or 0 to 255 in decimal since there is
no sign and the entire 8 bits are used for the
magnitude.
• Short
The short data type is a 2-byte size data whose bits
are designated as D15-D0. It can be signed or
unsigned.
• In the signed format, the D15 bit is used for the +
or - sign and takes values between -32,768 to
+32,767.
• In the unsigned short we have values between
0x0000 to 0xFFFF in hex or 0 to 65,535 in
decimal since there is no sign and the entire 16
bits are used for the magnitude.
• long
• In the signed format the D31 bit is used for the +
or - sign.
• In the unsigned long we have values between
0x00000000 to 0xFFFFFFFF in hex.
• long long
• The long long data type is an 8-byte size data
whose bits are designated as D63-D0. It can be
signed or unsigned.
• In the signed format the D63 bit is used for the +
or – sign.
• In the unsigned long long we have values
between 0x0000000000000000 to
0xFFFFFFFFFFFFFFFF in hex.
Section 1.2: Bit-wise Operations in C
• bitwise operators AND (&), OR (|), XOR (^),
• inverter (~), shift right (>>), and shift left (<<).
These bit-wise operators are widely used in
software engineering for embedded systems and
control.
• The following shows some examples using the
C bit-wise operators:
• 0x35 & 0x0F results in 0x05 /* ANDing */
• 0x04 | 0x68 results in 0x6C /* ORing */
• 0x54 ^ 0x78 results in 0x2C /* XORing */
• ~0x55 results in 0xAA /* Inverting 0x55 */
& | ^ ~
00110101 00000100 01010100 01010101
00001111 01101000 01111000
00000101 01101100 00101100 10101010
05 6C 2C AA
#include "TExaS.h"
#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))
#define GPIO_PORTF_DIR_R (*((volatile unsigned long *)0x40025400))
#define GPIO_PORTF_AFSEL_R (*((volatile unsigned long *)0x40025420))
#define GPIO_PORTF_PUR_R (*((volatile unsigned long *)0x40025510))
#define GPIO_PORTF_DEN_R (*((volatile unsigned long *)0x4002551C))
#define GPIO_PORTF_LOCK_R (*((volatile unsigned long *)0x40025520))
#define GPIO_PORTF_CR_R (*((volatile unsigned long *)0x40025524))
#define GPIO_PORTF_AMSEL_R (*((volatile unsigned long *)0x40025528))
#define GPIO_PORTF_PCTL_R (*((volatile unsigned long *)0x4002552C))
#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
• “volatile“. By low level programming, we mean a
piece of C code which is dealing with peripheral
devices, IO ports (mainly memory mapped IO
ports), Interrupt Service Routines (ISRs) which
interact with Hardware.
• a compiler converts C code to Machine code so that
the executable can be run without having actual
source code.
• While translating Source code to Machine code,
compilers typically try to optimize the output so
that lesser Machine code needs to be executed
finally. One such optimization is removing
unnecessary Machine code for accessing variable
which is not changing from Compiler’s perspective.
while (status == 0)
{
/*Let us assume that status isn't being changed
in this while loop or may be in our whole program*/

/*So long as status (which could be reflecting


status of some IO port) is ZERO, do something*/
}
An optimizing Compiler would see that status isn’t
being changed by while loop. So there’s no need to
access status variable again and again after each
iteration of loop. So the Compiler would convert this
loop to a infinite loop i.e. while (1) so that the
Machine code to read status isn’t needed.
• Example 1-3
Run the following program on your simulator and examine the results.
int main(void)
{
volatile unsigned char temp; /* declare volatile otherwise the optimizer will remove it.
*/
temp = 0x35 & 0x0F; /* ANDing : 0x35 & 0x0F = 0x05 */
temp = 0x04 | 0x68; /* ORing : 0x04 | 0x68 = 0x6C */
temp = 0x54 ^ 0x78; /* XORing : 0x54 | 0x78 = 0x2C */
temp = ~0x55; /* Inverting : ~0x55 = 0xAA */
}
/* applied to a variable when it is declared. It tells the
compiler that the value of the variable may change at any
time--without any action being taken by the code the
compiler finds nearby. */
Setting and Clearing (masking) bits

• OR can be used to set a bit, and


• AND can be used to clear a bit.
• Anything ORed with a 1 results in a 1;
• Anything ORed with a 0 results in no change.
• Anything ANDed with a 1 results in no change;
• Anything ANDed with a 0 results in a zero.
• Anything EX-ORed with a 1 results in the complement;
• Anything EX-ORed with a 0 results in no change.
• The function called at program startup is
named main.
• It shall be defined with a return type of int and with
no parameters:
int main(void)
{

}
when void is used as a function return type, it
indicates that the function does not return a value.
• In most computer programming languages, a while loop
is a control flow statement that allows code to be
executed repeatedly based on a given Boolean
condition. The Boolean condition is either true or false
while(1)
• It is an infinite loop which will run till a break statement
is issued explicitly. Interestingly not while(1) but any
integer which is non-zero will give the similar effect as
while(1). Therefore, while(1), while(2) or while(-255),
all will give infinite loop only.
while(1) or while(any non-zero integer)
{
// loop runs infinitely
}
• The return value of the main function is
considered the "Exit Status" of the application.
On most operating systems returning 0 is a
success status like saying
"The program worked fine".
Setting and Clearing (masking) bits

• OR can be used to set a bit, and


• AND can be used to clear a bit.
• Anything ORed with a 1 results in a 1;
• Anything ORed with a 0 results in no change.
• Anything ANDed with a 1 results in no change;
• Anything ANDed with a 0 results in a zero.
• Anything EX-ORed with a 1 results in the complement;
• Anything EX-ORed with a 0 results in no change.
• Example 1-4--int main(void)
when void is used as a function return type, it indicates that the
function does not return a value.
The following program toggles only bit 4 of var1 continuously
without disturbing the rest of the bits.
int main(void)
{
volatile unsigned char var1;
while(1)
{
var1 = var1 | 0x10; /* Set bit 4 (5th bit) of var1 0001 0000 */
var1 = var1 & 0xEF; /* Clear bit 4 (5th bit) of var1 1110 1111 */
}
return 0;
• Example 1-5
The following program toggles only bit 4 of var1
continuously without disturbing the rest of the bits
using XOR.

while(1)
{
var1 = var1 ^ 0x10;
}
Testing bit with bit-wise operators in C

• In many cases of system programming and


hardware interfacing, it is necessary to test a
given bit to see if it is high or low.
• For example, many devices send a high signal to
state that they are ready for an action or to
indicate that they have data.
• How can the bit (or bits) be tested? In such cases
the unused bits are masked and then the
remaining data is tested.
• Write a C program to monitor bit 5 of var1. If it is HIGH,
change value of var2 to 0x55; otherwise, change value of
var2 to 0xAA.
Solution:
...

while(1)
{
if (var1 & 0x20) /* check bit 5 (6th bit) of var1 */
var2 = 0x55; /* this statement is executed if bit 5 is a 1 */
else
var2 = 0xAA; /* this statement is executed if bit 5 is a 0 */
}
...
Bit-wise shift operation in C
• There are two bit-wise shift operators in C.

• The following shows some examples of shift


operators in C:
1. 0b00010000 >> 3/* it equals 00000010. Shifting right 3 times */
2. 0b00010000 << 3/* it equals 10000000. Shifting left 3 times */
Compound Operators
• In C language, whenever the left-hand-side of the
assignment operator (=) and the first operand on
the right-hand-side are identical we can avoid
repeating the operand by using the compound
operands.
Example 1
Using a flowchart describe the control algorithm that
a control temperature of the oven.
• A start button the user pushes to activate the machine.
Other input that measures bread temperature.
The desired temperature is preprogrammed into the
machine.
• The output is a heater, which can be on or off.
• The bread is automatically lowered into the oven
when heat is applied and is ejected when the heat
is turned off.
• The program starts at main when power is applied,
and the system behaves like an oven until it is
unplugged.
• The system initially waits for the operator to push
the start button. If the switch is not pressed, the
system loops back reading and checking the switch
over and over.
• After the start button is pressed, heat is turned on.
• When the bread temperature reaches the desired
value, heat is turned off, and the process is
repeated.
Flowchart illustrating the
process of Temp. Control.
Example 2: Design Function Call
• The system has one input and one output.
• An event should be recognized when the input goes
from 0 to 1 and back to 0 again.

• Assume the input and the output are initially 0.


• The output should be and remain 1 after four
events are detected.

Design a flowchart to solve this problem.


How to detect an event
• The main program first sets the output to
zero.

• Calls the function Event four times.


• Set the output to one.
• To detect the 0 to 1 to 0 edges in the input, it
first waits for 1, and then it waits for 0 again.
Flowchart illustrating the process waiting for four
events.
‫الحمد هلل رب العالمين‬

‫األسئلة ؟‬

‫‪43‬‬
‫هللا أكبر هللا اكبر هللا اكبر‬
‫أشهد أال اله إال هللا‬
‫اشهد أال اله إال هللا‬
‫أشهد أن محمد رسول هللا‬
‫أشهد أن محمد رسول هللا‬
‫حي على الصالة حي على الصالة‬
‫حي على الفالح حي على الفالح‬
‫هللا أكبر هللا أكبر‬
‫ال إله إال هللا‬
‫وصلى اللهم على سيدنا محمد وآله وصحبه وسلم‬

You might also like