This document provides a brief introduction to the Hitachi H8/3062F microcontroller and its development tools. It describes the key features of the H8/3062F microcontroller, including its 32-bit architecture, 16M address space, timers, serial ports, and I/O pins. It then discusses the various development tools needed, such as the Hitachi Embedded Workshop C/C++ compiler, Flash Development Toolkit for flashing programs, and WinH8 for downloading programs to RAM. The document provides an overview of creating and building projects with these tools.
Original Description:
This is an introduction to the operational aspects of the Hitachi H8 Microcontroller.
This document provides a brief introduction to the Hitachi H8/3062F microcontroller and its development tools. It describes the key features of the H8/3062F microcontroller, including its 32-bit architecture, 16M address space, timers, serial ports, and I/O pins. It then discusses the various development tools needed, such as the Hitachi Embedded Workshop C/C++ compiler, Flash Development Toolkit for flashing programs, and WinH8 for downloading programs to RAM. The document provides an overview of creating and building projects with these tools.
This document provides a brief introduction to the Hitachi H8/3062F microcontroller and its development tools. It describes the key features of the H8/3062F microcontroller, including its 32-bit architecture, 16M address space, timers, serial ports, and I/O pins. It then discusses the various development tools needed, such as the Hitachi Embedded Workshop C/C++ compiler, Flash Development Toolkit for flashing programs, and WinH8 for downloading programs to RAM. The document provides an overview of creating and building projects with these tools.
Hitachi H8/3062F Microcontroller and its Development Tools A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 1 1 Objectives Familiarisation with the various H8/3062F development tools, resources and solutions so that you can explore and learn on your own. 2 Overview 2.1 Features 32-bit internal architecture with 16-bit general registers H8/300H CPU core with general-purpose register architecture and 16M linear address space Up to 2OMHz clock resulting in 100ns minimum instruction cycle time. 128K FLASH/ 4K RAM Interrupt controller (3 level priority, 7 external and 27 internal interrupts). Bus controller (8 areas with independent bus specification possible) with chip select pins. 3x 16-bit timers 4x 8-bit timers; can be cascaded to 2x 16-bit timer. 16-bit Timing Pattern Controller (TPC). 1 reset-generating watchdog timer 2 channel Serial Communications Interface (SCI) 8 channel 10-bit ADC. 2 channel 8-bit DAC. Up to 70 I/O and 9 input only pins (for single chip modes only). Low Power Modes Include Sleep, Standby, Module standby and clock gearing. See Appendix B for an internal block diagram of the CPU. 2.2 Choice of various configuration and packaging HD64F3062F20 FP-100B package, Vcc = 4.5 to 5.5V, 20MHz * (used in Alpha Centre) See Appendix B for a diagram on the pin arrangement of this package. 2.3 CPU The CPU core is H8/300H, which is used in various other Hitachi microcontrollers. All IO registers are memory mapped and can be easily access though pointers in C language. Hence theres little need to learn the CPU architecture and assembly language as everything can be done through C. Nevertheless, it is helpful to browse through the Hitachi H83062h Hardware Manual, h83062h.pdf , and has a general understanding of the CPU architecture. 2.4 IO subsystems The 3062f has many on chip IO subsystems, just like any other microcontroller. The IO subsystems include ADC, DAC, timers, serial port, input/output ports, etc. It is recommended that you study these subsystems as and when you need them. C functions for some of the common IO subsystem configurations are available from my website. A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 2 3 Development Support 3.1 Websites Visit the following website for more information: www.eu.renesas.com/products/mpumcu/16bit/h8300h/3062/h8_3062_eu.html#introduction www.np.edu.sg/~nbk 3.2 C Compiler 3.2.1 Introduction The Hitachi Embedded Workshop (HEW) C/C++ cross compiler has a similar user interface to Visual C. It has 5 main components: Compiler for compiling your C program into assembly language program Assembler for assembling your assembly code into object code Linker - for linking various object code files and library files into a load module Object Converter for converting the load module into S-record format file Librarian for building your own library files. 3.2.2 Project template The HEW is able to compile to various type of CPUs operating in various modes and having different memory configuration. Hence, it is rather complex to use as there are many things that need to be configured for a particular target hardware setup. To facilitate software development, a template project, MyProj is provided that will work just fine for the AlphaCentre 3062 microcontroller board. To use, copy the whole directory content into your working directory. Use HEW to open the workspace file, myproj.hws. You should get something similar to that shown in Figure-2.2.2. A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 3 File extension notations. c C program h C include file src assembly language program inc assembly include file The various files in this template project are: dbsct.c data structure for the various data section hwsetup.c function for hardware setup; called from resetprg.c intprg.src dummy interrupt routines lowsrc.c, lowsrc.h serial IO stream routines main.c, main.h C main program. Your code should start here. resetprg.c C startup program ( reset routine ) sbrk.c, sbrk.h miscellaneous routines vecttbl.src, vect.inc for interrupt vector table cl3062f.h include file for some C utility functions iodefine.h structure and pointer declaration for on chip IO stacksct.h stack size declaration You might need to modify hwsetup.c if your hardware configuration is different. For example, if your RAM bus width is 8 bit wide or your RAM is located on a different chip select output. It is advisable that you place your C functions into new C files. To add a new file, from the menu, select Project->Add Files... Fig 2.2.2 A typical HEW session A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 4 3.2.3 Building your project There are various ways to compile and link your project files. From the menu, select Build->Build File - to compile or assemble current file Build->Build to build project. Files that are changed will be compiled/assembled followed by linking. Build->Build All compile and assemble all files followed by linking. There are two build configurations provided in MyProj. They are flash and Release. The flash configuration is used to build the project when you want to flash your program. Use the release configuration when building programs suitable for running from RAM. To select the configuration, select from the menu Options->Build Configurations... After selecting the configuration, remember to do a build. 3.3 FDT The Hitachi Flash Development Toolkit (FDT) is used to download your S-Record program into the on chip flash. The flash is guaranteed for only 100 writes. Hence for development, it is advisable to download the program into RAM for testing. Only when the program is finalized should you put your program in flash. You may install the FDT by executing FDT15.EXE The FDT manual is in fdt_man.pdf Host Computer FDT flash 3062f RAM Microcontroller Board RS232 Figure 3.3 Flashing the 3062f with FDT File.mot A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 5 3.3.1 Flash procedure The following is the procedure for downloading a S-record format program file that you have built. 1) Connect your microcontroller board to the PC using a rs232 cable. 2) Set the boards jumper to flash programming mode (boot mode). 3) Power on the board and press the reset button 4) Run the FDT 5) From the menu, select File->Open. Select your s-record file. 6) From the menu, select Image->Download Image. Your should get a dialog box as shown in figure 2.3.1. Make sure you setting is the same except for the COM port, which you set according to yours. 7) Pressed the Connect button. A small program required for flashing will be downloaded into the on-chip RAM. The content in the flash will also be erased. 8) Press Download file your_file.mot to device button. 9) After downloading, change the microcontroller board jumper to run mode and reset. Figure 2.3.1 The FDT download image dialog A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 6 3.4 WinH8 The WinH8 is used to download your H8/3062 program into the microcontroller board external RAM for execution. To download the last program, press F4 or select D from the toolbar. To download a new program, select Dnew from the toolbar. Use Options from the menu to change the COM port parameters and the terminal display size. Figure 2.4 WinH8 program Host Computer WinH8 flash 3062f RAM Microcontroller Board RS232 Figure 3.4 Downloading program into RAM with WinH8 File.mot Program Loader A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 7 3.5 Program loader The program loader is a small H8/3062 program that resides in the microcontroller flash. It allows the WinH8 to download your H8/3062 program into the RAM and execute it. As the monitor program needs to know the RAM location so that it can initialize chip select IO pin, there are different versions of the loaders for different board configurations. The two main loaders are: H8MonCS0.mot For 16-bit bus RAM on CS0 H8MonCS0_8.mot For 8-bit bus RAM on CS0 Use the FDT to put the program loader into the 3062f flash. If the program loader is flashed successfully, you should get a display as shown on the WinH8 as shown in Figure 2.4. If your external RAM is connected to a different chip select output, you will need a different loader. A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 8 4 Writing C program with HEW 4.1 Accessing IO registers IO registers are memory mapped. Hence, we can access them through C pointers. To facilitate IO access, a number of structures and pointers have been predefined for all the IO registers in the file iodefine.h. For example the following C statements in iodefine.h define structure and pointers useful for accessing port 6. union un_port1 { /* union PORT1 */ unsigned char BYTE; /* Byte Access */ struct { /* Bit Access */ unsigned char B7:1; /* Bit 7 */ unsigned char B6:1; /* Bit 6 */ unsigned char B5:1; /* Bit 5 */ unsigned char B4:1; /* Bit 4 */ unsigned char B3:1; /* Bit 3 */ unsigned char B2:1; /* Bit 2 */ unsigned char B1:1; /* Bit 1 */ unsigned char B0:1; /* Bit 0 */ } BIT; /* */ }; /* */ #define P6DDR (*(volatile union un_port1 *)0xFEE005) /* P6DDR Address*/ #define P6DR (*(volatile union un_port1 *)0xFFFFD5) /* P6DR Address*/ P6DDR is used to access the data direction register, which control whether a port pin is a input or output port pin. P6DR is used to access the port pin itself. #define OUTPUT P6DR.BIT.B0 void fn() { P6DDR.BYTE = 0x01; // Init port 6 pin 0 as an output port. // Refer to manual for details OUTPUT = 1; // send a pulse OUTPUT = 0; } 4.2 Interrupt HEW provides means to write an interrupt service routine (ISR) in C. // Use #pragma interrrupt to indicate that a C function is an ISR #pragma interrupt TimerISR void TimerISR(void); void TimerISR(void) { ITU.TISRA.BIT.IMFA1 = 0; // Clear interrupt source // Do your stuff here } #define INTNUM_ITU1CMA 28 /* Init PD interrupt at every msec */ void InitTimer(void) A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 9 { InstallInterrupt(INTNUM_ITU1CMA, TimerISR); ITU1.TCR.BIT.TPSC = 3; //20Mhz/8 clock = 2.5Mhz ITU1.GRA = 2500; //for 1msec interval, count = 2500 ITU1.TCR.BIT.CCLR = 1; //clear by GRA compare match ITU.TMDR.BIT.PWM1 = 0; //counter 1 operates normally ITU.TSTR.BIT.STR1 = 1; //start counter ITU.TISRA.BIT.IMIEA1 = 1; //IMIA1 interrupt requested by IMFA1 flag=1 } A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 10 5 MCU Operating Modes The H8/3062F micro-controller has seven operating modes (modes 1 to 7) that are selected by mode pins, MD2 to MD0 (refer to table 4 below). The different operating modes determine the size of the memory address space (from 64kbytes to 16Mbytes) and also whether the chip is operating in expanded or single-chip mode. Mode 5 (expanded mode) is the preferred mode in our kinds of applications as it allows maximum address space and maximum flexibility. Table 4: Operating Mode Selection Mode Pins Operating Mode MD2 MD1 MD0 ------ 0 0 0 Mode 1 0 0 1 Mode 2 0 1 0 Mode 3 0 1 1 Mode 4 1 0 0 Mode 5 1 0 1 Mode 6 1 1 0 Mode 7 1 1 1 A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 11 5.1 Mode 5 Memory Map Table 4.1: Mode 5 memory map H000000 H0000FF Vector area H000100 H01FFFF On-chip ROM/Flash H020000 H1FFFFF Area 0 H200000 H3FFFFF Area 1 H400000 H5FFFFF Area 2 H600000 H7FFFFF Area 3 H800000 H9FFFFF Area 4 HA00000 HBFFFFF Area 5 HC00000 HDFFFFF Area 6 HE00000 HFEDFF External address space Area 7 HFEE000 HFEE0FF On-chip I/O Registers (1) HFF8000 External address Space HFFEF1F HFFEF20 On-chip RAM* HFFFF00 HFFFF1F HFFFF20 HFFFFE9 On-chip I/O Registers (2) HFFFFEA HFFFFFF External Address Space The memory address space of mode 5 is 16 Mbytes, divided into 8 address area. Each address area is accessible through the chip select port pins. The A20-A34 address pins are not required for address decoding, hence they can be used as normally I/O port pins. A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 12 6 On-Board (Flash) Programming Mode When pins are set to on-board programming mode and a reset-start is executed, the chips enters the on-board programming state in which on-chip flash memory programming, erasing, and verifying can be carried out. There are two operating modes in this mode: Boot mode (For erasing entire flash area and programming flash) User program mode (For user program to selectively erase and program flash area) Both operating modes can be set by modes pins (MD2 MD0) and the FWE pin. The pin settings for entering each mode are shown in table 2. Table 2 On-Board Programming Mode Settings Program Mode Operating mode FWE MD2 MD1 MD0 Boot mode Mode 5 1 0 0 1 User program mode: Mode 5 1 1 0 1 The AlphaCentre 3062F board allows only mode 5 boot and mode 5 normal operating mode. A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 13 7 IO PORTS 7.1 Overview 10 input/out ports ( port 1,2,3,4,5,6,8,9, A and B) and 1 input only port (port 7) For single chip mode, a total of 70 I/O pins and 9 input-only pins are available For Mode 5 (expanded mode), the available IO pins are reduced as some pins are used for external memory interface. Table 6.1 Summary of the 3062f port pins. PORTS Functions *Port 1 Address output pins (A0-A7) *Port 2 Address output pins (A8-A15) *Port 3 Data bus high byte (D8-D15) // For 8 bit bus, D8-D15 is used **Port 4 Data bus low byte (D0-D7) // Available if there is no 16 bit bus device *Port 5(4 bits) address output pins (A16-A19) *P6.3 Address select *P6.4 RD (read signal) *P6.5 HWR (high byte write) *P6.6 LWR(low byte write) P6.7 CPU clock output pin (can be used as input port) *Port 6 P6.0-P6.2 available as input/output ports, P6.7 as input port P7.0-P0.5 analog input or digital input Port 7 P7.6-P7.7 analog input or analog ouput or digital input Multi functions port pins All pins can be used as input/output IRQ0-IRQ3 interrupt input pins CS0 CS3 chip select output Port 8 (5 bits) *P8.4 is CS0 (normally used for RAM select) Multi functions port pins All pins can be used as input/ouput IRQ4, IRQ5 interrput input pins *RxD1, TxD1 serial comm interface 1 ( for flashing) Port 9 (6 bits) RxD0, TxD0 serial comm interface 0 Multi function pins All pins can be used as input/ouput PA.4-PA.7/A20 A23 (optional address pins, to be used only if required) Port A 16 bit timers related pins Multi function pins All pins can be used as input/ouput PB.0 PB.3/CS4 CS7 (chip select outputs) Port B 8 bit timers related pins Note: *Not available for use **Available for use only if there are no 16 bit devices. For the AlphaCentre board, as the RAM is 16-bit wide, this port cannot be used for other IO purposes A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 14 7.2 Data Direction Register Most port has a data direction register, DDR. Note that all the DDRs are write only register. That is you cannot read the content of the register. Hence all port pins of the same port should have their data direction assignment done at one place. // Example code PBDDR.BYTE = 0x08; // Set Port B.3 as output, the rest are input P3DDR.BYTE=0; PBDDR.BIT.B3 = 1; // THIS WILL NOT DO !!!!!!!!!!!!! 8 Interrupts There are a number of internal and external interrupt sources. Many of the on chip devices are capable of generating interrupts. For external interrupts, the NMI(Non Maskable Interrupt) and IRQ0 to IRQ5 can be used. Refer to hardware manual for details. 9 8-bit timers 4 channels, TMR0, TMR1, TMR2 and TMR3 Channel 0 &1 and 2&3 can be combined into 16 bit timers Can be used to generate PWM outputs Used generate regular interrupt 10 16-bit timers 3 channels Can be used to generate PWM outputs Channel 2 can be used for phase counting Used to generate regular interrupt A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 15 11 PWM Generation Both the 8-bit and 16-bit timers can be used to generate PWM signal. Theoretically, we can have a total of 7 PWM outputs. In reality, we need some of these timers for other purposes. For example, a timer is required to generate the regular 1 ms interrupt required for PID control. 11.1 8-bit timer PWM output The 4 possible 8-bit timer PWM output port pins are PB.0 PB.3 (TMO0 TMO3). The highest frequency available is 9.8kHz, which is audible and rather irritating. If possible, use the 16 bit timers for PWM generations and choose a frequency just outside the audible range. Please note that with higher PWM frequency, the switching loss will be higher, so there is a compromise. // Example code #define MOTOR0PWM TMR0.TCORA // Port B.0 - MOTOR0 PWM output TMR0.TCORA PBDDR.BYTE = 0x80; TMR0.TCR.BIT.CKS = 1; // select internal clk, 20/8MHz TMR0.TCR.BIT.CCLR = 2; // counter cleared by compare match B TMR0.TCSR.BIT.OS = 6; // 1 is output when compare match A occurs TMR0.TCORB = MAXPWMVALUE; // max count of counter is 255
// Set PWM output pulse width
MOTOR0PWM = 10; // set PWM 11.2 16-bit timer PWM output pins The 3 possible 16-bit timer PWM output port pins are TIOCA0(PA.2), TIOCA1(PA.4) and TIOCA2(PA.6). The 16 bit timers allow a much wider range of input clock selection and the 16 bit counter allows a much finer resolution (In practice, for PID control, 8 bit is more than enough). // Example code #define MOTORPWM ITU0.GRA #define MAXPWMVALUE 1000 PADDR.BYTE = 0x04; // Port A.2 - motor PWM output TIOCA0 ITU0.TCR.BIT.TPSC=0; //select internal clock, 20MHz ITU0.GRB = MAXPWMVALUE; //PWM period = 0.05msec ITU0.TCR.BIT.CCLR=2; //TCNT cleared by GRB compare match ITU.TMDR.BIT.PWM0 = 1; // Select PWM mode for TIOCA0 ITU.TSTR.BIT.STR0=1;
MOTORPWM = 500; // set PWM duty
A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 16 12 Two phase encoder counting Only channel 2 of the 3 16-bit timers can be used as a two-phase encoder counter. The input pins are TCLKA(PA.0) and TCLKB(PA.1). // Example code Void InitEncoderCounter() { // 16bit timer counter 2 for motor encoder signal decoding ITU.TMDR.BIT.MDF = 1; // select phase counting mode ITU.TSTR.BIT.STR2 = 1; // start phase counting; } int newCount, oldCount, countDelta; void ReadCount() { newCount = ITU2.TCNT; countDelta = (newCount - oldCount); oldCount = newCount; } 13 Bus controller and chip select 13.1 Chip select outputs There are altogether 8 chip select outputs that can be used to qualify the 8 address area (refer to the mode-5 memory map for address details). With these chip select outputs, no external address decoding is required, thus simplifying hardware requirements when interfacing to bus devices. To standardize circuit design and program loader in AlphaCentre, use CS0 for the external RAM select. You may use any other chip select outputs for other devices. 13.2 Data bus width The 3062F is capable of interfacing to both 8 bit or 16 bit devices. For 8 bit interface, use address pin A0 onwards and D8-D15. For 16-bit interface, use address pin A1 onwards and HWR & LWR to select the high byte or low byte data (refer to microcontroller board schematic diagram in Appendix B for example). 13.3 Two state or three state selection The bus cycle for each memory area has a programmable number of 2 or 3 states bus cycles. Additionally, extra wait states can be added to allow interface to device of slower speed. Refer to hardware manual for details. A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 17 13.4 Example - Interfacing to the phase counter IC, NEC uDP7401 As the 3062 provides only one on chip two-phase counter, external two-phase counter IC is normally required. The NEC uDP7401 has two such counters in each IC. It can be easily interfaced to the 3062 data bus as shown below: CS1 needs to be configured as a chip select output. To read the counter, use a C pointer to point to CS1 base address and access the pointer content. // Init chip select. CS0 is init for an 8-bit wide RAM, and CS1 for the uDP7401 counter void InitCS0CS1 ( ) { BSC.ABWCR.BIT.B0=1; // Select 8-bit access for CS0 BSC.ASTCR.BIT.B0=1; // 3-states access for CS0 BSC.WCR.BIT.W0=0; // 0 program wait state for CS0
BSC.ABWCR.BIT.B1=1; // Select 8-bit access for CS1 BSC.ASTCR.BIT.B1=1; // 3-states access for CS1 BSC.WCR.BIT.W1=0; // 0 program wait state for CS1
BSC.BRCR.BYTE=0xf0; // A20-A23 use as IO pins, no ext bus master BSC.BCR.BYTE=0x42; // Idle cycle inserted in consecutive R/W // cycles // Area division is 2Mbytes each P1DDR.BYTE = 0xFF; // Port 1 used as A0-A7 P2DDR.BYTE = 0xFF; // Port 2 used as A8-A15 P5DDR.BYTE = 0x0F; // Port 5:0-3 used as A16-A19 P8DDR.BYTE = 0xF8; // Set P8.4 as CS0, P8.3 as CS1, P8.2 as CS2 // This instruction affects other port-8 pins } char newEncoderData[2]; char encoderDiff[2]; char oldEncoderData[2]; int wheelErr[2]; void ReadEncoder() { newEncoderData[0] = (*(char*)0x200000); D0-D7 U/L* X*/Y CS* ResetX/Y IC-7401 D8-D15 P6.0 CS1* A0 A1 XA XB YA YB Figure 12.3 Interfacing 7401 to the 3062F Motor1 encoder signal channel A Motor1 encoder signal channel B Motor2 encoder signal channel A Motor2 encoder signal channel B A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 18 newEncoderData[1] = (*(char*)0x200001); encoderDiff[0] = ( newEncoderData[0] - oldEncoderData[0]); encoderDiff[1] = ( newEncoderData[1] - oldEncoderData[1]); wheelErr[0] -= encoderDiff[0]; wheelErr[1] -= encoderDiff[1]; oldEncoderData[0] = newEncoderData[0]; oldEncoderData[1] = newEncoderData[1]; } A very brief introduction to Hitachi H8/3062F and its development tools- Ng BengKiat 19 Appendix A Lab exercise Lab1 (day1) Flash the monitor program, H8MonCS0.mot. Download a program that says SARS in the middle of the terminal screen. Use gotoxy(x,y) functions to control the screen cursor position. Rebuild the SARS program in flash configuration and flash it. Lab2 (day2) Write a program to generate a square wave on any output port pin. Use a scope to monitor the output pin. Suggest a way to generate a square wave of certain frequency. Lab3 (day3) Write a program to display clock time on the terminal screen. The output should be: HH:MM:SS Verify that the clock can count correctly to 24 hours. Use the CPU clock to ensure accuracy. Lab4 (day3) Write a program to generate 3 PWM output at 20%, 50% and 90% duty cycle. Modify the program to allow user to vary the duty cycle on one of the output. Use U and D keys to increase or decrease the duty cycle. Appendix B B.1 About AlphaCentre 3062F Microcontroller Board A general purpose board with 256 Kbyte (16 bit wide) external RAM. Both RAM must be soldered. Jumper for selecting MCU operating mode 5 operating or On board programming Boot mode. See jumper (PGM/RUN). Red switch connected to IRQ0. Little black switch connected to REST. Figure B1 :Top and bottom views of the 3062 microcontroller board