drivers icon indicating copy to clipboard operation
drivers copied to clipboard

Using Rotary Encoders with Blue Pill STM32F103

Open olablt opened this issue 5 years ago • 2 comments

I have Blue Pill board and Rotary Encoder connected. I can not find any info on how to implement interrupts on GPIO input pins.

Are there any attempts to make a driver for encoder?

Arduino library code: https://github.com/PaulStoffregen/Encoder/blob/master/Encoder.h

olablt avatar Aug 30 '20 06:08 olablt

Well...this might work, except that machine.PinInputPullup has not been implemented on Bluepill yet (I only tested it on a BBC micro:bit). You would have to use external pull-ups.

package main

import (
	"machine"
	"time"
)

func main() {

	clk := machine.PB9
	dta := machine.PB8
	sw := machine.PB7
	clk.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
	dta.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
	sw.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
	var clkNow, clkPrv bool

	for {

		if !sw.Get() {
			println("Switch pressed")
			time.Sleep(time.Millisecond * 250)
		}

		clkNow = clk.Get()
		if clkNow != clkPrv && clkNow {
			if dta.Get() != clkNow {
				println("Turing clockwise")
			} else {
				println("Turing anti-clockwise")
			}
		}
		clkPrv = clkNow

		time.Sleep(time.Millisecond * 1)

	}

}

alankrantas avatar Sep 01 '20 13:09 alankrantas

This code worked for me on RPi Pico (with minor changes in pin names). Clockwise and anti-clockwise were reversed in my case, but that's easy to change. tinygo version 0.27.0 linux/amd64 Thanks, @alankrantas !

pavelanni avatar Jul 16 '23 22:07 pavelanni