MG84FG516 DataSheet A1.3 PDF
MG84FG516 DataSheet A1.3 PDF
MG84FG516 DataSheet A1.3 PDF
MG84FG516
Data Sheet
Version: A1.3
This document contains information on a new product under development by Megawin. Megawin reserves the right to change or
discontinue this product without notice.
Megawin Technology Co., Ltd. 2005 All rights reserved. 2014/02 version A1.3
2 MG84FG516 Data Sheet MEGAWIN
Features
1-T 80C51 Central Processing Unit
MG84FG516 with 64K Bytes flash ROM
━ ISP memory zone could be optioned as 1KB/1.5KB~4KB
━ Flexible IAP size.
━ Code protection for flash memory access
━ Flash erase/program cycle: 10,000 times
━ Flash data retention: 100 years at 25℃
━ MG84FG516 Flash space mapping
AP Flash(60KB, 0000h~EFFFh)
IAP Flash(1.5KB, F000h~F5FFh)
(USB boot-code space)
USB DFU boot-Code(2.5KB, F600h~FFFFh)
Data RAM
━ On-chip 256 bytes scratch-pad RAM
━ 4096 bytes expanded RAM (XRAM)
━ Up to 5120 bytes expanded RAM (XRAM) by sharing USB buffer
Dual data pointer
Variable length MOVX for slow SRAM/Peripherals
Interrupt controller
━ 17 sources, four-level-priority interrupt capability
━ Four external interrupt inputs, nINT0, nINT1, nINT2 and nINT3
━ All external interrupts support High/Low level or Rising/Falling edge trigger
Four 16-bit timer/counters, Timer 0, Timer 1, Timer 2 and Timer 3
━ T0CKO on P34, T1CKO on P35, T2CKO on P10 and T3CKO on P01
━ X12 mode enabled for T0/T1/T2/T3
Programmable 16-bit counter/timer Array (PCA) with 6 compare/capture modules
━ Capture mode
━ 16-bit software timer mode
━ High speed output mode
━ 8/10/12/16-bit PWM (Pulse Width Modulator) mode with phase shift function
Keypad Interrupt (P0/P2/P5/P6)
12-Bit ADC
━ Programmable throughput up to 250 ksps
━ Up to 8 channel single-ended inputs or 4 channel differential inputs
Enhanced UART (S0)
━ Framing Error Detection
━ Automatic Address Recognition
━ Speed improvement mechanism (X2/X4 mode)
Secondary UART (S1)
━ Dedicated Baud Rate Generator
━ S1 shares baud rate generator to S0.
Master/Slave SPI serial interface
Master/Slave two wire serial interface (TWSI)
USB Device Controller
━ USB Full speed (12Mbps) operation and USB specification 2.0 compliant
━ Intel 8X931 like USB control flow
*: Tested by sampling.
The MG84FG516 has 64K bytes of embedded Flash memory for code and data. The Flash memory can be
programmed either in serial writer mode (via ICP, In-Circuit Programming) or in In-System Programming mode (via
USB DFU). And, it also provides the In-Application Programming (IAP) capability. ICP and ISP allow the user to
download new code without removing the microcontroller from the actual end product; IAP means that the device
can write non-volatile data in the Flash memory while the application program is running. There needs no external
high voltage for programming due to its built-in charge-pumping circuitry.
The MG84FG516 retains all features of the standard 80C52 with 256 bytes of scratch-pad RAM, four 8-bit I/O ports,
two external interrupts, a multi-source 4-level interrupt controller, a serial port (UART0) and three timer/counters. In
addition, the MG84FG516 has a full speed USB device function, three extra I/O ports (P4[6:0], P5, P6), one XRAM
of 4096 bytes, two extra external interrupts with High/low trigger option, 12-bit ADC, a 6-channel PCA, SPI, TWSI,
secondary serial port (UART1), keypad interrupt, Watchdog Timer, 4th 16-bit timer, two Brown-out Detectors, an
on-chip crystal oscillator(shared with P6.0 and P6.1), an internal high precision oscillator to fit USB full speed
application, an internal low speed RC oscillator (ILRCO) and an enhanced serial function in UART0 that facilitates
multiprocessor communication and a speed improvement mechanism (X2/X4 mode).
The MG84FG516 has multiple operating modes to reduce the power consumption: idle mode, power down mode,
slow mode, sub-clock mode, watch mode and monitor mode. In the Idle mode the CPU is frozen while the
peripherals and the interrupt system are still operating. In the Power-Down mode the RAM and SFRs’ value are
saved and all other functions are inoperative; most importantly, in the Power-down mode the device can be waked
up by many interrupt or reset sources. In slow mode, the user can further reduce the power consumption by using
the 8-bit system clock pre-scaler to slow down the operating speed. Or select sub-clock mode which clock source is
derived from internal low speed oscillator (ILRCO) for CPU to perform an ultra low speed operation. In watch mode,
it keeps WDT running in power-down or idle mode and resumes CPU when WDT overflows. Monitor mode provides
the Brown-Out detection in power down mode and resumes CPU when chip VDD reaches the specific detection
level.
Additionally, the MG84FG516 is equipped with the Megawin proprietary On-Chip Debug (OCD) interface for
In-Circuit Emulator (ICE). The OCD interface provides on-chip and in-system non-intrusive debugging without any
target resource occupied. Several operations necessary for an ICE are supported such as Reset, Run, Stop, Step,
Run to Cursor and Breakpoint Setting. The user has no need to prepare any development board during firmware
developing or the socket adapter used in the traditional ICE probe head. All the thing the user needs to do is to
prepare a connector for the dedicated OCD interface. This powerful feature makes the developing very easy for any
user.
ALE(P4.6)
nWR(P3.6)
nRD(P3.7)
8051 CPU (1T) RST
nINT0(P3.2) WDT
nINT1(P3.3) Ext. INT
nINT2(P4.3)
nINT3(P4.2)
OCD OCD_SCL
Interface OCD_SDA
RXD0(P3.0)
UART0
TXD0(P3.1)
Flash
64K X 8
RXD1(P1.2)
UART1
TXD1(P1.3)
RAM
256 X 8
T0/T0CKO(P3.4) Timer0
T1/T1CKO(P3.5) Timer1
XRAM
4096 X 8
T2/T2CKO(P1.0)
Timer2
T2EX(P1.1)
ISP/IAP
T3/T3CKO(P0.1)
Timer3 Port0 P0.0~P0.7
T3EX(P0.0)
Port1 P1.0~P1.7
ECI(P2.1) PCA
CEX0~CEX5 Timer
(P2.2~P2.7) Port2 P2.0~P2.7
AIN0~AIN7
12-bit ADC
(P1.0~P1.7) Port3 P3.0~P3.7
KBI0~KBI7
Keypad Int.
(P0/P2/P5/P6) Port4 P4.0~P4.6
nSS(P1.4)
MOSI(P1.5) SPI Port5 P5.0~P5.7
MISO(P1.6)
SPICLK(P1.7)
Port6 P6.0~P6.7
TWI_SCL(P4.0)
TWSI
TWI_SDA(P4.1) USB FIFO
1K Bytes
Regulator
USB XCVR DP, DM
3.3V
Bit 7~4: Reserved. Software must write “0” on these bits when SFRPI is written.
Bit 3~0: SFR Page Index. The available pages are only page “0” and “1”.
There are 13 register sets in Page 0, S0CON(98H), S0BUF(99H), S0CFG(9AH), S1CFG(9BH),
PUCON0(B4H), P5M0(B5H), T2CON(C8H), T2MOD(C9H), RCAP2L(CAH), RCAP2H(CBH),
MG84FG516 has an auxiliary SFR page which is indexed by page P and the SFRs’ write is a different way from
standard 8051 SFR page. The registers in auxiliary SFR map are addressed by IFMT and SCMD like ISP/IAP
access flow. Page P has 256 bytes space that can target to 5 physical bytes and 7 logical bytes. The 5 physical
bytes include IAPLB, CKCON2, PCON2, PCON3 and SPCON0. The 7 logical bytes include PCON0, PCON1,
CKCON0, CKCON1, WDTCR, P4 and P6. Write on the 7 logical bytes gets the coherence content with the same
SFR in Page 0~F. Please refer Section “24 Page P SFR Access” for more detail information.
P1.0 (T2/AIN0/T2CKO)
P1.2 (AIN2/RXD1)
P1.3 (AIN3/TXD1)
P1.1 (T2EX/AIN1)
P0.7 (AD7/KBI7)
P0.6 (AD6/KBI6)
P0.5 (AD5/KBI5)
P0.4 (AD4/KBI4)
P0.3 (AD3/KBI3)
VREF+
VDD
P5.2
P5.1
P5.0
VSS
VR0
64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49
(nSS/AIN4) P1.4 1 48 V33
(MOSI/AIN5) P1.5 2 47 DP
(MISO/AIN6) P1.6 3 46 DM
(SPICLK/AIN7) P1.7 4 45 P0.2 (AD2/KBI2)
P5.3 5 44 P0.1 (AD1/KBI1/T3)
P5.4 6 43 P0.0 (AD0/KBI0/T3EX)
P5.5 7 42 VSS
P5.6 8 41 P6.0 (XTAL2/CKO)
LQFP64
(S1CKO/A8) P2.0 9 40 P6.1 (XTAL1)
(ECI/A9) P2.1 10 39 P6.2
(CEX0/A10) P2.2 11 38 P6.3
(CEX1/A11) P2.3 12 37 P6.4
(CEX2/A12) P2.4 13 36 P4.6 (ALE)
(CEX3/A13) P2.5 14 35 P4.5 (OCD_SDA)
(CEX4/A14) P2.6 15 34 P4.4 (OCD_SCL)
(CEX5/A15) P2.7 16 33 RST
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
(TXD0) P3.1
P5.7
P6.7
P6.6
P6.5
(T0CKO/T0) P3.4
(T1CKO/T1) P3.5
(nWR) P3.6
(nRD) P3.7
(TWI_SCL) P4.0
(TWI_SDA) P4.1
(RXD0) P3.0
(nINT0) P3.2
(nINT1) P3.3
(nINT3) P4.2
(nINT2) P4.3
P1.0 (T2/AIN0/T2CKO)
P1.2 (AIN2/RXD1)
P1.3 (AIN3/TXD1)
P1.1 (T2EX/AIN1)
P0.7 (AD7/KBI7)
P0.6 (AD6/KBI6)
P0.5 (AD5/KBI5)
P0.4 (AD4/KBI4)
P0.3 (AD3/KBI3)
VDD
VSS
VR0
48 47 46 45 44 43 42 41 40 39 38 37
(nSS/AIN4) P1.4 1 36 V33
(MOSI/AIN5) P1.5 2 35 DP
(MISO/AIN6) P1.6 3 34 DM
(SPICLK/AIN7) P1.7 4 33 P0.2 (AD2/KBI2)
(S1CKO/A8) P2.0 5 32 P0.1 (AD1/KBI1/T3)
(ECI/A9) P2.1 6 31 P0.0 (AD0/KBI0/T3EX)
LQFP48
(CEX0/A10) P2.2 7 30 P6.0 (XTAL2/CKO)
(CEX1/A11) P2.3 8 29 P6.1 (XTAL1)
(CEX2/A12) P2.4 9 28 P4.6 (ALE)
(CEX3/A13) P2.5 10 27 P4.5 (OCD_SDA)
(CEX4/A14) P2.6 11 26 P4.4 (OCD_SCL)
(CEX5/A15) P2.7 12 25 RST
13 14 15 16 17 18 19 20 21 22 23 24
(TXD0) P3.1
(T0CKO/T0) P3.4
(T1CKO/T1) P3.5
(nWR) P3.6
(nRD) P3.7
(TWI_SCL) P4.0
(TWI_SDA) P4.1
(RXD0) P3.0
(nINT0) P3.2
(nINT1) P3.3
(nINT3) P4.2
(nINT2) P4.3
Many I/O pins, in addition to their normal I/O function, also serve the alternate function for internal peripherals. For
the peripherals Keypad interrupt, PCA, SPI, UART0, UART1, Timer 2 and Timer3, Port 0, Port 1, Port 2 and Port 3
serve the alternate function in the default state. However, the user may select Port 4 and Port 5 to serve their
alternate function by setting the corresponding control bits P4KB, P4PCA, P5SPI and P4S1 in AUXR1 register. It is
especially useful when the packages more than 40 pins are adopted. Note that only one of the four control bits can
be set at any time.
Bit 7~6: P6.0 function configured control bit 1 and 0. The two bits only act when internal RC oscillator (IHRCO or
ILRCO) is selected for system clock source. In crystal mode, XTAL2 and XTAL1 are the alternated function of P6.0
and P6.1. In external clock input mode, P6.0 is the dedicated clock input pin. In internal oscillator condition, P6.0
provides the following selections for GPIO or clock source generator. When P60OC[1:0] index to non-P6.0 GPIO
function, P6.0 will drive the on-chip RC oscillator output to provide the clock source for other devices.
Bit 7~6: INT3IS1~0, nINT3 input selection bits which function is defined as following table.
INT3IS1~0 nINT3 Selected Port Pin
00 nINT3 Port Pin P4.2 or P4.5
01 RXD1 Port Pin P1.2 or P5.2
10 TWSI SDA Port Pin P4.1
11 SPI nSS Port Pin P1.4 or P5.4
Bit 5~4: INT2IS1~0, nINT2 input selection bits which function is defined as following table.
INT2IS1~0 nINT2 Selected Port Pin
00 nINT2 Port Pin P4.3 or P4.4
01 RXD0 Port Pin P3.0 or P4.4
10 TWSI SDA Port Pin P4.1
11 SPI nSS Port Pin P1.4 or P5.4
The program status word (PSW) contains several status bits that reflect the current state of the CPU. The PSW,
shown above, resides in the SFR space. It contains the Carry bit, the Auxiliary Carry(for BCD operation), the two
register bank select bits, the Overflow flag, a Parity bit and two user-definable status flags.
The Carry bit, other than serving the function of a Carry bit in arithmetic operations, also serves as the “Accumulator”
for a number of Boolean operations.
The bits RS0 and RS1 are used to select one of the four register banks shown in Section “6.2 On-Chip Data RAM”.
A number of instructions refer to these RAM locations as R0 through R7.
The Parity bit reflects the number of 1s in the Accumulator. P=1 if the Accumulator contains an odd number of 1s
and otherwise P=0.
The Stack Pointer holds the location of the top of the stack. The stack pointer is incremented before every PUSH
operation. The SP register defaults to 0x07 after reset.
The DPL register is the low byte of the 16-bit DPTR. DPTR is used to access indirectly addressed XRAM and Flash
memory.
The DPH register is the high byte of the 16-bit DPTR. DPTR is used to access indirectly addressed XRAM and Flash
memory.
ACC: Accumulator
SFR Address = 0xE0 RESET = 0000-0000
7 6 5 4 3 2 1 0
ACC.7 ACC.6 ACC.5 ACC.4 ACC.3 ACC.2 ACC.1 ACC.0
R/W R/W R/W R/W R/W R/W R/W R/W
B: B Register
SFR Page = 0~F
SFR Address = 0xF0 RESET = 0000-0000
7 6 5 4 3 2 1 0
B.7 B.6 B.5 B.4 B.3 B.2 B.1 B.0
R/W R/W R/W R/W R/W R/W R/W R/W
The MG84FG516 is a single-chip microcontroller based on a high performance 1-T architecture 80C51 CPU that
has an 8051 compatible instruction set, and executes instructions in 1~7 clock cycles (about 6~7 times the rate of a
standard 8051 device). It employs a pipelined architecture that greatly increases its instruction throughput over the
standard 8051 architecture. The instruction timing is different than that of the standard 8051.
In many 8051 implementations, a distinction is made between machine cycles and clock cycles, with machine cycles
varying from 2 to 12 clock cycles in length. However, the 1T-80C51 implementation is based solely on clock cycle
timing. All instruction timings are specified in terms of clock cycles. For more detailed information about the
1T-80C51 instructions, please refer section “29 Instruction Set” which includes the mnemonic, number of bytes, and
number of clock cycles for each instruction.
Direct Addressing(DIR)
In direct addressing the operand is specified by an 8-bit address field in the instruction. Only internal data RAM and
SFRs can be direct addressed.
Indirect Addressing(IND)
In indirect addressing the instruction specified a register which contains the address of the operand. Both internal
and external RAM can be indirectly addressed.
The address register for 8-bit addresses can be R0 or R1 of the selected bank, or the Stack Pointer.
The address register for 16-bit addresses can only be the 16-bit data pointer register – DPTR.
Register Instruction(REG)
The register banks, containing registers R0 through R7, can be accessed by certain instructions which carry a 3-bit
register specification within the op-code of the instruction. Instructions that access the registers this way are code
efficient because this mode eliminates the need of an extra address byte. When such instruction is executed, one of
the eight registers in the selected bank is accessed.
Register-Specific Instruction
Immediate Constant(IMM)
The value of a constant can follow the op-code in the program memory.
Index Addressing
Only program memory can be accessed with indexed addressing and it can only be read. This addressing mode is
intended for reading look-up tables in program memory. A 16-bit base register(either DPTR or PC) points to the
base of the table, and the accumulator is set up with the table entry number. Another type of indexed addressing is
used in the conditional jump instruction.
In conditional jump, the destination address is computed as the sum of the base pointer and the accumulator.
Program memory (ROM) can only be read, not written to. There can be up to 64K bytes of program memory. In the
MG84FG516, all the program memory are on-chip Flash memory, and without the capability of accessing external
program memory because of no External Access Enable (/EA) and Program Store Enable (/PSEN) signals
designed.
Data memory occupies a separate address space from program memory. In the MG84FG516, there are 256 bytes of
internal scratch-pad RAM and 4096 bytes of on-chip expanded RAM(XRAM). If USB function is disabled, the USB
buffer is moved to on-chip expanded RAM region which will provide 5120 bytes XRAM in MG84FG516. If USB
function is enabled, the un-used USB buffer can be access by USB re-directed mechanism to extend MCU memory.
Please refer Section “22.4 Access USB 1K FIFO” for more detail description.
Program memory is the memory which stores the program codes for the CPU to execute, as shown in Figure 6–1.
After reset, the CPU begins execution from location 0000H, where should be the starting of the user’s application
code. To service the interrupts, the interrupt service locations (called interrupt vectors) should be located in the
program memory. Each interrupt is assigned a fixed location in the program memory. The interrupt causes the CPU
to jump to that location, where it commences execution of the service routine. External Interrupt 0, for example, is
assigned to location 0003H. If External Interrupt 0 is going to be used, its service routine must begin at location
0003H. If the interrupt is not going to be used, its service location is available as general purpose program memory.
The interrupt service locations are spaced at an interval of 8 bytes: 0003H for External Interrupt 0, 000BH for Timer
0, 0013H for External Interrupt 1, 001BH for Timer 1, etc. If an interrupt service routine is short enough (as is often
the case in control applications), it can reside entirely within that 8-byte interval. Longer service routines can use a
jump instruction to skip over subsequent interrupt locations, if other interrupts are in use.
Interrupt 001BH
Locations
0013H
000BH 8 bytes
0003H
Reset 0000H
Figure 6–2 shows the internal and external data memory spaces available to the MG84FG516 user. Internal data
memory can be divided into three blocks, which are generally referred to as the lower 128 bytes of RAM, the upper
128 bytes of RAM, and the 128 bytes of SFR space. Internal data memory addresses are always 8-bit wide, which
implies an address space of only 256 bytes. Direct addresses higher than 7FH access the SFR space; and indirect
addresses higher than 7FH access the upper 128 bytes of RAM. Thus the SFR space and the upper 128 bytes of
RAM occupy the same block of addresses, 80H through FFH, although they are physically separate entities.
The lower 128 bytes of RAM are present in all 80C51 devices as mapped in Figure 6–3. The lowest 32 bytes are
grouped into 4 banks of 8 registers. Program instructions call out these registers as R0 through R7. Two bits in the
Program Status Word (PSW) select which register bank is in use. This allows more efficient use of code space,
since register instructions are shorter than instructions that use direct addressing. The next 16 bytes above the
register banks form a block of bit-addressable memory space. The 80C51 instruction set includes a wide selection of
single-bit instructions, and the 128 bits in this area can be directly addressed by these instructions. The bit
addresses in this area are 00H through 7FH.
All of the bytes in the Lower 128 can be accessed by either direct or indirect addressing while the Upper 128 can
only be accessed by indirect addressing.
Figure 6–4 gives a brief look at the Special Function Register (SFR) space. SFRs include the Port latches, timers,
peripheral controls, etc. These registers can only be accessed by direct addressing. Sixteen addresses in SFR
space are both byte- and bit-addressable. The bit-addressable SFRs are those whose address ends in 0H or 8H.
To access the external data memory, the EXTRAM bit should be set to 1. Accesses to external data memory can
use either a 16-bit address (using ‘MOVX @DPTR’) or an 8-bit address (using ‘MOVX @Ri’), as described below.
8-bit addresses are often used in conjunction with one or more other I/O lines to page the RAM. If an 8-bit
address is being used, the contents of the Port 2 SFR remain at the Port 2 pins throughout the external memory
cycle. This will facilitate paging access. Figure 6–5 shows an example of a hardware configuration for accessing
up to 2K bytes of external RAM. In multiplexed mode, Port 0 serves as a multiplexed address/data bus to the
RAM, and 3 lines of Port 2 are being used to page the RAM. The CPU generates nRD and nWR (alternate
functions of P3.7 and P3.6) to strobe the memory. Of course, the user may use any other I/O lines instead of P2
to page the RAM.
16-bit addresses are often used to access up to 64k bytes of external data memory. Figure 6–6 shows the
hardware configuration for accessing 64K bytes of external RAM. Whenever a 16-bit address is used, in addition
to the functioning of P0, nRD and nWR, the high byte of the address comes out on Port 2 and it is held during
the read or write cycle.
Non-address mode is provided to access the external data memory without MCU address limitation. Figure 6–7
shows the hardware configuration for accessing the external RAM. It also supports the FIFO structure memory,
such as NAND flash. Whenever a non-address mode is selected, in addition to the functioning of P0, nRD and
nWR, the address phase is skipped to speed up the access performance.
In multiplexed case, the low byte of the address is time-multiplexed with the data byte on Port 0. ALE (Address Latch
Enable) should be used to capture the address byte into an external latch. The address byte is valid at the negative
transition of ALE. Then, in a write cycle, the data byte to be written appears on Port 0 just before nWR is activated,
and remains there until after nWR is deactivated. In a read cycle, the incoming byte is accepted at Port 0 just before
the read strobe is deactivated. During any access to external memory, the CPU writes 0FFH to the Port 0 latch (the
Special Function Register), thus obliterating whatever information the Port 0 SFR may have been holding.
To access the on-chip expanded RAM (XRAM), the EXTRAM bit should be cleared to 0. Refer to Figure 6–2, the
4096/5120 bytes of XRAM (0000H to 0FFFH/13FFFH) are indirectly accessed by move external instruction, MOVX.
Addressable by
Indirect External
Off-Chip Addressing
XRAM
Area Using MOVX
without
EXTRAM case
30H
2FH
Bit Addressable
20H
Bank 3 1FH
18H
Bank 2 17H
Four banks of 8 10H
registers R0~R7 Bank 1 0FH
08H Reset value of
Bank 0 07H
00H Stack Pointer
B0H Port 3
A0H Port 2
90H Port 1
80H Port 0
Figure 6–5. External RAM Accessing by an 8-Bit Address (Using ‘MOVX @ Ri’ and Page Bits)
MG84FG516 SRAM
Latch
P2
Page Bits
I/O
Note that in this case, the other bits of P2 are available as general I/O pins.
Figure 6–6. External RAM Accessing by a 16-Bit Address (Using ‘MOVX @ DPTR’)
Latch
(P2) A[15:8]
Figure 6–7. External RAM Accessing in expand memory by I/O configured Address
Peripheral
MG84FG516 Controller
Addr/Cmd
Port I/O
Control Lines
I/O
It also fits the FIFO Architecture Accessing as a NAND type flash application.
To access the on-chip expanded RAM (XRAM), refer to Figure 6–2, the 4906/5120 bytes of XRAM (0000H to
0FFFH/13FF) are indirectly accessed by move external instruction, “MOVX @Ri” and “MOVX @DPTR”. For
KEIL-C51 compiler, to assign the variables to be located at XRAM, the “pdata” or “xdata” definition should be used.
After being compiled, the variables declared by “pdata” and “xdata” will become the memories accessed by “MOVX
@Ri” and “MOVX @DPTR”, respectively. Thus the MG84FG516 hardware can access them correctly.
If ENUSB (CKCON0.7) is cleared to disable USB function, the USB 1024 bytes buffer is moved to on-chip expanded
RAM area which will provide 5120 bytes XRAM in MG84FG516. If ENUSB is set to enable USB function,
MG84FG516 just provides 4096 bytes XRAM in software application. In most application case, software does not
spend all 1K bytes USB buffer, the unused USB buffer can provide MCU additional data RAM which is access
through USB re-directed mechanism. Please refer Section “22.4 Access USB 1K FIFO” to get the detail information
for the buffer access.
Bit 7: EMAI1, EMAI1 configures the External data Memory Access Interface mode as following:
0: Multiplexed address/data.
1: No Address phase access
Bit 6: Reserved. Software must write “0” on this bit when STRETCH is written.
Bit 5~4: ALES[1:0], EMAI ALE pulse width select bits. It only has effect when EMAI in Multiplexed mode.
00: ALE high and ALE low pulse width = 1 SYSCLK cycle.
01: ALE high and ALE low pulse width = 2 SYSCLK cycle.
10: ALE high and ALE low pulse width = 3 SYSCLK cycle.
11: ALE high and ALE low pulse width = 4 SYSCLK cycle.
Bit 2~0: RWS[2:0], EMAI Read/Write command pulse width select bits.
000: /RD and /WR pulse width = 1 SYSCLK cycle.
001: /RD and /WR pulse width = 2 SYSCLK cycle.
010: /RD and /WR pulse width = 3 SYSCLK cycle.
011: /RD and /WR pulse width = 4 SYSCLK cycle.
100: /RD and /WR pulse width = 5 SYSCLK cycle.
101: /RD and /WR pulse width = 6 SYSCLK cycle.
110: /RD and /WR pulse width = 7 SYSCLK cycle.
111: /RD and /WR pulse width = 8 SYSCLK cycle.
ADDR[15:8] P2
ALES[1:0] ALES[1:0]
ADDR[15:8] P2
ALES[1:0] ALES[1:0]
ALES[1:0] ALES[1:0]
ALES[1:0] ALES[1:0]
ADDR[15:8] P2
ADDR[15:8] P2
The declaration identifiers in a C51-compiler for the various MG84FG516 memory spaces are as follows:
data
128 bytes of internal data memory space (00h~7Fh); accessed via direct or indirect addressing, using instructions
other than MOVX and MOVC. All or part of the Stack may be in this area.
idata
Indirect data; 256 bytes of internal data memory space (00h~FFh) accessed via indirect addressing using
instructions other than MOVX and MOVC. All or part of the Stack may be in this area. This area includes the data
area and the 128 bytes immediately above it.
sfr
Special Function Registers; CPU registers and peripheral control/status registers, accessible only via direct
addressing.
xdata
External data or on-chip eXpanded RAM (XRAM); duplicates the classic 80C51 64KB memory space addressed via
the “MOVX @DPTR” instruction. The MG84FG516 has 4096/5120 bytes of on-chip xdata memory.
pdata
Paged (256 bytes) external data or on-chip eXpanded RAM; duplicates the classic 80C51 256 bytes memory space
addressed via the “MOVX @Ri” instruction. The MG84FG516 has 256 bytes of on-chip pdata memory which is
shared with on-chip xdata memory.
code
64K bytes of program memory space; accessed as part of program execution and via the “MOVC @A+DTPR”
instruction. The MG84FG516 has 64K bytes of on-chip code memory.
DPTR1
DPH DPL
DPS=1
DPS
DPS=0
AUXR1(A2H)
DPH DPL
DPTR0
DPTR Instructions
The six instructions that refer to DPTR currently selected using the DPS bit are as follows:
Bit 0: DPTR select bit, used to switch between DPTR0 and DPTR1.
0: Select DPTR0.
1: Select DPTR1.
The MG84FG516 always boots from IHRCO on 12MHz and reserves crystal pads as P6.0/P6.1 GPIO function.
Software can select the one of the four clock sources by application required and switches them on the fly. But
software needs to settle the clock source stably before clock switching. If software selects external crystal mode,
port pin of P6.0 and P6.1 will be assigned to XTAL2 and XTAL1. And P6.0/P6.1 GPIO function will be inhibited. In
external clock input mode (ECKI), the clock source comes from P6.0 input and P6.1 still reserves GPIO function.
The built-in IHRCO provides the high precision frequency at 12.0MHz with 2% deviation over all operating voltage
and temperature. In IHRCO or ILRCO mode, P6.0 can be configured to internal MCK output or MCK/2 and MCK/4
for system application.
The MG84FG516 device includes a Clock Multiplier to generate the high speed clock for system clock source. It
generates 4/5.33/8 times frequency of CKMI, CKMI is shown in Figure 8–1 and its typical input is 6MHz. This function
provides the high speed operation on MCU without external high-frequency crystal. To find the detailed CKM
performance, please refer Section “28.6 CKM Characteristics”).
The built-in ILRCO provides the low power and low speed frequency about 32KHz to WDT and system clock source.
MCU can selects the ILRCO to system clock source by software for low power operation. To find the detailed IHRCO
performance, please refer Section “28.5 ILRCO Characteristics”). In ILRCO mode, P6.0 can be configured to internal
MCK output or MCK/2 and MCK/4 for system application.
The system clock, SYSCLK, is obtained from one of these four clock sources through the clock divider, as shown in
Figure 8–1. The user can program the divider control bits SCKS2~SCKS0 (in CKCON0 register) to get the desired
system clock.
Figure 8–1 presents the principal clock systems in the MG84FG516. The system clock can be sourced by the
external oscillator circuit or either internal oscillator.
2~25MHz 0 ¸1 24MHz
(System Clock)
XTAL1 (P6.1) Oscillating 3
Circuit
1 OSCin ¸2 6MHz
Clock
32MHz ENUSB
XTAL2 (P6.0) Multiplier (CKCON0.7)
2 ¸4 (CKM) 48MHz USB Logic
XTALE
enable
3 ¸6
(CKCON2.5) 32KHz
ILRCO P6.0
0
SFR
0~36MHz
ECKI (P6.0) ENCKM MCKS1,0 1
(CKCON0.6) (CKCON2.3~2) P6.0(XTAL2)
00: OSCin ¸2 2
OSCS1,0 CKMIS1,0
(CKCON2.1~0) 00: OSCin = IHRCO (CKCON0.5~4) 00: 6MHz
01: 24MHz
10: 32MHz ¸4 3
01: OSCin = XTAL 01: 12MHz
11: Reserved 00: P6.0
10: OSCin = ILRCO 10: 24MHz
01: MCK
11: OSCin = ECKI 11: 36MHz
10: MCK/2
AUXR0.7~6 11: MCK/4
(P60OC[1:0])
Bit 3: Reserved. Software must write “0” on this bit when CKCON0 is written.
Bit 7~6: Reserved. Software must write “0” on these bits when CKCON1 is written.
Bit 5~0: This is set the OSCin frequency value to define the time base of ISP/IAP programming. Fill with a proper
value according to OSCin, as listed below.
For examples,
(1) If OSCin=12MHz, then fill [XCKS5~XCKS0] with 11, i.e., 00-1011B.
(2) If OSCin=6MHz, then fill [XCKS5~XCKS0] with 5, i.e., 00-0101B.
OSCin XCKS[4:0]
1MHz 00-0000
2MHz 00-0001
Bit 7~6: XTGS1~XTGS0, XTAL oscillator Gain control Register. Software must writ “01” on the two bits.
Bit 7~6: P6.0 function configured control bit 1 and 0. The two bits only act when internal RC oscillator (IHRCO or
ILRCO) is selected for system clock source. In crystal mode, XTAL2 and XTAL1 are the alternated function of P6.0
and P6.1. In external clock input mode, P6.0 is the dedicated clock input pin. In internal oscillator condition, P6.0
provides the following selections for GPIO or clock source generator. When P60OC[1:0] index to non-P6.0 GPIO
function, P6.0 will drive the on-chip RC oscillator (IHRCO or ILRCO) output to provide the clock source for other
devices.
(1). Required Function: Select XTAL as OSCin source when MCU using IHRCO or ILRCO (default is IHRCO)
Assembly Code Example:
C Code Example:
(2). Required Function: Select ILRCO as OSCin source when MCU using IHRCO, ECKI or XTAL (default is IHRCO)
Assembly Code Example:
ANL IFD,#~(HSE) ; Disable HSE when SYSCLK 6MHz for power saving
CALL _page_p_sfr_write ; Write data to PCON2
C Code Example:
IFD &= ~HSE; // Disable HSE when SYSCLK 6MHz for power saving
page_p_sfr_write(); // Write data to PCON2
(3). Required Function: Select 32M as OSCin source when MCU using CKM (default is IHRCO)
Assembly Code Example:
ORL IFD,#(HSE) ; Set HSE =1, Due to SYSCLK=32M more than 25MHz (HSE=1)
CALL _page_p_sfr_write ; Write data to PCON2,
C Code Example:
(4). Required Function: Select IHRCO as OSCin source when MCU using ILRCO, ECKI or XTAL
Assembly Code Example:
Delay_32us
Delay 32us
C Code Example:
SFRPI = 1; // SFRPI=1.
P6M0 |= P6M0; // P6.0 select push-pull output mode.
SFRPI = 0; // SFRPI=0.
AUXR0 &= ~(P60OC0 | P60OC1); // Switch P6.0 to GPIO function
AUXR0 |= (P60OC0 | P6FD); // P6.0 output IHRCO/1
// AUXR0 = P60OC1|P6FD; // P6.0 output IHROC/2
// AUXR0 = P60OC1|P60OC0|P6FD; // P6.0 output IHRCO/4
The Watch-dog Timer (WDT) is intended as a recovery method in situations where the CPU may be subjected to
software upset. The WDT consists of a 9-bit free-running counter, a 7-bit prescaler and a control register (WDTCR).
Figure 9–1 shows the WDT structure in MG84FG516.
When WDT is enabled, it derives its time base from the 32KHz ILRCO. The WDT overflow will set the WDTF on
PCON1.0 which can be configured to generate an interrupt by enabled WDTFIE (SFIE.0) and enabled ESF (EIE1.3).
The overflow can also trigger a system reset when WREN (WDTCR.7) is set. To prevent WDT overflow, software
needs to clear it by writing “1” to the CLRW bit (WDTCR.4) before WDT overflows.
Note: the WDTFIE function is under verifying.
Once the WDT is enabled by setting ENW bit, there is no way to disable it except through power-on reset or page-p
SFR over-write on ENW, which will clear the ENW bit. The WDTCR register will keep the previous programmed
value unchanged after hardware (RST-pin) reset, software reset and WDT reset.
WREN, NSW and ENW are implemented to one-time-enabled function, only writing “1” valid in general SFR page.
Page-P SFR Access on WDTCR can disable WREN, NSW and ENW, writing “0” on WDTCR.7~5. Please refer
Section “9.3 WDT Register” and Section “24 Page P SFR Access” for more detail information.
WDT Reset
WREN
WDTCR Register WREN NSW ENW CLRW WIDL PS2 PS1 PS0
In the Idle mode, the WIDL bit (WDTCR.3) determines whether WDT counts or not. Set this bit to let WDT keep
counting in the Idle mode. If the hardware option NSWDT is enabled, the WDT always keeps counting regardless of
WIDL bit.
In the Power down mode, the ILRCO won’t stop if the NSW (WDTCR.6) is enabled. The MUC enters Watch mode.
That lets WDT keep counting even in Power down mode (Watch Mode). After WDT overflows, it will wake up the
CPU from interrupt or reset by software configured.
Bit 7: WREN, WDT Reset Enable. The initial value can be changed by hardware option, WRENO.
0: The overflow of WDT does not set the WDT reset. The WDT overflow flag, WDTF, may be polled by software or
trigger an interrupt.
1: The overflow of WDT will cause a system reset. Once WREN has been set, it can not be cleared by software in
page 0~F. In page P, software can modify it to “0” or “1”.
Bit 6: NSW. Non-Stopped WDT. The initial value can be changed by hardware option, NSWDT.
0: WDT stop counting while the MCU is in power-down mode.
1: WDT always keeps counting while the MCU is in power-down mode (Watch Mode) or idle mode. Once NSW has
been set, it can not be cleared by software in page 0~F. In page P, software can modify it to “0” or “1”.
Bit 2~0: PS2 ~ PS0, select prescaler output for WDT time base input.
PS[2:0] Prescaler Value WDT Period
0 0 0 1 15 ms
0 0 1 2 31 ms
0 1 0 4 62 ms
0 1 1 8 124 ms
1 0 0 16 248 ms
1 0 1 32 496 ms
1 1 0 64 992 ms
1 1 1 128 1.984 S
In addition to being initialized by software, the WDTCR register can also be automatically initialized at power-up by
the hardware options WRENO, NSWDT, HWENW, HWWIDL and HWPS[2:0], which should be programmed by a
universal Writer or Programmer, as described below.
If HWENW is programmed to “enabled”, then hardware will automatically do the following initialization for the
WDTCR register at power-up: (1) set ENW bit, (2) load WRENO into WREN bit, (3) load NSWDT into NSW bit, (4)
load HWWIDL into WIDL bit, and (5) load HWPS[2:0] into PS[2:0] bits.
If both of HWENW and WDSFWP are programmed to “enabled”, hardware still initializes the WDTCR register
content by WDT hardware option at power-up. Then, any CPU writing on WDTCR bits will be inhibited except writing
“1” on WDTCR.4 (CLRW), clear WDT, even though access through Page-P SFR mechanism.
WRENO:
: Enabled. Set WDTCR.WREN to enable a system reset function by WDTF.
: Disabled. Clear WDTCR.WREN to disable the system reset function by WDTF.
WDSFWP:
: Enabled. The WDT SFRs, WREN, NSW, WIDL, PS2, PS1 and PS0 in WDTCR, will be write-protected.
: Disabled. The WDT SFRs, WREN, NSW, WIDL, PS2, PS1 and PS0 in WDTCR, are free for writing of software.
(1) Required function: Enable WDT and select WDT period to 248ms
Assembly Code Example:
C Code Example:
C Code Example:
(3). Required Function: Enable WDT reset function and select WDT period to 62ms
Assembly Code Example:
C Code Example:
C Code Example:
The following sections describe the reset happened source and corresponding control registers and indicating flags.
Figure 10–1 presents the reset systems in the MG84FG516 and all of its reset sources.
Power-On Reset
EXRF
External Reset
SWRF
Software Reset
WDT Reset
WDT Overflow
WDTCR.WREN
Power-on reset (POR) is used to internally reset the CPU during power-up. The CPU will keep in reset state and will
not start to work until the VDD power rises above the voltage of Power-On Reset. And, the reset state is activated
again whenever the VDD power falls below the POR voltage. During a power cycle, VDD must fall below the POR
voltage before power is reapplied in order to ensure a power-on reset
The Power-on Flag, POF0, is set to “1” by hardware during power up or when VDD power drops below the POR
voltage. It can be clear by firmware and is not affected by any warm reset such as external reset, Brown-Out reset,
software reset (ISPCR.5) and WDT reset. It helps users to check if the running of the CPU begins from power up or
not. Note that the POF0 must be cleared by firmware.
A reset is accomplished by holding the RESET pin HIGH for at least 24 oscillator periods while the oscillator is
running. To ensure a reliable power-up reset, the hardware reset from RST pin is necessary.
Software can trigger the CPU to restart by software reset, writing “1” on SWRST (ISPCR.5), and set the SWRF flag
(PCON1.7). SWBS decides the CPU is boot from ISP or AP region after the reset action
In MG84FG516, if software program runs to illegal address such as over program ROM limitation, it triggers a
RESET to CPU.
In MG84FG516, there are two Brown-Out Detectors (BOD0 & BOD1) to monitor VDD power. BOD0 services the
fixed detection level at VDD=2.2V. BOD1 detects the VDD level by software selecting 4.2V, 3.7V, 2.4V or 2.0V. If
VDD power drops below BOD0 or BOD1 monitor level. Associated flag, BOF0 and BOF1, is set. If BO0RE
(PCON2.1) is enabled, BOF0 indicates a BOD0 Reset occurred. If BO1RE (PCON2.3) is enabled, BOF1 indicates a
BOD1 Reset occurred.
When WDT is enabled to start the counter, WDTF will be set by WDT overflow. If WREN (WDTCR.7) is enabled, the
WDT overflow will trigger a system reset that causes CPU to restart. Software can read the WDTF to recognize the
WDT reset occurred.
C Code Example:
C Code Example:
BOD0 and BOD1 report the chip power status on the flags, BOF0 and BOF1, which provide the capability to
interrupt CPU or to reset CPU by software configured. The six power-reducing modes provide the different
power-saving scheme for chip application. These modes are accessed through the CKCON0, CKCON2, PCON0,
PCON1, PCON2, PCON3 and WDTCR register.
In MG84FG516, there are two Brown-Out Detectors (BOD0 & BOD1) to monitor VDD power. Figure 11–1 shows the
functional diagram of BOD0 and BOD1. BOD0 services the fixed detection level at VDD=2.2V and BOD1 detects the
software selection levels (4.2V/3.7V/2.4V/2.0V) on VDD. Associated flag, BOF0 (PCON1.1), is set when BOD0
meets the detection level. If both of ESF (EIE1.3) and BOF0IE (SFIE.1) are enabled, a set BOF0 will generate a
system flag interrupt. It can interrupt CPU either CPU in normal mode or idle mode. The BOD1 has the same flag
function, BOF1, and same interrupt function. The BOD1 interrupt also wakes up CPU in power down mode if
AWBOD1 (PCON3.3) is enabled.
If BO0RE (PCON2.1) is enabled, the BOD0 event will trigger a system reset and set BOF0 to indicate a BOD0 Reset
occurred. The BOD0 reset restart the CPU either CPU in normal mode or idle mode. BOD1 also has the same reset
capability with associated control bit, BO1RE (PCON2.3). The BOD1 reset also restart CPU in power down mode if
AWBOD1 (PCON3.3) is enabled in BOD1 reset operation.
Voltage “1”
Comparator
- Load
ESF
2.2V + (EIE1.3)
The ADC input channels must be set to “Analog Input Only” in P1AIO SFR when MCU is in idle mode or
power-down mode.
The user should not attempt to enter (or re-enter) the power-down mode for a minimum of 4 μs until after one of the
following conditions has occurred: Start of code execution (after any type of reset), or Exit from power-down mode.
To ensure minimum power consumption in power down mode, software must confirm all I/O not in floating state,
including the port I/Os un-appearance on package pins. For example, P5.7~P5.0 and P6.7~P6.2 are un-appearance
in MG84FG516AD48 (LQFP48) package pins. Software may configure P5/P6 corresponding bit SFR to “0” (output
low) to avoid pin floating in power-down mode.
AUXR0.INT0H IE.EX0
force to level-sensitive in PD
TCON.IT1=0
nINT1 input 0
nINT1 Wakeup
IE1
1
AUXR0.INT1H IE.EX1
force to level-sensitive in PD
XICON.IT2=0
nINT2 input 0
nINT2 Wakeup
IE2
1
XICON.INT2H XICON.EX2
force to level-sensitive in PD
XICON.IT3=0
nINT3 input 0
nINT3 Wakeup
IE3
1
XICON.INT3H XICON.EX3
force to level-sensitive in PD
Keypad Wakeup
KBIF
EIE1.EKB
URSM EIE1.EUSB
USB Wakeup
USBSFR: UPCON.1 Clear PCON0.PD
Event OR & Wakeup CPU
URST USBSFR: IEN.EFSR
USBSFR: UPCON.2
Timer 2 External Input
Wakeup
EXF2
IE.ET2
EIE2.ET3
SCFG1.S1TME
S1 Timer External Input
RXD1 Pin 0 Wakeup
RI1
1
SCON1.REN1 EIE1.ES1
SCON1.RB81
EIE1.ESF
WDT Wakeup
WDTCR.ENW SFIE.WDTFIE
En Overflow
ILRCO WDT WDTF
PCON0.PD
WDT Reset
WDTCR.NSW
WDTCR.WREN RESET Wakeup
External Reset
PCON2.BO1RE BOD1 Reset
EIE1.ESF
BOD1 Wakeup
SFIE.BOF1IE
PCON2.EBOD1 En
BOD1 BOF1
PCON0.PD
PCON3.AWBOD1
When terminating Power-down by an interrupt, the wake up period is internally timed. At the falling edge on the
interrupt pin, Power-down is exited, the oscillator is restarted, and an internal timer begins counting. The internal
clock will not be allowed to propagate and the CPU will not resume execution until after the timer has reached
internal counter full. After the timeout period, the interrupt service routine will begin. To prevent the interrupt from
re-triggering, the ISR should disable the interrupt before returning. The interrupt pin should be held low until the
device has timed out and begun executing.
It should be noted that when idle is terminated by a hardware reset, the device normally resumes program execution,
from where it left off, up to two machine cycles before the internal reset algorithm takes control. On-chip hardware
inhibits access to internal RAM in this event, but access to the port pins is not inhibited. To eliminate the possibility of
an unexpected write to a port pin when Idle is terminated by reset, the instruction following the one that invokes Idle
should not be one that writes to a port pin or to external memory.
Wakeup from Power-down through an enabled wakeup KBI is same to the interrupt. At the matched condition of
enabled KBI pattern and enabled KBI interrupt (EIE1.5, EKB), Power-down is exited, the oscillator is restarted, and
an internal timer begins counting. The internal clock will not be allowed to propagate to the CPU until after the timer
has reached internal counter full. After the timeout period, CPU will meet a KBI interrupt and execute the interrupt
service routine.
Bit 5~3: Reserved. Software must write “0” on these bits when PCON1 is written.
Bit 2: EBOD1, Enable BOD1 that monitors VDD power dropped at a BO1S1~0 specified voltage level.
0: Disable BOD1 to slow down the chip power consumption.
1: Enable BOD1 to monitor VDD power dropped.
Bit 0: Reserved. Software must write “1” on this bit when PCON2 is written.
Bit 7~4: Reserved. Software must write “0” on these bits when PCON3 is written.
Bit 2~1: Reserved. Software must write “0” on these bits when PCON3 is written.
Bit 0: OCDE, OCD enable. The initial value is loaded from OR and reset by POR.
0: Disable OCD interface on P4.4 and P4.5
1: Enable OCD interface on P4.4 and P4.5.
(1) Required function: Select Slow mode with OSCin/128 (default is OSCin)
Assembly Code Example:
ANL IFD,#~(HSE) ; Disable HSE when SYSCLK 6MHz for power saving
CALL _page_p_sfr_write ; Write data to PCON2
C Code Example:
IFD &= ~HSE; // Disable HSE when SYSCLK 6MHz for power saving
page_p_sfr_write(); // Write data to PCON2
ANL IFD,#~(HSE) ; Disable HSE when SYSCLK 6MHz for power saving
CALL _page_p_sfr_write ; Write data to PCON2
C Code Example:
IFD = IFD & ~(HSE); // Disable HSE when SYSCLK 6MHz for power saving
page_p_sfr_write(); // Write data to PCON2
(3). Required Function: Switch MCU running with 32.768KHz XTAL mode
Assembly Code Example:
ANL IFD,#~(HSE) ; Disable HSE when SYSCLK 6MHz for power saving
CALL _page_p_sfr_write ; Write data to PCON2
C Code Example:
Dealy_10mS();
IFD &= ~HSE; // Disable HSE when SYSCLK 6MHz for power saving
page_p_sfr_write(); // Write data to PCON2
ORG 0003Bh
SystemFlag_ISR:
ANL PCON1,#(WDTF) ; Clear WDT flag (write “1”)
RETI
main:
ANL PCON1,#WDTF ; Clear WDTF flag (write “1”)
C Code Example:
ORG 0003Bh
SystemFlag_ISR:
ANL PCON1,#(BOF1) ; Clear BOD1 flag (write “1”)
RETI
main:
MOV IFADRL,#PCON3 ; Index Page-P address to PCON3
CALL _page_p_sfr_read ; Read PCON3 data
C Code Example:
void main()
{
12.1. IO Structure
The I/O operating modes are distinguished two groups in MG84FG516. The first group is only for Port 3 to support
four configurations on I/O operating. These are: quasi-bidirectional (standard 8051 I/O port), push-pull output,
input-only (high-impedance input) and open-drain output. The Port 3 default setting is quasi-bidirectional mode with
weakly pull-up resistance.
All other general port pins belong to the second group. They can be programmed to two output modes, push-pull
output and open-drain output with pull-up resistor control. The default setting of this group I/O is open-drain mode
with output high, which means input mode with high impedance state.
Following sections describe the configuration of the all types I/O mode.
Port 3 pins in quasi-bidirectional mode are similar to the standard 8051 port pins. A quasi-bidirectional port can be
used as an input and output without the need to reconfigure the port. This is possible because when the port outputs
a logic high, it is weakly driven, allowing an external device to pull the pin low. When the pin outputs low, it is driven
strongly and able to sink a large current. There are three pull-up transistors in the quasi-bidirectional output that
serve different purposes.
One of these pull-ups, called the “very weak” pull-up, is turned on whenever the port register for the pin contains a
logic “1”. This very weak pull-up sources a very small current that will pull the pin high if it is left floating. A second
pull-up, called the “weak” pull-up, is turned on when the port register for the pin contains a logic “1” and the pin itself
is also at a logic “1” level. This pull-up provides the primary source current for a quasi-bidirectional pin that is
outputting a 1. If this pin is pulled low by the external device, this weak pull-up turns off, and only the very weak
pull-up remains on. In order to pull the pin low under these conditions, the external device has to sink enough
current to over-power the weak pull-up and pull the port pin below its input threshold voltage. The third pull-up is
referred to as the “strong” pull-up. This pull-up is used to speed up low-to-high transitions on a quasi-bidirectional
port pin when the port register changes from a logic “0” to a logic “1”. When this occurs, the strong pull-up turns on
for one CPU clocks, quickly pulling the port pin high.
1 clock
Very
delay Strong Weak
weak
Port
Pin
Input data
The push-pull output configuration on Port 3 has the same pull-down structure as both the open-drain and the
quasi-bidirectional output modes, but provides a continuous strong pull-up when the port register contains a logic “1”.
The push-pull mode may be used when more source current is needed from a port output. In addition, the input path
of the port pin in this configuration is also the same as quasi-bidirectional mode.
Strong
Port
Pin
Input data
The input-only configuration on Port 3 is an input without any pull-up resistors on the pin, as shown in Figure 12–3.
Port
Input data
Pin
The open-drain output configuration on Port 3 turns off all pull-ups and only drives the pull-down transistor of the port
pin when the port register contains a logic “0”. To use this configuration in application, a port pin must have an
external pull-up, typically a resistor tied to VDD. The pull-down for this mode is the same as for the
Port
Pin
Input data
The open-drain output configuration on general port pins only drives the pull-down transistor of the port pin when the
Port Data register contains a logic “0”. To use this configuration in application, a port pin can select an external
pull-up, or an on-chip pull-up by software enabled in PUCON0 and PUCON1.
PUxx Very
Weak
weak
Port
Pin
Input data
The push-pull output configuration on general port pins has the same pull-down structure as the open-drain output
modes, but provides a continuous strong pull-up when the port register contains a logic “1”. The push-pull mode may
be used when more source current is needed from a port output. In addition, the input path of the port pin in this
configuration is also the same as open-drain mode.
VDD
Strong
Port
Pin
Input data
A Port pin is configured as a digital input by setting its output mode to “Open-Drain” and writing a logic “1” to the
associated bit in the Port Data register. For example, P1.7 is configured as a digital input by setting P1M0.7 to a
logic 0 and P1.7 to a logic 1.
All I/O port pins on the MG84FG516 may be individually and independently configured by software to select its
operating mode. Only Port 3 has four operating modes, as shown in Table 12–2. Two mode registers select the
output type for each port 3 pin.
Other general port pins support two operating modes, as shown in Table 12–3. One mode register selects the output
type for each port pin.
P3.7 and P3.6 have alternated function for /RD and /WR in off-chip memory access cycle.
Bit 7: Reserved. Software must write “1” on this bit when P4 is written.
Bit 7: Reserved. Software must write “0” on this bit when P4M0 is written.
Bit 6~0:
0: Port pin output mode is configured to open-drain.
1: Port pin output mode is configured to push-pull.
Bit 7 ~ 4: Reserved. Software must write “0” on this bit when PUCON1 is written.
(1). Required Function: Set P1.0 to input mode with on-chip pull-up resistor enabled
Assembly Code Example:
C Code Example:
Table 13–1 lists all the interrupt sources. The ‘Request Bits’ are the interrupt flags that will generate an interrupt if it
is enabled by setting the ‘Enable Bit’. Of course, the global enable bit EA (in IE0 register) should have been set
previously. The ‘Request Bits’ can be set or cleared by software, with the same result as though it had been set or
cleared by hardware. That is, interrupts can be generated or pending interrupts can be cancelled in software. The
‘Priority Bits’ determine the priority level for each interrupt. The ‘Priority within Level’ is the polling sequence used to
resolve simultaneous requests of the same priority level. The ‘Vector Address’ is the entry point of an interrupt
service routine in the program memory.
Figure 13–1 shows the interrupt system. Each of these interrupts will be briefly described in the following sections.
S0CON.RI0 IE.ES0
S0CON.TI0
T2CON.TF2 IE.ET2
T2CON.EXF2
XICON.IT2
XICON.EX2
nINT2 0
IE2
1
XICON.INT2H
XICON.IT3
XICON.EX3
nINT3 0
IE3
1
XICON.INT3H
EIE1.ESPI
SPSTAT.SPIF
EIE1.EADC
ADCON0.ADCI
EIE1.EPCA
PCON1.BOF0
SFIE.BOF0IE
EIE1.ESF
PCON1.BOF1
SFIE.BOF1IE
PCON1.WDTF
SFIE.WDTFIE
EIE1.ES1
S1CON.RI1
S1CON.TI1
EIE1.EKB
KBCON.KBIF
EIE1.ETWSI
SICON.SI
EIE1.EUSB
USB Interrupt Flags
T3CON.TF3 EIE2.ET3
T3CON.EXF3
Lowest Priority
Level Interrupt
The external interrupt nINT0, nINT1, nINT2 and nINT3 can each be either level-activated or transition-activated,
depending on bits IT0 and IT1 in register TCON, IT2 and IT3 in register XICON. The flags that actually generate
these interrupts are bits IE0 and IE1 in TCON, IE2 and IE3 in XICON. When an external interrupt is generated, the
flag that generated it is cleared by the hardware when the service routine is vectored to only if the interrupt was
transition –activated, then the external requesting source is what controls the request flag, rather than the on-chip
hardware.
The Timer0 and Timer1 interrupts are generated by TF0 and TF1, which are set by a rollover in their respective
Timer/Counter registers in most cases. When a timer interrupt is generated, the flag that generated it is cleared by
the on-chip hardware when the service routine is vectored to.
The serial port 0 interrupt is generated by the logical OR of RI0 and TI0. Neither of these flags is cleared by
hardware when the service routine is vectored to. The service routine should poll RI0 and TI0 to determine which
one to request service and it will be cleared by software.
The timer2 interrupt is generated by the logical OR of TF2 and EXF2. Just the same as serial port, neither of these
flags is cleared by hardware when the service routine is vectored to.
SPI interrupt is generated by SPIF in SPSTAT, which are set by SPI engine finishes a SPI transfer. It will not be
cleared by hardware when the service routine is vectored to.
The ADC interrupt is generated by ADCI in ADCON0. It will not be cleared by hardware when the service routine is
vectored to.
The PCA interrupt is generated by the logical OR of CF, CCF5, CCF4, CCF3, CCF2, CCF1 and CCF0 in CCON.
Neither of these flags is cleared by hardware when the service routine is vectored to. The service routine should poll
these flags to determine which one to request service and it will be cleared by software.
The serial port 1 interrupt is generated by the logical OR of RI1 and TI1. Neither of these flags is cleared by
hardware when the service routine is vectored to. The service routine should poll RI1 and TI1 to determine which
one to request service and it will be cleared by software.
The keypad interrupt is generated by KBCON.KBIF, which is set by Keypad module meets the input pattern. It will
not be cleared by hardware when the service routine is vectored to.
The TWSI interrupt is generate by SI in SICON, which is set by TWSI engine detecting a new bus state updated. It
will not be cleared by hardware when the service routine is vectored to.
The USB interrupt is generate by a grouping of USB event flags in USB SFR, which are set by USB engine detecting
a new bus state or USB function event happened. They will not be cleared by hardware when the service routine is
vectored to.
The timer3 interrupt is generated by the logical OR of TF3 and EXF3. Just the same as serial port, neither of these
flags is cleared by hardware when the service routine is vectored to.
All of the bits that generate interrupts can be set or cleared by software, with the same result as though it had been
set or cleared by hardware. In other words, interrupts can be generated or pending interrupts can be canceled in
software.
There are 17 interrupt sources available in MG84FG516. Each of these interrupt sources can be individually
enabled or disabled by setting or clearing an interrupt enable bit in the registers IE, EIE1, EIE2 and XICON. IE also
contains a global disable bit, EA, which can be cleared to disable all interrupts at once. If EA is set to ‘1’, the
interrupts are individually enabled or disabled by their corresponding enable bits. If EA is cleared to ‘0’, all interrupts
are disabled.
Each interrupt source has two corresponding bits to represent its priority. One is located in SFR named IPnH and the
other in IPnL register. Higher-priority interrupt will be not interrupted by lower-priority interrupt request. If two
interrupt requests of different priority levels are received simultaneously, the request of higher priority is serviced. If
interrupt requests of the same priority level are received simultaneously, an internal polling sequence determine
which request is serviced. Table 13–2 shows the internal polling sequence in the same priority level and the interrupt
vector address.
Each interrupt flag is sampled at every system clock cycle. The samples are polled during the next system clock. If
one of the flags was in a set condition at first cycle, the second cycle(polling cycle) will find it and the interrupt
system will generate an hardware LCALL to the appropriate service routine as long as it is not blocked by any of the
following conditions.
Block conditions:
Any of these three conditions will block the generation of the hardware LCALL to the interrupt service routine.
Condition 2 ensures that the instruction in progress will be completed before vectoring into any service routine.
Condition 3 ensures that if the instruction in progress is RETI or any access to IE or IP0L, IP0H, EIE1, EIP1L or
EIP1H, then at least one or more instruction will be executed before any interrupt is vectored to.
The MG84FG516 provides flexible nINT2 and nINT3 source selection to share the port pin input of on-chip serial
interface. That will support the additional remote wakeup function for communication peripheral in power-down
mode. The nINT2/nINT3 input can be routed to the interface pin to catch port change and set them as an interrupt
input event to wakeup MCU. INT3H (XICON.7) and INT2H (XICON.3) configure the port change detection level on
low/falling or high/rising event. In MCU power-down mode, both of the falling edge or rising edge configurations of
the external interrupts are forced to level-sensitive operation.
AUXR0.P4FS1~0 = 10
P3.0 0 RXD0
P4.4 1 0
XICON.IT2
AUXR0.P4FS1~0 = 01 1 nINT2 input
TWSI_SDA 0
P4.1 2 IE2
1
3
XICON.INT2H
P1.4 0
P5.4 1 SPI_nSS
AUXR1.P5SPI = 1
AUXR2.INT2IS.1~0
AUXR0.P4FS1~0 = 10
P1.2 0 RXD1
P5.2 1 0
XICON.IT3
AUXR1.P5S1 = 1 1 nINT3 input
TWSI_SDA 0
P4.1 2 IE3
1
3
XICON.INT3H
P1.4 0
P5.4 1 SPI_nSS
AUXR1.P5SPI = 1
AUXR2.INT3IS.1~0
Bit 6: Reserved. Software must write “0” on this bit when IE is written.
Bit 7 ~1: Reserved. Software must write “0” on these bits when EIE2 is written.
Bit 7 ~3: Reserved. Software must write “0” on these bits when EIE2 is written.
Bit 7 ~ 1: Reserved. Software must write “0” on these bits when EIP2L is written.
Bit 7 ~ 1: Reserved. Software must write “0” on these bits when EIP2H is written.
Bit 5~4: INT2IS1~0, nINT2 input selection bits which function is defined as following table.
INT2IS1~0 nINT2 Selected Port Pin Switch Condition
00 nINT2 Port Pin P4.3 or P4.4 AUXR0.P4FS1~0
01 RXD0 Port Pin P3.0 or P4.4 AUXR0.P4FS1~0
10 TWSI SDA Port Pin P4.1 None
11 SPI nSS Port Pin P1.4 or P5.4 AUXR1.P5SPI
(1). Required Function: Set INT0 high level wake-up MCU in power-down mode
Assembly Code Example:
ORG 00003h
ext_int0_isr:
to do.....
RETI
main:
SETB P32 ;
C Code Example:
void main(void)
{
P32 = 1;
In the “timer” function, the timer rate is prescaled by 12 clock cycle to increment register value. In other words, it is to
count the standard C51 machine cycle. AUXR2.T0X12, AUXR2.T1X12, T2MOD.T2X12 and T3MOD.T3X12 are the
function for Timer 0/1/2/3 to set the timer rate on every clock cycle. It behaves X12 times speed than standard C51
timer function.
In the “counter” function, the register is incremented in response to a 1-to-0 transition at its corresponding external
input pin, T0, T1, T2 or T3. In this function, the external input is sampled by every timer rate cycle. When the
samples show a high in one cycle and a low in the next cycle, the count is incremented. The new count value
appears in the register at the end of the cycle following the one in which the transition was detected.
The timer register is configured as a 13-bit register. As the count rolls over from all 1s to all 0s, it sets the timer
interrupt flag TFx. The counted input is enabled to the timer when TRx = 1 and either GATE=0 or nINTx = 1. Mode 0
operation is the same for Timer0 and Timer1.
SYSCLK /12 0
0 Overflow
SYSCLK 1 TLx[4:0] THx[7:0] TFx Interrupt
Tx Pin 1
AUXR2.TxX12
C/T
TRx
GATE x = 0 or 1
nINTx Pin 0
AUXR0.INTxH
Mode1 is the same as Mode0, except that the timer register is being run with all 16 bits.
SYSCLK /12 0
0 Overflow
SYSCLK 1 TLx[7:0] THx[7:0] TFx Interrupt
Tx Pin 1
AUXR2.TxX12
C/T
TRx
GATE x = 0 or 1
nINTx Pin 0
AUXR0.INTxH
Mode 2 configures the timer register as an 8-bit counter(TLx) with automatic reload. Overflow from TLx not only set
TFx, but also reload TLx with the content of THx, which is determined by software. The reload leaves THx
unchanged. Mode 2 operation is the same for Timer0 and Timer1.
SYSCLK /12 0
0 Overflow
SYSCLK 1 TLx[7:0] TFx Interrupt
Tx Pin 1
AUXR2.TxX12
C/T
Reload
TRx
GATE x = 0 or 1
THx[7:0]
nINTx Pin 0
AUXR0.INTxH
Timer1 in Mode3 simply holds its count, the effect is the same as setting TR1 = 1. Timer0 in Mode 3 enables TL0
and TH0 as two separate 8-bit counters. TL0 uses the Timer0 control bits such like C/T, GATE, TR0, INT0 and TF0.
TH0 is locked into a timer function (can not be external event counter) and take over the use of TR1, TF1 from
Timer1. TH0 now controls the Timer1 interrupt.
SYSCLK /12 0
0 Overflow
SYSCLK 1 TL0[7:0] TF0 Interrupt
T0 Pin 1
AUXR2.T0X12
C/T
TR0
GATE
nINT0 Pin 0
AUXR0.INT0H
AUXR2.T0X12 TR1
Timer 0 and Timer 1 have a Clock-Out Mode (while C/Tx=0 & TxCKOE=1). In this mode, Timer 0 or Timer 1
operates as 8-bit auto-reload timer for a programmable clock generator with 50% duty-cycle. The generated clocks
come out on P3.4 (T0CKO) and P3.5 (T1CKO) individually. The input clock (SYSCLK/12 or SYSCLK) increments
the 8-bit timer (TL0 or TL1). The timer repeatedly counts to overflow from a loaded value. Once overflows occur, the
contents of (TH0 and TH1) are loaded into (TL0, TL1) for the consecutive counting. The following formula gives the
clock-out frequency:
Note:
(1) Timer 0/1 overflow flag, TF0/1, will be set when Timer 0/1 overflows but not generate interrupt.
(2) For SYSCLK=12MHz & TxX12=0, Timer 0/1 has a programmable output frequency range from 1.95KHz to
500KHz.
(3) For SYSCLK=12MHz & TxX12=1, Timer 0/k1 has a programmable output frequency range from 23.43KHz to
6MHz.
AUXR2.TxX12
C/T=0 Reload
TRx
GATE=0
THx[7:0]
nINTx Pin 0
x = 0 or 1
1 AUXR2.TxCKOE = 1
AUXR0.INTxH
• Select T0X12/T1X12 bit in AUXR2 register to decide the Timer 0/1 clock source.
• Set T0CKOE/T1CKOE bit in AUXR2 register.
• Clear C/T bit in TMOD register.
• Determine the 8-bit reload value from the formula and enter it in the TH0/TH1 register.
• Enter the same reload value as the initial value in the TL0/TL1 registers.
• Set TR0/TR1 bit in TCON register to start the Timer 0/1.
In the Clock-Out mode, Timer 0/1 rollovers will not generate an interrupt. This is similar to when Timer 1 is used as a
baud-rate generator. It is possible to use Timer 1 as a baud rate generator and a clock generator simultaneously.
Note, however, that the baud-rate and the clock-out frequency depend on the same overflow rate of Timer 1.
14.2. Timer 2
Timer 2 is a 16-bit Timer/Counter which can operate either as a timer or an event counter, as selected by C/T2 in
T2CON register. Timer 2 has four operating modes: Capture, Auto-Reload (up or down counting), Baud Rate
Generator and Programmable Clock-Out, which are selected by bits in the T2CON and T2MOD registers.
In the capture mode there are two options selected by bit EXEN2 in T2CON. If EXEN2=0, Timer 2 is a 16-bit timer or
counter which, upon overflow, sets bit TF2 (Timer 2 overflow flag). This bit can then be used to generate an interrupt
(by enabling the Timer 2 interrupt bit in the IE register). If EXEN2=1, Timer 2 still does the above, but with the added
feature that a 1-to-0 transition at external input T2EX causes the current value in the Timer 2 registers, TH2 and TL2,
to be captured into registers RCAP2H and RCAP2L, respectively. In addition, the transition at T2EX causes bit
EXF2 in T2CON to be set, and the EXF2 bit (like TF2) can generate an interrupt which vectors to the same location
as Timer 2 overflow interrupt. The capture mode is illustrated in Figure 14–6.
T2MOD.T2X12
C/T2 Capture
TR2
Timer2 Interrupt
RCAP2L RCAP2H
Transition
Detection
T2EX Pin 0
EXF2
1
T2MOD.T2EXH
EXEN2
Figure 14–7 shows DCEN=0, which enables Timer 2 to count up automatically. In this mode there are two options
selected by bit EXEN2 in T2CON register. If EXEN2=0, then Timer 2 counts up to 0FFFFH and sets the TF2
(Overflow Flag) bit upon overflow. This causes the Timer 2 registers to be reloaded with the 16-bit value in RCAP2L
and RCAP2H. The values in RCAP2L and RCAP2H are preset by firmware. If EXEN2=1, then a 16-bit reload can be
triggered either by an overflow or by a 1-to-0 transition at input T2EX. This transition also sets the EXF2 bit. The
Timer 2 interrupt, if enabled, can be generated when either TF2 or EXF2 are 1.
T2MOD.T2X12
C/T2
TR2 Reload
Timer2 Interrupt
RCAP2L RCAP2H
Transition
Detection
T2EX Pin 0
EXF2
1
T2MOD.T2EXH
EXEN2
Figure 14–8 shows DCEN=1, which enables Timer 2 to count up or down. This mode allows pin T2EX to control the
counting direction. When a logic 1 is applied at pin T2EX, Timer 2 will count up. Timer 2 will overflow at 0FFFFH and
set the TF2 flag, which can then generate an interrupt if the interrupt is enabled. This overflow also causes the 16-bit
value in RCAP2L and RCAP2H to be reloaded into the timer registers TL2 and TH2. A logic 0 applied to pin T2EX
causes Timer 2 to count down. The timer will underflow when TL2 and TH2 become equal to the value stored in
RCAP2L and RCAP2H. This underflow sets the TF2 flag and causes 0FFFFH to be reloaded into the timer registers
TL2 and TH2.
The external flag EXF2 toggles when Timer 2 underflows or overflows. This EXF2 bit can be used as a 17th bit of
resolution if needed. The EXF2 flag does not generate an interrupt in this mode.
SYSCLK /12 0
Timer2 Interrupt
0 TL2 TH2
SYSCLK 1 TF2
T2 Pin 1 (8 Bits) (8 Bits)
T2MOD.T2X12
C/T2
TR2 Count Direction
1 = UP
0 = DOWN
RCAP2L RCAP2H
T2EX Pin 0
T2MOD.T2EXH
Bits TCLK and/or RCLK in T2CON register allow the serial port transmit and receive baud rates to be derived from
either Timer 1 or Timer 2. When TCLK=0, Timer 1 is used as the serial port transmit baud rate generator. When
TCLK= 1, Timer 2 is used as the serial port transmit baud rate generator. RCLK has the same effect for the serial
port receive baud rate. With these two bits, the serial port can have different receive and transmit baud rates – one
generated by Timer 1, the other by Timer 2.
Figure 14–9 shows the Timer 2 in baud rate generation mode to generate RX Clock and TX Clock into UART engine
(See Figure 15–6.). The baud rate generation mode is like the auto-reload mode, in that a rollover in TH2 causes the
Timer 2 registers to be reloaded with the 16-bit value in registers RCAP2H and RCAP2L, which are preset by
firmware.
The Timer 2 as a baud rate generator mode is valid only if RCLK and/or TCLK=1 in T2CON register. Note that a
rollover in TH2 does not set TF2, and will not generate an interrupt. Thus, the Timer 2 interrupt does not have to be
disabled when Timer 2 is in the baud rate generator mode. Also if the EXEN2 (T2 external enable bit) is set, a 1-to-0
transition in T2EX (Timer/counter 2 trigger input) will set EXF2 (T2 external flag) but will not cause a reload from
(RCAP2H, RCAP2L) to (TH2,TL2). Therefore when Timer 2 is in use as a baud rate generator, T2EX can be used as
an additional external interrupt, if needed.
When Timer 2 is in the baud rate generator mode, one should not try to read or write TH2 and TL2. As a baud rate
generator, Timer 2 is incremented at 1/2 the system clock or asynchronously from pin T2; under these conditions, a
read or write of TH2 or TL2 may not be accurate. The RCAP2 registers may be read, but should not be written to,
because a write might overlap a reload and cause write and/or reload errors. The timer should be turned off (clear
TR2) before accessing the Timer 2 or RCAP2 registers.
Note:
Refer to Section “15.7.3 Baud Rate in Mode 1 & 3” to get baud rate setting value when using Timer 2 as the
baud rate generator.
Timer 1 Overflow 1
SMOD1
SYSCLK /2 0 0
0 TL2 TH2 Overflow RX Clock
SYSCLK 1 1
T2 Pin 1 (8 Bits) (8 Bits)
T2MOD.T2X12 RCLK
C/T2
TR2 Reload
0
TX Clock
1
T2MOD.T2EXH
EXEN2
Timer 2 has a Clock-Out Mode (while CP/RL2=0 & T2OE=1). In this mode, Timer 2 operates as a programmable
clock generator with 50% duty-cycle. The generated clocks come out on P1.0. The input clock (SYSCLK/2 or
SYSCLK) increments the 16-bit timer (TH2, TL2). The timer repeatedly counts to overflow from a loaded value.
Once overflows occur, the contents of (RCAP2H, RCAP2L) are loaded into (TH2, TL2) for the consecutive counting.
The following formula gives the clock-out frequency:
Note:
(1) Timer 2 overflow flag, TF2, will be set when Timer 2 overflows but not generate interrupt.
(2) For SYSCLK=12MHz & T2X12=0, Timer 2 has a programmable output frequency range from 45.7Hz to 3MHz.
(3) For SYSCLK=12MHz & T2X12=1, Timer 2 has a programmable output frequency range from 91.5Hz to 6MHz.
T2X12
( T2MOD.4 ) C/T2 = 0 TR2 Reload T2OE
( T2CON.1 ) ( T2CON.2 ) ( T2MOD.1 )
• Select T2X12 bit in T2MOD register to decide the Timer 2 clock source.
• Set T2OE bit in T2MOD register.
• Clear C/T2 bit in T2CON register.
• Determine the 16-bit reload value from the formula and enter it in the RCAP2H and RCAP2L registers.
• Enter the same reload value as the initial value in the TH2 and TL2 registers.
• Set TR2 bit in T2CON register to start the Timer 2.
Bit 7~6: Reserved. Software must write “0” on these bits when T2MOD is written.
Bit 3~2: Reserved. Software must write “0” on these bits when T2MOD is written.
When the DCEN2 is cleared, which makes the function of Timer 2 as the same as the standard 8052 (always counts
up). When DCEN2 is set, Timer 2 can count up or count down according to the logic level of the T2EX pin (P1.1).
Table 14–1 shows the operation modes of Timer 2.
14.3. Timer 3
Timer 3 is a 16-bit Timer/Counter which can operate either as a timer or an event counter, as selected by C/T3 in
T3CON register. Timer 3 may operate in 16-bit auto-reload mode, (split) 8-bit auto-reload mode and Programmable
Clock-Out, which are selected by bits in the T3CON and T3MOD registers.
In default, Timer 3 operates as a 16-bit timer/counter with auto-reload. As the 16-bit timer register increments and
overflows from 0xFFFF to 0x0000, the 16-bit value in the Timer 3 reload registers (RCAP3H and RCAP3L) is loaded
into the Timer 3 register as shown in Figure 14–11, and the Timer 3 High Byte Overflow Flag (TF3, T3CON.7) is set.
If Timer 3 interrupt is enabled, an interrupt will be generated on each Timer 3 overflow. C/T3=1 sets the Timer 3 as
counter mode which clock source comes from T3 pin. In timer mode, T3X12 selects SYSCLK/12 or SYSCLK for
Timer 3 clock source. TR3 control the timer running or stopped.
A port change detection on T3EX is built in Timer 3 module. If EXEN3=1 and T3EXH=0, EXF3 is set by a 1-to-0
transition at input T3EX. If EXEN3=1 and T3EXH=1, EXF3 is set by a 0-to-1 transition at input T3EX. If Timer 3
interrupt is enabled, and interrupt will be generate on each time EXF3 is set. When EXEN3 is enabled, software
must check the TF3 and EXF3 flags to determine the source of the Timer 3 interrupt. The TF3 and EXF3 interrupt
flags are not cleared by hardware and must be manually cleared by software.
In power-down mode, EXF3 is forced to level-sensitive operation and has the capability to wake up CPU if Timer 3
interrupt is enabled. This function provides an additional wake-up CPU path like external interrupt inputs. Detection
level of EXF3 is decided by T3EXH. If T3EXH=1, detecting T3EX high level sets EXF3 in power down mode.
Otherwise, detecting T3EX low sets EXF3.
RCAP3L RCAP3H
Transition
Detection
T3EX Pin 0
EXF3
1
( T3CON.6 )
T3EXH
( T3MOD.5 ) EXEN3
( T3CON.3 )
When T3SPL=1, Timer 3 is separated to two 8-bit timers (TH3 and TL3). TH3 operates the same function with 16-bit
Timer 3 function except the timer length is 8-bit. TH3 still keeps the counter function by C/T3=1 and TF3 is also set
by TH3 overflow (T3OVF). The TH3 overflow continues to provide the trigger to peripheral, such as ADC. But the
peripheral clock selection may come from TL3 overflow (TL3OVF). Both 8-bit timers operate in auto-reload mode as
shown in Figure 14–12. RCAP3H holds the reload value for TH3; RCAP3L holds the reload value for TL3. The TR3
bit in T3CON handles the run control for TH3. TL3 is always running when configured for 8-bit Mode. T3X12 selects
SYSCLK/12 or SYSCLK for TH3 clock source. And TL3X12 selects SYSCLK/12 or SYSCLK for TL3 clock source.
The port change detecting function on T3EX and interrupt flag EXF3 function are independent for the timer mode
split or not. Software can fully control the detection level, interrupt enabled and flag handle. EXF3 also have the
wake-up capability when CPU is in power-down mode and EXEN3 is enabled.
RCAP3H
Transition
Detection
T3EX Pin 0
EXF3
1
( T3CON.6 )
T3EXH
( T3MOD.5 ) EXEN3
( T3CON.3 )
RCAP3L
Timer 3 has a Clock-Out Mode (while T3OE=1) in 16-bit timer. In this mode, Timer 3 operates as a programmable
clock generator with 50% duty-cycle. The generated clocks come out on P0.1. The input clock (SYSCLK/12 or
SYSCLK) increments the 16-bit timer (TH3, TL3). The timer repeatedly counts to overflow from a loaded value.
Once overflows occur, the contents of (RCAP3H, RCAP3L) are loaded into (TH3, TL3) for the consecutive counting.
Figure 14–13 shows the block diagram for the Timer 3 Clock Output mode. The following formula gives the clock-out
frequency:
Note:
(1) Timer 3 overflow flag, TF3, will be set when Timer 3 overflows but not generate interrupt.
(2) For SYSCLK=12MHz & T3X12=0, Timer 3 has a programmable output frequency range from 7.63Hz to 500KHz.
(3) For SYSCLK=12MHz & T3X12=1, Timer 3 has a programmable output frequency range from 91.5Hz to 6MHz.
T3X12
( T3MOD.4 ) C/T3 = 0 TR3 Reload T3OE
( T3CON.1 ) ( T3CON.2 ) ( T3MOD.1 )
• Select T3X12 bit in T3MOD register to decide the Timer 3 clock source.
• Clear C/T3 bit in T3CON register.
• Determine the 16-bit reload value from the formula and enter it in the RCAP3H and RCAP3L registers.
• Enter the same reload value as the initial value in the TH3 and TL3 registers.
• Set T3OE bit in T3MOD register.
• Set TR3 bit in T3CON register to start the Timer 3.
In the Clock-Out mode, Timer 3 rollovers will not generate an interrupt. However, that the clock-out frequency
depends on the overflow rate of Timer 3.
When Timer 3 is configured to split two 8-bit timers, the TL3 overflow triggers the Timer 3 clock output scheme. The
input clock (SYSCLK/12 or SYSCLK) increments the 8-bit timer (TL3). The timer repeatedly counts to overflow from
a loaded value. Once overflows occur, the content of RCAP3L is loaded in to TL3 for the consecutive counting.
Figure 14–14 shows the block diagram for the Timer 3 Clock Output mode. The following formula gives the clock-out
frequency.
Note:
(1) Timer 3 overflow flag, TF3, will not be set by TL3 overflow. TL3 overflow will not influence TF3.
(2) For SYSCLK=12MHz & TL3X12=0, TL3 has a programmable output frequency range from 1.95KHz to 500KHz.
(3) For SYSCLK=12MHz & TL3X12=1, TL3 has a programmable output frequency range from 23.43KHz to 6MHz.
Figure 14–14. Timer 3 Clock Output in Split two 8 bit Auto-Reload Mode (T3SPL=1)
Toggle PORTn for T3CKO
SYSCLK /12 0 TL3 Overflow
D Q
SYSCLK 1 (8 Bits)
TL3X12
( T3MOD.6 ) Reload T3OE
( T3MOD.1 )
• Select TL3X12 bit in T3MOD register to decide the TL3 clock source.
• Determine the 8-bit reload value from the formula and enter it in the RCAP3L registers.
• Enter the same reload value as the initial value in the TL3 registers.
• Set T3OE bit in T3MOD register.
• Set T3SPL bit in T3MOD register to select Timer 3 as split 8-bit mode and start the TL3 timer.
Bit 5~4: Reserved. Software must write “0” on these bits when T3CON is written.
Bit 0: Reserved. Software must write “0” on this bit when T3CON is written.
Bit 0: Reserved. Software must write “0” on this bit when T3MOD is written.
(1). Required Function: IDLE mode with T0 wake-up frequency 320Hz, SYSCLK = ILRCO
Assembly Code Example:
ORG 0000Bh
time0_isr:
to do…
RETI
ANL IFD,#~(HSE) ; Disable HSE when SYSCLK 6MHz for power saving
CALL _page_p_sfr_write ; Write data to PCON2
C Code Example:
void main(void)
{
IFADRL = CKCON2; // Index Page-P address to CKCON2
page_p_sfr_read(); // Read CKCON2 data.
IFD &= ~HSE; // Disable HSE when SYSCLK 6MHz for power saving
page_p_sfr_write(); // Write data to PCON2
CLR TR0 ;
MOV TH0,#0FFh ;
MOV TL0,#0FFh ;
C Code Example:
TR0 = 0;
MOV TH1,#0FFh ;
MOV TL1,#0FFh ;
C Code Example:
The serial port can operate in 4 modes: Mode 0 provides synchronous communication while Modes 1, 2, and 3
provide asynchronous communication. The asynchronous communication operates as a full-duplex Universal
Asynchronous Receiver and Transmitter (UART), which can transmit and receive simultaneously and at different
baud rates.
Mode 0: 8 data bits (LSB first) are transmitted or received through RXD0(P3.0). TXD0(P3.1) always outputs the shift
clock. The baud rate can be selected to 1/12 or 1/2 the system clock frequency by URM0X6 setting in S0CFG
register.
Mode 1: 10 bits are transmitted through TXD0 or received through RXD0. The frame data includes a start bit (0), 8
data bits (LSB first), and a stop bit (1), as shown in Figure 15–1. On receive, the stop bit would be loaded into RB80
in S0CON register. The baud rate is variable.
Start D0 D1 D2 D3 D4 D5 D6 D7 Stop
Mode 2: 11 bits are transmitted through TXD0 or received through RXD0. The frame data includes a start bit (0), 8
data bits (LSB first), a programmable 9th data bit, and a stop bit (1), as shown in Figure 15–2. On Transmit, the 9th
data bit comes from TB80 in S0CON register can be assigned the value of 0 or 1. On receive, the 9th data bit would
be loaded into RB80 in S0CON register, while the stop bit is ignored. The baud rate can be configured to 1/32 or
1/64 the system clock frequency.
Start D0 D1 D2 D3 D4 D5 D6 D7 D8 Stop
Mode 3: Mode 3 is the same as Mode 2 except the baud rate is variable.
In all four modes, transmission is initiated by any instruction that uses S0BUF as a destination register. In Mode 0,
reception is initiated by the condition RI0=0 and REN0=1. In the other modes, reception is initiated by the incoming
start bit with 1-to-0 transition if REN0=1.
In addition to the standard operation, the UART0 can perform framing error detection by looking for missing stop bits,
and automatic address recognition.
Serial data enters and exits through RXD0. TXD0 outputs the shift clock. 8 bits are transmitted/received: 8 data bits
(LSB first). The shift clock source can be selected to 1/12 or 1/2 the system clock frequency by URM0X6 setting in
S0CFG register. Figure 15–3 shows a simplified functional diagram of the serial port 0 in Mode 0.
Transmission is initiated by any instruction that uses S0BUF as a destination register. The “write to S0BUF” signal
triggers the UART0 engine to start the transmission. The data in the S0BUF would be shifted into the RXD0(P3.0)
pin by each raising edge shift clock on the TXD0(P3.1) pin. After eight raising edge of shift clocks passing, TI would
be asserted by hardware to indicate the end of transmission. Figure 15–4 shows the transmission waveform in Mode
0.
Reception is initiated by the condition REN0=1 and RI0=0. At the next instruction cycle, the Serial Port 0 Controller
writes the bits 11111110 to the receive shift register, and in the next clock phase activates Receive.
Receive enables Shift Clock which directly comes from RX Clock to the alternate output function of P3.1 pin. When
Receive is active, the contents on the RXD0(P3.0) pin would be sampled and shifted into shift register by falling
edge of shift clock. After eight falling edge of shift clock, RI0 would be asserted by hardware to indicate the end of
reception. Figure 15–5 shows the reception waveform in Mode 0.
RXD Alternated
TX Clock TXBUF for Input/output
Function
RX Clock
RXBUF
UART engine
TXD Alternated
Shift-clock for output
REN RXSTART Function
RI
TI
RI
Read
SBUF
Write to
SBUF
P3.1/TXD
P3.0/RXD D0 D1 D2 D3 D4 D5 D6 D7
TI
RI
P3.1/TXD
P3.0/RXD D0 D1 D2 D3 D4 D5 D6 D7
TI
RI
10 bits are transmitted through TXD0, or received through RXD0: a start bit (0), 8 data bits (LSB first), and a stop bit
(1). On receive, the stop bit goes into RB80 in S0CON. The baud rate is determined by the Timer 1 or Timer 2
overflow rate. Figure 15–1 shows the data frame in Mode 1 and Figure 15–6 shows a simplified functional diagram
of the serial port in Mode 1.
Transmission is initiated by any instruction that uses S0BUF as a destination register. The “write to S0BUF” signal
requests the UART0 engine to start the transmission. After receiving a transmission request, the UART0 engine
would start the transmission at the raising edge of TX Clock. The data in the S0BUF would be serial output on the
TXD0 pin with the data frame as shown in Figure 15–1 and data width depend on TX Clock. After the end of 8th data
transmission, TI0 would be asserted by hardware to indicate the end of data transmission.
Reception is initiated when Serial Port 0 Controller detected 1-to-0 transition at RXD0 sampled by RCK. The data on
the RXD0 pin would be sampled by Bit Detector in Serial Port 0 Controller. After the end of STOP-bit reception, RI0
would be asserted by hardware to indicate the end of data reception and load STOP-bit into RB80 in S0CON
register.
Write
SBUF
2 2
RI Serial Port
1 TX Clock Interrupt
16 UART engine TI
0
BTI
STOP-Bit ESF
1 RCK RX Clock 0
16 RB8
0 1
9th-Bit
SM1 SM0
Read
SBUF
11 bits are transmitted through TXD0, or received through RXD0: a start bit (0), 8 data bits (LSB first), a
programmable 9th data bit, and a stop bit (1). On transmit, the 9th data bit (TB80) can be assigned the value of 0 or
1. On receive, the 9th data bit goes into RB80 in S0CON. The baud rate is programmable to select one of 1/16, 1/32
or 1/64 the system clock frequency in Mode 2. Mode 3 may have a variable baud rate generated from Timer 1 or
Timer 2.
Figure 15–2 shows the data frame in Mode 2 and Mode 3. Figure 15–5 shows a functional diagram of the serial port
in Mode 2 and Mode 3. The receive portion is exactly the same as in Mode 1. The transmit portion differs from Mode
1 only in the 9th bit of the transmit shift register.
The “write to S0BUF” signal requests the Serial Port 0 Controller to load TB80 into the 9th bit position of the transmit
shit register and starts the transmission. After receiving a transmission request, the UART0 engine would start the
transmission at the raising edge of TX Clock. The data in the S0BUF would be serial output on the TXD0 pin with the
data frame as shown in Figure 15–2 and data width depend on TX Clock. After the end of 9th data transmission, TI0
would be asserted by hardware to indicate the end of data transmission.
Reception is initiated when the UART0 engine detected 1-to-0 transition at RXD0 sampled by RCK. The data on the
RXD0 pin would be sampled by Bit Detector in UART0 engine. After the end of 9th data bit reception, RI0 would be
asserted by hardware to indicate the end of data reception and load the 9th data bit into RB80 in S0CON register.
In all four modes, transmission is initiated by any instruction that use S0BUF as a destination register. Reception is
initiated in mode 0 by the condition RI0 = 0 and REN0 = 1. Reception is initiated in the other modes by the incoming
start bit with 1-to-0 transition if REN0=1.
When used for framing error detection, the UART0 looks for missing stop bits in the communication. A missing stop
bit will set the FE bit in the S0CON register. The FE bit shares the S0CON.7 bit with SM00 and the function of
S0CON.7 is determined by SMOD0 bit (PCON.6). If SMOD0 is set then S0CON.7 functions as FE. S0CON.7
functions as SM00 when SMOD0 is cleared. When S0CON.7 functions as FE, it can only be cleared by firmware.
Start D0 D1 D2 D3 D4 D5 D6 D7 D8 Stop
PCON.SMOD0
Modes 2 and 3 have a special provision for multiprocessor communications as shown in Figure 15–8. In these two
modes, 9 data bits are received. The 9th bit goes into RB80. Then comes a stop bit. The port can be programmed
such that when the stop bit is received, the serial port interrupt will be activated only if RB80=1. This feature is
enabled by setting bit SM20 (in S0CON register). A way to use this feature in multiprocessor systems is as follows:
When the master processor wants to transmit a block of data to one of several slaves, it first sends out an address
byte which identifies the target slave. An address byte differs from a data byte in that the 9th bit is 1 in an address
byte and 0 in a data byte. With SM20=1, no slave will be interrupted by a data byte. An address byte, however, will
interrupt all slaves, so that each slave can examine the received byte and check if it is being addressed. The
addressed slave will clear its SM20 bit and prepare to receive the data bytes that will be coming. The slaves that
weren’t being addressed leave their SM20 set and go on about their business, ignoring the coming data bytes.
SM20 has no effect in Mode 0, and in Mode 1 can be used to check the validity of the stop bit. In a Mode 1 reception,
if SM20=1, the receive interrupt will not be activated unless a valid stop bit is received.
Pull-up
R
Slave 3 Slave 2 Slave 1 Master
RX TX RX TX RX TX RX TX
Automatic Address Recognition is a feature which allows the UART0 to recognize certain addresses in the serial bit
stream by using hardware to make the comparisons. This feature saves a great deal of firmware overhead by
eliminating the need for the firmware to examine every serial address which passes by the serial port. This feature is
enabled by setting the SM20 bit in S0CON.
In the 9 bit UART modes, mode 2 and mode 3, the Receive Interrupt flag (RI0) will be automatically set when the
received byte contains either the “Given” address or the “Broadcast” address. The 9-bit mode requires that the 9th
information bit is a 1 to indicate that the received information is an address and not data. Automatic address
recognition is shown in Figure 15–9. The 8 bit mode is called Mode 1. In this mode the RI flag will be set if SM20 is
enabled and the information received has a valid stop bit following the 8 address bits and the information is either a
Given or Broadcast address. Mode 0 is the Shift Register mode and SM20 is ignored.
SADEN is used to define which bits in the SADDR are to be used and which bits are “don’t care”. The SADEN mask
can be logically ANDed with the SADDR to create the “Given” address which the master will use for addressing each
of the slaves. Use of the Given address allows multiple slaves to be recognized while excluding others.
The following examples will help to show the versatility of this scheme:
Slave 0 Slave 1
SADDR = 1100 0000 SADDR = 1100 0000
SADEN = 1111 1101 SADEN = 1111 1110
Given = 1100 00X0 Given = 1100 000X
In the above example SADDR is the same and the SADEN data is used to differentiate between the two slaves.
Slave 0 requires a 0 in bit 0 and it ignores bit 1. Slave 1 requires a 0 in bit 1 and bit 0 is ignored. A unique address for
Slave 0 would be 1100 0010 since slave 1 requires a 0 in bit 1. A unique address for slave 1 would be 1100 0001
since a 1 in bit 0 will exclude slave 0. Both slaves can be selected at the same time by an address which has bit 0 =
0 (for slave 0) and bit 1 = 0 (for slave 1). Thus, both could be addressed with 1100 0000.
In a more complex system the following could be used to select slaves 1 and 2 while excluding slave 0:
In the above example the differentiation among the 3 slaves is in the lower 3 address bits. Slave 0 requires that bit 0
= 0 and it can be uniquely addressed by 1110 0110. Slave 1 requires that bit 1 = 0 and it can be uniquely addressed
by 1110 0101. Slave 2 requires that bit 2 = 0 and its unique address is 1110 0011. To select Slaves 0 and 1 and
exclude Slave 2 use address 1110 0100, since it is necessary to make bit 2 = 1 to exclude slave 2.
The Broadcast Address for each slave is created by taking the logical OR of SADDR and SADEN. Zeros in this
result are treated as don’t-cares. In most cases, interpreting the don’t-cares as ones, the broadcast address will be
FF hexadecimal.
Upon reset SADDR (SFR address 0xA9) and SADEN (SFR address 0xB9) are loaded with 0s. This produces a
given address of all “don’t cares” as well as a Broadcast address of all “don’t cares”. This effectively disables the
Automatic Addressing mode and allows the micro-controller to use standard 80C51 type UART drivers which do not
make use of this feature.
Start D0 D1 D2 D3 D4 D5 D6 D7 D8 Stop
Note: (1) After address matching(addr_match=1), Clear SM20 to receive data bytes
(2) After all data bytes have been received, Set SM20 to wait for next address.
Bits AUXR2.T1X12, URM0X6 and SMOD2 in S0CFG register provide a new option for the baud rate setting, as
listed below.
Note:
If URM0X6=0, the baud rate formula is as same as standard 8051.
2SMOD1 X 2(SMOD2 X 2)
Mode 2 Baud Rate = X FSYSCLK
64
Note:
If SMOD2=0, the baud rate formula is as same as standard 8051. If SMOD2=1, there is an enhanced function
for baud rate setting. Table 15–1 defines the Baud Rate setting with SMOD2 factor in Mode 2 baud rate
generator.
Note:
If SMOD2=0, T1X12=0, the baud rate formula is as same as standard 8051. If SMOD2=1, there is an enhanced
function for baud rate setting. Table 15–2 defines the Baud Rate setting with SMOD2 factor in Timer 1 baud rate
generator.
When Timer 2 is used as the baud rate generator (either TCLK or RCLK in T2CON is ‘1’), the baud rate is as follows.
Note:
If SMOD2=0, the baud rate formula is as same as standard 8051. If SMOD2=1, there is an enhanced function
for baud rate setting. Table 15–11 defines the Baud Rate setting with SMOD2 factor in Timer 2 baud rate
generator.
Table 15-12 ~ Table 15-19 list various commonly used baud rates and how they can be obtained from Timer 2 in its
Baud-Rate Generator Mode.
The secondary UART (S1) in MG84FG516 has an independent baud-rate generator. S0 can set URTS (S0CFG.7)
to select the S1BRT as the timer source for UART Mode 1 and Mode 3. See Section “16.5 S1 Baud Rate Timer for
S0” on for details for the S0 baud rate select.
All the four operation modes of the serial port are the same as those of the standard 8051 except the baud rate
setting. Three registers, PCON, AUXR2 and S0CFG, are related to the baud rate setting:
Bit 7: FE, Framing Error bit. The SMOD0 bit must be set to enable access to the FE bit.
0: The FE bit is not cleared by valid frames but should be cleared by software.
1: This bit is set by the receiver when an invalid stop bit is detected.
Bit 7: Serial port 0 mode bit 0, (SMOD0 must = 0 to access bit SM00)
Bit 6: Serial port 0 mode bit 1.
SADDR register is combined with SADEN register to form Given/Broadcast Address for automatic address
recognition. In fact, SADEN functions as the “mask” register for SADDR register. The following is the example for it.
The Broadcast Address for each slave is created by taking the logical OR of SADDR and SADEN. Zero in this result
is considered as “don’t care”. Upon reset, SADDR and SADEN are loaded with all 0s. This produces a Given
Address of all “don’t care” and a Broadcast Address of all “don’t care”. This disables the automatic address detection
feature.
(1) The UART1 has no enhanced functions: Framing Error Detection and Auto Address Recognition.
(2) The UART1 use the dedicated Baud Rate Timer as its Baud Rate Generator (S1BRG).
(3) The UART1 uses port pin P1.3 (TXD1) and P1.2 (RXD1) for transmit and receive, respectively.
(4) The Baud Rate Generator provide the toggle source for S1CKO and peripheral clock.
(5) S1 + S1BRG can be configured to an 8-bit auto-reload timer with port change detection.
The UART1 and UART0 in MG84FG516 can operate simultaneously in identical or different modes and
communication speeds.
The MG84FG516 has an embedded Baud Rate Generator to generate the UART clock for serial port 1 operation in
mode 1 and mode 3. This baud rate generator can also provide the time base for serial port 0 by software configured.
There is an addition clock output, S1CKO, from the S1BRC overflow rate by 2 (S1TOF/2). S1TOF also supplies the
toggle source for ADC, SPI, TWSI clock input.
The configuration of the Serial Port 1 Baud Rate Generator is shown in Figure 16–1.
UART1 (S1)
S1TX12 Interrupt
( S1CFG.2 ) S1TR Reload RI1
( S1CFG.4 ) RX Clock
S1BRT
(8 Bits)
S1BRC Overflow (S1TOF)
1. to S1CKO
2. to Peripheral Clock
FSYSCLK
S1 Mode 0 Baud Rate =
12
2S1MOD1
S1 Mode 2 Baud Rate = X FSYSCLK
64
2S1MOD1 FSYSCLK
S1 Mode 1, 3 Baud Rate = X ; S1TX12=0
32 12 x (256 – S1BRT)
2S1MOD1 FSYSCLK
or = X ; S1TX12=1
32 1 x (256 – S1BRT)
Table 16-1 ~ Table 16-4 list various commonly used baud rates and how they can be obtained from S1BRG, serial
port 1 baud rate generator.
If the UART1 is not necessary or pending by software, setting S1TME=1 in the MG84FG516 provides the pure timer
operating mode on S1 Baud Rate Generator (S1BRG). This timer operates as an 8-bit auto-reload timer and
provides the overflow flag which is set on the TI1 of S1CON.1. The RI1 of S1CON.0 serves the port change detector
on RXD1 port pin. Both of TI1 and RI1 in this mode keep the interrupt capability on UART1 interrupt resource and
have the individual interrupt enabled control (TB81 & REN1). RB81 selects the RI1 detection level on RXD1 port
input. If RB81=0, RI1 will be set by REN1=1 and RXD1 pin falling edge detecting. Otherwise, RI1 will detect the
rising edge on RXD1 port pin. In MCU power-down mode, the RI1 is forced to level-sensitive operation and has the
capability to wake up CPU if UART1 interrupt is enabled.
This timer provides additional clock input from USB engine detecting host SOF token, normally, the frequency of
SOF is around 1ms. The clock source function for ADC, SPI, TWSI or output on port pin is also valid in this mode.
S1CKOE=1 enables the S1CKO output on port pin and masks the RI1 interrupt.
The configuration of the Pure Timer mode of S1BRG is shown in Figure 16–2.
SM21 S1BRT
( S1CON.5 )
(8 Bits)
S1BRC Overflow (S1TOF)
1. to S1CKO
2. to Peripheral Clock
Transition
Detection
RXD1 Pin 0
RI1
1
( S1CON.0 )
RB81
( S1CON.2 ) REN1
( S1CON.4 )
When S1BRC overflows, the overflow flag, S1TOF, provides the toggle source for S1CKO and peripheral clock. The
input clock (SYSCLK/12 or SYSCLK) increments the 8-bit timer (S1BRC). The timer repeatedly counts to overflow
from a loaded value. Once overflows occur, the content of S1BRT is loaded in to S1BRC for the consecutive
counting. Figure 16–3 shows the block diagram for the Clock Output mode of S1 Baud Rate Generator. The
following formula gives the clock-out frequency.
Note:
(1) For SYSCLK=12MHz & S1TX12=0, S1BRG has a programmable output frequency range from 1.95KHz to
500KHz.
(2) For SYSCLK=12MHz & S1TX12=1, S1BRG has a programmable output frequency range from 23.43KHz to
6MHz.
S1TX12
( S1CFG.2 )
S1TR Reload S1CKOE
( S1CFG.4 ) ( S1CFG.1 )
USB SOF
SM21
(S1CON.5) S1BRT S1BRC Overflow (S1TOF)
(8 Bits)
1. to Peripheral Clock
• Select S1CFG.S1TX12 bit and S1CON.SM21 bit to decide the S1BRG clock source.
• Determine the 8-bit reload value from the formula and enter it in the S1BRT registers.
• Set S1CKOE bit in S1CFG register.
• Set S1TR to start the S1 BRC timer.
In the Mode 1 and Mode 3 operation of the UART0, the software can select Timer 1 as the Baud Rate Generator by
clearing bits TCLK and RCLK in T2CON register. At this time, if URTS bit (in S0CFG register) is set, then Timer 1
overflow signal will be replaced by the overflow signal of the UART1 Baud Rate Timer. In other words, the user can
adopt UART1 Baud Rate Timer as the Baud Rate Generator for Mode 1 or Mode 3 of the UART0 as long as
RCLK=0, TCLK=0 and URTS=1. In this condition, Timer 1 is free for other application. Of course, if UART1 (Mode 1
or Mode 3) is also operated at this time, these two UARTs will have the same baud rates.
0
RX Clock
Timer 2 Overflow 1
RCLK
( T2CON.5 )
The following special function registers are related to the operation of the UART1:
Bit 7~0: It is used as the reload value register for baud rate timer generator that works in a similar manner as Timer
1.
Bit 7~0: It is used as the reload value register for baud rate timer generator that works in a similar manner as Timer 1.
This register can be always read/written by software. If S1CFG.S1TME = 0, software writing S1BRT will store the
data content to S1BRT and S1BRC concurrently.
Bit 7~5: Reserved. Software must write “0” on these bits when S1CFG is written.
ORG 00023h
uart_ri_idle_isr:
JB RI,RI_ISR ;
JB TI,TI_ISR ;
RETI ;
RI_ISR:
; Process
CLR RI ;
RETI ;
TI_ISR:
; Process
CLR TI ;
RETI ;
main:
CLR TI ;
CLR RI ;
SETB SM1 ;
SETB REN ; 8bit Mode2, Receive Enable
C Code Example:
if(TI)
{
TI=0;
// to do ...
}
}
void main(void)
{
TI = RI = 0;
SM1 = REN = 1; // 8bit Mode2, Receive Enable
ES = 1; // Enable S0 interrupt
EA = 1; // Enable global interrupt
The PCA consists of a dedicated timer/counter which serves as the time base for an array of six compare/capture
modules. Figure 17–1 shows a block diagram of the PCA. Notice that the PCA timer and modules are all 16-bits. If
an external event is associated with a module, that function is shared with the corresponding Port 2 pin. If the
module is not using the port pin, the pin can still be used for standard I/O.
Each of the six modules can be programmed in any one of the following modes:
All of these modes will be discussed later in detail. However, let's first look at how to set up the PCA timer and
modules.
Module 0 P2.2/CEX0
Module 1 P2.3/CEX1
PCA Timer/Counter
Module 3 P2.5/CEX3
Module 4 P2.6/CEX4
Module 5 P2.7/CEX5
The timer/counter for the PCA is a free-running 16-bit timer consisting of registers CH and CL (the high and low
bytes of the count values), as shown in Figure 17–2. It is the common time base for all modules and its clock input
can be selected from the following source:
Special Function Register CMOD contains the Count Pulse Select bits (CPS1 and CPS0) to specify the PCA timer
input. This register also contains the ECF bit which enables an interrupt when the counter overflows. In addition, the
user has the option of turning off the PCA timer during Idle Mode by setting the Counter Idle bit (CIDL). This can
further reduce power consumption during Idle mode.
SYSCLK/2
CH CL
PCA Interrupt
8 bits 8 bits
Timer0 Overflow
Control 16-bits Up Counter Enable
IDLE
CIDL FEOV -- -- -- CPS1 CPS0 ECF CMOD
Bit 5~3: Reserved. Software must write “0” on these bits when CMOD is written.
The CCON register shown below contains the run control bit for the PCA and the flags for the PCA timer and each
module. To run the PCA the CR bit (CCON.6) must be set by software. The PCA is shut off by clearing this bit. The
CF bit (CCON.7) is set when the PCA counter overflows and an interrupt will be generated if the ECF bit in the
CMOD register is set. The CF bit can only be cleared by software. CCF0 to CCF5 are the interrupt flags for module 0
to module 5, respectively, and they are set by hardware when either a match or a capture occurs. These flags also
can only be cleared by software. The PCA interrupt system is shown Figure 17–3.
PCA Timer/Counter
Module 0
Module 1
EIE1.EPCA IE.EA
To Interrupt
Module 2
Priority Processing
Module 3
Module 4
Module 5
CCAPMn.0 (n=0~5)
ECCF0~ECCF5
Each of the six compare/capture modules has a mode register called CCAPMn (n = 0,1,2,3,4 or 5) to select which
function it will perform. Note the ECCFn bit which enables an interrupt to occur when a module's interrupt flag is set.
Bit 7: Reserved. Software must write “0” on this bit when the CCAPMn is written.
Note: The bits CAPNn (CCAPMn.4) and CAPPn (CCAPMn.5) determine the edge on which a capture input will be
active. If both bits are set, both edges will be enabled and a capture will occur for either transition.
Each module also has a pair of 8-bit compare/capture registers (CCAPnH, CCAPnL) associated with it. These
registers are used to store the time when a capture event occurred or when a compare event should occur.
When a module is used in the PWM mode, in addition to the above two registers, an extended register PCAPWMn is
used to improve the range of the duty cycle of the output. The improved range of the duty cycle starts from 0%, up to
100%, with a step of 1/256.
Bit 1: ECAPnH, Extended 9th bit (MSB bit), associated with CCAPnH to become a 9-bit register used in PWM mode.
Bit 0: ECAPnL, Extended 9th bit (MSB bit), associated with CCAPnL to become a 9-bit register used in PWM mode.
Table 17–1 shows the CCAPMn register settings for the various PCA functions.
To use one of the PCA modules in the capture mode, either one or both of the bits CAPN and CAPP for that module
must be set. The external CEX input for the module is sampled for a transition. When a valid transition occurs the
PCA hardware loads the value of the PCA counter registers (CH and CL) into the module’s capture registers
(CCAPnL and CCAPnH). If the CCFn and the ECCFn bits for the module are both set, an interrupt will be generated.
PCA Interrupt
(To CCFn)
PCA Timer/Counter
CH CL
Capture
CEXn
CCAPnH CCAPnL
CAPPn or CAPNn =1
The PCA modules can be used as software timers by setting both the ECOM and MAT bits in the module’s CCAPMn
register. The PCA timer will be compared to the module’s capture registers, and when a match occurs an interrupt
will occur if the CCFn and the ECCFn bits for the module are both set.
1 0
Enable Match
16-Bit Comparator
CH CL
PCA Timer/Counter
In this mode the CEX output associated with the PCA module will toggle each time a match occurs between the PCA
counter and the module’s capture registers. To activate this mode, the TOG, MAT and ECOM bits in the module’s
CCAPMn register must be set.
1 0
Enable Match
16-Bit Comparator
Toggle
CH CL CEXn
PCA Timer/Counter
All of the PCA modules can be used as PWM outputs. The frequency of the output depends on the clock source for
the PCA timer. All of the modules will have the same frequency of output because they all share the PCA timer.
th
The duty cycle of each module is determined by the module’s capture register CCAPnL and the extended 9 bit,
ECAPnL. When the 9-bit value of { 0, [CL] } is less than the 9-bit value of { ECAPnL, [CCAPnL] } the output will be
low, and if equal to or greater than the output will be high.
When CL overflows from 0xFF to 0x00, { ECAPnL, [CCAPnL] } is reloaded with the value of { ECAPnH, [CCAPnH] }.
This allows updating the PWM without glitches. The PWMn and ECOMn bits in the module’s CCAPMn register must
be set to enable the PWM mode.
Using the 9-bit comparison, the duty cycle of the output can be improved to really start from 0%, and up to 100%.
The formula for the duty cycle is:
Where, [CCAPnH] is the 8-bit value of the CCAPnH register, and ECAPnH (bit-1 in the PCAPWMn register) is 1-bit
value. So, { ECAPnH, [CCAPnH] } forms a 9-bit value for the 9-bit comparator.
For examples,
a. If ECAPnH=0 & CCAPnH=0x00 (i.e., 0x000), the duty cycle is 100%.
b. If ECAPnH=0 & CCAPnH=0x40 (i.e., 0x040) the duty cycle is 75%.
c. If ECAPnH=0 & CCAPnH=0xC0 (i.e., 0x0C0), the duty cycle is 25%.
d. If ECAPnH=1 & CCAPnH=0x00 (i.e., 0x100), the duty cycle is 0%.
9 Bits
ECAPnL CCAPnL
MATn
Match
Enable 9-Bit S Q 0
Comparator CEXn
1
R Q
9 Bits
(Fixed 0) CL CL PIVO
Overflow
PCA Timer/Counter
The PCA provides the variable PWM mode to enhance the control capability on PWM application. There are
additional 10/12/16 bits PWM can be assigned in each channel and each PWM channel with different resolution and
different phase delay can operate concurrently.
(To CCFn)
10/12/16 Bits
CCAPnH CCAPnL
MATn
Enable Match
10/12/16-Bit Comparator S Q 0
CEXn
Overflow 1
R Q
16 Bits
CH CL
PIVO
PCA Timer/Counter
In default PCA PWM mode, all PWM outputs are cleared on CL overflow (See Figure 17–7). All PWM outputs go to
low simultaneously and are set to high by the match event from individual CCAPnL setting and CL counter. This
mode PWM behaves a same phase PWM because the PWM outputs always start at the same time. The PCA
enhanced PWM mode provides the phase delay function on each PWM channel with different PWM resolution. The
following table indicates the counter value to clear PWM output if comparator result is matched. The set condition of
PWM outputs keeps the original matched event by {CCFnH, CCFnL} and {CH, CL}. So after setting the phase delay
parameter, software only take care the value of the PWM END count (PWM output SET) to implement the variable
phase delay PWM.
Bit 1: ECAPnH: Extended MSB bit, associated with CCAPnH to become a 9th-bit register used in 8-bit PWM mode.
th
As well as for 10/12/16 bit PWM, it will become a 11th/13th/17 bit register.
Bit 0: ECAPnL: Extended MSB bit, associated with CCAPnL to become a 9th-bit register used in 8-bit PWM mode.
th
As well as for 10/12/16 bit PWM, it will become a 11th/13th/17 bit register.
(1). Required Function: Set PWM2/PWM3 output with 25% & 75% duty cycle
Assembly Code Example:
PWM2_PWM3:
MOV CCON,#00H ; stop CR
MOV CMOD,#02H ; PCA clock source = system clock / 2
C Code Example:
void main(void)
{
// set PCA
CCON = 0x00; // disable PCA & clear CCF0, CCF1, CF flag
CMOD = 0x02; // PCA clock source = system clock / 2
while (1);
}
MISO
Receive Holding Auto-Load Input Shift
CPU Read SPDAT (P1.6)
Register (RHR) Register (ISR) I/O
Control
MOSI
(P1.5)
/4
/8 SPI Control nSS
/16 (P1.4)
SYSCLK
/32
/64
/128
S1TOF S1TOF/6
T3OF T3OF/6
or TL3OF
SSIG SPEN DORD MSTR CPOL CPHA SPR1 SPR0 SPCON
if T3SPL=1
The SPI interface has four pins: MISO (P1.6), MOSI (P1.5), SPICLK (P1.7) and /SS (P1.4):
• SPICLK, MOSI and MISO are typically tied together between two or more SPI devices. Data flows from master to
slave on the MOSI pin (Master Out / Slave In) and flows from slave to master on the MISO pin (Master In / Slave
Out). The SPICLK signal is output in the master mode and is input in the slave mode. If the SPI system is disabled,
i.e., SPEN (SPCTL.6) = 0, these pins function as normal I/O pins.
• /SS is the optional slave select pin. In a typical configuration, an SPI master asserts one of its port pins to select
one SPI device as the current slave. An SPI slave device uses its /SS pin to determine whether it is selected. The
/SS is ignored if any of the following conditions are true:
Note: See the AUXR1 in Section “4.3 Alternate Function Redirection”, for its alternate pin-out option.
Note that even if the SPI is configured as a master (MSTR=1), it can still be converted to a slave by driving the /SS
pin low (if SSIG=0). Should this happen, the SPIF bit (SPSTAT.7) will be set. (See Section “18.2.3 Mode Change on
nSS-pin”)
For the master: any port pin, including P1.4 (/SS), can be used to drive the /SS pin of the slave.
For the slave: SSIG is ‘0’, and /SS pin is used to determine whether it is selected.
SPICLK SPICLK
MISO MISO
Master MOSI MOSI Slave
Two devices are connected to each other and either device can be a master or a slave. When no SPI operation is
occurring, both can be configured as masters with MSTR=1, SSIG=0 and P1.4 (/SS) configured in
quasi-bidirectional mode. When any device initiates a transfer, it can configure P1.4 as an output and drive it low to
force a “mode change to slave” in the other device. (See Section “18.2.3 Mode Change on nSS-pin”)
Figure 18–3. SPI dual device configuration, where either can be a master or a slave
SPICLK SPICLK
MISO MISO
Master/ Slave/
Slave MOSI MOSI Master
nSS nSS
For the master: any port pin, including P1.4 (/SS), can be used to drive the /SS pins of the slaves. For all the slaves:
SSIG is ‘0’, and /SS pin are used to determine whether it is selected.
SPICLK SPICLK
MISO MISO
Master
SPICLK
MISO
MOSI Slave #2
Table 18–1 shows configuration for the master/slave modes as well as usages and directions for the modes.
Salve
1 0 0 0 output input input Selected as slave.
(selected)
Slave
1 0 1 0 Hi-Z input input Not selected.
(not selected)
Mode change to slave
Slave if /SS pin is driven low, and MSTR
1 0 0 10 (by mode output input input will be cleared to ‘0’ by H/W
change)
automatically.
When CPHA is 0, SSIG must be 0 and /SS pin must be negated and reasserted between each successive serial
byte transfer. Note the SPDAT register cannot be written while /SS pin is active (low), and the operation is undefined
if CPHA is 0 and SSIG is 1.
When CPHA is 1, SSIG may be 0 or 1. If SSIG=0, the /SS pin may remain active low between successive transfers
(can be tied low at all times). This format is sometimes preferred for use in systems having a single fixed master and
a single slave configuration.
In SPI, transfers are always initiated by the master. If the SPI is enabled (SPEN=1) and selected as master, writing
to the SPI data register (SPDAT) by the master starts the SPI clock generator and data transfer. The data will start to
appear on MOSI about one half SPI bit-time to one SPI bit-time after data is written to SPDAT.
Before starting the transfer, the master may select a slave by driving the /SS pin of the corresponding device low.
Data written to the SPDAT register of the master is shifted out of MOSI pin of the master to the MOSI pin of the slave.
And, at the same time the data in SPDAT register of the selected slave is shifted out on MISO pin to the MISO pin of
the master.
After shifting one byte, the SPI clock generator stops, setting the transfer completion flag (SPIF) and an interrupt will
be created if the SPI interrupt is enabled. The two shift registers in the master CPU and slave CPU can be
considered as one distributed 16-bit circular shift register. When data is shifted from the master to the slave, data is
also shifted in the opposite direction simultaneously. This means that during one shift cycle, data in the master and
the slave are interchanged.
If SPEN=1, SSIG=0, MSTR=1 and /SS pin=1, the SPI is enabled in master mode. In this case, another master can
drive this pin low to select this device as an SPI slave and start sending data to it. To avoid bus contention, the SPI
becomes a slave. As a result of the SPI becoming a slave, the MOSI and SPICLK pins are forced to be an input and
MISO becomes an output. The SPIF flag in SPSTAT is set, and if the SPI interrupt is enabled, an SPI interrupt will
occur. User software should always check the MSTR bit. If this bit is cleared by a slave select and the user wants to
continue to use the SPI as a master, the user must set the MSTR bit again, otherwise it will stay in slave mode.
To speed up the SPI transmit performance, a specially designed Transmit Holding Register (THR) improves the
latency time between byte to byte transmitting in CPU data moving. And a set THR-Full flag, THRF, indicates the
data in THR is valid and waiting for transmitting. If THR is empty (THRF=0), software writes one byte data to SPDAT
will store the data in THR and set the THRF flag. If Output Shift Register (OSR) is empty, hardware will move THR
data into OSR immediately and clear the THRF flag. In SPI mater mode, valid data in OSR triggers a SPI transmit. In
SPI slave mode, valid data in OSR is waiting for another SPI master to shift out the data. If THR is full (THRF=1),
software writes one byte data to SPDAT will set a write collision flag, WCOL (SPSTAT.6).
The SPI in MG84FG516 is double buffered data both in the transmit direction and in the receive direction. New data
for transmission can not be written to the THR until the THR is empty. The read-only flag, THRF, indicates the THR
is full or empty. The WCOL (SPSTAT.6) bit is set to indicate data collision when the data register is written during set
THRF. In this case, the SPDAT writing operation is ignored.
While write collision is detected for a master or a slave, it is uncommon for a master because the master has full
control of the transfer in progress. The slave, however, has no control over when the master will initiate a transfer
and therefore collision can occur.
The SPI clock rate selection (in master mode) uses the SPR1 and SPR0 bits in the SPCON register and SPR2 in the
SPSTAT register, as shown in Table 18–2.
Clock Phase Bit (CPHA) allows the user to set the edges for sampling and changing data. The Clock Polarity bit,
CPOL, allows the user to set the clock polarity. The following figures show the different settings of Clock Phase Bit,
CPHA.
The following special function registers are related to the SPI operation:
Bit 1~0: SPR1-SPR0, SPI clock rate select 0 & 1 (associated with SPR2, when in master mode)
SPI Clock SPI Clock Rate @
SPR2 SPR1 SPR0
Selection SYSCLK=12MHz
0 0 0 SYSCLK/4 3 MHz
0 0 1 SYSCLK/8 1.5 MHz
0 1 0 SYSCLK/16 750 KHz
0 1 1 SYSCLK/32 375 KHz
1 0 0 SYSCLK/64 187.5 KHz
1 0 1 SYSCLK/128 93.75 KHz
1 1 0 S1TOF/6 Variable
1 1 1 T3OF/6 Variable
Note:
1. SYSCLK is the system clock.
2. S1TOF is UART1 Baud-Rate Timer Overflow.
3. T3OF is Timer 3 Overflow.
4. In Timer 3 split mode, T3OF is replaced by TL3OF.
Bit 5: THRF, Transmit Holding Register (THR) Full flag. Read only.
0: Means the THR is “empty”. This bit is cleared by hardware when the THR is empty. That means the data in THR
is loaded (by H/W) into the Output Shift Register to be transmitted, and now the user can write the next data byte
to SPDAT for next transmission.
1: Means the THR is “full”. This bit is set by hardware just when SPDAT is written by software.
Bit 3~1: Reserved. Software must write “0” on these bits when SPSTAT is written.
Bit 0: SPR2, SPI clock rate select 2 (associated with SPR1 and SPR0).
SPDAT has two physical buffers for writing to and reading from during transmit and receive, respectively.
MOV SPCON,#( SPEN | SSIG | MSTR) ;enable SPI and set sampling data at rising edge,
;SPICLK is sysclk/ 4.
MOV P1M0,#0B0H ; set P14 to push-pull
CLR P14 ; enable slave device select
MOV SPDAT,#55H ; SPI send Addr=0x55 to slave
MOV a,#20H
check_THRF_0:
ANL a,SPSTAT
JNZ check_THRF_0
MOV SPDAT,#0FFH ; SPI send Data=0xff dummy data, and read back data
MOV a,#10H
check_SPIBSY_0:
ANL a,SPSTAT
JNZ check_SPIBSY_0
SETB P14 ; disable slave device select
MOV A,SPDAT
;SPDAT=read back Data
C Code Example:
#define nCS P14
void main(void)
{
Unsigned char SPI_read_Data;
SPCON = ( SPEN | SSIG | MSTR); //enable SPI and set sampling data at rising edge, SPICLK is sysclk / 4.
P1M0 = 0xB0; //set P14 to push-pull
nCS = 0; //enable slave device select
SPDAT = 0x55; // SPI send Addr=0x55 to slave;
while(SPSTAT & THRF);
SPDAT = 0xAA; //SPI send Data=0xAA to slave;
while(SPSTAT & SPIBSY);
nCS = 1; //disable slave device select
//;
nCS = 0; //enable slave device select
SPDAT = 0x55; // SPI send Addr=0x55 to slave;
while(SPSTAT & THRF);
SPDAT = 0xFF; // SPI send Data=0xff dummy data, and read back data
while(SPSTAT & SPIBSY);
nCS = 1; //disable slave device select
SPI_read_Data = SPDAT;
while (1);
}
TWI_SDA
TWI_SCL
The TWSI bus may operate as a master and/or slave, and may function on a bus with multiple masters. The CPU
interfaces to the TWSI through the following four special function registers: SICON configures the TWSI bus; SISTA
reports the status code of the TWSI bus; and SIDAT is the data register, used for both transmitting and receiving
TWSI data. SIADR is the slave address register. And, the TWSI hardware interfaces to the serial bus via two lines:
SDA (serial data line, P4.1) and SCL (serial clock line, P4.0).
Output Shift
CPU Write SIDAT
Register
TWI_SDA
(P4.1)
Input Shift I/O
CPU Read SIDAT TWSI Control
Register Control
TWI_SCL
(P4.0)
Slave Addr
CPU R/W SIADR
Register
/8
/16
/32
SYSCLK
/64
/128 SICON
/256 CR2 ENSI STA STO SI AA CR1 CR0
S1TOF S1TOF/6
There are four operating modes for the TWSI: 1) Master/Transmitter mode, 2) Master/Receiver mode, 3)
Slave/Transmitter mode and 4) Slave/Receiver mode. Bits STA, STO and AA in SICON decide the next action which
the TWSI hardware will take after SI is cleared by software. When the next action is completed, a new status code in
SISTA will be updated and SI will be set by hardware in the same time. Now, the interrupt service routine is entered
(if the TWSI interrupt is enabled), and the new status code can be used to determine which appropriate routine the
software is to branch to.
In the master transmitter mode, a number of data bytes are transmitted to a slave receiver. Before the master
transmitter mode can be entered, SICON must be initialized as follows:
SICON
7 6 5 4 3 2 1 0
CR2 ENSI STA STO SI AA CR1 CR0
Bit rate 1 0 0 0 x Bit rate
CR0, CR1, and CR2 define the serial bit rate. ENSI must be set to logic 1 to enable TWSI. If the AA bit is reset,
TWSI will not acknowledge its own slave address or the general call address in the event of another device
becoming master of the bus. In other words, if AA is reset, TWSI cannot enter a slave mode. STA, STO, and SI must
be reset.
The master transmitter mode may now be entered by software setting the STA bit. The TWSI logic will now test the
serial bus and generate a START condition as soon as the bus becomes free. When a START condition is
transmitted, the serial interrupt flag (SI) is set, and the status code in the status register (SISTA) will be 08H. This
status code must be used to vector to an interrupt service routine that loads SIDAT with the slave address and the
data direction bit (SLA+W). The SI bit in SICON must then be reset before the serial transfer can continue.
When the slave address and the direction bit have been transmitted and an acknowledgment bit has been received,
the serial interrupt flag (SI) is set again, and a number of status codes in SISTA are possible. There are 18H, 20H, or
38H for the master mode and also 68H, 78H, or B0H if the slave mode was enabled (AA=1). The appropriate action
to be taken for each of these status codes is detailed in the following operating flow chart. After a repeated START
condition (state 10H), TWSI may switch to the master receiver mode by loading SIDAT with SLA+R.
In the master receiver mode, a number of data bytes are received from a slave transmitter. SICON must be
initialized as in the master transmitter mode. When the start condition has been transmitted, the interrupt service
routine must load SIDAT with the 7-bit slave address and the data direction bit (SLA+R). The SI bit in SICON must
then be cleared before the serial transfer can continue.
When the slave address and the data direction bit have been transmitted and an acknowledgment bit has been
received, the serial interrupt flag (SI) is set again, and a number of status codes in SISTA are possible. They are
40H, 48H, or 38H for the master mode and also 68H, 78H, or B0H if the slave mode was enabled (AA=1). The
appropriate action to be taken for each of these status codes is detailed in the following operating flow chart. After a
repeated start condition (state 10H), TWSI may switch to the master transmitter mode by loading SIDAT with
SLA+W.
In the slave transmitter mode, a number of data bytes are transmitted to a master receiver. To initiate the slave
transmitter mode, SIADR and SICON must be loaded as follows:
SIADR
7 6 5 4 3 2 1 0
X X X X X X X GC
|<------------------------------ Own Slave Address ------------------------>|
The upper 7 bits are the address to which TWSI will respond when addressed by a master. If the LSB (GC) is set,
TWSI will respond to the general call address (00H); otherwise it ignores the general call address.
SICON
7 6 5 4 3 2 1 0
CR2 ENSI STA STO SI AA CR1 CR0
x 1 0 0 0 1 x x
CR0, CR1, and CR2 do not affect TWSI in the slave mode. ENSI must be set to “1” to enable TWSI. The AA bit must
be set to enable TWSI to acknowledge its own slave address or the general call address. STA, STO, and SI must be
cleared to “0”.
When SIADR and SICON have been initialized, TWSI waits until it is addressed by its own slave address followed
by the data direction bit which must be “1” (R) for TWSI to operate in the slave transmitter mode. After its own slave
address and the “R” bit have been received, the serial interrupt flag (SI) is set and a valid status code can be read
from SISTA. This status code is used to vector to an interrupt service routine, and the appropriate action to be taken
for each of these status codes is detailed in the following operating flow chart. The slave transmitter mode may also
be entered if arbitration is lost while TWSI is in the master mode (see state B0H).
If the AA bit is reset during a transfer, TWSI will transmit the last byte of the transfer and enter state C0H or C8H.
TWSI is switched to the not-addressed slave mode and will ignore the master receiver if it continues the transfer.
Thus the master receiver receives all 1s as serial data. While AA is reset, TWSI does not respond to its own slave
address or a general call address. However, the serial bus is still monitored, and address recognition may be
resumed at any time by setting AA. This means that the AA bit may be used to temporarily isolate TWSI from the
bus.
In the slave receiver mode, a number of data bytes are received from a master transmitter. Data transfer is initialized
as in the slave transmitter mode.
When SIADR and SICON have been initialized, TWSI waits until it is addressed by its own slave address followed
by the data direction bit which must be “0” (W) for TWSI to operate in the slave receiver mode. After its own slave
address and the W bit have been received, the serial interrupt flag (SI) is set and a valid status code can be read
from SISTA. This status code is used to vector to an interrupt service routine, and the appropriate action to be taken
for each of these status codes is detailed in the following operating flow chart. The slave receiver mode may also be
entered if arbitration is lost while TWSI is in the master mode (see status 68H and 78H).
If the AA bit is reset during a transfer, TWSI will return a not acknowledge (logic 1) to SDA after the next received
data byte. While AA is reset, TWSI does not respond to its own slave address or a general call address. However,
the serial bus is still monitored and address recognition may be resumed at any time by setting AA. This means that
the AA bit may be used to temporarily isolate from the bus.
There are two SISTA codes that do not correspond to a defined TWSI hardware state, as described below.
S1STA = F8H:
This status code indicates that no relevant information is available because the serial interrupt flag, SI, is not yet set.
This occurs between other states and when TWSI is not involved in a serial transfer.
S1STA = 00H:
This status code indicates that a bus error has occurred during an TWSI serial transfer. A bus error is caused when
a START or STOP condition occurs at an illegal position in the format frame. Examples of such illegal positions are
during the serial transfer of an address byte, a data byte, or an acknowledge bit. A bus error may also be caused
when external interference disturbs the internal TWSI signals. When a bus error occurs, SI is set. To recover from a
bus error, the STO flag must be set and SI must be cleared by software. This causes TWSI to enter the
“not-addressed” slave mode (a defined state) and to clear the STO flag (no other bits in SICON are affected). The
SDA and SCL lines are released (a STOP condition is not transmitted).
The TWSI is byte-oriented and interrupt based. Interrupts are issued after all bus events, like reception of a byte or
transmission of a START condition. Because the TWSI is interrupt-based, the application software is free to carry on
other operations during a TWSI byte transfer. Note that the TWSI interrupt enable bit ETWSI bit (AUXIE.6) together
with the EA bit allow the application to decide whether or not assertion of the SI Flag should generate an interrupt
request. When the SI flag is asserted, the TWSI has finished an operation and awaits application response. In this
case, the status register SISTA contains a status code indicating the current state of the TWSI bus. The application
software can then decide how the TWSI should behave in the next TWSI bus operation by properly programming the
STA, STO and AA bits (in SICON).
The following operating flow charts will instruct the user to use the TWSI using state-by-state operation. First, the
user should fill SIADR with its own Slave address (refer to the previous description about SIADR). To act as a master,
after initializing the SICON, the first step is to set “STA” bit to generate a START condition to the bus. To act as a
slave, after initializing the SICON, the TWSI waits until it is addressed. And then follow the operating flow chart for a
number a next actions by properly programming (STA,STO,SI,AA) in the SICON. Since the TWSI hardware will take
next action when SI is just cleared, it is recommended to program (STA,STO,SI,AA) by two steps, first STA, STO
and AA, then clear SI bit (may use instruction “CLR SI”) for safe operation. “don’t care”
(STA,STO,SI,AA)=(0,0,0,X) Setting for the next bus operation. "X" means "don't care".
SLA+W will be transmitted;
ACK bit will be received.
The expected next bus operation.
08H
A START has been
transmitted
(STA,STO,SI,AA)=(0,0,0,X)
SLA+W will be transmitted;
ACK bit will be received.
B From Master/Receiver
18H
SLA+W will be transmitted;
ACK bit will be received.
or
20H
SLA+W will be transmitted;
NOT ACK bit will be received.
38H
Arbitration lost in
SLA+W or Data bytes
(STA,STO,SI,AA)=(0,0,0,X)
SLA+R will be transmitted;
ACK will be received;
TWSI will be switched to
Master/Receiver mode
(STA,STO,SI,AA)=(0,0,0,X) (STA,STO,SI,AA)=(1,0,0,X)
The bus will be released; A START will be transmitted
Not addressed Slave mode when the bus becomes free.
will be entered.
A
To Master/Receiver
Send a START
Enter NAslave when bus becomes
free
08H
A START has been
transmitted.
(STA,STO,SI,AA)=(0,0,0,X)
SLA+R will be transmitted;
ACK will be received.
From Master/Transmitter
A
48H 40H
SLA+R has been transmitted; SLA+R has been transmitted;
NOT ACK has been received. ACK has been received.
(STA,STO,SI,AA)=(0,0,0,0) (STA,STO,SI,AA)=(0,0,0,1)
Data byte will be received; Data byte will be received;
NOT ACK will be returned. ACK will be returned.
58H 50H
Data byte has been received; Data byte has been received;
NOT ACK has been returned. ACK has been returned.
38H (STA,STO,SI,AA)=(0,0,0,X)
Arbitration lost in SLA+R SLA+W will be transmitted;
or NOT ACK bit. ACK will be received;
TWSI will be switched to MST/TRX mode.
(STA,STO,SI,AA)=(1,0,0,X)
A START will be transmitted
(STA,STO,SI,AA)=(0,0,0,X)
The bus will be released;
B
when the bus becomes free. Not addressed SLV mode will be entered. To Master/Transmitter
A8H
Own SLA+R has been received;
ACK has been returned.
or
B0H
Arbitration lost in SLA+R/W as master;
Own SLA+R has been received;
ACK has been returned.
(STA,STO,SI,AA)=(0,0,0,0) (STA,STO,SI,AA)=(0,0,0,1)
Last data byte will be transmitted; Data byte will be transmitted;
ACK will be received. ACK will be received.
(STA,STO,SI,AA)=(0,0,0,0) (STA,STO,SI,AA)=(0,0,0,1)
Last data byte will be transmitted; Data byte will be transmitted;
ACK will be received. ACK will be received.
Enter NAslave
`
Send a START
when bus becomes free
C
To Master Mode
60H
Own SLA+W has been received;
ACK has been returned.
or
68H
Arbitration lost in SLA+R/W as master;
Own SLA+W has been received;
ACK has been returned.
(STA,STO,SI,AA)=(0,0,0,0) (STA,STO,SI,AA)=(0,0,0,1)
Data byte will be received; Data byte will be received;
NOT ACK will be returned. ACK will be returned.
88H 80H
Data byte has been received; Data byte has been received;
NOT ACK has been returned. ACK has been returned.
Enter NAslave
`
Send a START
when bus becomes free
C
To Master Mode
70H
General Call address has been received;
ACK has been returned.
or
78H
Arbitration lost in SLA+R/W as master;
General Call address has been received;
ACK has been returned.
(STA,STO,SI,AA)=(0,0,0,0) (STA,STO,SI,AA)=(0,0,0,1)
Data byte will be received; Data byte will be received;
NOT ACK will be returned. ACK will be returned.
98H 90H
Previously addressed with General Call address; Previously addressed with General Call address;
Data byte has been received; Data byte has been received;
NOT ACK has been returned. ACK has been returned.
Enter NAslave
`
Send a START
when bus becomes free
C
To Master Mode
The CPU can read from and write to this register directly. SIADR is not affected by the TWSI hardware. The
contents of this register are irrelevant when TWSI is in a master mode. In the slave mode, the seven most significant
bits must be loaded with the microcontroller’s own slave address, and, if the least significant bit (GC) is set, the
general call address (00H) is recognized; otherwise it is ignored. The most significant bit corresponds to the first bit
received from the TWSI bus after a START condition.
This register contains a byte of serial data to be transmitted or a byte which has just been received. The CPU can
read from or write to this register directly while it is not in the process of shifting a byte. This occurs when TWSI is in
a defined state and the serial interrupt flag (SI) is set. Data in SIDAT remains stable as long as SI is set. While data
is being shifted out, data on the bus is simultaneously being shifted in; SIDAT always contains the last data byte
present on the bus. Thus, in the event of lost arbitration, the transition from master transmitter to slave receiver is
made with the correct data in SIDAT.
SIDAT and the ACK flag form a 9-bit shift register which shifts in or shifts out an 8-bit byte, followed by an
acknowledge bit. The ACK flag is controlled by the TWSI hardware and cannot be accessed by the CPU. Serial data
is shifted through the ACK flag into SIDAT on the rising edges of serial clock pulses on the SCL line. When a byte
has been shifted into SIDAT, the serial data is available in SIDAT, and the acknowledge bit is returned by the control
logic during the 9th clock pulse. Serial data is shifted out from SIDAT on the falling edges of clock pulses on the SCL
line.
When the CPU writes to SIDAT, the bit SD7 is the first bit to be transmitted to the SDA line. After nine serial clock
pulses, the eight bits in SIDAT will have been transmitted to the SDA line, and the acknowledge bit will be present in
the ACK flag. Note that the eight transmitted bits are shifted back into SIDAT.
The CPU can read from and write to this register directly. Two bits are affected by the TWSI hardware: the SI bit is
set when a serial interrupt is requested, and the STO bit is cleared when a STOP condition is present on the bus.
The STO bit is also cleared when ENSI="0".
Bit 7: CR2, TWSI Clock Rate select bit 2 (associated with CR1 and CR0).
If the AA flag is reset to “0”, a not acknowledge (high level to SDA) will be returned during the acknowledge clock
pulse on SCL when:
1) A data has been received while TWSI is in the master/receiver mode.
2) A data byte has been received while TWSI is in the addressed slave/receiver mode.
Bit 7, 1~0: CR2, CR1 and CR0, the Clock Rate select Bits
These three bits determine the serial clock frequency when TWSI is in a master mode. The clock rate is not
important when TWSI is in a slave mode because TWSI will automatically synchronize with any clock frequency,
which is from a master, up to 100KHz. The various serial clock rates are shown in Table 19–1.
SISTA is an 8-bit read-only register. The three least significant bits are always 0. The five most significant bits
contain the status code. There are a number of possible status codes. When SISTA contains F8H, no serial interrupt
is requested. All other SISTA values correspond to defined TWSI states. When each of these states is entered, a
status interrupt is requested (SI=1). A valid status code is present in SISTA when SI is set by hardware.
In addition, state 00H stands for a Bus Error. A Bus Error occurs when a START or STOP condition is present at an
illegal position, such as inside an address/data byte or just on an acknowledge bit.
SICON |= STA;
SICON &= ~SI;
while(( SICON & SI ) != SI );
SICON &= ~STA;
SICON |= STO;
SICON &= ~SI;
while(( SICON & STO ) == STO );
return usData;
}
Main:
MOV SICON,#( CR2 | ENSI | CR1 | CR0 ); ;enable TWSI and clock source Timer3 overflow
MOV T3MOD,#01h
CLR T3CON
MOV RCAP3H,#0FFh
MOV TH3,#0FFh
MOV RCAP3L,#0ECh ;0x10000 - 0xFFEC = 0x14h = 20d
MOV TL3,#0ECh ;20*6 = 120 * 83ns = 9.96us
SETB TR3
MOV SFRPI,#0h
ORL SICON,#STO;
ANL SICON,#~SI;
while(( SICON & STO ) == STO );
CALL delay_10ms;
P0 = I2C_Read(0xA0, 0x30);
JMP $;
}
C Code Example:
uCHAR I2C_Read(uCHAR Dev_Addr, uCHAR Reg_Addr)
{
uCHAR usData = 0;
SICON |= STA;
SICON &= ~SI;
while(( SICON & SI ) != SI );
SICON &= ~STA;
SICON |= STO;
SICON &= ~SI;
while(( SICON & STO ) == STO );
return usData;
}
SICON |= STO;
SICON &= ~SI;
while(( SICON & STO ) == STO );
}
void Timer3_Initial(void)
{
SFRPI = 1; //select SFR page index to "1"
T3MOD = 0x10;
T3CON = 0;
RCAP3H = 0xFF;
TH3 = 0xFF;
RCAP3L = 0xEC; //0x10000 - 0xFFEC = 0x14h = 20d
TL3 = 0xEC; //20*6 = 120 * 83ns = 9.96us
TR3 = 1;
SFRPI = 0;
}
void main()
{
SICON |= ( CR2 | ENSI | CR1 | CR0 ); //enable TWSI and clock source Timer3 overflow
Timer3_Initial(); //I2C freq is 100K @ MCU run 12MHz.
while(1);
}
There are three SFRs used for this function. The Keypad Interrupt Mask Register (KBMASK) is used to define which
input pins connected to Port 2 are enabled to trigger the interrupt. The Keypad Pattern Register (KBPATN) is used
to define a pattern that is compared to the value of keypad input. The Keypad Interrupt Flag (KBIF) in the Keypad
Interrupt Control Register (KBCON) is set by hardware when the condition is matched. An interrupt will be generated
if it has been enabled by setting the EKBI bit in EIE1 register and EA=1. The PATN_SEL bit in the Keypad Interrupt
Control Register (KBCON) is used to define “equal” or “not-equal” for the comparison. The keypad input can be
selected from the port pins on Port 0, Port 2, Port 5 and Port 6 by KBIPS1~0, AUXR1.7~6. The default keypad input
is indexed on Port 0.
In order to use the Keypad Interrupt as the “Keyboard” Interrupt, the user needs to set KBPATN=0xFF and
PATN_SEL=0 (not equal), then any key connected to keypad input which is enabled by KBMASK register will cause
the hardware to set the interrupt flag KBIF and generate an interrupt if it has been enabled. The interrupt may wake
up the CPU from Idle mode or Power-Down mode. This feature is particularly useful in handheld, battery powered
systems that need to carefully manage power consumption but also need to be convenient to use.
The following special function registers are related to the KBI operation:
Bit 7~2: Reserved. Software must write “0” on these bits when KBCON is written.
RETI
main:
MOV PUCON0, #0Fh ;enable P0, P1 internal pull high
ORL EIE1, #20h
SETB EA
delay_ms 5
CLR P1.0
ORL PCON0, #02h ;into power down
Loop:
JMP Loop
C Code Example:
void main(void)
{
PUCON0 = 0x0F; // Enable P0 ~P1 on-chip pull-up resistor
EIE1 |= EKB; // Enable KBI interrupt
EA = 1; // Enable global interrupt
Delay_5mS();
KBPATN=0xFF;
KBCON=0;
KBMASK=0xFF;
P10=0;
S1TOF S1TOF/2 ADCEN ADCMS AZEN ADCI ADCS CH2 CH1 CH0 ADCON0
T3OF T3OF/2
or TL3OF
if T3SPL=1
ADCKS2 ADCKS1 ADCKS0 ADRJ -- -- ADTM1 ADTM0 ADCFG0
ADC has a maximum conversion speed of 250 ksps. The ADC conversion clock is a divided version of the system
clock or the timer overflow rate of S1BRG and Timer 3, determined by the ADCKS2~0 bits in the ADCFG0 register.
The ADC conversion clock should be no more than 6 MHz.
After the conversion is complete (ADCI is high), the conversion result can be found in the ADC Result Registers
(ADCDH, ADCDL). For single ended conversion, the result is
VIN 4096
ADC Result =
VDD Voltage
The analog multiplexer (AMUX) selects the inputs to the ADC, allowing any of the pins on Port 1 to be measured in
single-ended mode. The ADC input channels are configured and selected by CHS.2~0 in the ADCON0 register as
described in Figure 21–1. The selected pin is measured with respect to GND. In Fully-differential mode, ADC will
support 4 channels differential input on Port 1 and output the result value with signed 2’s complement format.
Now, user can set the ADCS bit to start the A-to-D conversion. The conversion time is controlled by bits ADCKS2,
ADCKS1 and ADCKS0. Once the conversion is completed, the hardware will automatically clear the ADCS bit, set
the interrupt flag ADCI and load the 12 bits of conversion result into ADCH and ADCL (according to ADRJ bit)
simultaneously. If user sets the ADCS and selects the ADC trigger mode to timer0/3 over flow or free-run, then the
ADC will keep conversion continuously unless ADCEN is cleared or configure ADC to manual mode.
As described above, the interrupt flag ADCI, when set by hardware, shows a completed conversion. Thus two ways
may be used to check if the conversion is completed: (1) Always polling the interrupt flag ADCI by software; (2)
Enable the ADC interrupt by setting bits EADC (in EIE1 register) and EA (in IE register), and then the CPU will jump
into its Interrupt Service Routine when the conversion is completed. Regardless of (1) or (2), the ADCI flag should be
cleared by software before next conversion.
The user can select the appropriate conversion speed according to the frequency of the analog input signal. The
maximum input clock of the ADC is 6MHz and it operates a fixed conversion time with 24 ADC clocks. User can
configure the ADCKS2~0 in ADCFG0 to specify the conversion rate. For example, if SYSCLK =12MHz and the
ADCKS = SYSCLK/2 is selected, then the frequency of the analog input should be no more than 250KHz to maintain
the conversion accuracy. (Conversion rate = 12MHz/2/24 = 250KHz.)
The analog input pins used for the A/D converters also have its I/O port ‘s digital input and output function. In order
to give the proper analog performance, a pin that is being used with the ADC should have its digital output as
disabled. It is done by putting the port pin into the input-only mode. And when an analog signal is applied to the
ADCI7~0 pin and the digital input from this pin is not needed, software could set the corresponding pin to
analog-input-only in P1AIO to reduce power consumption in the digital input buffer. The port pin configuration for
analog input function is described in the Section “12.2.2 Port 1 Register”.
If the ADC is turned on in Idle mode and Power-Down mode, it will consume a little power. So, power consumption
can be reduced by turning off the ADC hardware (ADCEN=0) before entering Idle mode and Power-Down mode.
If software triggers the ADC operation in Idle mode, the ADC will finish the conversion and set the ADC interrupt flag,
ADCI. When the ADC interrupt enable (EADC, EIE1.1) is set, the ADC interrupt will wake up CPU from Idle mode.
Bit 2~0: CHS2 ~ CHS1, Input Channel Selection for ADC analog multiplexer.
In Single-ended mode:
CHS[2:0] Selected Channel
0 0 0 AIN0 (P1.0)
0 0 1 AIN1 (P1.1)
0 1 0 AIN2 (P1.2)
0 1 1 AIN3 (P1.3)
1 0 0 AIN4 (P1.4)
1 0 1 AIN5 (P1.5)
1 1 0 AIN6 (P1.6)
1 1 1 AIN7 (P1.7)
In Fully-differential mode:
CHS[2:1] Selected Channel
0 0 AIN0P (P1.0)
AIN0M (P1.1)
0 1 AIN1P (P1.2)
AIN1M (P1.3)
1 0 AIN2P (P1.4)
AIN2M (P1.5)
1 1 AIN3P (P1.6)
AIN3M (P1.7)
Note:
1. AIN0P, AIN1P, AIN2P and AIN3P are the positive inputs in fully-differential mode.
2. AIN0M, AIN1M, AIN2M and AIN3M are the negative inputs in fully-differential mode
If ADRJ = 0
ADCDH: ADC Date High Byte Register
SFR Page = 0~F
SFR Address = 0xC6 RESET = xxxx-xxxx
7 6 5 4 3 2 1 0
(B11) (B10) (B9) (B8) (B7) (B6) (B5) (B4)
R R R R R R R R
If ADRJ = 1
ADCDH
7 6 5 4 3 2 1 0
-- -- -- -- (B11) (B10) (B9) (B8)
R R R R R R R R
ADCDL
7 6 5 4 3 2 1 0
(B7) (B6) (B5) (B4) (B3) (B2) (B1) (B0)
R R R R R R R R
When in Single-ended Mode, conversion codes are represented as 12-bit unsigned integers. Inputs are measured
from ‘0’ to VREF x 4095/4096. Example codes are shown below for both right-justified and left-justified data. Unused
bits in the ADCDH and ADCDL registers are set to ‘0’.
When in Differential Mode, conversion codes are represented as 12-bit signed 2’s complement numbers. Inputs are
measured from –VREF to VREF x 2047/2048. Example codes are shown below for both right-justified and
left-justified data. Unused bits in the ADCDH and ADCDL registers are set to ‘0’.
Bit 3~2: Reserved. Software must write “0” on these bits when ADCFG0 is written.
// initial ADC
MOV ADCON0,#81H ; Enable ADCEN, Select single-end mode, Select AIN1 (P1.1)
MOV ADCFG0,#10H ; ADC clock = SYSCLK/2, ADRJ=1, Set ADCS to Start ADC conversion
ORL P1AIO,#02H ; configure P1.1 as Input-Only Mode
MOV ADCR0_H, ADCDH ;now, the 12-bit ADC result is in the ADCH and ADCL.
MOV ADCR0_L, ADCDL
// initial ADC
ADCON0 = 0x81; // Enable ADCEN, Select single-end mode, Select AIN1 (P1.1)
ADCFG0 = 0x10; // ADC clock = SYSCLK/2, ADRJ=1 (right-justified), Set ADCS to Start ADC conversion
P1AIO |= 0x02; // configure P1.1 as Input-Only Mode
Before using MG84FG516 USB function, we assume that user has a comprehensive understanding on USB
protocol and application. So, the following descriptions in this chapter would not focus on the detail of USB
specification. If user is interesting in USB specification, user can download the latest version of USB specification
document from the USB official website http://www.usb.org/home.
Megawin Inc. also offer a development kit which contain sample code, C language library and application note on
the website http://www.megawin.com.tw/ to help user to implement design more quickly and easily.
Note:
MG84FG516 can’t be used as a USB HOST device or USB OTG device.
22.1. Features
Compliant with USB specification v1.1/v2.0.
Supports USB full speed 12M bps serial data transmission
Supports USB suspend/resume and remote wake-up
1K bytes FIFO for USB endpoint-shared buffer
Support ping-pong mode (dual bank)
64 bytes FIFO for EP0 Control In/Out buffer
64 bytes FIFO for EP1 Interrupt/Bulk IN buffer
64 bytes FIFO for EP1 Interrupt/Bulk OUT buffer
64 bytes FIFO for EP2 Interrupt/Bulk IN buffer (Ping-Pong Pair 1)
64 bytes FIFO for EP3 Interrupt/Bulk IN buffer (Ping-Pong Pair 1)
64 bytes FIFO for EP2 Interrupt/Bulk OUT buffer (Ping-Pong Pair 2)
64 bytes FIFO for EP3 Interrupt/Bulk OUT buffer (Ping-Pong Pair 2)
96 bytes FIFO for EP4 Interrupt/Bulk/Isochronous IN buffer (Ping-Pong Pair 3)
96 bytes FIFO for EP5 Interrupt/Bulk/Isochronous IN buffer (Ping-Pong Pair 3)
192 bytes FIFO for EP4 Interrupt/Bulk/Isochronous OUT buffer (Ping-Pong Pair 4)
192 bytes FIFO for EP5 Interrupt/Bulk/Isochronous OUT buffer (Ping-Pong Pair 4)
1KB FIFO
There are 1K bytes FIFO for temporary USB data store unit accessed by USB core and a total of 11 endpoints are
available in MG84FG516 as shown in Figure 22–2. Endpoint 0 supports a bi-direction control transfer. Endpoint
1/2/3/4/5 supports Interrupt/Bulk IN/OUT transaction. Endpoint 4/5 also has additional function to support
Isochronous IN/OUT transaction. The maximum data packet size can be up to 64 bytes for endpoint 0/1/2/3 IN/OUT
function, 96 bytes for endpoint 4/5 IN function, and 192 bytes for endpoint 4/5 OUT function.
Figure 22–3 shows a USB Ping-Pong Pair FIFO configuration. In this configuration, two 64/96 bytes FIFO combined
to a ping-pong pair will be dedicated to endpoint 2/4 IN function to improve data throughput performance. Endpoint
3/5 has two 64/192 bytes FIFO for OUT function.
Note: the Ping-Pong FIFO function is under verifying.
INT INT
BULK
Endp 1 IN 64 Byte 64 Byte Endp 1 OUT BULK
INT INT
BULK
Endp 2 IN 64 Byte 64 Byte Endp 2 OUT BULK
INT INT
BULK
Endp 3 IN 64 Byte 64 Byte Endp 3 OUT BULK
INT INT
BULK Endp 4 IN 96 Byte 192 Byte Endp 4 OUT BULK
ISO ISO
INT INT
BULK Endp 5 IN 96 Byte 192 Byte Endp 5 OUT BULK
ISO ISO
INT INT
BULK
Endp 1 IN 64 Byte 64 Byte Endp 1 OUT BULK
64 Byte 64 Byte
INT INT
BULK
Endp 2 IN Endp 3 OUT BULK
64 Byte 64 Byte
If ENUSB is set to enable USB function, a 1K bytes FIFO would be a dedicated buffer for USB application. But in
most application case, it is not necessary and wasted that all 1K bytes FIFO would reserve for USB buffer, the
unused USB buffer can provide MCU additional data RAM which is access through USB re-directed mechanism.
Figure 22–4 shows the USB FIFO access mechanism to share the USB FIFO for MCU application.
To activate the USB operation, the user should enable Clock Multiplier (CKM) unit by setting ENCKM bit, wait 100us
for CKM ready to work, and then enable USB function by setting ENUSB bit. Clearing bit ‘ENUSB’ will deactivate the
USB operation and let the USB function enter its power-down mode. These relevant control bits are contained in the
CKCON0 register, as follows.
There are two ways that software can use to read/write USB SFR in MG84FG516. One is using MOVX instruction to
access USB SFR like MG84FL54B. Another is using USB read/write procedure by accessing USBADR/USBDAT in
CPU SFR.
Note:
If using MOVX instruction to access USB SFR, the system clock (SYSCLK) must not be more than
3MHz. When using USBADR/USBDAT access procedure, it has no the criteria on SYSCLK speed.
The special function registers which are dedicated to the USB operation are grouped in the external memory
address space and share the addresses 0xFF00 to 0xFFFF with the physical external data memory. The bit
‘EXTRAM’ (AUXR1.1) determines accessing to the USB SFRs or the physical external data memory. That is, before
accessing the USB SFRs, the user must clear EXTRAM bit and then use the instruction “MOVX @DPTR” to access
these SFRs.
Table 22–1 shows the USB SFR and their indirect address from 00H~FFH. USB SFR can be indirectly accessed
according to a 6-bit address hold in USBADR. Read/Write USBDAT will target the register indicated by USBADR.
Figure 22–5 shows the USB interrupt structure and there are 18 interrupt flags which are located in USB SFRs
shown in Section “22.8.2 USB Function SFR Bit Assignment”. The USB interrupt is generated on the combination of
USB event flags and USB endpoint flags contained in USB SFRs. The USB event flags include USB reset flag
(URST), USB resume flag (URSM) and USB suspend flag (USUS) can indicate that the upstream host has sent the
USB reset, resume or suspend event on USB bus to device. The USB endpoint flags, as UTXDx and URXDx
(x=0~5), show the USB data transmission or reception of respective endpoint had been done by USB transceiver.
The associated interrupt enable bits are located in UIE, UIE1 and IEN registers.
URSM
USUS
UIE
SOFIE
SOFIF
URXIE2
URXD2
UTXIE2 EF
UTXD2 UIFLG (IEN.1)
URXIE1
URXD1 USB interrupt to MCU
UTXIE1
UTXD1
URXIE0
URXD0
UTXIE0
UTXD0
UIE1
RXNAKE
RXNAK
TXNAKE
TXNAK
URXIE5 EF
URXD5 UIFLG1 (IEN.1)
UTXIE5
UTXD5
URXIE4
URXD4
UTXIE4
UTXD4
URXIE3
URXD3
UTXD3
All USB SFRs would be reset by the reset sources as listed in Section “10 System Reset” (SYSRST) and the most of
USB SFRs would be reset when device receives the USB reset event (USBRST) except DCON0, DCON1, IEN,
SIOCTL registers and CONEN bit in UPCON register.
FFF8H -- -- -- -- -- -- -- -- FFFFH
FFE8H -- -- -- -- -- -- -- -- FFEFH
FFD0H -- -- -- -- -- -- -- -- FFD7H
Bit 4~3: Reserved. Software must write “0” on these bits when DCON0 is written.
Bit 1~0: Reserved. Software must write “0” on these bits when DCON0 is written.
Bit 7~6: Reserved. Software must write “0” on these bits when DCON1 is written.
Bit 0: Reserved. Software must write “0” on this bit when DCON1 is written.
Bit 6: Reserved. Software must write “0” on this bit when UPCON is written.
Bit 4: Reserved. Software must write “0” on these bits when UPCON is written.
Bit 7~3: Reserved. Software must write “0” on these bits when IEN is written.
Bit 0: Reserved. Software must write “0” on this bit when IEN is written.
Bit 6: Reserved. Software must write “0” on this bit when UIE is written.
Bit 6: Reserved. Software must write “0” on this bit when UIFLG is written.
Bit7~3: Reserved. Software must write “0” on these bits when EPINDEX is written.
Bit1~0: Reserved. Software must write “0” on these bits when RXSTAT is written.
Bit6~5: Reserved. Software must write “0” on these bits when RXCON is written.
Bit3~0: Reserved. Software must write “0” on these bits when RXCON is written.
Bit6~4: Reserved. Software must write “0” on these bits when TXSTAT is written.
Bit6~5: Reserved. Software must write “0” on these bits when TXCON is written.
Bit2~0: Reserved. Software must write “0” on these bits when TXCON is written.
There are total 64K bytes of Flash Memory in MG84FG516 and Figure 23–1 shows the device flash configuration.
The flash can be partitioned into AP-memory, IAP-memory and ISP-memory. AP-memory is used to store user’s
application program; IAP-memory is used to store the non-volatile application data; and, ISP-memory is used to
store the boot loader program for In-System Programming. When MCU is running in ISP region, MCU could modify
the AP and IAP memory for software upgraded. If MCU is running in AP region, MCU could only modify the IAP
memory for storage data updated.
Note: 0x0000
(1) ISP Start Address:
0xF000 if ISP Size = 4KB
0xF200 if ISP Size = 3.5KB
0xF400 if ISP Size = 3KB
0xF600 if ISP Size = 2.5KB
0xF800 if ISP Size = 2KB
Application Code AP-memory
0xFA00 if ISP Size = 1.5KB
0xFC00 if ISP Size = 1KB
(2) IAP Size :
IAPLB = IAP Low Boundary (High-Byte address)
IAP Start Address = { IAPLB, 00H }
IAP Size = ISP Start Address – IAP Start Address Flash Memory
Set LAPLB = Change IAP Size Total: 64KB
(3) If ISP is enabled: IAP Low Boundary
IAPLB = 0xF0 (default)
IAP High Boundary = ISP Start Address – 1 IAP start 0xF000
IAP Low Boundary = ISP Start Address – IAP Size
IAP Data IAP-memory
(4) If ISP is disabled:
IAP High Boundary = 0xFFFF
IAP Low Boundary = 0xFFFF – IAP Size + 1 IAP High Boundary
ISP Start Address
ISP start 0xF600
(default)
ISP-memory
ISP Code
0xFFFF
Note:
In default, the samples that Megawin shipped had configured the flash memory for 2.5K ISP, 1.5K IAP and
Lock enabled. The 2.5K ISP region is inserted Megawin proprietary ISP code to perform
In-System-Programming through USB DFU operation. For more detail information for USB DFU, please refer
MG84FG516 Development Kit on Megawin web site.
MG84FG516 does not make use of idle-mode to perform ISP and IAP function. Instead, it freezes CPU running to
release the flash memory for ISP or IAP engine operating. Once ISP/IAP operation finished, CPU will be resumed
and advanced to the instruction which follows the previous instruction that invokes ISP/AP activity. During ISPIAP
operation, interrupt service is also blocked until ISP/IAP finished.
The flash memory of MG84FG516 can be both programming by the universal Writer/Programmer or the way of
In-System Programming (ISP). ISP makes it possible to update the user’s application program (in AP-memory) and
non-volatile application data (in IAP-memory) without removing the MCU chip from the actual end product. This
useful capability makes a wide range of field-update applications possible.
Note:
(1) Before using the ISP feature, the user should configure an ISP-memory space and pre-program the ISP
code into the ISP-memory by a universal Writer/Programmer or Megawin proprietary Writer/Programmer.
(2) ISP code in the ISP-memory can only program the AP-memory and IAP-memory.
The following special function registers are related to the access of ISP, IAP and Page-P SFR:
IFD is the data port register for ISP/IAP/Page-P operation. The data in IFD will be written into the desired address in
operating ISP/IAP/Page-P write and it is the data window of readout in operating ISP/IAP read.
IFADRH is the high-byte address port for all ISP/IAP modes. It is not defined in Page-P mode.
IFADRL is the low byte address port for all ISP/IAP/Page-P modes. In flash page erase operation, it is ignored.
Bit 7~4: Reserved. Software must write “0000_0” on these bits when IFMT is written.
IFMT is used to select the flash mode for performing numerous ISP/IAP function or to select page P SFR access.
Bit 7~0: The IAPLB determines the IAP-memory lower boundary. Since a Flash page has 512 bytes, the IAPLB must
be an even number.
To read IAPLB, MCU need to define the IMFT for mode selection on IAPLB Read and set ISPCR.ISPEN. And then
write 0x46h & 0xB9h sequentially into SCMD. The IAPLB content is available in IFD. If write IAPLB, MCU will put
new IAPLB setting value in IFD firstly. And then select IMFT, enable ISPCR.ISPEN and then set SCMD. The IAPLB
content has already finished the updated sequence.
The range of the IAP-memory is determined by IAPLB and the ISP start address as listed below.
IAP lower boundary = IAPLBx256, and
IAP higher boundary = ISP start address – 1.
For example, if IAPLB=0xE0 and ISP start address is 0xF000, then the IAP-memory range is located at 0xE000 ~
0xEFFF.
Additional attention point, the IAP low boundary address must not be higher than ISP start address.
SCMD is the command port for triggering ISP/IAP/Page-P activity. If SCMD is filled with sequential 0x46h, 0xB9h
and if ISPCR.7 = 1, ISP/IAP/Page-P activity will be triggered.
Bit 2~0: Reserved. Software must write “0” on these bits when ISPCR is written.
CPU in MG84FG516 can be vectored into ISP-memory space from two possible ways: When HWBS (in hardware
option bits) enabled, MG84FG516 will always boot from the ISP-memory on the specified “ISP start address” since
power-on. This way is named as hardware approach. Another way is software approach that allows MG84FG516
execution switched into ISP-memory from AP memory by software setting ISPCR.7 ~ ISPCR.5 to “111”
simultaneously.
Once CPU in ISP memory region and ISPEN = 1, accurate values in ISP-related registers should be confirmed by
ISP software. Then, sequentially writing 0x46h, 0xB9h into SCMD are used to really trigger memory access
operations (flash page erase, flash byte programming and flash byte read).
After ISP operation has been finished, software writes “001” on ISPCR.7 ~ ISPCR.5 which triggers an software
RESET and makes CPU reboot into application program memory (AP-memory) on the address 0x0000.
Before perform ISP operation, the user should fill the bits XCKS4~XCKS0 in CKCON1 register with a proper value.
(Refer to Section “8.2 Clock Register”)
To do Byte Program
To do Read
The following Figure 23–2 shows a sample code for ISP operation.
;=============================================================================
; 1. Page Erase Mode (512 bytes per page)
;=============================================================================
ORL IFMT,#03h ;MS[2:0]=[0,1,1], select Page Erase Mode
MOV IFADRH,?? ;fill page address in IFADRH & IFADRL
MOV IFADRL,?? ;
MOV SCMD,#46h ;trigger ISP processing
MOV SCMD,#0B9h ;
;Now in processing...(CPU will halt here until complete)
;=============================================================================
; 2. Byte Program Mode
;=============================================================================
ORL IFMT,#02h ;MS[2:0]=[0,1,0], select Byte Program Mode
ANL ISPCR,#0FAh ;
MOV IFADRH,?? ;fill byte address in IFADRH & IFADRL
MOV IFADRL,?? ;
MOV IFD,?? ;fill the data to be programmed in IFD
MOV SCMD,#46h ;trigger ISP processing
MOV SCMD,#0B9h ;
;Now in processing...(CPU will halt here until complete)
;=============================================================================
; 3. Verify using Read Mode
;=============================================================================
The device is In Application Programmable (IAP), which allows some region in the Flash memory to be used as
non-volatile data storage while the application program is running. This useful feature can be applied to the
application where the data must be kept after power off. Thus, there is no need to use an external serial EEPROM
(such as 93C46, 24C01, .., and so on) for saving the non-volatile data.
In fact, the operating of IAP is the same as that of ISP except the Flash range to be programmed is different. The
programmable Flash range for ISP operating is located within the AP and IAP memory, while the range for IAP
operating is only located within the configured IAP-memory.
Note:
(1) Before using the IAP feature, the software should specify an IAP-memory space by writing IAPLB in Page-P
SFR. The IAP-memory space can be also configured by a universal Writer/Programmer or Megawin
proprietary Writer/Programmer which configuration is corresponding to IAPLB initial value.
(2) The program code to execute IAP is located in the AP-memory and just only program IAP-memory not
ISP-memory.
If ISP-memory is specified, the range of the IAP-memory is determined by IAP and the ISP starts address as listed
below.
If ISP-memory is not specified, the range of the IAP-memory is determined by the following formula.
For example, if ISP-memory is 2.5K, so that ISP start address is 0xF600, and IAP-memory is 1.5K, then the
IAP-memory range is located at 0xF000 ~ 0xF5FF. The IAP low boundary in MG84FG516 is defined by IAPLB
register which can be modified by software to adjust the IAP size in user’s AP program.
The special function registers are related to ISP/IAP would be shown in Section 23.2.1 ISP/IAP Register .
Because the IAP-memory is a part of Flash memory, only Page Erase, no Byte Erase, is provided for Flash erasing.
To update “one byte” in the IAP-memory, users can not directly program the new datum into that byte. The following
steps show the proper procedure:
Step 1: Save the whole page flash data (with 512 bytes) into XRAM buffer which contains the data to be updated.
Step 2: Erase this page (using Page Erase mode of ISP).
Step 3: Modify the new data on the byte(s) in the XRAM buffer.
Step 4: Program the updated data out of the XRAM buffer into this page (using Byte Program mode of ISP).
To read the data in the IAP-memory, users can use either the “MOVC A,@A+DPTR” instruction or the Read mode
of ISP.
Bit 7~0: The IAPLB determines the IAP-memory lower boundary. Since a Flash page has 512 bytes, the IAPLB must
be an even number.
To read IAPLB, MCU need to define the IFADRL for SFR address in Page-P, the IMFT for mode selection on Page-P
Read and set ISPCR.ISPEN. And then write 0x46h & 0xB9h sequentially into SCMD. The IAPLB content is available
in IFD. If write IAPLB, MCU will put new IAPLB setting value in IFD firstly. And index IFADRL, select IMFT, enable
ISPCR.ISPEN and then set SCMD. The IAPLB content has already finished the updated sequence.
The range of the IAP-memory is determined by IAPLB and the ISP Start address as listed below.
IAP lower boundary = IAPLBx256, and
IAP higher boundary = ISP start address – 1.
For example, if IAPLB=0xE0 and ISP start address is 0xF000, then the IAP-memory range is located at 0xE000 ~
0xEFFF.
Additional attention point, the IAP low boundary address must not be higher than ISP start address.
Bit 7~6: XTGS1~XTGS0, XTAL oscillator Gain control Register. Software must writ “01” on the two bits.
Bit 5~4: BO1S[1:0]. Brown-Out detector 1 monitored level Selection. The initial values of these two bits are loaded
from OR1.BO1S1O and OR1.BO1S0O.
BO1S[1:0] BOD1 detecting level
0 0 2.0V
0 1 2.4V
1 0 3.7V
1 1 4.2V
Bit 2: EBOD1, Enable BOD1 that monitors VDD power dropped at a BO1S1~0 specified voltage level.
0: Disable BOD1 to slow down the chip power consumption.
1: Enable BOD1 to monitor VDD power dropped.
Bit 0: Reserved. Software must write “1” on this bit when PCON2 is written.
Bit 7~4: Reserved. Software must write “0” on these bits when PCON3 is written.
Bit 7: Reserved. Software must write “0” on this bit when SPCON0 is written.
_page_p_sfr_read:
page_p_sfr_read:
MOV IFADRH,000h
MOV IFMT,#(MS2|MS0) ; PageP_Read=0x05
ANL ISPCR,#CFAIL ;
ORL ISPCR,#ISPEN ; Enable Function
MOV SCMD,#046h ;
MOV SCMD,#0B9h ;
RET
C Code Example:
SCMD = 0x46; //
SCMD = 0xB9; //
_page_p_sfr_write:
page_p_sfr_write:
MOV IFADRH,000h ;
MOV SCMD,#046h ;
MOV SCMD,#0B9h ;
RET
C Code Example:
SCMD = 0x46; //
SCMD = 0xB9; //
MOV IFADRL,#SPCON0 ;
CALL page_p_sfr_read ;
C Code Example:
IFADRL = SPCON0; //
page_p_sfr_read(); //
(4). Required Function: Enable CKCTL0 for SYSCLK divider (CKCON0) changed in Page-P
Assembly Code Example:
MOV IFADRL,#SPCON0 ;
CALL page_p_sfr_read ;
C Code Example:
IFADRL = SPCON0; //
page_p_sfr_read (); //
Bit 7~6: P6.0 function configured control bit 1 and 0. The two bits only act when internal RC oscillator (IHRCO or
ILRCO) is selected for system clock source. In crystal mode, XTAL2 and XTAL1 are the alternated function of P6.0
and P6.1. In external clock input mode, P6.0 is the dedicated clock input pin. In internal oscillator condition, P6.0
provides the following selections for GPIO or clock source generator. When P60OC[1:0] index to non-P6.0 GPIO
function, P6.0 will drive the on-chip RC oscillator output to provide the clock source for other devices.
Bit 7~6: INT3IS1~0, nINT3 input selection bits which function is defined as following table.
INT3IS1~0 nINT3 Input Selected Port Pin Switch Condition
00 nINT3 Port Pin P4.2 or P4.5 AUXR0.P4FS1~0
01 RXD1 Port Pin P1.2 or P5.2 AUXR1.P5S1
10 TWSI SDA Port Pin P4.1 None
11 SPI nSS Port Pin P1.4 or P5.4 AUXR1.P5SPI
Bit 5~4: INT2IS1~0, nINT2 input selection bits which function is defined as following table.
INT2IS1~0 nINT2 Selected Port Pin Switch Condition
00 nINT2 Port Pin P4.3 or P4.4 AUXR0.P4FS1~0
01 RXD0 Port Pin P3.0 or P4.4 AUXR0.P4FS1~0
10 TWSI SDA Port Pin P4.1 None
11 SPI nSS Port Pin P1.4 or P5.4 AUXR1.P5SPI
Bit 7~4: Reserved. Software must write “0” on these bits when SFRPI is written.
Bit 3~0: SFR Page Index. The available pages are only page “0” and “1”.
There are 13 register sets in Page 0, S0CON(98H), S0BUF(99H), S0CFG(9AH), S1CFG(9BH), PUCON0(B4H),
P5M0(B5H), T2CON(C8H), T2MOD(C9H), RCAP2L(CAH), RCAP2H(CBH), TL2(CCH), TH2(CDH) and P5(F8H).
13 register sets in Page 1, S1CON(98H), S1BUF(99H) and S1BRT(9AH), S1BRC(9BH), PUCON1(B4H),
P6M0(B5H), T3CON(C8H), T3MOD(C9H), RCAP3L(CAH), RCAP3H(CBH), TL3(CCH), TH3(CDH) and P6(F8H).
LOCK:
: Enabled. Code dumped on a universal Writer or Programmer is locked to 0xFF for security.
: Disabled. Not locked.
ISP-memory Space:
The ISP-memory space is specified by its starting address. And, its higher boundary is limited by the Flash end
address, i.e., 0xFFFF. The following table lists the ISP space option in this chip. In default setting, MG84FG516
ISP space is configured to 2.5K that had been embedded Megawin USB DFU boot loader to perform on-USB-line
Device Firmware Upgrade.
HWBS:
: Enabled. When powered up, MCU will boot from ISP-memory if ISP-memory is configured.
: Disabled. MCU always boots from AP-memory.
HWBS2:
: Enabled. Not only power-up but also any reset will cause MCU to boot from ISP-memory if ISP-memory is
configured.
: Disabled. Where MCU boots from is determined by HWBS.
IAP-memory Space:
The IAP-memory space specifies the user defined IAP space. The IAP-memory Space can be configured by
hardware option or MCU software by modifying IAPLB. In default, it is configured to 1.5K bytes.
BO1S1O, BO1S0O:
,: Select BOD1 to detect 2.0V.
,: Select BOD1 to detect 2.4V.
,: Select BOD1 to detect 3.7V.
,: Select BOD1 to detect 4.2V.
BO0REO:
: Enabled. BOD0 will trigger a RESET event to CPU on AP program start address. (2.2V)
: Disabled. BOD0 can not trigger a RESET to CPU.
BO1REO:
: Enabled. BOD1 will trigger a RESET event to CPU on AP program start address. (4.2V, 3.7V, 2.4V or 2.0V)
: Disabled. BOD1 can not trigger a RESET to CPU.
WRENO:
: Enabled. Set WDTCR.WREN to enable a system reset function by WDTF.
WDSFWP:
: Enabled. The WDT SFRs, WREN, NSW, WIDL, PS2, PS1 and PS0 in WDTCR, will be write-protected.
: Disabled. The WDT SFRs, WREN, NSW, WIDL, PS2, PS1 and PS0 in WDTCR, are free for writing of software.
To have the MG84FG516 work with power supply varying from 2.0V to 5.5V, adding some external decoupling and
bypass capacitors is necessary, as shown in Figure 27–1.
Power Supply
MG84FG516
VDD
0.1uF 10uF
V33
4.7uF
VR0
0.1uF
VSS 4.7uF
Normally, the power-on reset can be successfully generated during power-up. However, to further ensure the MCU
a reliable reset during power-up, the external reset is necessary. Figure 27–2 shows the external reset circuit, which
consists of a capacitor CEXT connected to VDD (power supply) and a resistor REXT connected to VSS (ground).
In general, REXT is optional because the RST pin has an internal pull-down resistor (RRST). This internal diffused
resistor to VSS permits a power-up reset using only an external capacitor CEXT to VDD.
Power Supply
MG84FG516
VDD
4.7uF CEXT
RST
VSS
To achieve successful and exact oscillating (up to 24MHz), the capacitors C1 and C2 are necessary, as shown in
Figure 27–3. Normally, C1 and C2 have the same value. Table 27–1 lists the C1 & C2 value for the different
frequency crystal application.
MG84FG516
XTAL2
Crystal
XTAL1
C1 C2
MG84FG516 devices include an on-chip Megawin proprietary debug interface to allow In-Chip-Programming (ICP)
and in-system On-Chip-Debugging (OCD) with the production part installed in the end application. The ICP and
OCD share the same interface to use a clock signal (ICP_SCL/OCD_SCL) and a bi-directional data signal
(ICP_SDA/OCD_SDA) to transfer information between the device and a host system.
The ICP interface allows the ICP_SCL/ICP_SDA pins to be shared with user functions so that In-Chip Flash
Programming function could be performed. This is practicable because ICP communication is performed when the
device is in the halt state, where the on-chip peripherals and user software are stalled. In this halted state, the ICP
interface can safely ‘borrow’ the ICP_SCL (P4.4) and ICP_SDA (P4.5) pins. In most applications, external resistors
are required to isolate ICP interface traffic from the user application. A typical isolation configuration is shown in
Figure 27–4.
It is strongly recommended to build the ICP interface circuit on target system. It will reserve the whole
capability for software programming and device options configured.
After power-on, the P4.4 and P4.5 of MG84FG516 are configured to OCD_SCL/OCD_SDA for in-system
On-Chip-Debugging function. This is possible because OCD communication is typically performed when the CPU is
in the halt state, where the user software is stalled. In this halted state, the OCD interface can safely ‘use’ the
OCD_SCL (P4.4) and OCD_SDA (P4.5) pins. As mentioned ICP interface isolation in Figure 27–4, external resistors
are required to isolate OCD interface traffic from the user application.
If user gives up the OCD function, software can configure the OCD_SCL and OCD_SDA to port pins: P4.4 and P4.5
by clearing OCDE on bit 0 of PCON3. When user would like to regain the OCD function, user can predict an event
that triggers the software to switch the P4.4 and P4.5 back to OCD_SCL and OCD_SDA by setting OCED as “1”. Or
“Erase” the on-chip flash by ICP which cleans the user software to stop the port pins switching.
Target System
MG84FG516
RESET 4.7KΩ
RST
Input
4.7KΩ
Input 1 OCD_SCL
Output 1
4.7KΩ
Input 2 OCD_SDA
Output 2
The ICP, like the traditional parallel programming method, can be used to program anywhere in the MCU, including
the Flash and MCU’s Hardware Option. And, owing to its dedicated serial programming interface (via the On-Chip
Debug path), the ICP can update the MCU without removing the MCU chip from the actual end product, just like the
ISP does.
The proprietary 6-pin “Megawin 8051 ICE Adapter” can support the In-Circuit Programming of MG84FG516.
“Megawin 8051 ICE Adapter” has the in-system storage to store the user program code and device options. So, the
tools can perform a portable and stand-alone programming without a host on-line, such as connecting the tool to PC.
Following lists the features of the ICP function:
Features
No need to have a loader program pre-programmed in the target MCU.
Dedicated serial interface; no port pin is occupied.
The target MCU needn’t be in running state; it just needs to be powered.
Capable of portable and stand-alone working without host’s intervention.
The above valuable features make the ICP function very friendly to the user. Particularly, it is capable of stand-alone
working after the programming data is downloaded. This is especially useful in the field without a PC. The system
diagrams of the ICP function for the stand-alone programming are shown in Figure 27–5. Only five pins are used for
the ICP interface: the SDA line and SCL line function as serial data and serial clock, respectively, to transmit the
programming data from the 6-pin “Megawin 8051 ICE Adapter” to the target MCU; the RST line to halt the MCU, and
the VCC & GND are the power supply entry of the 6-pin “Megawin 8051 ICE Adapter” for portable programming
application. The USB connector can be directly plugged into the PC’s USB port to download the programming data
from PC to the 6-pin “Megawin 8051 ICE Adapter”.
Target System
ICP & OCD
MG84FG516 Interface
START button: for code programming
N.C.
SCL P3.0 USB
OCD_SCL
SCL
VDD
VCC
(less than 20cm) VCC MEGAWIN
MAKE YOU WIN
SDA SDA Program code
OCD_SDA
GND GND 8051 ICE Adapter download path
VSS
RST RST
RST
"Megawin 8051 OCD ICE"
The MG84FG516 is equipped with a Megawin proprietary On-Chip Debug (OCD) interface for In-Circuit Emulator
(ICE). The OCD interface provides on-chip and in-system non-intrusive debugging without any target resource
occupied. Several operations necessary for an ICE are supported, such as Reset, Run, Stop, Step, Run to Cursor
and Breakpoint Setting.
Using the OCD technology, Megawin provides the “Megawin 8051 OCD ICE” for the user, as shown in Figure 27–6.
The user has no need to prepare any development board during developing, or the socket adapter used in the
traditional ICE probe. All the thing the user needs to do is to reserve a 6-pin connector on the system for the
dedicated OCD interface: P3.0, RST, VCC, OCD_SDA, OCD_SCL and GND as shown in Figure 27–6.
In addition, the most powerful feature is that it can directly connect the user’s target system to the Keil 8051 IDE
software for debugging, which directly utilizes the Keil IDE’s dScope-Debugger function. Of course, all the
advantages are based on your using Keil 8051 IDE software.
Note: “Keil” is the trade mark of “Keil Elektronik GmbH and Keil Software, Inc.”.
Features
Megawin proprietary OCD (On-Chip-Debug) technology
On-chip & in-system real-time debugging
5-pin dedicated serial interface for OCD, no target resource occupied
Directly linked to the debugger function of the Keil 8051 IDE Software
USB connection between target and host (PC)
Helpful debug actions: Reset, Run, Stop, Step and Run to Cursor
Programmable breakpoints, up to 4 breakpoints can be inserted simultaneously
Several debug-helpful windows: Register/Disassembly/Watch/Memory Windows
Source-level (Assembly or C-language) debugging capability
Target System
ICP & OCD
MG84FG516 Interface
PC
N.C. "Megawin 8051 OCD ICE"
SCL P3.0
OCD_SCL Keil 8051
SCL
VDD
VCC
(less than 20cm) VCC MEGAWIN
MAKE YOU WIN
USB IDE
SDA SDA
OCD_SDA
GND GND
8051 ICE Adapter
VSS
RST RST
RST
Note: For more detailed information about the OCD ICE, please feel free to contact Megawin.
VDD = 5.0V±10%, VSS = 0V, TA = 25 ℃ and execute NOP for each machine cycle, unless otherwise specified
Limits Unit
Symbol Parameter Test Condition
min typ max
Input/Output Characteristics
VIH1 Input High voltage (All I/O Ports) Except P6.0, P6.1 0.6 VDD
Input High voltage (RST, P6.0,
VIH2 0.75 VDD
P6.1)
VIL1 Input Low voltage (All I/O Ports) Except P6.0, P6.1 0.15 VDD
Input Low voltage (RST, P6.0,
VIL2 0.2 VDD
P6.1)
Input High Leakage current (All I/O
IIH VPIN = VDD 0 10 uA
Ports)
Logic 0 input current (P3 in
IIL1 quasi-mode or other Input port with VPIN = 0.4V 20 50 uA
on-chip pull-up resistor)
Logic 0 input current (All Input only
IIL2 VPIN = 0.4V 0 10 uA
or open-drain Ports)
Logic 1 to 0 input transition current
IH2L (P3 in quasi-mode or other Input VPIN =1.8V 330 500 uA
port with on-chip pull-up resistor)
Output High current (P3 in
quasi-Mode or other open-drain
IOH1 VPIN =2.4V 150 200 uA
output port with on-chip pull-up
resistor)
Output High current (All push-pull
IOH2 VPIN =2.4V 12 mA
output ports)
IOL1 Output Low current (All I/O Ports) VPIN =0.4V 12 mA
RRST Internal reset pull-down resistance 85 Kohm
Power Consumption
SYSCLK = 32MHz @
IOP1 Normal mode operating current 10.5 mA
IHRCO with PLL
SYSCLK = 24MHz @
IOP2 9 mA
IHRCO with PLL
SYSCLK = 12MHz @
IOP3 5.3 mA
IHRCO
SYSCLK = 12MHz @
IOP4 9.3 mA
IHRCO with ADC
SYSCLK = 24MHz @
IOP5 11 mA
XTAL
SYSCLK = 12MHz @
IOP6 6.4 mA
XTAL
SYSCLK = 6MHz @
IOP7 4 mA
XTAL
SYSCLK = 2MHz @
IOP8 2.5 mA
XTAL
Normal mode operation current on SYSCLK = 12MHz @
IOP9 12 mA
USB connected IHRCO with PLL, USB
SYSCLK = 12MHz/128
IOPS1 Slow mode operating current 1 mA
@ IHRCO
SYSCLK = 12MHz/128
IOPS2 2 mA
@ XTAL
SYSCLK = 12MHz @
IIDLE1 Idle mode operating current 2 mA
IHRCO
SYSCLK = 12MHz @
IIDLE2 3 mA
XTAL
0.2VDD - 0.1
0.45V
tCLCX
tCLCL
Limits Unit
Parameter Test Condition
min typ max
Supply Voltage TA = -40℃ to +125℃ 2.4 5.5 V
(1) (1)
Clock Input Range TA = -40℃ to +125℃ 5 6 7 MHz
(2) (2)
CKM Start-up Time TA = -40℃ to +125℃ 20 100 us
CKM Power Consumption TA = +25℃, VDD=5.0V 1 mA
(1)
Data guaranteed by design, not tested in production.
(2)
Data based on characterization results, not tested in production.
Limits Unit
Parameter Test Condition
min typ max
Supply Voltage TA = -40℃ to +125℃ 2.0 5.5 V
Flash Write (Erase/Program) Voltage TA = -40℃ to +125℃ 2.2 5.5 V
Flash Erase/Program Cycle TA = -40℃ to +125℃ 10,000 times
Flash Data Retention TA = +25℃ 100 year
tXLXL
CLOCK
tQVXH
tXHQX
WRITE TO SBUF 0 1 2 3 4 5 6 7
tXHDX
OUTPUT DATA tXHDV SET TI
CLEAR RI VALID VALID VALID VALID VALID VALID VALID VALID
Clock Cycle 1 2 3 4 5 6 7 8
SPICLK(CPOL=0)
tCKH tCKL
SPICLK(CPOL=1)
tMIS tMIH
MISO
tMOH
MOSI
Clock Cycle 1 2 3 4 5 6 7 8
SPICLK(CPOL=0)
tCKH tCKL
SPICLK(CPOL=1)
tMIS tMIH
MISO
tMOH
MOSI
Clock Cycle 1 2 3 4 5 6 7 8
tSE
SPICLK(CPOL=0)
SPICLK(CPOL=1)
tSIS tSIH
MOSI
MISO
nSS
Clock Cycle 1 2 3 4 5 6 7 8
tSE
SPICLK(CPOL=0)
tCKL
tCKH tSD
SPICLK(CPOL=1)
tSIS tSIH
MOSI
tSOH tSLH
MISO
tSEZ tSDZ
nSS
Under operating conditions, load capacitance for Port 0, ALE, and PSEN = 100 pF; load capacitance for all other
outputs = 80pF. TA = -40℃ to +125℃, VDD=5.0V±10%, VSS=0V
T: Clock Cycle M: Clock number of ALE Stretch, M = 0T~3T
N: Clock number of Read/Write Pulse Width Stretch, N = 0T ~ 7T
L: Clock number of Read/Write pulse Setup/Hold Stretch, L = 0T ~ 1T
Oscillator
36MHz Without 36MHz with Stretched
Symbol Parameter Unit
Stretched MOVX MOVX
Min. Max Min. Max
1/tCLCL Oscillator Frequency 36 36 MHz
tLHLL ALE Pulse Width T-10 T+M-10 ns
tAVLL Address Valid to ALE Low T-12 T+M-12 ns
tLLAX Address Hold after ALE Low T-12 T+M-12 ns
tRLRH nRD Pulse Width T-10 T+N-10 ns
tWLWH nWR Pulse Width T-10 T+N-10 ns
tRLDV nRD Low to valid Data In T-20 T+N-20 ns
tRHDX Data Hold After nRD 0 0 ns
tRHDZ Data Float After nRD 10 10 ns
tLLDV ALE Low to Valid Data In 3T-20 3T+M+L+N ns
-20
tAVDV Address to Valid Data In 4T-20 4T+2M+L+N ns
-20
tLLWL ALE Low to nRD or nWR Low 2T-10 2T+10 2T+2M+L 2T+2M+L ns
-10 +10
tAVWL Address to nRD or nWR Low 3T-10 3T+2M+L ns
-10
tWHQX Data Hold After nWR T-10 T+L-10 ns
tQVWH Data Valid to nWR High 2T-10 2T+L+N ns
-10
tQVWX Data Valid to nWR High to Low Transition T-10 T+L-10 ns
tRLAZ nRD Low to Address Float 0 0 ns
tWHLH nRD or nWR High to ALE High T-10 T+L-10 ns
Explanation of Symbols Each timing symbol has 5 characters. The first character is always a ‘t’ (stands for time).
The other characters, depending on their positions, stand for the name of a signal or the logical status of that signal.
The following is a list of all the characters and what they stand for.
A: Address Q: Output data
C: Clock R: RD signal
D: Input data t: Time
H: Logic level HIGH V: Valid
L: Logic level LOW or ALE W: WR signal
X: No longer a valid logic level
Z: High Impedance (Float)
For example:
tAVLL = Time from Address Valid to ALE Low
tRLRH = nRD Pulse Width
Port 2 P2.0 – P2.7 P2.0 – P2.7 OR A8 - A15 FROM DPH P2.0 – P2.7
tAVDV
tLLDV
tRLAZ tRHDX
tLHLL tLLAX
tRLDV tRHDZ
ALE P4.6 P4.6
tAVWL
Port 2 P2.0 – P2.7 P2.0 – P2.7 OR A8 - A15 FROM DPH P2.0 – P2.7
tAVWL
Note 1: The cycle time for access of external auxiliary RAM is:
EMAI1 = 0: 5 + 2 x ALE_Stretch + RW_Stretch + 2 x RWSH; (5~20)
EMAI1 = 1: 3 + RW_Stretch + 2 x RWSH; (3~12)
1. Correct ISPCR SFR address to 0xE7 in section 10.4 and section 23. 2011/10/12
2. Modify CLRW description in WDT section and update WDT block diagram. 2011/10/12
3. In section 21.2.2, modify timer1/3 timer0/3 2011/10/12
4. Remove DISREG, POF1 & IARF. 2011/10/12
5. Correct SPEN0 to SPCON0 in Section 3.3. 2011/10/12
6. Correct SICON address from F8H to D4H in Section 3.2. 2011/10/12
7. In Section 21, modify description for “Figure 16-1 “to “Figure 21-1”. 2011/10/12
8. Correct bit name in P1AIO SFR in Section 12.2.2 and Section 21.3. 2011/11/10
9. In Section 12.2.8, correct “P4.6 & P4.4” and “P4.3 & P4.0” to “P4.6 ~ P4.4” and “P4.3 ~
2011/11/10
P4.0”.
10. Correct SFR name “SCON0, SBUF0, SCFG0, SCON1, SBUF1 and SCFG1” to
2011/11/10
“S0CON, S0BUF, S0CFG, S1CON, S1BUF and S1CFG.
11. Change bus naming rule from “[n]” to “.n”, n = 0~ 7, such as S0BUIF.7. 2011/11/10
12. Add description for EXEN2 in Section 14.2.5. 2011/11/10
13. Update PCA port I/O to Port 2 in Section 17 and PWM diagrams. 2011/11/10
14. In Section 13.7 and 26, modify bit address to 5~4 for INT2IS1 and INT2SI0. 2011/11/10
v1.21 15. In Section 14.3.3, modify description for T3CKO on P0.1. 2011/11/10
16. Update section 16.2.3 equation for S1TX12. 2011/11/10
17. Update System Clock Diagram. 2011/11/10
18. Add tables for Timer 1 baud rate setting. 2011/11/10
19. Correct the equation for Timer2 baud rate generator and add tables for Timer 2 baud
2011/11/10
rate setting.
20. Add S1 Mode 1 & 3 baud rate setting description in section 16.2.3. 2011/11/10
21. Combine ISP & IAP to one Section 2011/11/10
22. Modify Electrical Characteristics 2011/11/14
23. Modify Hardware Option description and modify Absolute Maximum Rating 2011/11/14
24. Add Section 27: Application Notes. 2011/11/14
25. Move Absolute Maximum Rating into Section Electrical Characteristics 2011/11/30
26. In section 17.1, modify PCA port pin from Port 1 to Port 2. 2011/12/20
27. In section 17.2, modify ECI from P1.1 to P2.1. 2011/12/20
28. In section 21.2, modify ADC result from {ADCH, ADCL} to {ADCDH, ADCDL}. 2011/12/20
29 Add Section 12.1.7 for General Port Input Configured. 2012/01/18
30. Add Package Dimension & remove 84FG532 in Figure 6-5 ~ 6-7. 2012/01/19
Life Support — This product is not designed for use in medical, life-saving or life-sustaining
applications, or systems where malfunction of this product can reasonably be expected to result in
personal injury. Customers using or selling this product for use in such applications do so at their own risk
and agree to fully indemnify Megawin for any damages resulting from such improper use or sale.
Right to Make Changes — Megawin reserves the right to make changes in the products - including
circuits, standard cells, and/or software - described or contained herein in order to improve design and/or
performance. When the product is in mass production, relevant changes will be communicated via an
Engineering Change Notification (ECN).