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 Standby mode After reset Musical scale output mode (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: Scale Lower-key do (C) Higher-key do (C) 587.32 659.25 698.45 783.98 880.00 987.76 1046.50 Frequency (Hz) 523.25 (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: re (D) mi (E) fa (F) sol (G) la (A) si (B) 40-5F 60-7F 80-9F A0-BF C0-DF E0-FF Valid A-D value 00-1F 20-3F 25.00 33.75 42.50 50.00 56.25 62.50 68.75 75.00 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 On Off Off Off During musical scale Off Off On On output mode (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 Output time (ms) Output value (µs) (tzpr + tzsc) (tzpr + tzsc) frequency (Hz) Lower-key do (C) 60 24 80 1.92 520.8333 re (D) 53 21.2 80 1.696 589.6226 mi (E) 47 18.8 80 1.504 664.8936 fa (F) 45 18 80 1.44 694.4444 sol (G) 40 16 80 1.28 781.2500 la (A) 36 14.4 80 1.152 868.0556 si (B) 32 12.8 80 1.024 976.5625 Higher-key do (C) 30 12 80 0.96 1041.6667 Table 4.2: Timer Z value/ A-D value range Primary (tzpr) Secondary (tzsc) 001F 20 60 203F 27 53 405F 34 46 607F 40 40 809F 45 35 A0BF 50 30 C0DF 55 25 E0FF 60 20 20406080A0C0E0Scale/A-D value range/ 001F 3F 5F 7F 9F BF DF FF output time ms 0.480 0.648 0.816 0.960 1.080 1.200 1.320 1.440 Lower-key do (C) tzpr tzsc 1.440 1.272 1.104 0.960 0.840 0.720 0.600 0.480 Prescaler 48 tzpr 0.424 0.572 0.721 0.848 0.954 1.060 1.166 1.272 re (D) tzsc 1.272 1.124 0.975 0.848 0.742 0.636 0.530 0.424 Prescaler 43 tzpr 0.376 0.508 0.639 0.752 0.846 0.940 1.034 1.128 mi (E) tzsc 1.128 0.996 0.865 0.752 0.658 0.564 0.470 0.376 Prescaler 38 tzpr 0.360 0.486 0.612 0.720 0.810 0.900 0.990 1.080 fa (F) tzsc 1.080 0.954 0.828 0.720 0.630 0.540 0.450 0.360 Prescaler 36 tzpr 0.320 0.432 0.544 0.640 0.720 0.800 0.880 0.960 sol (G) tzsc 0.960 0.848 0.736 0.640 0.560 0.480 0.400 0.320 Prescaler 32 tzpr 0.288 0.389 0.490 0.576 0.648 0.720 0.792 0.864 la (A) tzsc 0.864 0.763 0.662 0.576 0.504 0.432 0.360 0.288 Prescaler 28 tzpr 0.256 0.346 0.435 0.512 0.576 0.640 0.704 0.768 si (B) tzsc 0.768 0.678 0.589 0.512 0.448 0.384 0.320 0.256 Prescaler 25 0.240 0.324 0.408 0.480 0.540 0.600 0.660 0.720 Higher-key do (C) tzpr tzsc 0.720 0.636 0.552 0.480 0.420 0.360 0.300 0.240 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 Prescaler X 1/8 20MHz Timer X Set value = (100 – 1) Set value = (250 – 1) Main loop, 10 ms Settings of the Timer X Mode Register b7 b0 00001000 Timer X Mode Register [address 008B16] TXMR Selects timer mode INT1/CNTR0 polarity select bit Timer X count start flag Set to “1” when count starts Set to “0” in timer mode. Other than pulse period measurement mode Set to “0” in timer mode. Settings of the Timer Count Source Setup Register b7 b0 00010001 Timer Count Source Setup Register [address 008E16] TCSS 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 00000000 Timer Z Waveform Output Control Register [address 008416] PUM Reserved bit. Set to “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 20MHz 1/8 Prescaler Z Timer Z primary Set value = see Table 4 Set value = see Table 4 Buzzer output = high, see Table 4 Timer Z secondary Buzzer output = Set value = see Table 4 low, 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 00010001 Timer Count Source Setup Register [address 008E16] TCSS 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 01010000 Timer Z Mode Register [address 008016] TZMR 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 10011100 A-D Control Register 0 [00D616] ADCON0 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 00110000 A-D Control Register 1 [00D716] ADCON1 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 0 000 A-D Control Register 2 [00D416] ADCON2 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 cm13 = 1; ; XIN-XOUT pin cm15 = 1; ; Selects XIN-XOUT drive capability; set to HIGH cm05 = 0; ; Main clock oscillation cm16 = 0; ; Main clock not divided cm17 = 0; ; cm06 = 0; ; Enables main clock division CM16, CM17 asm("nop"); ; Wait until stabilizes asm("nop"); ; Wait until stabilizes asm("nop"); ; Wait until stabilizes asm("nop"); ; Wait until stabilizes ocd2 = 0; ; Selects the main clock prcr = 0; ; Protects the system control registers Initialize SFR SFR_INITIAL ; Initializes the SFR (port initialization and timer setup) ; Enables interrupts asm("FSET I"); Main period elapsed (10 ms)? Yes ir_txic = 0; Reset SFR SFR_REF Port output PORT_OUT Port input PORT_IN Mode processing MODE_FUNC Soft timer processing No ; Wait for timer X request ; Clears the timer X request flag ; Refreshes the port direction register ; Sets the port for output (LED and buzzer) ; Sets the port for input (key input processing) ; Mode processing (Mode processing within refresh mode) ; Soft timer (10 ms based timer value subtraction) 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 p1 = 0x16; ; Initializes port P1 p4 = 0; ; Initializes port P4 tcss = 0x11; prex = 250-1; tx = 100-1; txs = 1; tzmr = 0x50; pum = 0; tzs = 0; ; Timer X = divided-by-8, timer Z = divided-by-8 ; Prescaler X = 250 ; Timer X = 100 ; Timer X starts counting ; Timer Z mode = programmable waveform generation mode ; Primary period = high, secondary period = low, timer stopped = low ; Timer Z stops counting adcon1 = 0x30; ; 8-bit A-D sampling adcon0 = 0x9C; ; Selects divided-by-2 fAD and reads A-D0 adst = 1; Refresh SFR SFR_REF ; A-D sampling starts ; Refreshes the SFR RTS 4.3 SFR Refresh sfr_ref pd1 = 0x1E; ; Initializes/refreshes the port P1 direction register pd4 = 0; ; 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; Yes ; The red LED turns off and others stay on RTS p1 = 0x06; ; The red LED turns on and others turn off md_bz_down = 0; ; Buzzer output mode up-count md_buzzer = 1; ; Initializes buzzer output mode 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; No 1 ad_cnt = 4 ; Adds sampled A-D to the A-D work area ; Increments A-D sampling count by 1 ; Checks whether A-D is sampled 4 times Yes ad_fix = ad_sum >>2; ; Deletes 2 low-order bits, 8 high-order bits effective ad_cnt = 0; ; Clears A-D sampling count ad_sum = 0; ; Clears A-D work area i = 0; i<8 ; Clears checked count ; A-D area determination processing No Yes adfunc_table[i] >= ad_fix Yes No i + 1; ad_level != i Yes ad_level = i; No ; Determines whether to change A-D area ; 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; SW4 = SW_ON No Yes i = 0; sw4_bit << 1; sw4_bit | i; sw4_bit & 7; No ; Reserves storage in which to save the input data ; Saves the input data ; Deletes 5 high-order bits sw4_bit = 0 | sw4_bit = 7 Yes sw4_bit = 0 Yes No mode = 1 & fixsw4 = 1 No Yes mode = 0 & fixsw4 = 1 No Yes ; Changes modes mode = 0; ; Valid data = high fixsw4 = 1; fixsw4 = 0; mode = 1; ; 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 mode = 0 Yes No mode = 1 No mode = 0; Yes tzs = 0; ; Refreshes mode ; Stops timer Z mode = 1; tm_buzzer <= 0 No RTS Yes md_buzzer = 0 Yes No md_bz_down = 0; ; Mode up-count tm_buzzer = 50; ; No-sound timer 500 ms tzs = 0; md_buzzer = 9 Yes md_bz_down = 1; No tm_buzzer = 50; tzs = 0; md_buzzer > 9 ; Stops timer Z ; Mode down-count ; No-sound timer 500 ms ; Stops timer Z No tzs = 1; ; Resets mode Yes md_buzzer = 0; tzs = 0; ; Stops timer Z 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; 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 md_bz_down = 0 ; Checks mode up-count/down-count instruction Yes No md_buzzer - 1; md_buzzer = 0 md_buzzer + 1; ; Changes mode No Yes md_buzzer = 0; RTS 4.7 Soft Timer Processing stimer tm_buzzer != 0 No ; Checks for underflow Yes tm_buzzer - 1; ; Buzzer-on time = 0 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 : 1.00 * * note : * * * ************************************************************************************/ /* Definition of RAM area */ char mode = 0; /* Mode number */ char sw4_bit = 0; /* Input SW4 data */ char fixsw4 = 1; /* Input SW4 Settlement data */ char ad_cnt = 0; /* Number of A-D value sampling */ char md_bz_down = 0; /* Buzzer output counting direction */ unsigned unsigned unsigned unsigned unsigned int int int int int md_buzzer = 0; tm_buzzer = 0; ad_fix = 0; ad_sum = 0; ad_level = 0; /* /* /* /* /* Buzzer output mode */ Buzzer control timer */ A-D fixed data */ A-D calculation result data */ Output range */ /* 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); /* Initial setting of SFR registers */ extern void sfr_ref(void); /* Refresh of SFR registers */ extern void port_out(void); /* Port output */ extern void port_in(void); /* Port input */ extern void mode_func(void); /* Mode processing */ extern void stimer(void); /* Software timer */ /* Definition of base section */ #define SW_ON 0 #define SW_OFF 1 #define LOW 0 #define HIGH 1 /* Definition of port #define GREEN_LED2 #define GREEN_LED1 #define GREEN_LED0 #define RED_LED #define SW3 #define SW4 #define BUZZER */ p1_1 p1_2 p1_3 p1_4 p1_0 p4_5 REJ05B0273-0200Z/Rev.2.00 p1_3 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 : 1.00 * * note : * * * ************************************************************************************/ #include "sfr_r817.h" /* Definition of the R8C/17 SFR */ #include "ad_onkai.h" /* 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; sfr_init(); /* /* /* /* /* /* 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 */ /* Initial setting of SFR registers */ while(1){ /* Main processing */ asm("FSET I"); /* Interrupt enable */ while(ir_txic == 0){} /* Main cycle 10ms */ ir_txic = 0; sfr_ref(); /* Refresh of SFR registers */ port_out(); /* Port output */ port_in(); /* Port input */ mode_func(); /* Mode processing */ stimer(); /* Software timer */ } } REJ05B0273-0200Z/Rev.2.00 July 2004 Page 14 of 22 R8C/Tiny Series Sample Program: Musical Scales /************************************************************************************ Name: sfr_init Parameters: None Returns: None Description: Initial setting of SFR registers ************************************************************************************/ void sfr_init(void){ /* Setting port registers */ p1 = 0x16; /* p17-14 = H(Led 4.3.2.1) p13 = L(Buzzer) */ p4 = 0; tcss = 0x11; /* division = X:1/8,Z:1/8 */ /* Setting main cycle timer */ /* 20MHz* 1/8 * 250 * 100 =10ms */ prex = 250 - 1; /* Setting Prescaler X register */ tx = 100 - 1; /* Setting timer X register */ txs = 1; /* Timer X count start flag = start */ /* Setting Buzzer output timer */ tzmr = 0x50; /* Timer Z mode = Programmable waveform generation mode */ pum = 0; /* Timer Z waveform output control register */ tzs = 0; /* Timer Z count start flag = stop */ /* A-D adcon1 adcon0 adst = operation mode = Repeat mode */ = 0x30; /* 8/10-bit mode select bit = 8-bit mode */ = 0x9C; /* fAD/2 and AN8 is selected */ 1; /* 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: port_out Parameters: None Returns: None Description: Port output ************************************************************************************/ void port_out(void){ if (mode == 0){ /* LED output processing */ p1 = 0x06; /* Buzzer output processing */ md_bz_down = 0; /* Buzzer output counting direction = increment counting */ md_buzzer = 1; /* Buzzer output mode = C(1) */ tm_buzzer = 0; /* Buzzer control timer = 1 second */ tzs = 0; /* 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: port_in Parameters: None Returns: None Description: Port input ************************************************************************************/ void port_in(void){ unsigned int i; /* A-D input */ ad_sum = ad_sum + ad; /* A-D calculation result data + picking A-D */ ad_cnt = ad_cnt + 1; if (ad_cnt == 4){ /* Number of sampling become 4 */ ad_fix = ad_sum >> 2; /* 4 times shift to the right */ ad_cnt = 0; /* picking A-D space clear */ ad_sum = 0; /* A-D Range settlement processing */ i = 0; while (i < 8){ /* Output range is settled */ if (adfunc_table[i] >= ad_fix){break;} i = i + 1; } if (ad_level != i){ ad_level = i; /* Chenge Output range */ } } /* Determination of input level SW4 */ i = 1; if (SW4 == SW_ON)i = 0; /* Now determination SW4 */ sw4_bit = sw4_bit << 1; /* Check pulses matching a trigger input level 3 times */ sw4_bit = sw4_bit | i; sw4_bit = sw4_bit & 7; if (sw4_bit == 0 || sw4_bit == 7){ /* Determinate input SW4 */ if (sw4_bit == 0){ /* Mode change */ if (mode == 1 & fixsw4 == 1){ mode = 0; /* Setting the standby mode */ }else{ if (mode == 0 & fixsw4 == 1){ mode = 1; /* Setting the buzzer output mode */ } } fixsw4 = 0; /* Input on */ }else{ fixsw4 = 1; /* Input off */ } } } REJ05B0273-0200Z/Rev.2.00 July 2004 Page 17 of 22 R8C/Tiny Series Sample Program: Musical Scales /************************************************************************************ Name: mode_func Parameters: mode Returns: None 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; /* Timer Z stops Counting */ }else if (md_buzzer > 9){ md_buzzer = 0; /* Buzzer output mode = clear */ tzs = 0; /* 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; /* setting of timer Z Primary register */ work = 80 - work; tzsc = work - 1; /* setting of timer Z secondary register */ i = md_buzzer - 1; work = bz_preztable[i]; /* setting of Prescaler Z register */ prez = work - 1; tzs = 0; /* Timer Z stops Counting */ tzs = 1; /* Timer Z starts Counting */ tm_buzzer = 100; /* 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; tzs = 0; break; } /* Set mode of the standby mode */ /* Timer Z count start flag = stop */ } /************************************************************************************ 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 [email protected] REJ05B0273-0200Z/Rev.2.00 July 2004 Page 20 of 22 R8C/Tiny Series Sample Program: Musical Scales REVISION HISTORY Rev. Date Page 1.00 2.00 Sep 09, 2003 Jul 1, 2004 ALL REJ05B0273-0200Z/Rev.2.00 Description Summary First edition issued For R8C/Tiny Series July 2004 Page 21 of 22 R8C/Tiny Series Sample Program: Musical Scales Keep safety first in your circuit designs! Notes regarding these materials 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