( MOT_0.txt -- Turns motor on if sensor_1 is pressed, off if not ) 
( Does not loop so user has to press sensor button before running ) 

: init       ( initialize sensor_1 ) 
  1 0 SENSOR_TYPE 
  20 0 SENSOR_MODE ; 
 
: on_off     ( turn motor A on or off depending on sensor_1 value )   
  init  
  7                                   ( push motor power on stack ) 
  0 DUP SENSOR_READ DROP SENSOR_BOOL  ( determine if on or off ) 
  IF 1 ELSE 3 THEN     ( if on push 1-forward, if not push 3-stop ) 
  0 MOTOR_SET (    push 0-motor_A, so stack all set for MOTOR_SET ) ; 


( MOT_ON_OFF2.txt -- turn motor on if sensor_1 pressed, off if not ) 
( does it until user presses run button to stop program ) 

: init 
  1 0 SENSOR_TYPE 
  20 0 SENSOR_MODE ; 

: on_off_but 
  init 
  BEGIN   ( until run button is off )   
      7 
      0 DUP SENSOR_READ DROP SENSOR_BOOL ( on or off )  
      IF 1 ELSE 3 THEN 
      0 MOTOR_SET 
      RCX_BUTTON DUP BUTTON_GET @ 1 AND 
  UNTIL ; 


 ( TIMER_TST1.txt  -- tests high-res timer ) 
 ( displays count on LCDs and also on console ) 
 ( number of ticks and timer number should be on stack ) 

HEX 
: tst_timer 
  SWAP OVER timer_SET 
  BEGIN 
    DUP timer_GET 
    3002 OVER 301F LCD_NUMBER LCD_REFRESH 
    DUP . 
    0= 
  UNTIL DROP ;


 ( TIMER_WAIT.txt  -- implements a WAIT word )  
 ( number of ticks and timer number must be on stack ) 

: WAIT 
  0 timer_SET 
  BEGIN 0 timer_GET 0= UNTIL ; 


 ( TIMER_WAIT_MOT_TST.txt  -- turns motor on, waits, turns it off )  
 ( number of ticks and timer number must be on stack ) 

: mot_wait 
  7 1 0 MOTOR_SET 
  WAIT 
  7 3 0 MOTOR_SET ;


\ ----------------------------------------------------------------------------- 
\ h8300/rShift.txt - shift word n bits to the right 
\ 
\ Requires: h8300/assembler.txt 
\ 
\ ----------------------------------------------------------------------------- 
\ Revision History 
\ 
\ R. Hempel   2002-04-25 - Original 
\ ----------------------------------------------------------------------------- 

BASE @ 
HEX 

CODE newRSHIFT ( u1 n -- u2 ) 
  r0         POP,       \ Pull u1 into R0 - it's the number we're shifting 
  0F ## r6l  AND,       \ Limit the shifting to 16 bits 

  BEGIN, 
    NE                  \ Previous operation sets Z if R6L is 0 
  WHILE, 
    r0h  SHLR,          \ Logical shift MSB right, puts zero in high bit 
    r0l  ROTXR,         \ Rotate right, putting carry in high bit 
    r6l  DEC,           \ Decrement count of bits to shift 
  REPEAT, 

  r0    r6   MOV,       \ Copy the result to TOS 
  NEXT,                 \ Compile jump to NEXT 
END-CODE 

: TEST0 0 DO I 8    RSHIFT DROP LOOP ; 
: TEST1 0 DO I 8 newRSHIFT DROP LOOP ; 

: newSPLIT-WORD ( u -- lsb msb ) 
  DUP FF AND SWAP    \  ( -- lsb u   ) Isolate the lsb 
  8 newRSHIFT ;      \  ( -- lsb msb ) Isolate the msb 

: TEST2 0 DO I newSPLIT-WORD 2DROP LOOP ; 

BASE !   

\ -----------------------------------------------------------------------------