plz i want an example code so that i can deal with the encoders.
note that i’m using basic atom pro28
thanks a lot
If I remember correctly you earlier asked for this in either C or C++, which unfortionatly I do not have… I have written a few different versions before in Basic with inline assembly support. For example: here is a version of the code that I did for another member who was using a rotary switch that had an equivelent of a encoder on the end.
SButtonCounter var sword
...
ONASMINTERRUPT IRQ1INT, HANDLE_IRQ1
input p10 ; make sure p10 is setup for input
PMR1.bit5 = 1 ; enable pin to IRQ1 interrupt instead of normal I/O
IEGR1.bit1 = 1 ; Interrupt IRQ1 on rising edge
ENABLE IRQ1INT
enable
; SButtonCounter will have the encoder count...
...
;==============================================================================
; OK lets try an ASM version of Encoder handler...
; Two IO pins 8: 10. : Pin 8 is IRQ1.
;==============================================================================
BEGINASMSUB
HANDLE_IRQ1
push.w r1 ; first save away R1 as we will mess with it.
bclr #1,@IRR1:8 ; clear the IRQ1 bit in the interrupt pending mask
andc #0x7f,ccr ; allow other interrupts to happen
; bset.b #5,@PCR8:8 ; make sure set to output, basic should have done earlier!
; bnot.b #5, @PDR8:8 ; Ok lets try setting one LED ON to debug
; make sure P10 is in input mode. - Should have been done earlier in basic...
; mov.b @(PDR8-0xd0+0x70), r0l ; get the current value for PCRx byte from the shadow location
; bclr.b #2, r0l ; make sure P82 is cleared
; mov.b r0l, @(PDR8-0xd0+0x70) ; update the shadow location
; mov.b r0l, @PCR8 ; update the actual direction
mov.w @SBUTTONCOUNTER:16,r1 ; Get our current count
; get the state of Pin 10->P82, will get directly should probably setup more general...
bld.b #2, @PDR8:8 ; Get the state of Pin10 into carry
bxor.b #1,@IEGR1:8 ; XOR it with which edge we are interrupting on...
bcc _HI1EQ:8 ; Edge is equal to Pin10
dec.w #1,r1 ; edges different decrement our count
jmp _HI1UPDC:8 ; go to save our updated count
_HI1EQ:
inc.w #1,r1 ; edge is equal to increment our count
_HI1UPDC:
mov.w r1,@SBUTTONCOUNTER:16
bnot.b #1,@IEGR1:8 ; And update which edge we are looking at
pop.w r1
rte
ENDASMSUB
The interrupt handler here is written in ASM for speed. You could try to convert to basic, but you may miss some of the encoder information.
Good Luck
Kurt
Sorry to have to tell ya. I don’t think the Atom Pro can do this without resorting to complex inline assembly code, which is not for the beginner programmer. I think the encoder generates pulses at a rate too fast for the processor to keep up with. Basic Micro has some new motor controllers coming out soon that do use the encoders. I do not know when it will be ready, but it shouldn’t be too much longer.
so what should i do for now according to robot dude reply
i’m lost now
if u have a sample code with basic, this will help
thanks in advance
Kurt just gave you a possible solution. Look into it. Beyond that, I don’t know what to tell ya.
okai i will try kurte’s solution
but i’m searching for assembly to basic converter and i cant find one, please if u know where can i download it, tell me
hope it will workout
Here is the problem:
Suppose your motor runs at 150RPM. With this encoder there are 100 cycles per revolution (actually 4 transition per cycle, but we only interrupt on 1 or 2 of these).
That implies that you are needing to process 15000 to 30000 interrupts per second for one of these. Alternatively if your program is inside a very tight loop you can poll the data instead of use interrupts. I have done this on some other processors before (AVR Atmega32) to be precise. It is very likely at high speeds you may miss some transitions. Have you tried any experiments yet? Try something simple like:
Encoder1Count var slong
bI18 var byte
bI18Prev var byte
; init things
; Assume that two IO pins for the encoder1 are on P16 and P18
; and encoder 2 are pins P17 and P19
; Why later you can experiment with Interrupts with IRQ2 and IRQ3 on P18 and P19
; now just dealing with Encoder 1
Encoder1Count = 0
input p16
input p18
main:
bI18Prev = bI18 ; save previous value
bI18 = in18
; Start simple increment each time there is a transition.
if bI18 <> bI18Prev then
Encoder1Count = Encoder1Count + 1
; once this is working you can now start playing with comparing the value
; of In18 vs In16 to see what type of transisiton...
endif
; maybe add some serout on every change of 50 or the like...
goto main
This is a real simple program that watches only one pin. If you google: Quadrature Encoder - you can find lots of stuff that talks about what to do with the different transitions…
Kurt
Check US Digital, there are chips to read encoders that simplify the job. I’d also consider using a dsPIC33FJ204MC family part to do the encoder stuff, then read the chip. Actually, the entire motion control stuff could be done in a PIC. JrKerr sells chips.
I believe JrKerr had an encoder chip, and used it on there PIC-SERVO board.
I hope Basic Micro is going back to the PIC chips. I’d like to see some dsPIC33 products.
Alan KM6VV