* * library of functions for Mini Board rev 1.5 * (C) 1992 Fred Martin * * for Dave Dunfield's C compiler * last updated 4/9/92 * pre-process with "MOTOR_BEEP" to enable tone command to produce * output on motor 4 * * The following functions are defined: * ----------------------------------------------------------------------- * * int analog(int port) Returns analog reading from port. * Ports are numbered 0 to 7. * Ports 0 to 3 default as motor feedback * for motors 1 to 4, respectively. * * int digital(int port) Returns 1 or 0 (true or false) value * from Port C. * * void motor(int m, int s) Sets motor m at speed s. Motors are * numbered from 1 to 4; speeds are numbered * from -16 (full backward) to +16 (full * forward). * * void off(int m) Turns off motor m. * * int button(void) Returns 1 (true) when IRQ button is depressed. * System will hang while button is held * down, so effectively, returns true * when button is released. * * Button presses are automatically debounced, * but a press will get queued if it happens * before you ask for it. * * int dipsw(int sw) Returns state of DIP switch plugged into * Port D. Port D2 is switch #1; D5 is sw #4. * Open (NC) position is true, closed is false. * * void msleep(int msec) Wastes time for msec milliseconds. * * void tone(int freq, Generates a beep of frequency "freq" for * int duration) "duration" milliseconds. * digital EQU * TSX LDAB PORTC * get PORTC into B LDAA 3,X * get port # into A * digloop BEQ digdone LSRB * shift B bits down DECA BRA digloop * digdone ANDB #1 * mask off all but low bit EORB #1 * invert logical sense due to hardware * A is already zero RTS analog EQU * TSX LDAA 3,X * get port # into A SEI * disable interrupts STAA ADCTL analoop LDAA ADCTL ANDA #$80 BEQ analoop CLI * re-enable them LDAB ADR1 CLRA RTS dipsw EQU * TSX LDAB PORTD LSRB * desired bits in pos'n 2 to 5 LDAA 3,X * switch number (1 to 4) in A diploop LSRB * shift B bits down DECA BNE diploop ANDB #1 RTS * A is already zero; B has 1 or 0 * * motor(int m, int s) * m= 1, 2, 3, or 4 * s= -16 (full on backward) to +16 (full on forward) * motor EQU * TSX LDAB 5,X * motor number PSHB * save for later LDAA #$08 * bit mask SEC * set carry, will be rotated into A msloop ROLA DECB BNE msloop * A now has motor enable mask PSHA ORAA motctrl STAA ?temp PULA ANDA #$0F * keep dir bit only TST 2,X * high byte of speed BPL mset2 * negative speed => clear direction bit COMA ANDA ?temp BRA mset1 mset2 LDAA ?temp mset1 STAA motctrl * LDD 2,X * speed word BPL speedok LDD #0 SUBD 2,X * number from 0 to 16 now in B speedok ASLB * double it LDX #mtable ABX * index into speed table LDY 0,X * get speed bits PULB * get motor number from 1 to 4 ASLB LDX #speed1-2 ABX * ptr to speed entry STY 0,X * store speed bits * done! RTS * table of motor speeds mtable FDB %0000000000000000 * 0/16 FDB %0000000000000001 * 1/16 FDB %0000000100000001 * 2/16 FDB %0000100001000010 * 3/16 FDB %0001000100010001 * 4/16 FDB %0010001001001001 * 5/16 FDB %0100100100101001 * 6/16 FDB %0101001010101010 * 7/16 FDB %0101010101010101 * 8/16 FDB %1010101010101011 * 9/16 FDB %0101011010101011 * 10/16 FDB %1101110110110110 * 11/16 FDB %1110111011101110 * 12/16 FDB %1110111011101111 * 13/16 FDB %1110111111101111 * 14/16 FDB %1111111011111111 * 15/16 FDB %1111111111111111 * 16/16 * * void off(int m) * off TSX LDAB 3,X * motor number LDAA #$08 * bit mask offloop ROLA DECB BNE offloop * A now has motor enable mask COMA ANDA motctrl STAA motctrl RTS * delay for nn,nnn milliseconds * msleep EQU * TSX sleepsub SEI * halt interrupts until we perform calculation LDD st_lo ADDD 2,X CLI * okay again sleeplp CPD st_lo BNE sleeplp RTS * * button(void) * returns 1 if irqval is one (sets irqval to zero in this case) * else returns 0 * button EQU * CLRA LDAB irqval BEQ butndone CLR irqval butndone RTS * * tone(int freq, int duration) * tone EQU * TSX LDD 4,X ; frequency LSRD LSRD LSRD LSRD ; divide by 16 PSHX LDX #62500 ; 1E6 divided by 16 XGDX IDIV ; answer in X STX beeptone LDX #BASE BSET TCTL1,X;$01 ; set beeper pin to toggle BSET TMSK1,X;$08 ; turn on beeper interrupt #ifdef MOTOR_BEEP BSET PORTB,X;$80 ; enable motor 4 #endif PULX BSR sleepsub ; sleep for desired duration LDX #BASE BCLR TMSK1,X;$08 ; turn off beeper interrupt BCLR TCTL1,X;$01 ; set beeper pin to do nothing BCLR PORTA,X;$08 ; turn off beeper #ifdef MOTOR_BEEP BCLR PORTB,X;$80 ; disable motor 4 #endif LDX #0 STX beeptone RTS