![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | LinkBack | Thread Tools | Display Modes |
| | (permalink) |
| Hi, I'm pretty new to all this, having recently bought a Velleman VM111 programmer board and PIC 16F627 chip. I am working in assembly language. My plan is to have a stepper motor rotate a turntable to predefined positions. I can drive the motor and I can place positions in EEprom as the number of steps from my zero position. I can put things into EEPROM and access them, I can drive a stepper motor in both directions. I am currently slightly clueless as to how I move the motor from one position to the next. For example: If position 1 is at step 24 (90 degrees from zero [the motor has 3.75 degree steps]) and pos 2 is at step 32 (120 degrees), then I want the motor to move 8 steps clockwise. If position 3 is at step 4 (15 degrees from zero) then the motor would be best moving anticlockwise 28 steps as opposed to continuing clockwise for 76 steps. I hope this is clear! Am I right in thinking I can't have negative binary numbers? I know SUBLW will come into is somewhere, but what happens if I subtract a large number from a small one? How can I find out which number is the larger? Do I need "If, Then and Else"? I assume they exist in assembly language? Any pointers would be more than welcome, but please remember that I am new to all this and the learning curve, though fun, is steep. | |
| |
| | (permalink) |
| You can add negative binary numbers, Code: ; if w contains 32 then addlw 0x100-d'12' ;will result in w containing 20 ;the 0x100 is to stop the assembler issuing a warning. Mike. | |
| |
| | (permalink) |
| markaut, The PIC handles signed 8 bit numbers. Just test the result and perform a 2's complement on it if it's negative (bit 7 = 1). If you simulate the following code and "watch" the posFm, posTo, steps, and direction variables you'll find that going from position 32 to position 20 is 12 steps in a negative direction.... Mike Code: ;
movf posFm,W ; current position
subwf posTo,W ; going to
movwf steps ; number of steps, -127..128
clrf direction ; indicate positive direction
btfss steps,7 ; negative? yes, skip, else
goto cont ; branch (positive, all done)
bsf direction,0 ; indicate negative direction
comf steps,F ; 2's complement result
incf steps,F ;
;
; direction = 0 (positive) or 1 (negative)
; steps = number of steps to target
;
cont: Code: ;
; same thing optimized a bit
;
movf posFm,W ; current position
subwf posTo,W ; going to
movwf direction ; bit 7 is direction
btfsc direction,7 ; negative? no, skip, else
sublw 0 ; 2's complement W
movwf steps ; number of steps
;
; direction bit 7 = 0 (positive) or 1 (negative)
; steps = number of steps to target
; Last edited by Mike, K8LH; 15th August 2008 at 01:36 PM. | |
| |
| | (permalink) |
| Thanks Guys, I have had a bash at making my own code, though it is nowhere near as concise as yours Mike, so thank you for the pointers, I still have plenty to learn. I'll have a go with the simulator this weekend and see how far I get. It's a long time since I learnt a new programming language and I forgot the thrill of 'discovering' all the things that are possible. In truth I already have code and a prototype circuit which will do what I originally wanted. Problem is that as I learn new things I cant stop trying to make things better. I wish I had discovered PICs many years ago, I would have had far more success with my (then over ambitious) electronics projects. | |
| |
| | (permalink) |
| Or, you might use the negative/positive result something like this; Mike Code: ;
Chg_position
movf posFm,W ; current position
subwf posTo,W ; going to
movwf steps ; number of steps
;
btfss steps,7 ; skip if negative, else
goto step_pos ; branch
step_neg
call step_anticw ; 1 step anti clockwise
incfsz steps,F ; done? yes, skip, else
goto step_neg ; do another step
return ;
step_pos
call step_cw ; 1 step clockwise
decfsz steps,F ; done? yes, skip, else
goto step_pos ; do another step
return ; | |
| |
| | (permalink) |
| That is more like what I was putting together, though I was not as tidy about it. Shows I was at least thinking in the right direction even if my coding wasn't up to the task. Many thanks, I'll put it all together this weekend and see how it runs in the simulator. -- Mark. | |
| |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| | ||||
| Title | Starter | Forum | Replies | Latest |
| BCD Arithmetic unit-please help! | elec123 | Electronic Projects Design/Ideas/Reviews | 1 | 8th February 2005 01:44 AM |