-
Notifications
You must be signed in to change notification settings - Fork 30
/
encoder.py
57 lines (45 loc) · 1.51 KB
/
encoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import pyb
import stm
# This script sets up a timer to do quadrature decoding
#
# It was tested using a switch similar to https://www.sparkfun.com/products/9117
pin_a = pyb.Pin('X1', pyb.Pin.AF_PP, pull=pyb.Pin.PULL_NONE, af=pyb.Pin.AF1_TIM2)
pin_b = pyb.Pin('X2', pyb.Pin.AF_PP, pull=pyb.Pin.PULL_NONE, af=pyb.Pin.AF1_TIM2)
enc_timer = pyb.Timer(2, prescaler=1, period=100000)
enc_channel = enc_timer.channel(1, pyb.Timer.ENC_B)
def dump_regs(timer_base):
smcr = stm.mem16[timer_base + stm.TIM_SMCR]
sms = smcr & 0x0007
ece = (smcr & 0x4000) >> 14
print('SMS = {:03b} ECE = {:1b}'.format(sms, ece))
dump_regs(stm.TIM2)
out_idx = 0
out_seq = [0, 1, 3, 2]
pin_a2 = pyb.Pin('X3', pyb.Pin.OUT_PP)
pin_b2 = pyb.Pin('X4', pyb.Pin.OUT_PP)
def set_out():
print("Writing {:d}{:d}".format((out_seq[out_idx] & 0x01) != 0, (out_seq[out_idx] & 0x02) != 0))
pin_a2.value((out_seq[out_idx] & 0x01) != 0)
pin_b2.value((out_seq[out_idx] & 0x02) != 0)
def incr():
global out_idx
out_idx = (out_idx + 1) % 4
set_out()
def decr():
global out_idx
out_idx = (out_idx - 1) % 4
set_out()
if False:
set_out()
while True:
for i in range(10):
print("Counter =", enc_timer.counter(), " channel =", enc_channel.capture());
incr()
for i in range(10):
print("Counter =", enc_timer.counter(), " channel =", enc_channel.capture());
decr()
break
else:
while True:
print("Counter =", enc_timer.counter());
pyb.delay(200)