R8C13 introduction - Where to Buy ? - Your own projects - An initial Report - Debugger KD30 - The R8C service page - Design Contest: the winners!
Developing your own projects from scratch
B. Kainka
The time has come : a low-cost processor circuit board with an R8C/13 microcontroller and the necessary software are available to readers of Elektor Electronics thanks to Glyn. There are three good reasons for using the Renesas R8C/Tiny family: first, it provides 16-bit computing power at a low price ; second, it comes with a free but nevertheless professional C compiler ; and third, no programming hardware is necessary, because code can be easily downloaded to flash memory via the RS232 port. Here we show you how to get started with this board.
The hardware, installation, and downloading ready-made programs were described in detail in the February 2006 issue of Elektor Electronics, along with editing previously written projects. However, one topic was not covered due to lack of space: starting your own projects from scratch.
Now, let’s see how you begin a completely new project. Start HEW, select the ‘Create a new project workspace’ option, and click ‘OK’ to confirm your choice (Figure 1). Alternatively, you can select ‘New Workspace’ in the ‘File’ menu.
The first thing to do is to specify the name of the new project (Figure 2). You can accept the rest of the default settings in this window.

Figure 1. Starting a new project

Figure 2. Project name and CPU type
Next you have to select the microcontroller type (Figure 3). Select the R8C/Tiny family. The following figures (Figures 4–8) show additional settings. You can accept the default values for all these settings.

Figure 3. Selecting the microcontroller type

Figure 4. A C project with no RTOS

Figure 5. C source code without additional libraries

Figure 6. Stack settings

Figure 7. Debugger selection

Figure 8. Information about the generated start files

Figure 9. The predefined project in HEW
The third file, which is the one that matters here, is Test1.c. It contains the actual source code for your project. It initially contains only an empty ‘main’ function. You can write your first lines of code here. However, you also need the include file sfr_r813.h, which contains the declarations of all the function registers of the R8C/13 microcontroller. You can find this file on the CD in the directory \SFR_Header\ R8C13, or you might find it in another project. Regardless of where you find it, copy it to your working directory (‘Test1’). You must also declare this file in your source code:
#include "sfr_r813.h";
Your first program should be fairly simple, such as incrementing the 8 bits of port p1. You will need a variable with a width of 8 bits. It should be called ‘i’ and declared as ‘char i;’. The type ‘char’ corresponds to a character or byte.
Instruction block are always enclosed by curly brackets ( { } ) in C programs. The ‘main’ function thus includes all the instructions between the outer set of brackets. ‘Void’ means ‘empty’, which means the function ‘void main(void)’ does not return a value and no value is passed to it. The ‘main’ function is the first function that is called after the program is started, even if there are other functions in the program.
#include "sfr_r813.h";
char i;
void main(void)
{
pd1 = 0x0FF;
while (1)
{
i++;
p1=i;
}
}
Listing 1. The initial program
The first instruction in the ‘main’ function performs a write operation to the Port P1 Direction Register (pd1). In contrast to microcontrollers such as the 8051 with its bidirectional ports, the data direction must be specified for each port bit as ‘write’(1) or ‘read’(0) for the R8C (as well as microcontrollers in the 6800 family). After a reset, all ports are inputs and thus have a high impedance. If you write a 1 to a bit in pd1, the corresponding port pin will be an output and will have a low impedance in both the low and high states. As a result, LEDs with series resistors to ground can be connected to output port pins (in contrast to the 8051).
The first instruction is ‘pd1=0x0FF;’. As you may have already noticed, each instruction ends with a semicolon, just as in Pascal and Delphi. The instruction ‘pd1=0x0FF;’ could also be written as ‘pd1=255;’. In the hexadecimal notation used here, a digit can be located after the ‘x’, but not a letter. Consequently, ‘0x1E’ and ‘0x00’ are correct but ‘0xFF’ is not, and it will generate an error message. That’s why an extra zero has to be inserted here: ‘0x0FF’. Another thing that might form a small stumbling block for C beginners is that the difference between upper- and lower-case characters is important and must be observed correctly in the names of all variables and functions. For instance, if you write ‘PD1’ the compiler will have no idea what you meant and will issue an error message. In file sfr_r813.h, the name of this control byte is thus written in lower case: pd1.
The actual outputs are located in the endless loop ‘while (1) { instructions }’. Here ‘1’ stands for ‘true’. It could be replaced by another expression that can return the value ‘true’ or ‘false’. This instruction could be paraphrased as follows: ‘as long as a 1 is still a 1, execute the instructions between the curly brackets’.
There are only two lines of code in the instruction block, which will be executed forever or until the next reset. The instruction ‘i++;’ increments the content of variable ‘i’ by 1. In Basic, you would have written ‘i=i+1’ here. The second instruction is ‘p1=i;’. It outputs the content of variable ‘i’ to the port register. When that happens, the port bits assume new output states.
Now you should use ‘Build All’ to compile the program in the Release configuration. Next, transfer it to the microcontroller and run it. You will see constantly changing states on port 1. P1_0 will toggle at approximately 180 Hz, P1_1 at 90 Hz, P1_2 at 45 Hz, and P1_7 at only 0.7 Hz. Are they really so slow? They are indeed – those figures are not MHz or kHz, but just plain Hz. That’s not especially surprising, because the program you just wrote runs with a processor clock rate of only 125 kHz. To change that, switch to the full 20 MHz at the beginning of the program. After that, P1_0 will change states every 2.2 µs.
/***********************************************************************/
/* */
/* FILE :test1.c */
/* DATE :Wed, Oct 12, 2005 */
/* DESCRIPTION :Main Program */
/* CPU TYPE :Other */
/* */
/* This file is generated by Renesas Project Generator (Ver.4.0). */
/* */
/***********************************************************************/
#include "sfr_r813.h";
char i;
void main(void)
{
/*-------------------------------------------------
- Change on-chip oscillator clock to Main clock -
-------------------------------------------------*/
prc0 = 1; /* Protect off */
cm13 = 1; /* Xin Xout */
cm15 = 1; /* XCIN-XCOUT drive capacity select bit : HIGH */
cm05 = 0; /* Xin on */
cm16 = 0; /* Main clock = No division mode */
cm17 = 0;
cm06 = 0; /* CM16 and CM17 enable */
asm("nop"); /* Waiting for stable of oscillation */
asm("nop");
asm("nop");
asm("nop");
ocd2 = 0; /* Main clock change */
prc0 = 0; /* Protect on */
pd1 = 0x0FF;
while (1)
{
i++;
p1=i;
}
}
Listing 2. The program with code to switch to the 20-MHz oscillator