Microcontrollers
Download
Report
Transcript Microcontrollers
A Simple Tour of the MSP430
Light LEDs in C
LEDs can be connected in two standard ways.
Active high circuit, the LED illuminates if the pin is driven high (logic 1) and
extinguishes if the pin is driven low (logic 0).
Input and output is memory mapped, which means that nothing unusual is needed to
control output pins: We simply write in the normal way to the register that controls
them.
The complete program needs to carry out the following tasks:
1. Configure the microcontroller.
2. Set the relevant pins to be outputs by setting the appropriate bits of PxDIR.
3. Illuminate the LEDs by writing to PxOUT.
4. Keep the microcontroller busy in an infinite, empty loop.
Light LEDs in C
Assignment
The basic instruction in assembly language is mov.w source, destination to move a word or
mov.b for a byte.
A single character denotes the mode in the operand.
• immediate, #: The value itself (word or byte) is given and stored in the word following the
instruction.This is also known as a literal value.
• absolute, &: The address of a register in memory space is given and stored in the word
following the instruction.
• register, R: This specifies one of the 16 registers in the CPU.
The immediate mode is clearly useful only for the source but the other two can be used for
either the source or destination, provided that it is possible to write to the destination—it is
not in ROM, for instance.
Infinite Loop
The flow of control is based on jump instructions, which can be either conditional
or unconditional (always taken).
You have no choice in assembly language.
Normally the CPU executes instructions from memory one after the other. The
program counter (PC) is automatically incremented so that it is ready to fetch the
next instruction as soon as it is needed. This sequence is broken by a jump, which
causes a new address to be loaded into in the PC.
Program Is Stored in Memory
Generally the code should go into the flash ROM and variables should be allocated in RAM. We
put the code in the most obvious location, which is at the start of the flash memory (low
addresses). The Memory Organization section of the data sheet shows that the main flash
memory has addresses 0xF000–FFFF. We therefore instruct the assembler to start storing the
code at 0xF000.
Program Start
In the MSP430, the address of the first instruction to be executed is stored at a
specific location in flash memory, rather than the instruction itself. This address,
called the reset vector, occupies the highest 2 bytes of the vector table at
0xFFFE:FFFF. It is set up as follows:
1. Put a label in front of the first instruction to be executed.
2. Use an directive to tell the assembler where the reset vector should be stored. It
is at the same address in all current MSP430 devices.
3. This is followed by a DW Reset directive for “define word,” which tells the
assembler to store the following word (2 bytes) in memory. This is the address of
the first instruction to be executed and the assembler replaces the label with its
numerical value.
Read Input from a Switch
The pull-up resistor R pull holds the input at logic 1 (voltage VCC) while the button is
up; closing the switch shorts the input to ground, logic 0 or VSS. The input is
therefore active low, meaning that it goes low when the button is pressed.
Most MCUs offer internal pull-ups to reduce the number of external components
needed.
Read Input from a Switch
A program can respond to inputs in two ways.
◦ Polling
◦ Interrupt
Polling is simple but carries the overhead of regular checking, which is high if checks
must be made frequently. An interrupt needs special hardware and requires tidying
up so that normal operation can be resumed after the interrupt has been serviced.
Two Loops, One for Each State of the Button
The LED is continually being switched on or off in the first version. The action is
repeated continually.
The LED is turned on or off only when necessary in the second version, just at the
points when the button is pressed or released.