BasicMicro MBasic Wiki
Advertisement

Waveform I/O Commands Intro[]

This chapter includes specialized input/output commands such as those for A/D conversions, generating audio tones, controlling LCD displays, stepper motors, servos, etc.

This chapter contains the following sections:

  • Waveform I/O Commands
    • DTMFout
    • DTMFout2
    • Freqout
    • HPWM
    • PWM
    • Pulsout
    • Pulsin
    • Sound
    • Sound2


Conventions Used in this Chapter[]

{ ... } represent optional components in a command. The { } are not to be included.
[ ... ] used for lists - the [ ] are required.
( ... ) used for some arguments. The ( ) are required.

Important: Several of the commands described in this chapter specifically address hardware features of the microcontroller chip. Descriptions of these hardware features are beyond the scope of this manual.

Please refer to the H8/3664F or H8/3687F hardware manual, available at http://www.renesas.com for further details.


DTMFOUT[]

Outputs a two frequency DTMF tone on a single pin of the Atom Pro. This tone can be used for dialing a telephone or operating remote devices with DTMF decoders.

Syntax

dtmfout pin,{ontime,offtime,}[tone1, tone2, ... toneN]
pin is a variable or constant that specifies the I/O pin to use. The pin will be set to an output during tone generation. After tone generation is complete the pin will be set to an input.
ontime is an optional variable or constant (0 - 65535) that specifies the duration of each tone in milliseconds. If not specified, default is 200 ms.
offtime is an optional variable or constatnt (0 - 65535) that specifies the length of silence after each tone in milliseconds. If not specified, default is 50 ms.
tone1 - toneN is a list of tones to be generated in the form of variables or constants defined by the list below:
Tone value DTMF pair
0 to 9 0 to 9
10 *
11 #
12 to 15 fourth column tones A to D

Notes

DTMF tones consist of two sine waves at different frequencies.

  1209Hz 1336Hz 1477Hz 1633Hz
697Hz 1 2 3 A
770Hz 4 5 6 B
852Hz 7 8 9 C
941Hz * 0 # D

The DTMFOUT command causes the Atom Pro to create and mix two sine waves mathematically, then use the resulting data stream to control the duty cycle of a pulse width modulator (PWM). The resulting output must be filtered to remove the digitization "noise" and produce reasonable sine waves.

Note: The DTMFOUT2 command requires less complex filtering and is recommended if you have an extra I/O pin available.

The simplest circuit uses a resistor and capacitor as a low pass filter (shown connected to P1 in the diagram). Depending on the DTMF decoder used, this simple filter may be sufficient - it is not recommended for use with the PSTN (public switched telephone network). You may need to adjust the capacitor value for best results.

Important: Design of more sophisticated filters, if required, is beyond the scope of this manual.


Wavef01

Figure 14 - Simple Low Pass Filter

Examples

dtmfout p1,100,50,[2,3,3,5,5,5,5]

This command will generate the DTMF pairs required to dial the number 233-5555 using 100 ms tones with 50 ms silent spaces between them.


DTMFOUT2[]

Outputs a two frequency DTMF tone on two pins of the Atom Pro, one frequency per pin. This tone can be used for dialing a telephone or operating remote devices with DTMF decoders.

Syntax

dtmfout2 pin1\pin2,{ontime,offtime,}[tone1, tone2, ... toneN]
pin1\pin2 are variables or constants that specifies the two I/O pins to use. The pins will be set to outputs during tone generation. After tone generation is complete the pins will be set to inputs.
ontime is an optional variable or constant (0 - 65535) that specifies the duration of each tone in milliseconds. If not specified, default is 200 ms.
offtime is an optional variable or constant (0 - 65535) that specifies the length of silence after each tone in milliseconds. If not specified, default is 50 ms.
tone1 - toneN is a list of tones to be generated in the form of variables or constants defined by the list below:
Tone value DTMF pair
0 to 9 0 to 9
10 *
11 #
12 to 15 fourth column tones A to D

Notes

DTMF tones consist of two sine waves at different frequencies.

  1209Hz 1336Hz 1477Hz 1633Hz
697Hz 1 2 3 A
770Hz 4 5 6 B
852Hz 7 8 9 C
941Hz * 0 # D

The DTMFOUT2 command causes the Atom Pro to create two square waves, one at each of the required frequencies, and send them out on their respective pins. Filtering is required to create a reasonable approximation of sine waves, however the square waves have much less high frequency noise than the PWM tones generated by DTMFOUT and require less sophisticated filtering. The diagram assumes that the tones are generated on P1 and P2.

The capacitor value may require adjustment for best results. This circuit should work well on the PSTN if levels are correctly set.


Wavef02

Figure 15 - Filter/combiner for DTMFOUT2

Examples

dtmfout2 p1\p2,100,50,[2,3,3,5,5,5,5]

This command will generate the DTMF pairs required to dial the number 233-5555 using 100 ms tones with 50 ms silent spaces between them. Output is on I/O pins P1 and P2 and should be combined using a circuit similar to that shown above.


FREQOUT[]

This command generates one or two tones that are output on a single I/O pin. (FREQOUT generates a pulse width modulated signal designed to be filtered to create a sine wave. You may prefer to use one of the SOUND commands, which generate a square wave, if a single tone requiring less filtering is desired.) FREQOUT generates a pulse width modulated signal.

Syntax

freqout pin, duration, freq1{,freq2}
pin is a variable or constant that specifies the I/O pin to be used. This pin will be set to output mode during tone generation and left in that state after output is completed.
duration is a variable or constant that sets the duration of the output tone in milliseconds (0 - 65535).
freq1 is a variable or constant that specifies the frequency in Hz of the first tone (0 - 32767).
freq2 is an optional variable or constant that specifies the frequency in Hz of the second tone (0 - 32767).

Notes

The tone (or tones) is generated mathematically in the Atom Pro and output as a pulse width modulated (PWM) signal. The signal must be converted to a sine wave (or a pair of sine waves) by passing it through an integrator (low pass filter).

For non-critical applications, a simple filter such as the one shown below may suffice. You may need to experiment with the resistor and/or capacitor value for best results at the frequency you are using.

Important: Design of more sophisticated filter circuits is beyond the scope of this manual.

Wavef03

Figure 16 - Simple integrator/low pass filter

Examples

freq var word
dur var word
freq = 1000
dur = 5000
freqout p1, dur, freq

This will output a 1000 Hz tone for a duration of 5 seconds on Pin 1.


HPWM[]

This command gives access to the Atom-Pro’s built-in hardware PWM generator. It allows you to output a PWM signal with any desired period and duty cycle (within the limits of the hardware).

Important: A detailed description of the operation of this hardware is beyond the scope of this manual. Please refer to the H8/3664F or H8/3687F data sheet (see page 135 for availability).

Syntax

hpwm pin, period, duty
pin is a constant or variable that specifies the pin to be used for PWM output. This pin will be switched into output mode for the duration of the pulse generation, then returned to input mode. Only pins with the FTIOB/C or D option can use the HPWM command. See page 187 for module pinouts.
period is a constant or variable (0 to 524288) that specifies the period of the output signal in clock cycles.
duty is a constant or variable (0 to 524288) that specifies the duty of the output signal in clock cycles.

Note: duty must be less than period for this command to work properly.

Notes

The HPWM command uses the Atom-Pro's hardware module to generate a square wave signal with a definable duty cycle. Since it uses hardware generation the PWM signal may be output while program execution continues.

Once the HPWM command has executed, the PWM signal will be output continuously until cancelled, while the rest of your program will continue to execute.

To cancel the PWM signal.

Examples

select con 1 ; 1 uses CCP2 on pin 9
period var word
duty var word
period=100 ; 1000 us period
duty=25 ; 25% duty cycle
main
  hpwm select, period, duty
  pause 5000 ; wait 5 seconds
  ccp2con=0 ; turn off output
  pause 5000 ; wait 5 seconds
goto main ; repeat

This program generates a square wave of period 100 microseconds (frequency of 10,000 Hz) with a duty cycle of 25%. Output is on the Atom Pro module's pin 9. The signal will continue for 5 seconds, then be turned off (by setting register ccp2con to zero). After a further 5 seconds, the program will repeat.


PWM[]

The PWM command is used to generate an analog voltage from a digital calculation. The signal is generated in software; see HPWM (above) for a hardware generated PWM signal.

Syntax

pwm pin, period, duty, cycles
pin is a variable or constant that specifies the Atom-Pro I/O pin to use. This pin will be set to output during pulse generation.
period is a 32 bit expression that specifies the period of the pulse width signal in clock cycles.
duty is a variable or constant (0 - 255) that sets the duty cycle from 0% (0) to 100% (255).
duration is a 32 bit expression that specifies the number of pulses to output.

Notes The PWM command generates a pulse width modulated signal with a specified duty cycle. Note that the frequency of the pwm signal is not fixed and varies with the duty cycle, therefore the primary use for this command is to produce a signal to be filtered for analog output. The output of the PWM command must be integrated (using a low pass filter) to produce an analog voltage. A circuit such as the one shown below will suffice for most uses.


Wavef04

Figure 17 - Analog converter for PWM command

The values of capacitor, series resistor (RS) and load resistor (RL) may be varied to produce the desired output voltage and response time. The values shown produce adequate filtering. Note that response time is relatively slow, and the PWM command with a filter such as this is mainly suitable for producing steady-state voltages rather than rapidly varying waveforms.

The values shown will produce a voltage that varies linearly from 0V (with duty set to 0) to approximately 4.6V (with duty set to 255). The frequency of the pwm signal is approximately 125 kHz with duty set to 128 (50%) and drops significantly at both higher and lower duty cycles.

Examples

duty var byte
duration var word
duration=5000
main
   duty=0
   gosub generate
   duty=64
   gosub generate
   duty=128
   gosub generate
   duty=192
   gosub generate
   duty=255
   gosub generate
goto main
generate
   pwm p1, duty, duration
return

With a filter such as that shown above, this program will generate, in sequence, voltages of 0V, 1.15V, 2.3V, 3.45V and 4.6V for 5 seconds each, then repeat the same cycle indefinitely. (All voltages are approximate.)


PULSOUT[]

Generates a pulse on the specified pin. A "0" or "1" pulse will be generated, depending on the initial state of the pin.

Syntax

pulsout pin, time
pin is a variable or constant that specifies the I/O pin to use. This pin will be placed in output mode immediately before the pulse, and left in that mode after the instruction finishes.
time is a variable or constant (minimum value 8) that specifies the duration of the pulse in 0.5 microseconds increments.

Notes

PULSOUT toggles the pin's high/low state twice to generate a pulse. You can use the high or low commands to set the initial state of the pin, which controls the polarity of the pulse.

Once the pulse is issued, the pin will remain in the final state (which is the same as its initial state prior to the PULSOUT command) until further commands affect that pin. Thus, successive use of the PULSOUT command will produce successive pulses of the same polarity.

Examples

time var word
time=12
low p0 ; set pin0 to output, low state
pulsout p0, time ; generate a "high" pulse
   ; program continues

This program will produce a pulse similar to that shown on the left in Figure 18. If the "low p0" command is replaced by "high p0" the pulse will be similar to that shown on the right in Figure 18.


Wavef05

Figure 18 - Output of "pulsout" command


PULSIN[]

Measures the duration of an input pulse on a specified pin.

Syntax

pulsin pin, direction, {TimeoutLabel, TimeoutMultiplier,} duration
pin is a variable or constant that specifies the pin to be used for the input pulse. This pin will be placed into input mode during the execution of this command and left in that state after the command finishes.
direction is a variable or constant (0 or 1) that specifies the pulse direction. If state = 0, the pulse must begin with a 1-to-0 transition. If state = 1 the pulse must begin with a 0-to-1 transition.
TimeoutLabel is an optional label that specifies the target if a timeout occurs. If the command times out before a pulse is detected, program execution will continue at this label. The default timeout value is 65535 µs. If no TimeoutLabel is specified, PULSIN will wait 65535 µs for a pulse to occur, then program execution will continue with the next instruction.
TimeoutMultiplier is a variable or constant that specifies the multiplier to be used for the default 65536 µs timeout. a multiplier for the default 65535 µs timeout. For example, if timeoutmultiplier = 10, the timeout will be 655350 µs or 0.655 seconds. TimeoutMultiplier is required if TimeoutLabel is specified.
duration is a variable that stores the pulse duration in 1µs increments. Make sure the variable is large enough to store the longest expected pulse time (either 65535 µs or that set by TimeoutMultiplier). If the variable is too small only the least significant bits will be stored. If no pulse is detected within the timeout value duration will be set to 0.

Notes

These illustrations will show the results of the PULSIN instruction in several situations.


Pin is initially high, and a low pulse occurs:

Wavef06

Pin is initially low, and a high pulse occurs:

Wavef07

Pin is initially high, and a low pulse, followed by a high pulse, occurs:

Wavef08

Examples

pulsin p0,0,duration

will wait up to 65535 µs for a "low" pulse (i.e. a pulse starting with a falling edge) and measure its duration, saving the result in duration, then program execution will continue with the next instruction.

If there is no pulse within 65535 µs program execution will continue with the next instruction, and duration will be set to 0.

Note: If a "high" pulse occurs, timing will start at the end of the pulse, and PULSIN will time out.

pulsin p0,1,timeout,100,duration

will wait up to 6553500 µs (approx. 6.5 seconds) for a "high" pulse (i.e. a pulse starting with a rising edge) and measure its duration, saving the result in duration, then program execution will continue with the next instruction. Duration must be large enough to store a value up to 6553500.

If there is no pulse within 6553500 µs, program execution will jump to the label timeout and duration will be set to 0.

Note: If a "low" pulse occurs, timing will start at the end of the pulse, and PULSIN will time out, jumping to label timeout.

SOUND[]

Generates an audio tone or a sequence of tones on a specified I/O pin. (If only a single tone is needed, you may prefer to use the FREQOUT instruction which generates a pulse width modulated signal designed to be filtered to create a sine wave.) SOUND generates a square wave.

Syntax

sound pin,[dur1\note1, dur2\note2, ... durN\noteN]
pin is a variable or constant specifying the output pin to use. This pin will be set to output mode during tone generation and will remain in that mode after the instruction is completed.
dur is a constant or variable (or a number of constants or variables) that specify the duration, in milliseconds (1 - 65535) of each tone.
note is a constant or variable (or a number of constants or variables) that specify the frequency in Hz (1 - 32767) of each tone to be generated.

Notes

The SOUND command generates a square wave output. If you are using it to drive a small speaker or amplifier no filtering may be needed. However, a low pass filter is recommended to convert the square wave to something resembling a sine wave.

A simple RC filter, such as that shown in Figure 16, can be used to approximate a sine wave. You may need to adjust the capacitor value for best results with the frequencies you are using.

Since a square wave contains all odd harmonics of the fundamental signal, the best filter would have a sharp cutoff at less that 3 times the frequency of the tones used. The design of such a filter is beyond the scope of this manual.

Examples

note1 con 1000
note2 con 2000
note3 con 3000
dur con 1000
sound p1,[dur\note1,dur\note2,dur\note3]

will produce tones of 1000, 2000 and 3000 Hz in sequence, lasting 1 second each.


SOUND2[]

Generates two simultaneous tones, or a sequence of such tones, on two specified output pins. The tones generated are square waves.

Syntax

sound2 pin1\pin2,[dur1\note1\noteA,dur2\note2\noteB,...durN\note#\noteN]
pin1 and pin2 are constants or variables specifying the two output pins to be used, one for each tone.
dur is a constant or variable (or sequence of constants or variables) specifying the duration in milliseconds (1 - 65535) of each note pair. Both notes last the same duration in each case.
note1 to note# are constants or variables specifying the frequencies in Hz (0 - 32767) of the notes to be output on pin1.
noteA to noteN are constants or variables specifying the frequencies in Hz (0 - 32767) of the notes to be output on pin2.

Notes

The SOUND2 command generates square wave output on each of the two pins. If you are using it to drive a small speaker or amplifier no filtering may be needed. The pins can be connected together as shown on the left in Figure 19. However, a low pass filter is recommended to convert the square wave to something resembling a sine wave, as shown on the right in Figure 19. You may need to adjust the capacitor value for best results.


Wavef09

Figure 19 - Combining outputs for Sound2

Since a square wave contains all odd harmonics of the fundamental signal, the best filter would have a sharp cutoff at less that 3 times the frequency of the tones used. The design of such a filter is beyond the scope of this manual.

Examples

note1 con 1000
noteA con 1500
note2 con 1800
noteB con 2700
dur con 1000
sound2 p1\p2,[dur\note1\noteA,dur\note2\noteB]

This will output the frequency pair 1000/1500 Hz for 1 second, followed by the pair 1800/2700 Hz for 1 further second, on pins 1 and 2 respectively.

Advertisement