Page 247 - Robots Androids and Animatrons : 12 Incredible Projects You Can Build
P. 247

the program halts the stepper motor and stays in a holding loop for
                                as long as SW3 is closed (or pressed). Rotation direction, CW or
                                CCW, is controlled with SW4. Pressing SW4 reverses the stepper
                                motor direction. The direction stays in reverse for as long as SW4
                                is pressed (or closed).
                                ‘Stepper motor controller
                                Symbol TRISB = 134  ‘Initialize TRISB to 134
                                Symbol TRISA = 133  ‘Initialize TRISA to 133
                                Symbol PortB = 6  ‘Initialize portb to 6
                                Symbol PortA = 5  ‘Initialize porta to 5
                                symbol ti = b6  ‘Initial ti delay
                                ti = 100  ‘Set delay to 100 ms
                                Poke TRISB,0  ‘Set PORTB lines output
                                start:   ‘Forward stepper motor rotation sequence
                                poke portb,1  ‘Step 1
                                pause ti  ‘Delay
                                poke portb,2  ‘Step 2
                                pause ti  ‘Delay
                                poke portb,4  ‘Step 3
                                pause ti  ‘Delay
                                poke portb,8  ‘Step 4
           226                  pause ti  ‘Delay
                                goto check  ‘Jump to check switch status
                                start2:   ‘Reverse motor rotation sequence
                                poke portb,8  ‘Step 1
                                pause ti  ‘Delay
                                poke portb,4  ‘Step 2
                                pause ti  ‘Delay
                                poke portb,2  ‘Step 3
                                pause ti  ‘Delay
                                poke portb,1  ‘Step 4
                                pause ti  ‘Delay
                                goto check  ‘Jump to check switch status
                                Check:   ‘Switch status
                                Peek PortA, B0  ‘Peek the switches
                                If bit0 = 0 then loop1  ‘If SW1 is closed, increase ti
                                if bit1 = 0 then loop2  ‘If SW2 is closed, decrease ti
                                if bit2 = 0 then hold3  ‘Stop motor
                                if bit3 = 0 then start  ‘Go forward
                                goto start2  ‘Go reverse
                                loop1:   ‘Increase delay
                                poke portb,0  ‘Turn off transistors
                                ti = ti + 5  ‘Increase delay by 5 ms
                                pause 50  ‘Delay
                                                       Team LRN
            Chapter ten
   242   243   244   245   246   247   248   249   250   251   252