DC Motor Control With Rotary Encoder and PIC MCU - Mikroc Projects
DC Motor Control With Rotary Encoder and PIC MCU - Mikroc Projects
DC Motor Control With Rotary Encoder and PIC MCU - Mikroc Projects
Home PIC Projects Arduino Projects ESP8266 Projects MikroC Projects CCS C Projects MPLAB Projects PCBWay
PCBONLINE Contact
Last time I made a simple controller circuit that controls speed and direction of
Labels:
rotation of DC motor, project link is below:
7-SEGMENT 74HC595 ADC
DC Motor control with PIC microcontroller | mikroC Projects
BLDC MOTOR BME280 BMP280
Archives
The rotary encoder has 5 pins: GND, + (+5V or 3.3V), SW (push button), DT (pin B) and
March 2020
CLK (pin A).
February 2020
As an addition to the rotary encoder there is a push button and three pull up
January 2020
resistors for pins SW, DT and CLK of 10k ohm each. With the three pull-up resistors,
December 2019
the normal state of each terminal is logic high. November 2019
The rotary encoder generates (when rotating) two square waves on pins A (CLK) and October 2019
B (DT) with 90° out of phase as shown in the figure below: September 2019
August 2019
July 2019
June 2019
May 2019
April 2019
March 2019
February 2019
As shown in the figure, the rotary encoder has 4 phases: January 2019
Facebook Page:
Recent Posts
The L293D driver has 2 VCCs: VCC1 is +5V and VCC2 is +12V (same as motor nominal
Sponsored Links:
voltage). IN1 and IN2 are the control pins where:
L H Direction 1
H L Direction 2
The PIC16F887 generates a PWM signal on pin RC2 (#17) using CCP1 module (CCP:
Capture/Compare/PWM), this pin is connected to EN1,2 pin of the L293D chip. IN1
and IN2 pins are connected to RD0 and RD1 respectively (they can be reversed).
The rotary encoder push button terminal is connected to pin RB2, with this button
we can change the direction of rotation of the motor. CLK and DT pins are connected
to RB0 and RB1 respectively.
In this project the PIC16F887 microcontroller uses its internal oscillator @ 8 MHz,
MCLR pin is configured as an input pin.
Rotary encoder pushbutton (connected to RB2) and the control lines (connected to
RD0 and RD1) are defined in the code as shown below:
C
1 // define rotary encoder button pin connection
2 #define SW RB2_bit
3
4 // define motor control pins
5 #define IN1 RD1_bit
6 #define IN2 RD0_bit
Internal weak pull-up is enabled for pin RB2, the following lines are used for that:
C
1 // enable RB1 internal pull up
2 NOT_RBPU_bit = 0; // clear RBPU bit (OPTION_REG.7)
3 WPUB = 0x04; // WPUB register = 0b00000100
PORTB interrupt-on-change is enabled for pins RB0 and RB1 which are respectively
connected to CLK and DT pins of the rotary encoder. This interrupt detects falling
and rising of the two lines:
C
1 INTCON = 0xC8; // enable global, peripheral and PORTB change interrupt
2 IOCB = 0x03; // enable RB0 & RB1 pin change interrupt
C
1 /***********************************************************************
2
3 DC Motor control with PIC16F887 MCU and rotary encoder.
4 C Code for mikroC PRO for PIC compiler
5 Internal oscillator used @ 8MHz
6 Configuration words: CONFIG1 = 0x2CD4
7 CONFIG2 = 0x0700
8 This is a free software with NO WARRANTY.
9 https://simple-circuit.com/
10
11 ************************************************************************
12
13 // define rotary encoder button pin connection
14 #define SW RB2_bit
15
16 // define motor control pins
17 #define IN1 RD1_bit
18 #define IN2 RD0_bit
19
20
21 bit direction;
22 char last_read;
23 short quad = 0, change;
24 int motor_speed = 0;
25
26 void Interrupt()
27 {
Share this:
Leave a Reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.