Processor Register Set of M16C

Download Report

Transcript Processor Register Set of M16C

Device Registers
CPU registers are used for calculation and controlling CPU.
To interact with a peripheral device a CPU uses device registers.
Common device registers for peripheral devices:



Control register, selects the mode of operation of the device
Status register, shows operating mode, current device status, errors
Data register, to perform data i/o
Device register
Device registers
CPU
CPU
GPIO
GPIO
Port Direction
Timer Mode
Port Data
Counter
…
…
Control
Data
Address
Copyright © 2011 DSR Corporation
1
Control Register
 The control registers are used to set up different modes
 Some features are configured by separate bits
 The first step in programming peripheral devices is to understand
control registers of the device
Example of the configuration register
(M16C timer)
Name
Address
R/W
Access Size
7
Timer A0 mode
register
0x0396
R/W
8
Timer A0 register
0x0387
R
16
Counter start flag
0x0380
Copyright © 2011 DSR Corporation
R
Counter source
select bit
Timer
A0 mode register
6
TMOD0 TMOD1
5
4
3
2
1
0
MR0
MR1
MR2
MR3
TCK0
TCK1
Specify the operating mode
00: Timer mode
01: Event counter mode
10: One-shot timer mode
11: Pulse with modulation
(PWM) mode
8
2
Different
functions
depending on
the mode
Using the GPIO as an output
Task: Turn on external LED4
#define P0 ((char*)0x03E0)
#define PD0 ((char*)0x03E2)
int main(void)
{
/* P0.0 and P0.1 are OUT */
*PD0 = (1<<0) | (1<<1);
M16C (microcontroller)
CPU
PC
0xF120
Address
Program Memory
0xF000
mov.b #2h, 03e0h
0xF120
Write memory
instruction
*P0 = (1<<1);
VCC
GPIO
while(1)
{
…
}
P0 Register
… 10
LED3 : Off
0
1
0x03E0
…
1
1
0
}
LED4 : On
Copyright © 2011 DSR Corporation
3
Using the GPIO as an Input
Task: Turn on the LED only if the button is pressed
#define P0 ((char*)0x03E0)
#define PD0 ((char*)0x03E2)
#define P1 ((char*)0x03E1)
#define PD1 ((char*)0x03E3)
int main(void)
{
*PD0 = (1<<0) | (1<<1);
*P0 = (1<<1);
*PD1 = 0x00;
while(1)
{
if (*P1 & (1<<5))
{
*P0 |= (1<<1);
}
else
{
*P0 &= ~ (1<<1);
}
}
}
Copyright © 2011 DSR Corporation
M16C (microcontroller)
CPU
PC
0xF120
Address
Program Memory
0xF000
mov.b 03e1h, R1
0xF120
Read memory
instruction
VCC
GPIO
SW9 : On
P1 Register
6
5
01
0
0x03E1
1
0
SW8 : Off
0
1
4 GND
Pull-up and Pull-down
 High and low voltage values (Hi and Lo) are used to determine the ‘1’ and
‘0’ digital value
 Example: When the supply voltage is nearly 5v, 4 ~ 5v is Hi, 0 ~ 1v is Lo
 Digital signal voltage values must be in the range of Lo or Hi
 Pull-up and pull-down
 Circuit configuration for stable electrical input when connecting a switch to an input GPIO
port
Example of pull-up
Switch ON
Switch OFF
LSI
Vcc(5V)
LSI
Vcc(5V)
Port Register
ポートレジスタ
'1'
Port Register
ポートレジスタ
スイッチ
Switch
'0'
スイッチ
Switch
GND
Copyright © 2011 DSR Corporation
5
UART serial interface (cont.)

There are three key interfaces:




Configuration registers:
 Configure interrupts, baud rate, parity, etc.
 Check the status of the UART
Send register
 Write in data byte that need to be sent out
Receive register
 Read out data byte that have been received
 Sending data:
RX
Receiver
Transmitter
CPU
Interface
Transmission and reception of
data can be handled separately
and independently




Baud
Rate
Generator
Status/
Control
Registers
TX
Modem
I/O
Signals
 Receiving data:
 Wait for receiver buffer is not empty
(got some data)
 Read data byte
 Wait for next “not empty” event
Wait for transmitter buffer is empty
Write data to TX buffer (register)
Wait for transmitter buffer is empty
Write next data byte
6
…
SPI Bus (cont.)




Master
MOSI – Master Output, Slave Input
MISO – Master Input, Slave Output
SCK – Signal ClocK
CS(SS) – Chip Select (Slave Select)
SCK
MOSI
MISO
___
SS1
___
SS2
Slave 1
SCK
MOSI
MISO
__
SS
Slave 2
SCK
MOSI
MISO
__
SS
7
SPI Bus (cont.)




Set specific value to control registers to configure I/O port
Transmitter side: read value to send from special register, put it to transmit the
buffer
 Often FIFO is used
Receiver side: get received byte from buffer and put it to special register
To check if send or receive complete, check the status register
Slave
Master
Control / status register
Control / status register
Receive buffer
Transmit Buffer
SCK
FIFO
MOSI
7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
Shift register
Copyright © 2011 DSR Corporation
MISO
8
Shift register
CPU
CPU
FIFO
Programmable Interrupt Controller (PIC)
 Accept multiple interrupt requests, issue a single interrupt request to a
processor
 Each interrupt request can be configured to be ignored
 Interrupt Source Register is used to determine what interrupt occurred
 Several PICs can be used in cascade
Get interrupt
information
Stores information
about interrupt
Source Register
PIC
Interrupt
enable/disable
Mask register
Interrupt request 1
CPU
Interrupt request 2
Interrupt request 3
Interrupt request 4
Copyright © 2011 DSR Corporation
9
Development Environment
C Toolchain
C toolchain steps:
C code
 Preprocessor
 Processes C code, handles “include”,
pragma and macro expressions
Preprocessor
Compiler
 Compiler
 Transforms C language source code into
assembly code
 Assembler
Assembly Code
Assembly Code
Assembler
 Converts Assembly code into object code
 Linker
 Links one or more object code files with
multiple libraries and generates executable
code
 Cross development: the same
toolchain is used, but cross-compiler,
cross-assembler, and cross-linker are
used instead of common tools
Copyright © 2011 DSR Corporation
Library
Object code
Linker
Executable code
(Binary code)
10