;----------------------------------------------------------- ; ; 8chA/D + 7bits sensors --> MIDI, MIDI --> 100 Stepping Motor control : July 2008 ; ; for MAF2008 Aya Suzuki ; ;----------------------------------------------------------- ;##### Port Defines ##### smr .equ h'0fffb0 brr .equ h'0fffb1 scr .equ h'0fffb2 tdr .equ h'0fffb3 ssr .equ h'0fffb4 rdr .equ h'0fffb5 p1ddr .equ h'0fffc0 p1dr .equ h'0fffc2 p4ddr .equ h'0fffc5 p4dr .equ h'0fffc7 paddr .equ h'0fffd1 padr .equ h'0fffd3 pbddr .equ h'0fffd4 pbdr .equ h'0fffd6 iprb .equ h'0ffff8 tstr .equ h'0fff60 tsnc .equ h'0fff61 tmdr .equ h'0fff62 tfcr .equ h'0fff63 toer .equ h'0fff90 tocr .equ h'0fff91 tcr0 .equ h'0fff64 tior0 .equ h'0fff65 tier0 .equ h'0fff66 tsr0 .equ h'0fff67 tcnt0 .equ h'0fff68 gra0 .equ h'0fff6a addrah .equ h'0fffe0 addrbh .equ h'0fffe2 addrch .equ h'0fffe4 addrdh .equ h'0fffe6 adcsr .equ h'0fffe8 adcr .equ h'0fffe9 ;##### Vector Defines ##### .section vector,data,locate=h'000000 .data.l start .org h'000060 .data.l int_timer0 .org h'0000d0 .data.l int_rx_error .data.l int_rx_full ;##### Work RAM Data Defines ##### .section ram,data,locate=h'0fef10 timer1 .res.w 1 timer2 .res.w 1 timer3 .res.w 1 timer4 .res.w 1 timer5 .res.w 1 tx_top .res.w 1 tx_end .res.w 1 rx_top .res.w 1 rx_end .res.w 1 rsb .res.b 1 channel .res.b 1 dcb .res.b 1 keyno .res.b 1 data .res.b 1 led .res.b 1 d_in .res.b 1 flag .res.b 1 ad_no .res.b 1 ad_data .res.b 8 latch .res.b 50 motor_n .res.b 1 m_data .res.b 100 m_ctr .res.b 100 m_phase .res.b 100 .org h'0ff400 rx_fifo .res.b 1024 .org h'0ff800 tx_fifo .res.b 1024 ;***** Data Table ***** .section rom,data,locate=h'001000 table: .data.b b'00000001, b'00000011, b'00000010, b'00000110 .data.b b'00000100, b'00001100, b'00001000, b'00001001 ;***** Reset --> Initialize --> Main Loop ***** .section program,code,locate=h'002000 start: mov.l #h'0fff0f,er7 ; stack pointer set mov.l #h'0fef10,er2 mov.w #h'0fe0,r1 mov.b #0,r0l _ram_clear: mov.b r0l,@er2 inc.l #1,er2 dec.w #1,r1 bne _ram_clear jsr @sci0_init ; SCI initialize mov.b #b'00001000,r0l mov.b r0l,@adcsr ; A/D setting bclr.b #7,@adcr ; TRGE = 0 mov.b #b'00100001,r0l mov.b r0l,@tcr0 ; Timer0 init : 8MHz clock bclr #0,@tsr0 ; Clear IMFA bset #0,@tier0 ; timer INT set mov.w #800,r0 ; 100usec INT mov.w r0,@gra0 mov.b #b'00000000,r0l mov.b r0l,@p4ddr ; set : Port[4] all input mov.b #b'11111111,r0l mov.b r0l,@p1ddr ; set : Port[1] all output mov.b r0l,@paddr ; set : Port[A] all output mov.b r0l,@pbddr ; set : Port[B] all output jsr @motor_all_off jsr @wait_500msec bset.b #5,@adcsr ; A/D start ! mov.b #b'01110000,r0l ; tx/rx start ! mov.b r0l,@scr bset #0,@tstr ; timer start ! andc.b #b'01111111,ccr ; interrupt enable loop: jsr @ad_check jsr @timer_check jsr @tx_midi_check jsr @rx_midi_check bra loop ;***** Common Sub-Routines ***** sci0_init: mov.b #b'00000000,r0l mov.b r0l,@scr mov.b #b'00000000,r0l mov.b r0l,@smr mov.b #15,r0l mov.b r0l,@brr mov.w #500,r0 _sci0_wait: dec.w #1,r0 bne _sci0_wait mov.b @ssr,r0l ; (dummy read) mov.b #0,r0l mov.b r0l,@ssr mov.b #b'00001000,r0l mov.b r0l,@iprb ; SCI0-int priority UP ! rts int_rx_error: bclr #5,@ssr bclr #4,@ssr rte int_rx_full: push.w r0 push.l er5 btst #6,@ssr bclr #6,@ssr mov.w @rx_top,r5 mov.w #0,e5 mov.b @rdr,r0l mov.b r0l,@(rx_fifo,er5) inc.w #1,r5 bclr #2,r5h mov.w r5,@rx_top pop.l er5 pop.w r0 rte tx_midi_check: mov.w @tx_top,r1 mov.w @tx_end,r6 cmp.w r1,r6 bne _tx_exist rts _tx_exist: btst #7,@ssr ; test TRDE bne _tx_seq rts _tx_seq: mov.w #0,e6 mov.b @(tx_fifo,er6),r0l mov.b r0l,@tdr bclr #7,@ssr ; Transmit ! inc.w #1,r6 bclr #2,r6h mov.w r6,@tx_end rts tx_fifo_set: mov.w @tx_top,r6 mov.w #0,e6 mov.b r0h,@(tx_fifo,er6) ; transmit data = [r0h] inc.w #1,r6 bclr #2,r6h mov.w r6,@tx_top rts int_timer0: push.w r0 mov.b #1,r0l mov.b r0l,@flag btst #0,@tsr0 bclr #0,@tsr0 ; Clear IMFA pop.w r0 rte wait_500msec: mov.l #500,er1 _wait_1: jsr @wait_1msec sub.l #1,er1 bne _wait_1 rts wait_1msec: mov.l #2048,er2 _wait_2: sub.l #1,er2 bne _wait_2 rts timer_check: mov.b @flag,r0l cmp.b #0,r0l bne _100usec_event rts _100usec_event: mov.b #0,r0l mov.b r0l,@flag jsr @motor_control mov.b @timer1,r0l inc.b r0l mov.b r0l,@timer1 cmp.b #100,r0l beq _10msec_hit rts _10msec_hit: mov.b #0,r0l mov.b r0l,@timer1 jsr @check_10msec mov.w @timer2,r0 add.w #1,r0 mov.w r0,@timer2 cmp.w #100,r0 beq _1sec_hit rts _1sec_hit: mov.w #0,r0 mov.w r0,@timer2 mov.b @timer3,r0l inc.b r0l mov.b r0l,@timer3 rts mov.b @led,r0l xor.b #b'10000000,r0l mov.b r0l,@led mov.b r0l,@p1dr rts motor_all_off: mov.b #b'00000000,r0l mov.b r0l,@p1dr mov.b r0l,@padr mov.b #b'11111111,r0l mov.b r0l,@padr rts ;***** A/D Check Routine ***** ad_check: mov.w @timer4,r1 inc.w #1,r1 mov.w r1,@timer4 cmp.w #h'0100,r1 beq _ad_in rts _ad_in: mov.w #0,r1 mov.w r1,@timer4 btst.b #7,@adcsr ; A/D conv end ? bne _ad_go rts _ad_go: mov.b @ad_no,r0l mov.b r0l,r4l ; [r4l] = Data channel cmp.b #0,r0l beq _ad_1 cmp.b #4,r0l beq _ad_1 cmp.b #1,r0l beq _ad_2 cmp.b #5,r0l beq _ad_2 cmp.b #2,r0l beq _ad_3 cmp.b #6,r0l beq _ad_3 cmp.b #3,r0l beq _ad_4 cmp.b #7,r0l beq _ad_4 _ad_1: mov.b @addrah,r3h ; Get A/D data bra _ad_next _ad_2: mov.b @addrbh,r3h ; Get A/D data bra _ad_next _ad_3: mov.b @addrch,r3h ; Get A/D data bra _ad_next _ad_4: mov.b @addrdh,r3h ; Get A/D data bra _ad_next _ad_next: inc.b r0l and.b #b'00000111,r0l mov.b r0l,@ad_no bclr.b #7,@adcsr ; ADF clear mov.b r0l,@adcsr ; A/D setting bset.b #3,@adcsr ; speed bset.b #5,@adcsr ; A/D start ! shlr.b r3h ; R3H = now mov.b r3h,r3l ; --> r3l mov.w #0,e4 mov.b #0,r4h mov.b @(ad_data,er4),r2h ; R2H = old cmp.b r2h,r3l ; r3l-r2h bmi _minus ; if minus sub.b r2h,r3l ; r3l-r2h -> r3l cmp.b #2,r3l ; r3l>2 ? bpl _ad_ok rts _minus: sub.b r3l,r2h ; r2h-r3l -> r2h cmp.b #2,r2h ; r2h>2 ? bpl _ad_ok rts _ad_ok: mov.b r3h,@(ad_data,er4) ; now -> old mov.b #h'd0,r0h add.b r4l,r0h jsr @tx_fifo_set mov.b r3h,r0h jsr @tx_fifo_set rts ;***** Digital 7bits check Routine ***** check_10msec: mov.b @timer5,r0l inc.b r0l mov.b r0l,@timer5 cmp.b #10,r0l beq _100msec_hit rts _100msec_hit: mov.b #0,r0l mov.b r0l,@timer5 mov.b @p4dr,r3h and.b #b'01111111,r3h mov.b @d_in,r0l cmp.b r3h,r0l bne _sw_event rts _sw_event: mov.b r3h,@d_in mov.b #h'c0,r0h jsr @tx_fifo_set mov.b r3h,r0h jsr @tx_fifo_set rts ;***** MIDI Receive Routine ***** rx_midi_check: mov.w @rx_top,r1 mov.w @rx_end,r5 cmp.w r1,r5 bne _rx_exist rts _rx_exist: mov.w #0,e5 mov.b @(rx_fifo,er5),r0h ; received data = [r0h] inc.w #1,r5 bclr #2,r5h mov.w r5,@rx_end btst #7,r0h beq running mov.b r0h,r0l and.b #b'11111000,r0l cmp.b #b'11111000,r0l bne _lower_f8 rts _lower_f8: and.b #b'11110000,r0l cmp.b #b'11110000,r0l bne _lower_f0 mov.b #0,r0l mov.b r0l,@rsb rts _lower_f0 mov.b r0h,r0l and.b #b'00001111,r0l mov.b r0l,@channel mov.b r0h,r0l and.b #b'11110000,r0l mov.b r0l,@rsb mov.b #0,r0l mov.b r0l,@dcb rts running: mov.b @rsb,r0l bne _normal rts _normal: cmp.b #b'11000000,r0l beq _2byte cmp.b #b'11010000,r0l beq _2byte mov.b @dcb,r1l bne _3byte inc.b r1l mov.b r1l,@dcb mov.b r0h,@keyno rts _2byte: rts _3byte: mov.b #0,r1h mov.b r1h,@dcb mov.b r0h,@data mov.b @rsb,r0l cmp.b #b'10110000,r0l beq _hit rts _hit: mov.b @keyno,r4l mov.b #0,r4h mov.w #0,e4 mov.b @data,r0l mov.b r0l,@(m_data,er4) rts mov.b @led,r1h and.b #b'10000000,r1h or.b r1h,r0l mov.b r0l,@led mov.b r0l,@p1dr rts ;***** 100 Stepping Motors Control Routine ***** motor_control: mov.b @motor_n,r4l inc.b r4l cmp.b #100,r4l bne _motor_1 mov.b #0,r4l _motor_1: mov.b r4l,@motor_n ; R4L = motor No. (0-99) mov.b #0,r4h mov.w #0,e4 mov.b @(m_data,er4),r3h ; R3H = Rx motor parameters(7bits) mov.b r3h,r1h and.b #b'00111111,r1h ; R1H = Rx speed data (0-63) bne _motor_2 rts _motor_2: mov.b @(m_ctr,er4),r2h dec.b r2h beq _motor_3 mov.b r2h,@(m_ctr,er4) rts _motor_3: mov.b r1h,@(m_ctr,er4) mov.b @(m_phase,er4),r2l btst #6,r3h beq _motor_4 or.b #b'01000000,r2l dec.b r2l bra _motor_5 _motor_4: inc.b r2l _motor_5: and.b #b'00000111,r2l mov.b r2l,@(m_phase,er4) mov.b #0,r2h mov.w #0,e2 mov.b @(table,er2),r3h ; R3H = 4bit motor data mov.b @motor_n,r1l shlr.b r1l ; R1L = Latch No. (0-49) mov.b #0,r1h mov.w #0,e1 mov.b @(latch,er1),r2h ; R2H = Latch (2motors) old data btst #0,r4l bne _motor_6 and.b #b'11110000,r2h bra _motor_7 _motor_6: and.b #b'00001111,r2h shal.b r3h shal.b r3h shal.b r3h shal.b r3h _motor_7: or.b r3h,r2h mov.b r2h,@(latch,er1) ; R2H = Latch (2motors) new data mov.b r2h,@pbdr mov.b r2h,@p1dr mov.b r1l,@padr or.b #b'10000000,r1l mov.b r1l,@padr and.b #b'00111111,r1l mov.b r1l,@padr rts .end