TW8816 Built-in MCU Application Notes Disclaimer This document provides technical information for the user. Techwell Inc. reserves the right to modify the information in this document as necessary. The customer should make sure that they have the most recent data sheet version. Techwell Inc. holds no responsibility for any errors that may appear in this document. Customers should take appropriate action to ensure their use of the products does not infringe upon any patents. Techwell Inc. respects valid patent rights of third parties and does not infringe upon or assist others to infringe upon such rights. TECHWELL, INC. Silicon Rev. B
Functional Description Overview Techwell s TW8816 s internal MCU core is very similar to Dallas Semiconductor s DS82C320. It is running directly from SPI flash memory. External program memory space is up to 64 banks of 64Kbytes( Totally 4Mbytes ) see bank access application note. Variable running MCU clock more efficiently access Program SPI Flash memory. 4 clocks/instruction cycle versus 12 in standard 8051 - Up to three times faster execution on average versus standard 8051 Dual data pointers - Improves efficiency when moving large blocks of data Additional Interrupts for Power Down(INT5), TW8816IRQ(INT3) see application notes for interrupts Program can address up to 64KB of program memory at address 0000h-FFFFh. Internal 256 Bytes RAM module and external 256 Bytes RAM module Three timers(timer0, 1, 2) One external serial port it can be used as I/O ports also. One external interrupt it can be used as I/O ports also. 12 I/O ports MCU core 27MHz 27MHz 27MHz Timer Div0 16bit divider 9Ch, 9Dh Timer Div1 16bit divider 9Eh, 9Fh Timer Div1 16bit divider 93h, 94h Timer0 Timer1 Timer2 Port2.0 2.7 Port1.0 Port1.1 Port1.6 Port1.7 RXD-Port3.0 TXD-Port3.1 INT0-Port3.2 Address / Data/ Bank Register(9Ah) SPI Controller Block 83~89:P2.0 2.7 93:P1.0 94:P1.1 114:P1.6 113:P1.7 92:RXD(P3.0) 91:TXD(P3.1) 112:INT0(P3.2)_ SCLK SPICS MOSI MISO 27MHz Simple Block Diagram TECHWELL, INC. 2 REV. 0
Compiler Settings 1. Select processor: DS82C320 2. Use Dual DPTR: on this core has dual data pointer 3. Memory model compact core has 256bytes internal data and 256bytes external data 4. Code Rom Size depends user application program size, support up to 4Mbytes 5. Code banking depends user application, but support up to 64 banks Memory Organization Memory organization in the MCU is similar to that of the industry-standard 8051. There are three distinct memory areas: Program memory (ROM) Data memory (external RAM) Registers and Data memory(internal RAM) Figure Memory Map shows the MCU memory map. Program memory can be up to 64 KB address range (0000h FFFFh). Data memory is 256 bytes (00h-FFh). The internal RAM(256 bytes) addresses overlap the external RAM addresses, but are accessed through different instruction types. Inside the TW8816 TECHWELL, INC. 3 REV. 0
Program Memory The DW8051 can address up to 64 KB of program memory at addresses 0000h FFFFh. Program memory can be implemented as External ROM. (But user can extend it using bank accessing). TW8816 provides SPI interface for accessing Program Memory. Ext RAM Ext RAM 0h-255h is address indirectly as the same way to access external data memory with MOVX instruction. Address pointer are R0 and R1 of the selected register bank and DPTR register. TW8816 Registers TW8816 registers are mapped to external RAM C000h C0FFh. Actual TW8816 s register map is from 0h to 1FFh, but it is controlled by page selection. Register 0FFh is page selection register. This register can be accessed with MOVX instruction. Internal RAM The internal RAM, illustrated in Internal RAM Organization, consists of: 128 bytes of registers and scratchpad memory accessible through direct or indirect addressing. Upper 128 bytes of scratchpad memory accessible through indirect. 128 special function registers (SFRs) accessible through direct addressing (addresses 80h FFh). TECHWELL, INC. 4 REV. 0
Bank Access First use the compiler which is supporting bank mode programming. TW8816 provide SFR 9Ah for up to 64(6bit) banks selection. User should modify own bank switch routines.. Below is example for our demo board. BANKREG DATA 9AH ; Memory Bank select Function Register SWITCH0 MACRO ; Switch to Memory Bank #0 MOV BANKREG, #0H ENDM ; SWITCH1 MACRO ; Switch to Memory Bank #1 MOV BANKREG, #1H ENDM ; SWITCH2 MACRO ; Switch to Memory Bank #2 MOV BANKREG, #2H ENDM ; SWITCH3 MACRO ; Switch to Memory Bank #3 MOV BANKREG, #3H ENDM ; ** Bank switching routines are depend on compilers. See your compiler manuals. TECHWELL, INC. 5 REV. 0
Serial Port and Timer Setting TW8816 s MCU cannot use internal clock for making fixed frequency timer because of variable MCU clock. So using T0, T1 and T2 for getting fixed frequency. T0, T1 and T2 are derived from 16 bit dividers(named MCU External Timer Clock0(1, 2) Divider). Their input clock is 27MHz. MCU External Timer Clock0 Divider : SFR 0x9C(Upper 8bits), 0x9D(Lower 8bits) MCU External Timer Clock1 Divider : SFR 0x9E(Upper 8bits), 0x9F(Lower 8bits) MCU External Timer Clock2 Divider : SFR 0x93(Upper 8bits), 0x94(Lower 8bits) Serial Port 1.Serial port control register set to 50h SCON (98h) = 50h; // serial port mode 1( Async, Timer1, 8 Data bits, 1start, 1 stop, none 9 th bit) 2. Timer0, 1 control register set to 66h // serial port0 mode bit0 = 0, bi1 = 1, bit2 = 0 : baud rate = ToSerialPort/16 TMOD(89h) = 66h; // Timer0,1 using t0, t1 pin for clocking // Timer0, 1 Mode2 8bit counter with auto reload TECHWELL, INC. 6 REV. 0
Use Timer1 Mode for get Baud Rate. Below table shows MCU External Timer Clock1 divider number and TL1, TH1 value Baud Rate Timer Div1 value TH1 Real Baud Rate 9600 176 => 27MHz / 176 = 153.4KHz FFh 153.4 / 16 = 9588 4800 352 FFh 4794 2400 704 FFh 2397 1200 1408 FFh 1198 Timer0 for real time clock Use Timer0 for real time clock We can easily get combining Timer Div0 value(16bit) and TH0 27MHz / Timer Div0 / (256-TH0) = output Tick Ex1) Timer Div0 = 432, TH0 = 131 27MHz / 432 / (256-131) = 500 Hz Ex2) Timer Div0 = 270, TH0 = 56 27MHz / 270 / (256-56) = 500 Hz Ex3) Rimer Div0 = 540, TH0 = 206 27MHz / 540 / (256-206) = 1000Hz Timer2 for IR remote controller clock Use Timer0 for real time clock We can easily get combining Timer Div0 value(16bit) and TH2&TL2(16bit) IR timing = 1/27 * Timer Div2 * (0x10000 TH2&TL2) [usec] Ex1) RC5, Type 1, 209.62usec Timer Div2 = 283, TH2&TL2 = 65516 1/27 * 283 * 20 = 209.63usec Ex2) RC5, Type 2, 222.25usec ( one signal pulse is 889usec, sampling 4 times ) Timer Div2 = 300, TH2&TL2 = 65516 1/27 * 300 * 20 = 222.22usec Ex3) NEC type,186.7usec ( one signal pulse is 560usec, sampling 3 times) Timer Div2 = 252, TH2&TL2 = 65516 1/27 * 252 * 20 = 186.7usec TECHWELL, INC. 7 REV. 0
Using Low Speed ADC TW8816 provides 2 8bit resolution LADCs(Low Speed ADC). It s input range is 0V ~ 1.25V TW8816 REG 0C9h : LADC channel0 TW8816 REG 0CAh : LADC channel1 Application as Key Input Below diagram shows how to make key input using resister ladder. To LADC Key Input Using Resister Ladder TECHWELL, INC. 8 REV. 0
Algorithm for using LADC as Key Input Below sample source code shows how to make key input using resister ladder. BYTE ReadKey( void ) { BYTE i, j, min, max; WORD key_in; } i = GetLADCn Cn(); // get LADC value if ( i<0x10 ) return (0); // smaller than MIN return 0 Delay(30); // 30msec delay chattering i = GetLADCn Cn(); // get LADC value if ( i<0x10 ) return (0); // smaller than MIN return 0 key_in = i; // save Key value min = i; max = i; for ( j=0; j<17; j++ ) { // add 18 key inputs i = GetLADCn Cn(); if ( i<0x10 ) return (0); key_in += i; if ( i < min ) min = i; // save min value else if ( i > max ) max = i; // save max value } key_in -= min; // subtract min key_in -= max; // subtract max key_in >>= 4; // divide by 16 if ( key_in < 0x10 ) return( 0 ); // smaller than MIN return 0 else if ( key_in < 0x33 ) return( MODE ); // MODE else if ( key_in < 0x5a ) return( RIGHT ); // RIGHT else if ( key_in < 0x80 ) return( UP ); // UP else if ( key_in < 0xa0 ) return( MENU ); // MENU else if ( key_in < 0xcc ) return( DOWN ); // DOWN else if ( key_in < 0xf5 ) return( LEFT ); // LEFT else return( POWER ); // POWER TECHWELL, INC. 9 REV. 0