EhBaSIC 740 Code examples

Here are some short examples that demonstrate some of the Mitsubishi 740 series processor specific commands and functions in EhBASIC74.

A-D & D-A port example

In this example the SuprDupr is used as an analog multiplexer to display eight analog inputs as a ‘bargraph’ type display on a ‘scope.
The SuprDupr also generates a trigger pulse to synchronise the ‘scope on the second
analog output.
The upper trace is the multiplex out, the lower trace is the trigger pulse.

The code.

10 REM 8 channel analog multiplex
20 REM displays 8 analog inputs as
30 REM a 'bargraph' type display on
40 REM a two channel 'scope.
50 REM
60 REM Analog out 1 is the trigger signal
70 REM Analog out 2 is the multiplex
80 REM
100 DO             : REM start of main loop
110  D2A1 255      : REM trigger pulse high
120  D2A1 0        : REM trigger pulse low
130  FOR ch=0 TO 7 : REM for each channel
140   D2A2 A2D(ch) : REM read port & out to scope
150  NEXT ch
160  D2A2 0        : REM set bar output level
170 LOOP

Timers example

In this example the SuprDupr is used to read a joystick on an analog input and generate a servo PWM signal using two of the timers.

The joystick input gave a value of 90 to 165, the servo needs a pulse width of 0.5mS to 2.5mS which, with the prescale value used, means TIMERX values will range from 50 to 250

The values for S and M, the scaling and minimum offset values used in the code, were calculated as follows..

<b>S = timer range / input range
  = (250-50) / (165-90)
  = 2.67</b>

.. and ..
<b>M = minimum timer - minimum scaled value
  = 50 - (90*S)
  = -190</b>

The code.


10 REM generates timing pulses for a servo
20 REM control is via analog input 0
30 REM output is via timer X
40 REM timer 1 is used to generate the 20mS frame
50 REM
100 TIMERX STOP : REM stop output
110 PRES12 49 : TIMER1 199 :  REM set TIMER 1 for 20mS timebase
120 M = -190 : REM set minimum pulse width offset
130 PS = 4 : REM set timer X prescale value
140 S = 2.67 : REM set scale value
150 PRESX PS : REM set timer X prescaler
160 X = 0 : REM set input for timer X
180 TIMERX PULSE : REM timer X to output pulse
190 GOSUB 1010 : REM calculate initial X value
200 ON TIMERX 1000 : REM stop timer and calculate next
210 ON TIMER1 2000 : REM set and start TIMERX
220 DO : LOOP : REM just loop forever
997 REM
998 REM TIMER X routine
999 REM
1000 TIMERX STOP
1010 LX = A2D(X) : REM read port
1020 LX = LX*S : REM scale value
1030 LX = LX+M : REM add minimum width offset
1040 RETURN
1997 REM
1998 REM TIMER 1 routine
1999 REM
2000 TIMERX LX : REM set timer X
2010 TIMERX START : REM start timer
2020 RETURN

EVENT and PWM example

In this very basic example the SuprDupr is used to measure the width of +ve pulses on the counter 0 input.

The code.

10 TIMERX PWM : REM set PWM measure mode
20 CNTR0 - : REM -ve edge triggered, count when high
30 ON CNTR0 1000 : REM event code
40 TIMERX START : REM ensure timer can run

100 DO
110 DO : LOOP UNTIL EVENT : REM wait for event
120 PRINT TX : REM print result
130 LOOP : REM loop forever

1000 TX = TIMERX : REM read PWM value
1010 RETURN