R8C-MS [RENESAS]

Sample Program: Musical Scales; 示例程序:音阶
R8C-MS
型号: R8C-MS
厂家: RENESAS TECHNOLOGY CORP    RENESAS TECHNOLOGY CORP
描述:

Sample Program: Musical Scales
示例程序:音阶

文件: 总22页 (文件大小:117K)
中文:  中文翻译
下载:  下载PDF数据表文档文件
APPLICATION NOTE  
R8C/Tiny Series  
Sample Program: Musical Scales  
1. Abstract  
The musical-scale sample program uses the following functions:  
• Timer X (timer mode)  
• Timer Z (programmable waveform generation mode)  
• A-D converter (repeat mode)  
2. Introduction  
The example described in this application note is intended for use in the R8C/17 group of  
microcomputers.  
This program can also be used when operating other microcomputers within the R8C/Tiny, provided  
they have the same SFR (Special Function Registers) as the R8C/17 microcomputers. However, since  
it is possible that some functions of the R8C/Tiny series will have been altered for functional  
enhancements, etc., please be sure to obtain the SFR header file from the Renesas Web site shown  
below.  
http://www.renesas.com/en/r8ctiny  
Operation (1) The musical-scale sample program has the following two separate modes:  
• Standby mode  
• Musical scale output mode  
SW4(INT0)  
Key pressed  
Musical scale output mode  
Standby mode  
After reset  
(2) After reset, the microcomputer goes to standby mode.  
(3) If SW4 (INT0) is pressed during standby mode, the microcomputer goes to musical scale  
output mode.  
(4) If SW4 (INT0) is pressed during musical scale output mode, the microcomputer goes to  
standby mode.  
(5) In musical scale output mode, the microcomputer outputs the tones of a diatonic scale  
repeatedly from the buzzer in order of lower-key do re mi fa sol la si →  
higher-key do no sound (500 ms) higher-key do si la sol fa mi re →  
lower-key do no sound (500 ms) lower-key do re mi and so on.  
(6) Tones of a diatonic scale are changed every second. The frequencies (Hz) of tones are  
listed in Table 1.  
Table 1:  
Lower-key  
do (C)  
Higher-key  
do (C)  
Scale  
re (D)  
mi (E)  
fa (F)  
sol (G)  
la (A)  
si (B)  
523.25  
587.32  
659.25  
698.45  
783.98  
880.00  
987.76  
1046.50  
Frequency (Hz)  
(7) The buzzer output duty cycle is determined based on the A-D value that is obtained as  
valid data as described below in (9). The output sound volume changes as does the duty  
cycle. The duty cycles are listed in Table 2.  
Table 2:  
00-1F 20-3F  
25.00 33.75  
40-5F  
42.50  
60-7F  
50.00  
80-9F  
56.25  
A0-BF  
62.50  
C0-DF  
68.75  
E0-FF  
75.00  
Valid A-D value  
Duty cycle (%)  
(8) The analog input is converted to digital quantity by using a control (VR1). The A-D  
input is taken into the device in repeat mode, with the P10/AN8 pin used for it.  
(9) For the A-D input, the A-D converted value on P10/AN8 is sampled four times at 10 ms  
intervals, the average value of which is obtained as valid data. The calculation method  
is shown below.  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 1 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
1. Each sampled A-D value is added to the A-D work area.  
2. When sampled four times, the value in the A-D work area has its 2 low-order bits  
masked with 0.  
3. The 8 high-order bits of the value in the A-D work area is made the average value.  
(10) Key input is sampled at 10 ms intervals, and is confirmed to have been entered when  
sampled low three times consecutively. (Chattering elimination)  
(11) The key input SW4 (INT0) is active-low. (When low, the key is pressed; when high, not  
pressed.)  
(12) The LED indications during standby mode and musical scale output mode are listed in  
Table 3.  
Table 3:  
LED1 (red)  
LED2 (green)  
LED3 (green)  
LED4 (green)  
During standby mode  
During musical scale  
output mode  
On  
Off  
Off  
Off  
Off  
On  
Off  
On  
(13) The buzzer on/off signal is output from P13 (TZOUT) using timer-Z programmable  
waveform mode.  
(14) The timer Z register settings used for the buzzer output are listed in Tables 4.1 and 4.2.  
(The set values in Tables 4.1 and 4.2 are set to be close to those in Tables 1 and 2.)  
Table 4.1:  
Scale  
Prescaler Set time  
Timer total  
(tzpr + tzsc)  
Output time (ms)  
(tzpr + tzsc)  
Output  
frequency (Hz)  
value  
(µs)  
Lower-key do (C)  
re (D)  
mi (E)  
fa (F)  
sol (G)  
60  
53  
47  
45  
40  
36  
32  
30  
24  
21.2  
18.8  
18  
80  
80  
80  
80  
80  
80  
80  
80  
1.92 520.8333  
1.696  
1.504  
1.44  
589.6226  
664.8936  
694.4444  
781.2500  
868.0556  
976.5625  
1041.6667  
16  
1.28  
la (A)  
si (B)  
14.4  
12.8  
12  
1.152  
1.024  
0.96  
Higher-key do (C)  
Table 4.2:  
00-  
1F  
20  
60  
20-  
40-  
60-  
7F  
40  
40  
80-  
9F  
45  
35  
A0-  
BF  
50  
30  
C0-  
DF  
55  
25  
E0-  
FF  
60  
20  
Timer Z value/  
A-D value range  
Primary (tzpr)  
Secondary (tzsc)  
3F  
27  
53  
5F  
34  
46  
00-  
1F  
20-  
3F  
40-  
5F  
60-  
7F  
80-  
9F  
A0-  
BF  
C0-  
DF  
1.320  
E0-  
FF  
Scale/A-D value range/  
output time ms  
tzpr  
tzsc  
tzpr  
tzsc  
tzpr  
tzsc  
tzpr  
tzsc  
tzpr  
tzsc  
tzpr  
tzsc  
tzpr  
tzsc  
tzpr  
tzsc  
0.480  
1.440  
0.424  
1.272  
0.376  
1.128  
0.360  
1.080  
0.320  
0.960  
0.288  
0.864  
0.256  
0.768  
0.240  
0.720  
0.648  
1.272  
0.572  
1.124  
0.508  
0.996  
0.486  
0.954  
0.432  
0.848  
0.389  
0.763  
0.346  
0.678  
0.324  
0.636  
0.816  
1.104  
0.721  
0.975  
0.639  
0.865  
0.612  
0.828  
0.544  
0.736  
0.490  
0.662  
0.435  
0.589  
0.408  
0.552  
0.960  
0.960  
0.848  
0.848  
0.752  
0.752  
0.720  
0.720  
0.640  
0.640  
0.576  
0.576  
0.512  
0.512  
0.480  
0.480  
1.080  
0.840  
0.954  
0.742  
0.846  
0.658  
0.810  
0.630  
0.720  
0.560  
0.648  
0.504  
0.576  
0.448  
0.540  
0.420  
1.200  
1.440  
Lower-key do (C)  
Prescaler 48  
re (D)  
0.720  
1.060  
0.636  
0.940  
0.564  
0.900  
0.540  
0.800  
0.480  
0.720  
0.432  
0.640  
0.384  
0.600  
0.360  
0.600  
1.166  
0.530  
1.034  
0.470  
0.990  
0.450  
0.880  
0.400  
0.792  
0.360  
0.704  
0.320  
0.660  
0.300  
0.480  
1.272  
0.424  
1.128  
0.376  
1.080  
0.360  
0.960  
0.320  
0.864  
0.288  
0.768  
0.256  
0.720  
0.240  
Prescaler 43  
mi (E)  
Prescaler 38  
fa (F)  
Prescaler 36  
sol (G)  
Prescaler 32  
la (A)  
Prescaler 28  
si (B)  
Prescaler 25  
Higher-key do (C)  
Prescaler 24  
(15) If the A-D value range varies during musical scale output, the registers are set up  
newly again according to the new A-D value range.  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 2 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
3. About the Program  
3.1 Timer X (Timer Mode)  
The timer X (timer mode) is used to maintain a constant main loop period.  
The set value of the timer X is shown below.  
100 µs  
Timer X  
Prescaler X  
1/8  
20MHz  
Main loop, 10 ms  
Set value = (100 – 1)  
Set value = (250 – 1)  
Settings of the Timer X Mode Register  
b7  
b0  
Timer X Mode Register [address 008B16] TXMR  
0 0 0 0 1 0 0 0  
Selects timer mode  
INT1/CNTR0 polarity select bit  
Timer X count start flag  
Set to “0” in timer mode.  
Set to “1” when count starts  
Other than pulse period measurement mode  
Set to “0” in timer mode.  
Settings of the Timer Count Source Setup Register  
b7  
b0  
Timer Count Source Setup Register [address 008E16] TCSS  
0 0 0 1 0 0 0 1  
Timer X count source Set 01: 1/8  
Reserved bit. Set to “0”.  
Timer Z count source  
Reserved bit. Set to “0”.  
When the respective registers are set up as shown above, the timer X interrupt request bit is  
cyclically set at 10 ms intervals.  
Before the main processing, the program checks the timer X interrupt request bit to see if 10 ms has  
elapsed.  
If 10 ms has elapsed, the program clears the timer X interrupt request bit and executes the main  
processing.  
If 10 ms has not elapsed yet, the program waits until the timer X interrupt request bit is set.  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 3 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
3.2 Timer Z (Programmable Waveform Generation Mode)  
The timer Z (programmable waveform generation mode) is used to sound a buzzer.  
Musical scale output is accomplished by setting up the registers as shown below. The output level is  
set by using the timer Z output level latch (TZOPL) in the Timer Z Waveform Output Control Register  
(PUM). Setting the timer Z output level latch (TZOPL) to 0 causes the device to output a high during  
the primary period, a low during the secondary period, and a low when the timer is inactive.  
Settings of the Timer Z Waveform Output Control Register  
b7  
b0  
Timer Z Waveform Output Control Register [address 008416] PUM  
Reserved bit. Set to “0”.  
0 0 0 0 0 0 0 0  
Timer Z output level latch  
High during primary period, low during secondary period,  
and low when the timer is inactive  
INT0 pin one-shot trigger control bit  
INT0 pin one-shot trigger polarity select bit  
When the timer Z starts counting, the device outputs a high during the primary period and a low  
during the secondary period from P13 (TZOUT), and when the timer Z stops counting, the device  
outputs a low.  
The timer Z set values are shown below.  
See Table 4  
Timer Z primary  
Prescaler Z  
Buzzer output =  
high, see Table 4  
1/8  
20MHz  
Set value = see Table 4  
Set value = see Table 4  
Timer Z secondary  
Buzzer output =  
low, see Table 4  
Set value = see Table 4  
* The prescaler value, timer Z primary value, and timer Z secondary value are changed depending on tones of a diatonic  
scale. Please see Table 4.  
Settings of the Timer Count Source Setup Register  
b7  
b0  
Timer Count Source Setup Register [address 008E16] TCSS  
0 0 0 1 0 0 0 1  
Timer X count source Set 01: 1/8  
Reserved bit. Set to “0”.  
Timer Z count source Set 01: 1/8  
Reserved bit. Set to “0”.  
Settings of the Timer Z Mode Register  
b7  
b0  
Timer Z Mode Register [address 008016] TZMR  
0 1 0 1  
0 0 0 0  
Reserved bit. Set to “0”.  
01: Programmable waveform generation mode  
Set to “1” in programmable waveform generation mode.  
Timer Z count start flag  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 4 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
3.3 A-D Converter (Repeat Mode)  
The musical-scale sample program uses the A-D converter in repeat mode.  
The A-D register is read out synchronously with the main period (10 ms), and an average of four  
reads is obtained as valid data. The valid A-D value is examined to determine one of the 8 divided  
areas to which it belongs, and the data value of that area is set in the timer Z (timer Z  
primary/secondary). That way, the duty cycle is set.  
The analog input pin, resolution, operating clock, etc. are set by setting up the SFR (Special Function  
Registers) shown below. For repeat mode, select 8 bits resolution.  
A-D conversion begins at the same time the A-D conversion flag is set to “1”, and stops when the A-D  
conversion flag is set to “0”.  
Settings of A-D Control Register 0  
b0  
b7  
A-D Control Register 0 [00D616] ADCON0  
1 0 0 1 1 1 0 0  
Analog input pin select bits Set 100: AN8  
A-D mode select bit, “1” = repeat mode selected  
Selects port P1 group (AN8–AN11)  
Started by software trigger (ADST bit)  
A-D conversion start flag, “1” = start  
Frequency select bit, “1” = divided-by-2 fAD selected  
Settings of A-D Control Register 1  
b7  
b0  
A-D Control Register 1 [00D716] ADCON1  
0 0 1 1 0 0 0 0  
Reserved bit. Set to “0”.  
8/10 bit select bit, “0” = 8 bits selected. For repeat mode, select 8 bits resolution.  
Frequency select bit 1, “1” = fAD selected  
Vref connection bit, “1” = Vref connection selected  
Reserved bit. Set to “0”.  
Settings of A-D Control Register 2  
b7  
b0  
A-D Control Register 2 [00D416] ADCON2  
0 0 0 0  
A-D conversion method select bit, “0” = without sample-and-hold selected  
Reserved bit. Set to “0”.  
No functions assigned  
3.4 Chattering Elimination  
To protect the key input against chattering noise, SW4 is read multiple times to confirm that the  
detected input levels are the same. In the musical-scale sample program, key input is sampled at 10  
ms intervals, and is confirmed to have been entered when sampled low three times consecutively.  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 5 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
4. Flowchart  
4.1 Initial Settings and Main Loop  
Reset  
asm("FCLR I");  
prcr = 1;  
; Disables interrupts  
; Removes protection of the system control registers  
; XIN-XOUT pin  
cm13 = 1;  
cm15 = 1;  
cm05 = 0;  
cm16 = 0;  
cm17 = 0;  
cm06 = 0;  
asm("nop");  
asm("nop");  
asm("nop");  
asm("nop");  
ocd2 = 0;  
; Selects XIN-XOUT drive capability; set to HIGH  
; Main clock oscillation  
; Main clock not divided  
;
; Enables main clock division CM16, CM17  
; Wait until stabilizes  
; Wait until stabilizes  
; Wait until stabilizes  
; Wait until stabilizes  
; Selects the main clock  
prcr = 0;  
; Protects the system control registers  
; Initializes the SFR (port initialization and timer setup)  
Initialize SFR  
SFR_INITIAL  
asm("FSET I");  
; Enables interrupts  
No  
; Wait for timer X request  
Main period elapsed (10 ms)?  
Yes  
; Clears the timer X request flag  
ir_txic = 0;  
; Refreshes the port direction register  
Reset SFR  
SFR_REF  
; Sets the port for output (LED and buzzer)  
; Sets the port for input (key input processing)  
Port output  
PORT_OUT  
Port input  
PORT_IN  
; Mode processing  
Mode processing  
(Mode processing within refresh mode)  
; Soft timer (10 ms based timer value subtraction)  
MODE_FUNC  
Soft timer processing  
STIMER  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 6 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
4.2 SFR Initialization  
sfr_init  
; Initializes port P1  
; Initializes port P4  
p1 = 0x16;  
p4 = 0;  
tcss = 0x11;  
prex = 250-1;  
tx = 100-1;  
txs = 1;  
; Timer X = divided-by-8, timer Z = divided-by-8  
; Prescaler X = 250  
; Timer X = 100  
; Timer X starts counting  
tzmr = 0x50;  
; Timer Z mode = programmable waveform generation mode  
; Primary period = high, secondary period = low, timer stopped = low  
; Timer Z stops counting  
pum = 0;  
tzs = 0;  
adcon1 = 0x30;  
adcon0 = 0x9C;  
adst = 1;  
; 8-bit A-D sampling  
; Selects divided-by-2 fAD and reads A-D0  
; A-D sampling starts  
Refresh SFR  
; Refreshes the SFR  
SFR_REF  
RTS  
4.3 SFR Refresh  
sfr_ref  
pd1 = 0x1E;  
pd4 = 0;  
; Initializes/refreshes the port P1 direction register  
; Initializes/refreshes the port P4 direction register  
RTS  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 7 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
4.4 Port Output  
port_out  
No  
mode = 0  
p1 = 0x10;  
RTS  
; The red LED turns off and others  
stay on  
Yes  
p1 = 0x06;  
; The red LED turns on and others turn off  
; Buzzer output mode up-count  
; Initializes buzzer output mode  
;
md_bz_down = 0;  
md_buzzer = 1;  
tm_buzzer = 0;  
tzs = 0;  
; Stops timer Z  
RTS  
; Timer Z prescaler value table:  
bz_preztable[8] = {60,53,47,45,40,36,32,30};  
Timer Z primary value table:  
bz_tzsctable[8] = {20,27,34,40,45,50,55,60};  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 8 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
4.5 Port Input  
port_in  
ad_sum + ad;  
ad_cnt + 1;  
; Adds sampled A-D to the A-D work area  
; Increments A-D sampling count by 1  
No  
ad_cnt = 4  
; Checks whether A-D is sampled 4 times  
Yes  
1
ad_fix = ad_sum >>2;  
ad_cnt = 0;  
; Deletes 2 low-order bits, 8 high-order bits effective  
; Clears A-D sampling count  
; Clears A-D work area  
ad_sum = 0;  
i = 0;  
; Clears checked count  
; A-D area determination processing  
No  
i < 8  
Yes  
Yes  
adfunc_table[i] >= ad_fix  
No  
No  
ad_level != i  
i + 1;  
; Determines whether to  
change A-D area  
Yes  
ad_level = i;  
; Changes A-D area  
1
A-D threshold value table:  
adfunc_table[8]=  
{0x1F,0x3F,0x5F,0x7F,0x9F,0xBF,0xDF,0xFF};  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 9 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
1
i = 1;  
No  
SW4 = SW_ON  
Yes  
i = 0;  
sw4_bit << 1;  
sw4_bit | i;  
; Reserves storage in which to save the input data  
; Saves the input data  
sw4_bit & 7;  
; Deletes 5 high-order bits  
No  
sw4_bit = 0 | sw4_bit = 7  
Yes  
Yes  
sw4_bit = 0  
No  
No  
mode = 1 & fixsw4 = 1  
Yes  
No  
mode = 0 & fixsw4 = 1  
Yes  
; Changes modes  
mode = 0;  
mode = 1;  
; Valid data = high  
fixsw4 = 1;  
fixsw4 = 0;  
; Valid data = low  
RTS  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 10 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
4.6 Mode Processing  
mode_func  
Yes  
No  
mode = 0  
No  
mode = 1  
mode = 0;  
tzs = 0;  
; Refreshes mode  
; Stops timer Z  
Yes  
mode = 1;  
No  
tm_buzzer <= 0  
Yes  
RTS  
Yes  
md_buzzer = 0  
md_bz_down = 0;  
; Mode up-count  
No  
tm_buzzer = 50;  
tzs = 0;  
; No-sound timer 500 ms  
; Stops timer Z  
Yes  
md_buzzer = 9  
md_bz_down = 1;  
tm_buzzer = 50;  
tzs = 0;  
; Mode down-count  
; No-sound timer 500 ms  
; Stops timer Z  
No  
No  
md_buzzer > 9  
tzs = 1;  
; Resets mode  
Yes  
i = ad_level;  
md_buzzer = 0;  
work = bz_tzsctable[i];  
tzpr = work - 1;  
work = 80 - work;  
tzsc = work - 1;  
i = md_buzzer - 1;  
work = bz_preztable[i];  
prez = work - 1;  
tzs = 0;  
tzs = 0;  
; Stops timer Z  
tzs = 1;  
tm_buzzer = 100;  
2
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 11 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
2
; Checks mode up-count/down-count instruction  
Yes  
No  
md_bz_down = 0  
No  
md_buzzer - 1;  
md_buzzer + 1;  
; Changes mode  
md_buzzer = 0  
Yes  
md_buzzer = 0;  
RTS  
4.7 Soft Timer Processing  
stimer  
No  
; Checks for underflow  
tm_buzzer != 0  
Yes  
; Buzzer-on time = 0  
tm_buzzer - 1;  
RTS  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 12 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
5. Sample Program  
/************************************************************************************  
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
File Name : ad_onkai.h  
Contents  
: definition of R8C/17 Group SFR  
Copyright, 2004 RENESAS TECHNOLOGY CORPORATION  
AND RENESAS SOLUTIONS CORPORATION  
Version  
note  
: 1.00  
:
************************************************************************************/  
/* Definition of RAM area */  
char mode  
= 0;  
/* Mode number */  
char sw4_bit  
char fixsw4  
char ad_cnt  
char md_bz_down  
= 0;  
= 1;  
= 0;  
= 0;  
/* Input SW4 data */  
/* Input SW4 Settlement data */  
/* Number of A-D value sampling */  
/* Buzzer output counting direction */  
unsigned int md_buzzer= 0;  
unsigned int tm_buzzer= 0;  
unsigned int ad_fix  
unsigned int ad_sum  
unsigned int ad_level = 0;  
/* Buzzer output mode */  
/* Buzzer control timer */  
/* A-D fixed data */  
/* A-D calculation result data */  
/* Output range */  
= 0;  
= 0;  
/* A-D table */  
unsigned int adfunc_table[8] = {0x1F,0x3F,0x5F,0x7F,0x9F,0xBF,0xDF,0xFF};  
/* Prescaler Z table */  
unsigned int bz_preztable[8] = {60,53,47,45,40,36,32,30};  
/* timer Z secondary register table */  
unsigned int bz_tzsctable[8] = {20,27,34,40,45,50,55,60};  
/* Declaration of function prototype */  
extern void sfr_init(void);  
extern void sfr_ref(void);  
extern void port_out(void);  
extern void port_in(void);  
extern void mode_func(void);  
extern void stimer(void);  
/* Initial setting of SFR registers */  
/* Refresh of SFR registers */  
/* Port output */  
/* Port input */  
/* Mode processing */  
/* Software timer */  
/* Definition of base section */  
#define SW_ON  
#define SW_OFF  
#define LOW  
0
1
0
1
#define HIGH  
/* Definition of port */  
#define GREEN_LED2  
#define GREEN_LED1  
#define GREEN_LED0  
p1_1  
p1_2  
p1_3  
#define RED_LED  
#define SW3  
#define SW4  
#define BUZZER  
p1_4  
p1_0  
p4_5  
p1_3  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 13 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
/************************************************************************************  
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
File Name : main.c  
Contents  
: definition of R8C/17 Group SFR  
Copyright, 2004 RENESAS TECHNOLOGY CORPORATION  
AND RENESAS SOLUTIONS CORPORATION  
Version  
note  
: 1.00  
:
************************************************************************************/  
#include "sfr_r817.h"  
#include "ad_onkai.h"  
*/  
/* Definition of the R8C/17 SFR */  
/* Definition of processing for the musical scale program  
main(){  
asm("FCLR I");  
prcr = 1;  
cm13 = 1;  
cm15 = 1;  
cm05 = 0;  
cm16 = 0;  
cm17 = 0;  
cm06 = 0;  
asm("nop");  
asm("nop");  
asm("nop");  
asm("nop");  
ocd2 = 0;  
prcr = 0;  
/* Interrupt disable */  
/* Protect off */  
/* X-in X-out */  
/* XCIN-XCOUT drive capacity select bit : HIGH */  
/* X-in on */  
/* Main clock = No division mode */  
/* CM16 and CM17 enable */  
/* Main clock change */  
/* Protect on */  
sfr_init();  
/* Initial setting of SFR registers */  
while(1){  
/* Main processing */  
/* Interrupt enable */  
asm("FSET I");  
while(ir_txic == 0){} /* Main cycle 10ms */  
ir_txic = 0;  
sfr_ref();  
port_out();  
port_in();  
mode_func();  
stimer();  
/* Refresh of SFR registers */  
/* Port output */  
/* Port input */  
/* Mode processing */  
/* Software timer */  
}
}
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 14 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
/************************************************************************************  
Name:  
Parameters: None  
Returns: None  
sfr_init  
Description: Initial setting of SFR registers  
************************************************************************************/  
void sfr_init(void){  
/* Setting port registers */  
p1 = 0x16;  
p4 = 0;  
/* p17-14 = H(Led 4.3.2.1) p13 = L(Buzzer) */  
tcss = 0x11;  
/* division = X:1/8,Z:1/8 */  
/* Setting main cycle timer */  
/* 20MHz* 1/8 * 250 * 100 =10ms */  
prex = 250 - 1;  
tx = 100 - 1;  
txs = 1;  
/* Setting Prescaler X register */  
/* Setting timer X register */  
/* Timer X count start flag = start */  
/* Setting Buzzer output timer */  
tzmr = 0x50;  
pum = 0;  
tzs = 0;  
/* Timer Z mode = Programmable waveform generation mode */  
/* Timer Z waveform output control register */  
/* Timer Z count start flag = stop */  
/* A-D operation mode = Repeat mode */  
adcon1 = 0x30;  
adcon0 = 0x9C;  
adst = 1;  
/* 8/10-bit mode select bit = 8-bit mode */  
/* fAD/2 and AN8 is selected */  
/* A-D conversion started */  
sfr_ref();  
}
/************************************************************************************  
Name: sfr_ref  
Parameters: None  
Returns: None  
Description: Refresh of SFR registers  
************************************************************************************/  
void sfr_ref(void){  
/* Setting port direction registers */  
pd1 = 0x1E;  
pd4 = 0;  
}
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 15 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
/************************************************************************************  
Name:  
Parameters: None  
Returns: None  
port_out  
Description: Port output  
************************************************************************************/  
void port_out(void){  
if (mode == 0){  
/* LED output processing */  
p1 = 0x06;  
/* Buzzer output processing */  
md_bz_down = 0;  
md_buzzer = 1;  
tm_buzzer = 0;  
tzs = 0;  
/* Buzzer output counting direction = increment counting */  
/* Buzzer output mode = C(1) */  
/* Buzzer control timer = 1 second */  
/* Timer Z stops Counting */  
}else{  
/* LED output processing */  
p1 = 0x10;  
}
}
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 16 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
/************************************************************************************  
Name:  
Parameters: None  
Returns: None  
port_in  
Description: Port input  
************************************************************************************/  
void port_in(void){  
unsigned int i;  
/* A-D input */  
ad_sum = ad_sum + ad;  
ad_cnt = ad_cnt + 1;  
if (ad_cnt == 4){  
/* A-D calculation result data + picking A-D */  
/* Number of sampling become 4 */  
ad_fix = ad_sum >> 2; /* 4 times shift to the right */  
ad_cnt = 0;  
ad_sum = 0;  
/* picking A-D space clear */  
/* A-D Range settlement processing */  
i = 0;  
while (i < 8){  
if (adfunc_table[i] >= ad_fix){break;}  
i = i + 1;  
/* Output range is settled */  
}
if (ad_level != i){  
ad_level = i;  
}
/* Chenge Output range */  
}
/* Determination of input level SW4 */  
i = 1;  
if (SW4 == SW_ON)i = 0;  
sw4_bit = sw4_bit << 1;  
sw4_bit = sw4_bit | i;  
sw4_bit = sw4_bit & 7;  
/* Now determination SW4 */  
/* Check pulses matching a trigger input level 3 times */  
if (sw4_bit == 0 || sw4_bit == 7){ /* Determinate input SW4 */  
if (sw4_bit == 0){  
/* Mode change */  
if (mode == 1 & fixsw4 == 1){  
mode = 0;  
}else{  
if (mode == 0 & fixsw4 == 1){  
mode = 1; /* Setting the buzzer output mode */  
/* Setting the standby mode */  
}
}
fixsw4 = 0;  
}else{  
fixsw4 = 1;  
/* Input on */  
/* Input off */  
}
}
}
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 17 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
/************************************************************************************  
Name:  
Parameters: mode  
Returns: None  
mode_func  
Description: Mode processing  
************************************************************************************/  
void mode_func(void){  
unsigned int i,work;  
switch(mode){  
case 1:  
mode = 1;/* Set mode of the buzzer output mode */  
if (tm_buzzer <= 0){  
/* Buzzer output mode change */  
if (md_buzzer == 0){  
tm_buzzer = 50;  
/* Buzzer control timer = 0.5 second */  
md_bz_down =0;/*Buzzer output counting direction=increment counting */  
tzs = 0;  
/* Timer Z stops Counting */  
}else if (md_buzzer == 9){  
tm_buzzer = 50;  
/* buzzer control timer = 0.5 second */  
md_bz_down =1;/*Buzzer output counting direction=decrement counting */  
tzs = 0;  
}else if (md_buzzer > 9){  
md_buzzer = 0;  
tzs = 0;  
/* Timer Z stops Counting */  
/* Buzzer output mode = clear */  
/* Timer Z stops Counting */  
}else{  
/* output the musical scale now */  
tzs = 1;  
/* Timer Z starts Counting */  
i = ad_level;  
work = bz_tzsctable[i];  
tzpr = work - 1;  
work = 80 - work;  
tzsc = work - 1;  
i = md_buzzer - 1;  
work = bz_preztable[i];  
prez = work - 1;  
tzs = 0;  
/* setting of timer Z Primary register */  
/* setting of timer Z secondary register */  
/* setting of Prescaler Z register */  
/* Timer Z stops Counting */  
tzs = 1;  
tm_buzzer = 100;  
/* Timer Z starts Counting */  
/* buzzer control timer = 1 second */  
}
if (md_bz_down == 0){  
/* increment counting action */  
md_buzzer = md_buzzer + 1; /* increment counting of the timer */  
}else{  
/* decrement counting action */  
md_buzzer = md_buzzer - 1; /* decrement counting the timer */  
if (md_buzzer == 0){md_buzzer = 0;}  
}
}
break;  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 18 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
default:  
mode = 0;  
/* Set mode of the standby mode */  
/* Timer Z count start flag = stop */  
tzs = 0;  
break;  
}
}
/************************************************************************************  
Name: stimer  
Parameters: None  
Returns: None  
Description: Software timer  
************************************************************************************/  
void stimer(void){  
/* Countdown of buzzer control timer */  
if (tm_buzzer != 0){tm_buzzer = tm_buzzer - 1;}  
}
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 19 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
6. Reference Documents  
Datasheet  
R8C/Tiny Series Datasheet  
( For the most current version, please visit Renesas Technology Home Page )  
Hardware Manual  
R8C/Tiny Series Hardware Manual  
( For the most current version, please visit Renesas Technology Home Page )  
7. Home Page and Support Information Window  
Renesas Technology Home Page  
http://www.renesas.com/  
M16C Family MCU Technical Support Information Window  
support_apl@renesas.com  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 20 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
REVISION HISTORY  
Description  
Summary  
Rev.  
1.00  
2.00  
Date  
Page  
-
Sep 09, 2003  
Jul 1, 2004  
First edition issued  
For R8C/Tiny Series  
ALL  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 21 of 22  
R8C/Tiny Series  
Sample Program: Musical Scales  
Keep safety first in your circuit designs!  
Notes regarding these mate
Keep safety first in your circuit designs!  
1. Renesas Technology Corporation puts the maximum effort into making semiconductor products  
better and more reliable, but there is always the possibility that trouble may occur with them. Trouble  
with semiconductors may lead to personal injury, fire or property damage.  
Remember to give due consideration to safety when making your circuit designs, with appropriate  
measures such as (i) placement of substitutive, auxiliary circuits, (ii) use of nonflammable material or  
(iii) prevention against any malfunction or mishap.  
Notes regarding these materials  
1. These materials are intended as a reference to assist our customers in the selection of the Renesas  
Technology Corporation product best suited to the customer's application; they do not convey any  
license under any intellectual property rights, or any other rights, belonging to Renesas Technology  
Corporation or a third party.  
2. Renesas Technology Corporation assumes no responsibility for any damage, or infringement of any  
third-party's rights, originating in the use of any product data, diagrams, charts, programs,  
algorithms, or circuit application examples contained in these materials.  
3. All information contained in these materials, including product data, diagrams, charts, programs and  
algorithms represents information on products at the time of publication of these materials, and are  
subject to change by Renesas Technology Corporation without notice due to product improvements  
or other reasons. It is therefore recommended that customers contact Renesas Technology  
Corporation or an authorized Renesas Technology Corporation product distributor for the latest  
product information before purchasing a product listed herein.  
The information described here may contain technical inaccuracies or typographical errors.  
Renesas Technology Corporation assumes no responsibility for any damage, liability, or other loss  
rising from these inaccuracies or errors.  
Please also pay attention to information published by Renesas Technology Corporation by various  
means, including the Renesas Technology Corporation Semiconductor home page  
(http://www.renesas.com).  
4. When using any or all of the information contained in these materials, including product data,  
diagrams, charts, programs, and algorithms, please be sure to evaluate all information as a total  
system before making a final decision on the applicability of the information and products. Renesas  
Technology Corporation assumes no responsibility for any damage, liability or other loss resulting  
from the information contained herein.  
5. Renesas Technology Corporation semiconductors are not designed or manufactured for use in a  
device or system that is used under circumstances in which human life is potentially at stake.  
Please contact Renesas Technology Corporation or an authorized Renesas Technology Corporation  
product distributor when considering the use of a product contained herein for any specific  
purposes, such as apparatus or systems for transportation, vehicular, medical, aerospace, nuclear,  
or undersea repeater use.  
6. The prior written approval of Renesas Technology Corporation is necessary to reprint or reproduce  
in whole or in part these materials.  
7. If these products or technologies are subject to the Japanese export control restrictions, they must  
be exported under a license from the Japanese government and cannot be imported into a country  
other than the approved destination.  
Any diversion or reexport contrary to the export control laws and regulations of Japan and/or the  
country of destination is prohibited.  
8. Please contact Renesas Technology Corporation for further details on these materials or the  
products contained therein.  
REJ05B0273-0200Z/Rev.2.00  
July 2004  
Page 22 of 22  

相关型号:

R8C/10

SINGLE-CHIP 16-BIT CMOS MICROCOMPUTER
RENESAS

R8C/11

SINGLE-CHIP 16-BIT CMOS MICROCOMPUTER
RENESAS

R8C/12

SINGLE-CHIP 16-BIT CMOS MICROCOMPUTER
RENESAS

R8C/13

SINGLE-CHIP 16-BIT CMOS MICROCOMPUTER
RENESAS

R8C/14

16-BIT SINGLE-CHIP MICROCOMPUTER M16C FAMILY / R8C/Tiny SERIES
RENESAS

R8C/15

16-BIT SINGLE-CHIP MICROCOMPUTER M16C FAMILY / R8C/Tiny SERIES
RENESAS

R8C/16

16-BIT SINGLE-CHIP MICROCOMPUTER M16C FAMILY / R8C/Tiny SERIES
RENESAS

R8C/17

16-BIT SINGLE-CHIP MICROCOMPUTER M16C FAMILY / R8C/Tiny SERIES
RENESAS

R8C/18

SINGLE-CHIP 16-BIT CMOS MCU
RENESAS

R8C/19

SINGLE-CHIP 16-BIT CMOS MCU
RENESAS

R8C/1A

RENESAS 16-BIT SINGLE-CHIP MICROCOMPUTER R8C FAMILY / R8C/1x SERIES
RENESAS

R8C/1B

RENESAS 16-BIT SINGLE-CHIP MICROCOMPUTER R8C FAMILY / R8C/1x SERIES
RENESAS