EEE 204 - Lecture 7
EEE 204 - Lecture 7
EEE 204 - Lecture 7
Ex: Write an assembly program to add the contents of memory locations 1000H and 2000H. The result will
be displayed on PORT 5 and the Carry will be displayed on P4.1 if there is
mov.b #0xFF, P5DIR; Port 5 is output for the result
mov.b #0x02, P4DIR; P4.1 is out for the carry output
mov #0x0000, R6; Clear R6 for addition operation
mov #0x0000, R4; Clear for carry output
mov #0x1000, R5;
add.b (R5), R6
add.b 0x1000(R5), R6
jc carry
mov.b R6, P5OUT
jmp end; We don’t need to clear carry out, because it is initially zero
carry: mov.b R6, P5OUT
mov.b #0x02, P4OUT;
end:
1
Examples..
Ex: Write an assembly program to blink LED connected to Pin0 of PORT 1 of MSP430F5529
microcontroller continuously.
mov.b #0x01,P1DIR; P1.0 is output
here:
xor.b #0x01,P1OUT ; Always complement P1.0
mov.w #50000,R5 ; Load a big number for counting down, time killing
for1:
dec.w R5
cmp #0,R5 ; check if R5 is 0
jnz for1
end:
jmp here
2
Subroutines
•Subroutines are some kind of short functions that can be called from any point of the
program anytime.
•“call #Label” is the instruction that jumps the flow of the program to the desired
point
•“Label” is the title of the subroutine. It can be anything in English characters.
•“ret” instruction returns the flow of the program where the subroutine is called.
Apart from C/C++, It CANNOT return any value.
• Even though the subroutines are similar to the jump (conditional or unconditional)
instructions, they are different since they return. There are no return mechanisms for
jump instructions.
• Just like jump instructions. Program flow may be through a subroutine even though
it is not invoked.
3
Subroutines
Ex. Run the following code and observe the output
call #Subt; Call Subt
call #Sum; Call Sum
jmp Done; jump to end, otherwise it continues
Sum: mov.b #0x12,R7
mov.b #0x34,R8
add R7, R8
ret; Return to where it is invoked
Subt: mov.w #0x1234,R5
mov.w #0x5678,R6
sub R5, R6
ret
Done:; end of the program
4
Subroutines
Subroutines can really be helpful for making the loops…
Ex. call #Subt; Call Subt
call #KST; Kill Some Time
call #Sum; Call Sum
jmp Done; jump to end, otherwise it continues
Sum: mov.b #0x12,R7
mov.b #0x34,R8
add R7, R8
ret
KST: mov.b #0x12,R9; Kill Some Time between the operations
Check: dec r9
jnz Check
ret
Subt: mov.w #0x1234,R5
mov.w #0x5678,R6
sub R5, R6
ret
Done:; end of the program
5
Subroutines
Subroutines can also be nested…
Ex. call #Sum; Call Sum
jmp Done; jump to end, otherwise it continues
Sum: mov.b #0x12,R7
mov.b #0x34,R8
add R7, R8
call #KST; Kill Some Time
call #Subt; Call Subt
ret
KST: mov.b #0x12,R9; Kill Some Time between the operations
Check: dec r9
jnz Check
ret
Subt: mov.w #0x1234,R5
mov.w #0x5678,R6
sub R5, R6
ret
Done: ;end of the program
6
EEE 204
Fundamentals of Interfacing
Asst. Prof. Dr Mahmut AYKAÇ
Fundamentals of Interfacing
Any microprocessor, needs a basic interface to become usable. This interface
includes
four fundamental elements:
• A power source to feed power to the CPU and system peripherals
• A clock generator to synchronize the system operation
• A power‐on reset circuit (POR), to take the system to its initial state
• A booting function, to initiate the system software execution
8
Fundamentals of Interfacing
9
Processors’ Power Sources
Regardless of the processor type, power sources need to be externally
provided. They can be implemented from different sources: batteries, wall
connected AC power outlet, or some form of energy collecting or scavenging
device. In every case, selection and design criteria must be applied to
properly feed power to the CPU and its peripherals.
10
Processors’ Power Sources
A typical MCU power supply includes an unregulated power source and a
voltage regulator to feed the load. If more than one voltage level were
required, multiple regulators would be used, one for each different voltage
level.
11
Bypassing Techniques
Bypassing refers to the act of reducing a high frequency current flow in a circuit path by
adding a shunting component that reacts to the target frequency. The most commonly used
shunting devices in microprocessor-based designs are bypassing capacitors.
A bypass capacitor reduces the rate of change of the current circulating in the power line by
providing a high-frequency, low impedance path to the varying load current. Two factors
determine the effectiveness of a bypassing capacitor: size and location.
The size of a bypassing capacitor is selected in accordance to the frequency of the noise
they are intended to attenuate. Assuming a supply voltage variation 𝑉 , a supply current 𝐼 ,
and a worst case transient time T are known, the value for a bypass capacitor 𝐶 can be
estimated as 𝐶
/
In most cases, a standard non-electrolytic capacitor between 1 and 0.01μF shall work.
12
Source Decoupling
Decoupling refers to the isolation of two circuits in the same path or connection line. Power supply
decoupling is usually achieved through the installation of low-pass filters in strategic points of the
power distribution line to reduce the strength of high-frequency noise components from one side of the
system, reaching potentially sensitive components in the other side.
• In most cases a 10–100 μF electrolytic capacitor shall suffice.
• The value of the inductor, although not critical, is also recommended to be fairly large, with typical
values in the range from 10 to 100mH.
In circuits sensitive to power supply ringing, the decoupling circuit could make the power supply
13
Clock Sources
The need for a clock source in microprocessor-based systems arises from
the synchronous sequential nature of the digital logic making up most of the
system components.
The synchronous operation of the control unit’s finite state machine (FSM)
requires a periodic signal to yield precise transitions between the different
states assumed by the CPU.
In many cases, particularly in MCU-based designs, the clock sources for both
the CPU and peripheral devices are derived from the same clock generator,
which is designated for that reason System Clock.
15
Bootstrap Sequence
The bootstrap sequence, frequently shorthanded as the Boot Sequence
refers to the sequence of instructions that a CPU executes upon reset. What
does it have to deal with boots and straps? The term bootstrap seems to be
a metaphor of the phrase “pull oneself up by one’s bootstraps” denoting
those with the ability of helping themselves without external means.
In a sense, that is what the bootstrap sequence does for a computing
system: starting it when no other software is yet running. No basic
microprocessor or MCU interface is complete without a properly designed
boot sequence.
16
Bootstrap Sequence
Specific tasks performed in an MCU boot sequence are influenced by
multiple factors that include: specific processor used, system architecture,
application, and even the programming style. However, it is possible to
identify typical operations taking place in this sequence.
• Identifying and initializing system peripherals.
• Setting‐up the stack.
• Initializing system‐wide variables in memory.
• Performing diagnostics and system integrity check‐up.
• Loading an operating system or other type program.
17
MSP430 LaunchPad Block Diagram
18
EEE 204
C Programming
Asst. Prof. Dr Mahmut AYKAÇ
Introduction to C Programming
All C programs start with a header…
#include <msp430.h>; //include all MCUs in MSP430 Family
#include <msp430x11x1.h>; //include a specific MCU
While the first description includes all the MCUs belongings to MSP430 family,
second one includes a specific one.
Similar to standard C language, all programs must have main function and the
entire program must be in it.
int main(void)
{ //Program starts here
.
.
}//ends here
20
Registers in C Programming
Registers (PxDIR, PxIN, PxOUT etc.) are same as they are in Assembly programming
but to load the date to the related registers are little bit different.
P3DIR= 0xFF;// All bits of Port 3 are output
P3OUT= 0xAB;// Send AB (1010 1011) to Port3. P3.7, P3.5, P3.3, P3.1, P3.0 are ON, others are OFF
P2DIR=0xEE;// P2DIR=EEH=1110 1110, P2.0 and P2.4 are inputs, others are outputs
• In Assembly language, when we need to make a comment for each line of
instruction we must use “;” but “//” in C.
• In Assembly language, no lines of instructions require “;” to indicate the ending
point of line of instruction. But C requires “;”. Otherwise it yields a syntax error.
Recall from C or C++ lectures, control flow statements such as if, while, for
etc. don’t require “;”. Same also for programming our MCU in C
21
Bitwise in C Programming
It also allows the user to use bitwise. Here are some examples…
P1OUT |= BIT4; //Set P1.4
P1OUT ˆ= BIT4; //Toggle P1.4
P1OUT &= ˜BIT4; // Clear P1.4
Toogle means the corresponding PIN is 1 or 0 alternatively in each time of
processing.
22
C Programming
Ex: Run the following code and observe that P4.7 is ON and OFF continuously.
#include <msp430.h>
int main(void)
{// start of main
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
24
Define Directive in C Programming
Just like C/C++, it is also possible to use define to name constants with a
catchy way. Here are some examples…
#define LED1 BIT7// Defining the 7th bit as LED1
#define LED2 BIT4// Defining the 4th bit as LED2
#define BTN1 BIT1// Defining the 1st bit as Button 1
#define BTN2 BIT3// Defining the 3rd bit as Button 2
As seen from the above examples, even though the bit order is defined, port
numbers are not included. This gives us flexibility to use the same bit number
on different ports without extra code.
Extra code causes extra load on CPU performance.
25
Define Directive in C Programming
#include <msp430.h>
#define LED BIT7 // Defining the 7th bit as LED
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer
P4DIR=0xFF; // All bits of P4 are outputs
P4OUT=0x00; // Clear P4 for transferring data
P3DIR=0xFF; // All bits of P3 are outputs
P3OUT=0x00; // Clear P3 for transferring data
P5DIR=0xFF; // All bits of P5 are outputs
P5OUT=0x00; // Clear P5 for transferring data
while(1)
{
P4OUT ^= LED; // Toggle LED on P4.7
P3OUT ^= LED; // Toggle LED on P3.7
P5OUT ^= LED; // Toggle LED on P5.7
}
return 0;
}
26
Functions in C Programming
“A function is a block of code which only runs when it is called (invoked)”.
Recall from C++ lecture, A function is invoked with or without parameters
(depending on the function type), and the function returns to the invoking
point with or without a value (depending on the function type).
27
Example for Functions in C Programming
Even though toggling means to turn ON and OFF, it is not possible to observe it without a reasonable delay.
Ex: Same program with some delay…
#include <msp430.h>
void MyDelay(void); // Declaration of the functions
void MyDelay(void) // Function starts here
{
volatile unsigned long int i;// i is 32‐bit, unsigned and volatile(stored in RAM)
for(i=1; i<25000; i++);// Killing time
} // Function ends here BEWARE!!!Even though the
int main(void) program flow can be through a
{
label without a jump instruction
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
P4DIR=0xFF; // P4 is out
in Assembly language, program
P4OUT=0x00; // Clear P4 flow cannot be through a
while(1) // Infinite loop function without an invoking
{ instruction in C language.
P4OUT ^= BIT7; // Toggle P4.7
MyDelay(); // Call MyDelay function
}
return 0;
}
28
More Examples in C Programming
#include <msp430.h>
void MyDelay(void);
#define LED1 BIT7 // Defining the 7th bit as LED1
#define LED2 BIT0 // Defining the 1st bit as LED2
void MyDelay(void)
{
volatile unsigned long int i;
for(i=1; i<25000; i++);// Changing the ending value also changes the amount of delay
}
int main(void)
{
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
P4DIR=0xFF;
P4OUT=0x00;
P1DIR=0xFF;
P1OUT=0x00;
while(1)
{
P4OUT ^= LED1; // Toggle P4.7, LED1=P4.7
P1OUT ^= LED2; // Toggle P1.0, LED2=P1.0
MyDelay();
}
return 0;
}
29
More Examples in C Programming
#include <msp430.h>
#define BUTTON P2IN
#define LED P4OUT
void MyShortDelay(void);
void MyLongDelay(void);
void MyLongDelay(void) // Long Delay Function
{
volatile unsigned long int i;
for(i=1; i<25000; i++);
}
void MyShortDelay(void)// Short Delay Function
{
volatile unsigned long int j;
for(j=1; j<12500; j++);
}
int main(void)
{ P2.1 P1.1
WDTCTL = WDTPW | WDTHOLD;// stop watchdog timer
P2DIR=0x00;// P2 is input P1.0 P4.7
P4DIR=0xFF;// P4 is output
while(1)// Always check!, otherwise it checks once and ends the program
{
while (BUTTON!=0xFD)// While button (P2.1) is NOT pressed
{
LED ^= BIT7;
MyShortDelay();
}
LED ^= BIT7; )// While button (P2.1) is pressed * As default, the board receives
MyLongDelay();
}
logic 1 if any button is not pressed!
return 0; * Negative Logic
}
30