Professional Documents
Culture Documents
Embedded Systems/Microcontroller
Laboratory
Lecture 4
Computer Architecture & C Programming
Professor Rahul Mangharam
Electrical and Systems Engineering
University of Pennsylvania
Computer Architecture-Office Analogy
Memory in computers is like a filing cabinet in an office.
File folders have tab labels and contain contents.
$0002 $AA
$0001 $01
$0000 $1F
Accumulator
Desk
The secretary
With Machine language, writes down all numbers.
With Assembly language, writes down mnemonics.
With C language, writes an abstract statement to represent a set of
mnemonics that are normally used together.
4
Architecture-Office Analogy
Diagram
Secretary
CPU
ALU
Incoming Mail Outgoing Mail
Slots Input Control Output Slots
Memory
File cabinets
5
Memory Mapped I/O
Memory mapped I/O implies that I/O devices and memory
share the same map with some addresses reserved for I/O
and others for memory.
Advantage-Program flexibility: same instructions are
used by the CPU to read/write data to I/O devices as to
RAM and ROM (memory)
Disadvantage-Limit on amount of memory & I/O devices
due to sharing.
6
MC9S12C12
8 Memory
Map
A Memory Mapped I/O-PortB
Output port B:
Address=$1004
Pins:
B7 B6 B5 B4 B3 B2 B1 B0
Each pin can be HIGH=1, +5V or LOW=0, 0V
8
Isolated I/O
CPU treats memory and I/O devices separately, using
different control signals for each.
Advantage-CPU can communicate with 28=256 I/O
devices and memory can use all 65536 addresses (16-bit
address)
Disadvantage-Special program instructions to perform I/O
operations.
9
Isolated I/O-Intel 8085
Memory
CPU
Address Bus
RD I/O
WR
IO/M
Data Bus
Assembly Language
11
Language Translation
Regardless of what language a program is written with, the only language the
computer understands is machine language, that is, 0s and 1s.
C Language
Compiler/Interpreter
Assembly Language
Source
Program
Assembler
Hex Machine Language
Object
(Binary) Machine Language Program
Compiler translates the complete C program into an machine language object program.
Interpreter translates small chunk of the C program and executes as it goes along without
creating an object program.
Assembler converts (assembles) the Assembly language into machine language object
program. 12
Same Task-Different Languages
Add two numbers and save the result.
Hand assembly: LDAA $C300->B6 C3 00-> 1011 0110 1100 0011 0000 0000
13
Review of C Programming
#include <stdio.h> Include standard library definitions file
/*Comments*/
main() Function main implicitly returns int value
{
int a,b,c; 3 variables (int: 16-bit for MC9S12C128)
a=3;
b=5;
c=a+b; Library function that converts,
formats, and prints its argument
printf(“a+b=%d\r\n”,c);
on the standard output
return 0;
}
Predefined constant:
newline and carriage return
Character group
The more print statements you use the longer it will take your code to
compile and the slower it will execute, so if timing is an issue, reduce
the number of them.
Use LEDs as visual clues as to what signals are being input and
output.
15
Data Types & Flow Control
Data Types:
Flow Control:
if(a!=0){
b=1;
}
else {
b=0;
}
16
Conditional Loops
While and for loops test condition at the beginning of the loop.
If nonzero, execute statement cycle continues until
expression becomes zero, in which case execution
resumes after the while condition block of
statements
n=0;
while(n<5)
{
printf(“n=%d\r\n”,n);
n++; Same as n=n+1
}
for(n=0;n<5;n++)
{
printf(“n=%d\r\n”,n);
}
17
Switches
Switch allows multiway decision based on control expression.
Switch(n){ Possible values for n
case 1: printf(“.”);
break;
case 2: printf(“..”);
break;
case 3: printf(“…”);
Break keyword forces the
break;
program flow to drop out of the
default: switch statement so that only the
statements under the
} corresponding case label are
executed. If any break statement
is missing, then all statements
from that case label until next
break statement within the same
switch statement will be
18
executed.
Functions
A function processes information passed to it by the calling
part of the program and returns a single value.
Return type is void int add1(int x)
if function does not
{
return any value to
caller. int y; Returns information desired
y=x+1; by function call
return y;
}
int main()
{
int a, b;
a=2;
b=add1(a);
return 0;
} 19
Arrays
Arrays are multiple data items that have common characteristics.
― Share same name
― Can be characters, integers, floats, etc.
― Elements of an array must be same type
Memory:
$C000 1
$C002 10
$C004 20
21
Bitwise Operators
A bitwise operator performs bit-by-bit operation on variable(s).
char a,b; 5 B
01011011 a
a=0x5B;
b=0x33;
00110011 b
3 3
c=a|b; OR
c=a&b; AND
c=~a; NOT
c=a^b; XOR (Exclusive-OR)
22
Boolean Logic-Truth Table
23
Bitwise Operators
Applying the previous Boolean 5 B
Logic truth table:
01011011 a
c=a|b; 0111 1011=0x7B
c=a&b; 0001 0011=0x13 00110011 b
c=~a; 1010 0100=0xA4
c=a^b; 0110 1000=0x68 3 3
24
Bit Shift Operations
Shift left 1 bit: x 11000110
x=0xC6; 0
y=(x <<1); y 10001100
8 C
25