零知开源教程-编码器示例

代码

1、零知增强板编码器示例

这里直接使用多文件编程方式,把CPP文件和ino文件放置在一起,主文件如下:


												
	/**********************************************************
	*    文件: hal_encoder.ino      by 零知实验室([url]www.lingzhilab.com[/url])
	*    -^^- 零知开源,让电子制作变得更简单! -^^-
	*    时间: 2019/10/10 11:55
	*    说明: 
	************************************************************/
	#include "STM32Encoder.h"
	 
	//TIM1 PA_8,PA_9  -34,1
	//TIM3 PA_6,PA_7  -A6,A7
	//TIM4 PB_6,PB_7  -21,20
	//TIM8 PC_6,PC_7  -30,31
	 
	//argument:
	//Timer,
	//Timer prescaler,
	//pulses per Revolution on the encoder
	STM32Encoder rudder(TIM4,PB_6,PB_7, 1, 1024);
	 
	void setup() {
	    // put your setup code here, to run once:
	    Serial.begin(9600);
	     
	    rudder.begin();
	     
	}
	 
	uint32_t tprint;
	 
	void loop() {
	    //Report what you are doing...
	    if (millis() - tprint > 1000) {
	        tprint= millis();
	        Serial.print("Counts: ");
	        Serial.println(rudder.value());
	        Serial.print("direction: ");
	        Serial.println(rudder.getDirection());
	 
	    }
	     
	}
						

完整的工程附件:


STM32Encoder_f4.7z(点击下载)

2)测试,这里就使用一个模拟的信号来进行测试,使用下边这个编码器示例进行测试。

2、标准板编码器:

												
					#include "STM32Encoder.h"
					 
					//Variables for the encoder
					//prescaler must be between 1 and 2^16 
					//a prescaler of 2 will get half the counts.
					STM32Encoder rudder(TIMER2, COUNT_BOTH_CHANNELS, 1, 1024);
					unsigned long turns = 0; //this can count the number of turns the encoder gives. 
					 
					void countTurns() {
					  if (rudder.getDirection() == 1)
					    turns ++; 
					  else
					    turns--;
					  }
					 
					//Serial Port Variables
					unsigned long tprint = 0; 
					 
					//Simulation variables
					unsigned int freq = 250;
					unsigned long tim = 0;
					char dir = 'C';
					unsigned int mode = 0; 
					unsigned char states[4];
					 
					 
					void setup() {
					  // put your setup code here, to run once:
					  Serial.begin(19200);
					 
					//input for the Timer channels
					  pinMode(D10, INPUT_PULLUP);  //channel 1
					  pinMode(D11, INPUT_PULLUP);  //channel 2
					 
					//attach Interrupt to count the overflows/turns
					rudder.attachInterrupt(countTurns);
					 
					//Simulation setup
					  states[0] = 0; //00
					  states[1] = 1; //01
					  states[2] = 3; //11
					  states[3] = 2; //10
					//Simulator outputs  
					  pinMode(D4, OUTPUT);  //channel 1
					  pinMode(D5, OUTPUT);  //channel 2
					}
					 
					 
					void loop() {
					  //Report what you are doing...
					  if (millis() - tprint > 1000) {
					    tprint= millis();
					    Serial.print("Counts: "); 
					    Serial.println(rudder.value());
					    Serial.print("direction: ");
					    Serial.println(rudder.getDirection());
					    Serial.print("turns: ");
					    Serial.println(turns);
					  }
					//simulate encoder signals.   
					  if ( millis() - tim >= freq) { 
					    tim = millis(); //prepare next
					     
					    if (dir == 'F')  mode++;
					    else mode --;
					     
					    digitalWrite(D4, (states[mode%4] & 0x01));
					    digitalWrite(D5, (states[mode%4] >> 1));
					  }
					 
					}	
						

完整工程


STM32Encoder_f1.7z(点击下载)