0
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心
发布
  • 发文章

  • 发资料

  • 发帖

  • 提问

  • 发视频

创作活动
R8C-MS

R8C-MS

  • 厂商:

    RENESAS(瑞萨)

  • 封装:

  • 描述:

    R8C-MS - Sample Program: Musical Scales - Renesas Technology Corp

  • 数据手册
  • 价格&库存
R8C-MS 数据手册
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 After reset Standby mode 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: 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) Scale Lower-key do (C) Higher-key 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 20MHz 1/8 Prescaler X Set value = (250 – 1) Timer X Set value = (100 – 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 “0” in timer mode. Other than pulse period measurement mode Set to “0” in timer mode. Set to “1” when count starts Settings of the Timer Count Source Setup Register b7 b0 00010001 Timer Count Source Setup Register [address 008E16] TCSS Timer X count source Reserved bit. Set to “0”. Timer Z count source Reserved bit. Set to “0”. Set 01: 1/8 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 Set value = see Table 4 Timer Z primary Set value = see Table 4 Timer Z secondary Set value = see Table 4 Buzzer output = high, see Table 4 Buzzer output = 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 Reserved bit. Set to “0”. Timer Z count source Set 01: 1/8 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 b7 b0 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; cm13 = 1; cm15 = 1; cm05 = 0; cm16 = 0; cm17 = 0; cm06 = 0; asm("nop"); asm("nop"); asm("nop"); asm("nop"); ocd2 = 0; prcr = 0; ; Disables interrupts ; Removes protection of the system control registers ; XIN-XOUT pin ; 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 ; Protects the system control registers ; Initializes the SFR (port initialization and timer setup) Initialize SFR SFR_INITIAL asm("FSET I"); Main period elapsed (10 ms)? Yes ; Enables interrupts 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) ir_txic = 0; Reset SFR SFR_REF Port output PORT_OUT Port input PORT_IN Mode processing 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 p1 = 0x16; p4 = 0; tcss = 0x11; prex = 250-1; tx = 100-1; txs = 1; tzmr = 0x50; pum = 0; tzs = 0; adcon1 = 0x30; adcon0 = 0x9C; adst = 1; ; Initializes port P1 ; Initializes port P4 ; 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 ; 8-bit A-D sampling ; Selects divided-by-2 fAD and reads A-D0 ; A-D sampling starts ; Refreshes the SFR Refresh SFR SFR_REF RTS 4.3 SFR Refresh sfr_ref pd1 = 0x1E; pd4 = 0; RTS ; Initializes/refreshes the port P1 direction register ; Initializes/refreshes the port P4 direction register REJ05B0273-0200Z/Rev.2.00 July 2004 Page 7 of 22 R8C/Tiny Series Sample Program: Musical Scales 4.4 Port Output port_out mode = 0 Yes No p1 = 0x10; RTS p1 = 0x06; md_bz_down = 0; md_buzzer = 1; tm_buzzer = 0; tzs = 0; RTS ; The red LED turns off and others stay on ; The red LED turns on and others turn off ; Buzzer output mode up-count ; Initializes buzzer output mode ; ; Stops timer Z ; 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 ; 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 ad_cnt = 4 Yes 1 ad_fix = ad_sum >>2; ad_cnt = 0; ad_sum = 0; i = 0; i= ad_fix No Yes i + 1; ad_level != i Yes No ; Determines whether to change A-D area ; Changes A-D area ad_level = i; 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 Yes No i = 0; sw4_bit > 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
R8C-MS 价格&库存

很抱歉,暂时无法提供与“R8C-MS”相匹配的价格&库存,您可以联系我们找货

免费人工找货
12-22/BHR8C-A30/2C
  •  国内价格
  • 5+0.40802
  • 20+0.37202
  • 100+0.33602
  • 500+0.30002
  • 1000+0.28321
  • 2000+0.27121

库存:0

12-22/G6R8C-A30/2C
  •  国内价格
  • 5+0.4175
  • 20+0.38
  • 100+0.3425
  • 500+0.305
  • 1000+0.2875
  • 2000+0.275

库存:583

CL10C6R8CB8NNNC
  •  国内价格
  • 50+0.07299
  • 150+0.06256
  • 1000+0.05214
  • 5000+0.04796

库存:0

CL10C1R8CB8NNNC
  •  国内价格
  • 50+0.03518
  • 150+0.02978
  • 1000+0.02439
  • 5000+0.02223

库存:0

CL05C6R8CB5NNNC
  •  国内价格
  • 100+0.0226
  • 500+0.0211
  • 2000+0.0196
  • 5000+0.01811
  • 10000+0.01661
  • 30000+0.01556

库存:2914