Connection_2_Fritzing

SK40C – Controlling MDS40A

* MDS40A has been discontinued, please check MDS40B (Enhanced SmartDrive 40) as replacement.

Introduction

As discussed in my previous tutorial, I have explained about controlling SmartDrive40 (MDS40A) with Arduino-Due. At this time, I will explain further to use SK40C to drive two MDS40A. First of all, let me introduce a little bit about SK40C. It is another enhanced version of 40 pins PIC microcontroller start-up kit and offer an easy to start board for PIC MCU user. However, all interface and program should be developed by user. As shown in Figure 1, it comes with basic elements for user such as 2 programmable push buttons, 1 RESET button, connector of UIC00A/B Programmer (to load program into PIC), LED indicator, 40 pins IC socket for PIC MCU, UART connector, USB connector and so forth.

 

Figure 1: SK40C (newset version) Basic Elements Overview

**Note: Please refer to SK40C User Manual for further informations.

For this time, we need to use another software which is MPLAB IDE to program the PIC microcontroller. Thus, the programming technique for MPLAB IDE is no as easy as Arduino IDE because it does not given any library for peripherals such as LCD, UART and so on with the software and so the user have to write any function library before write the main program. Furthermode, for PIC microcontroller, we need to make the configuration on registers by setting/clearing the flag to make specific function works. Therefore, the programming environment for PIC microcontroller is harder than Arduino and also difficult to manipulate with. Here are some tutorials about how to create project, build and load the code for MPLAB IDE software as in the following links:

1. To create project, build and load the code for MPLAB IDE, please refer to the tutorial here.

2. To create project, build and load the code for MPLABX IDE, please refer to the tutorial here.

Therefore, in this tutorial, I will explain some sample code to drive the two DC motors in sign-magnitude PWM mode, locked-antiphase PWM mode and simplified serial mode. Thus, all of the sample code that given in this tutorial will only drive two DC motors in clockwise by pressing SW1 and in counter-clockwise by pressing SW2. Anyway, there already have the sample source code uploaded in Cytron website to drive 1 DC motor with 1 MDS40A, please download it at MDS40A product page here. To drive two DC motors, you can download the sample code in this tutorial. Let’s begin!

Requirements

Main Hardware list:

(i) SmartDrive40 (MDS40A): 2 Units

MDS40A_thumb.jpg

  (ii) Enhanced 40 pins start-up kit (SK40C): 1 Unit

SK40C

(iii) USB ICSP PIC Programmer (UIC00B): 1 Unit

UIC00B-222x296

   (iv) DC motors: 2 Units

dc-geared-motor-107-800x800

Main Accessories list:
(i) Male-to-Male Jumper Wire (WR-JW-MM65)

Jumpermale-male1-150x150

     (ii) 12V/7.0Ah Lead Acid (SLA-12-70): 1 Unit

12v-7-0ah-lead-acid-82-280x373

(iii) Adapter 12V 2A (AD-12-2): 1 Unit

adapter-12v-2a-3187-800x800

 (iv) Cable for UIC00B (WR-UIC): 1 Unit

4994-280x373

(v)  USB MiniB Cable 2.0 (WR-USB-M): 1 Unit

usb-minib-cable-2-0-5194-800x800

 (vi) Screw drivers (‘+’ and ‘-’): 1 Unit Each

pro-skit-4-in-1-screw-driver-set-2119-280x373

                                                                                                                          

Main Software list:

(i) MPLAB IDE V8.86 – download from here.

(ii) HI-TECH C Compiler for PIC10/12/16 (MCUs) – download V9.83 from here.

(iii) PICKit2 V2.61 – download from here.

 

Methodologies

Firstly, to interface SK40C with MDS40A, we need to select suitable PIC microcontroller such as PIC16F877A and then write the appropriate program using MPLAB IDE to control the DC motors. Since there are some hobbyist still interested in programming PIC microcontroller rather than Arduino microcontroller, so for this time I will explain how to controlling two DC motors with two MDS40A by using SK40C with PIC16F877A (datasheet) in this tutorial. To be short, I will explain only its basic connections and programming ideas since the descriptions of MDS40A has been discussed at last time. Also, the completed sample code for interface MDS40A in sign-magnitude PWM mode, locked-antiphase PWM mode and Simplified serial mode will be attached at the end of this tutorial.

 

PROJECT 1: Interface SK40C with MDS40A in PWM Mode

Introduction 

There are two types of PWM mode will be introduced in this tutorial which is the sign-magnitude PWM and locked-antiphase PWM. Basically, the connections of these two types of PWM mode are almost the same. But, there are some differences between them which are the connections to MDS40A and also their speed control method.

Firstly, we talk about the connections. For IN2 of MDS40A, we need connect it to the PIC digital output pin for sign-magnitude PWM mode so that we can change the motor direction but we no need to connect it for locked-antiphase mode because the speed and direction can be controlled by IN1 of MDS40A.  Meanwhile, for IN1 of MDS40A, it should be connected to PWM output port of the PIC so that we can vary the speed of the motor for sign-magnitude PWM mode and also vary the speed and direction for locked-antiphase PWM mode.

Apart from that, the speed control method for these two types of PWM mode is also different. It is done by programming. By referring to PIC16F87XA datasheet, there are two PWM output ports which are RC1 and RC2 available in PIC for the user, but we need to configure these two ports to be PWM by programming. By the way, we can set the motor speed by changing the PWM duty cycle register value, i.e. CCPR1L or CCPR2L. For example, to operate in sign-magnitude PWM mode and 8 bits PWM, we can set CCPRxL from 0 to 255 to change the motor speed. But for locked-antiphase PWM, it like a double-sided mode which we can set CCPRxL from 0 to 127 to turn the motor in counterclockwise (CCW) direction with variable speed and then set CCPRxL from 127 to 255 to turn the motor in clockwise (CCW) direction with variable speed also.

In the following sections, I will explain the connections and programming of these two types of PWM mode in separate sections:

 

Section 1: Sign-magnitude PWM mode

Connection Diagrams and Descriptions

 

   Figure 2: Completed working connection for sign-magnitude PWM mode

Figure 3: Full connection for Sign-magnitude PWM mode in Fritzing 

 

The following table shows the summary of the connection between MDS40A and SK40C:

MDS40A side SK40C side
IN1 (Left) RC2 (PWM with CCP1 module)
IN1 (Right) RC1 (PWM with CCP2 module)
IN2 (Left) RC3
IN2 (Right) RC0
Both +5V No connection needed
Both GND GND

**Notes:

1.  Please use the Male-to-Male jumper wires to connect them.

2. For sign-magnitude PWM mode, set the DIP switch to 10010001 (MCU mode ON) as shown below:

3. The +5V in MDS40A is the output supply, so please do not connect to another 5V source and just leave it no connected.

4. To power up the MDS40A, connect 12V battery to the Dean-T connector properly. Make sure the polarity of the battery is correctly connected to avoid damage on MDS40A.

5. Please refer to MDS40A User Manual for further information.

6. To view the full connection, please refer to Figure 3.

 

Programming 

In the following parts, we will discuss about the important portions of the sample source code to drive two DC motors in sign-magnitude PWM mode:

(a) To initialize the PWM mode, we need to configure both the CCP1 and CCP2 module to be PWM mode so that we can use them to control 2 DC motors. Here are the sample codes which are written in a separate function:

void pwm_initialize(void)
{
// Setting PWM frequency = 4.88khz
// using PIC16F877A with 20MHz Crystal
PR2 = 0xFF;
T2CKPS1 = 0;
T2CKPS0 = 1; // Timer 2 prescale = 4.
SPEED1 = 0;	// SPEED1=CCPR1L, SPEED2=CCPR2L
SPEED2 = 0; // Duty cycle = 0%.
TMR2ON = 1; // Turn on Timer 2.
// Configure CCP1 module to operate in PWM mode.
CCP1M3 = 1;
CCP1M2 = 1;
// Configure CCP2 module to operate in PWM mode.
CCP2M3 = 1;
CCP2M2 = 1;		
CCP1X = 0;
CCP1Y = 0;
CCP2X = 0;
CCP2Y = 0;
}

(b) The following show the main program of the sample code. It starts with initialization and declaration of I/O port and some registers. Also, it will call some subroutine to execute the function. For this mode, set CCP1L or CCP2L as 0 will stop the motor but set as 255 to drive the motor in full speed. Then, for the digital output port that connected to IN2 of MDS40A, it set HIGH for turn the motor in clockwise direction (CW) and set LOW to turn the motor in counter-clockwise direction (CCW). After that, the main program is executed within a forever loop. Here are the sample code:

int main(void)
{
// Set all pins on Port A as digital I/Os.
ADCON1 = 0b00000110;
// Initiate value for output pin
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
PORTE = 0;
// Initialize the I/Os.
TRISB = 0b00000011;
TRISC = 0;
// Initialize PWM
pwm_initialize();		
// Sign-Magnitude PWM mode.
// The DIP switch must be set to 10010001.
while (1) {
// If switch 1 is pressed, motor rotate in CW at full speed.
if (SW1 == 0) {
MDS40A_L_IN2 = 0;
MDS40A_R_IN2 = 0;
SPEED1 = 255;
SPEED2 = 255; // Duty cycle = 100%.
}	
// If switch 2 is pressed, motor rotate in CCW at full speed.
else if (SW2 == 0) {
MDS40A_L_IN2 = 1;
MDS40A_R_IN2 = 1;
SPEED1 = 255;
SPEED2 = 255; // Duty cycle = 100%.
}	
// If no switch is pressed, stop the motor.
else {
SPEED1 = 0;
SPEED2 = 0; // Duty cycle = 0%.
}
}
// Infinate loop at the end of program, prevent the PIC from reset
while(1) continue;
}

**Note: The completed sample source code in sign-magnitude PWM mode will be attached at the end of this tutorial.

 

Section 2: Locked-antiphase PWM mode

 Connection Diagrams and Descriptions

 

   Figure 4: Completed working connection for locked-antiphase PWM mode

Figure 5: Full connection for Locked-antiphase PWM mode in Fritzing

The following table shows the summary of the connection between MDS40A and SK40C: 

MDS40A side SK40C side
IN1 (Left) RC2 (PWM with CCP1 module)
IN1 (Right) RC1 (PWM with CCP2 module)
IN2 (Left) No connection needed
IN2 (Right) No connection needed
Both +5V No connection needed
Both GND GND

**Notes:

1.  Please use the Male-to-Male jumper wires to connect them. 

2. For locked-antiphase PWM mode, set the DIP switch to 10010000 (MCU mode OFF) as shown below:

 

4. The +5V in MDS40A is the output supply, so please do not connect to another 5V source and just leave it no connected.

5. To power up the MDS40A, connect 12V battery to the Dean-T connector properly. Make sure the polarity of the battery is correctly connected to avoid damage on MDS40A.

6. Please refer to MDS40A User Manual for further information.

7. To view the full connection, please refer to Figure 5.

 

Programming

In the following parts, we will discuss about the important portions of the sample source code to drive two DC motors in sign-magnitude PWM mode:

(a) To initialize the PWM mode, we need to configure both the CCP1 and CCP2 module to be PWM mode so that we can use them to control 2 DC motors. Here are the sample codes for locked-antiphase mode which are written in a separate function:

void pwm_initialize(void)
{
// Setting PWM frequency = 4.88khz
// using PIC16F877A with 20MHz Crystal
PR2 = 0xFF;
T2CKPS1 = 0;
T2CKPS0 = 1; // Timer 2 prescale = 4.
// Stop the motor in locked-antiphase mode.
SPEED1 = 127; // SPEED1=CCPR1L,SPEED2=CCPR2L
SPEED2 = 127; // Duty cycle = 50%;
TMR2ON = 1; // Turn on Timer 2.
// Configure CCP1 module to operate in PWM mode.
CCP1M3 = 1;
CCP1M2 = 1;
// Configure CCP2 module to operate in PWM mode.
CCP2M3 = 1;
CCP2M2 = 1;
}

(b) The following show the main program of the sample code. It starts with initialization and declaration of I/O port and some registers. Also, it will call some subroutine to execute the function. For this mode, set CCP1L or CCP2L as 127 will stop the motor, set as 0 to drive the motor in counterclockwise (CCW) with full speed and set as 255 to drive the motor in clockwise (CCW) with full speed. Therefore, it need only one PWM source from microcontroller to control both the speed and direction and it should connected to IN1 of MDS40A. After that, the main program is executed within a forever loop. Here are the sample code:

int main(void)
{
// Set all pins on Port A as digital I/Os.
ADCON1 = 0b00000110; 
// Initiate value for output pin
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
PORTE = 0;
// Initialize the I/Os.
TRISB = 0b00000011;
TRISC = 0;
// Initialize PWM
pwm_initialize();				
// PWM Mode (Locked-Antiphase).
// The DIP switch must be set to 10010000.
while (1) {
// If switch 1 is pressed, forward motor at full speed.
if (SW1 == 0) {
SPEED1 = 255;
SPEED2 = 255;
}	
// If switch 2 is pressed, reverse motor at full speed.
else if (SW2 == 0) {
SPEED1 = 0;
SPEED2 = 0;
}	
// If no switch is pressed, stop the motor.
else {
SPEED1 = 127;
SPEED2 = 127;
}
}
// Infinate loop at the end of program, prevent the PIC from reset
while(1) continue;
}

**Note: The completed sample source code for locked-antiphase PWM mode will be attached at the end of this tutorial.

 

PROJECT 2: Interface SK40C with MDS40A in Serial Mode 

Introduction

At last time, since I have been explain how to use this mode to interface Arduino with MDS40A, so at this time I will apply it for SK40C. For the description about the simplified serial mode, please refer to Tutorial: Arduino – Controlling MDS40A (Project 2) or MDS40A User Manual. To operate in simplified serial mode, we need a transmitter (Tx) pin of PIC to transmit the data  to IN1 of MDS40A (treated as Receiver (Rx) pin for this mode). Therefore, by referring to PIC16F87XA datasheet, there are only 1 port in PIC can be configured as Tx pin which is RC6. Apart from that, the IN2 of MDS40A (treated as Slave Select) must be set HIGH to make it works. 

Connection Diagrams and Descriptions

 

    Figure 6: Completed working connection for Simplified serial mode

Figure 7: Full connection for Simplified Serial mode in Fritzing

Here is the summary of connection between MDS40A and SK40C:

MDS40A side SK40C side
IN1 (Left) RC6 (UART Tx pin)
IN1 (Right) RC6 (UART Tx pin)
IN2 (Left) RC1 (Slave select)
IN2 (Right) RC0 (Slave select)
Both +5V No connection needed
Both GND GND

**Notes:

1.  Please use the Male-to-Male jumper wires to connect them.

2. DIP switch should be adjusted to 00100011 to operate in simplified serial mode as shown below:

3. The +5V in MDS40A is the output supply, so please do not connect to another 5V source and just leave it no connected.

4. To power up the MDS40A, connect 12V battery to the Dean-T connector properly. Make sure the polarity of the battery is correctly connected to avoid damage on MDS40A.

5. Please refer to MDS40A User Manual for further information.  

6. To view the full connection, please refer to Figure 7 in the following:

 

Programming

In the following parts, we will discuss about the important portions of the sample source code to drive two DC motors in Simplified Serial Mode.

(a) To initialiaze the UART transmit interface and also transmit the data. Thus, the baud rate that used in this sample code is 9600. Here are the two function codes:

void uart_initialize(void)
{
BRGH = 1; // Select high speed baud rate.
SPBRG = (_XTAL_FREQ / 16 / UART_BAUD) - 1; // Configure baud rate.
SPEN = 1; // Enable serial port.
TXEN = 1; // Enable transmission.
}
void uart_transmit(unsigned char uc_data)
{
// Wait until the transmit buffer is ready for new data.
while (TXIF == 0);
// Transmit the data.
TXREG = uc_data;
}

(b) The following show the main program of the sample code. It starts with initialization and declaration of I/O port and some registers. Also, it will call some subroutine to execute the function. For this mode, sending byte 127 through UART will stop both motors, send byte 0 to drive both motors in counterclockwise (CCW) with full speed and send byte 255 to drive both motors in clockwise (CW) with full speed. Then, the digital output pin that connected to IN2 of MDS40A must always be set HIGH. After that, the main program is executed within a forever loop. Here are the sample code:

int main(void)
{
// Set all pins on Port A as digital I/O.
ADCON1 = 0b00000110;
// Initiate value for output pin
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
PORTE = 0;
// Initialize the I/Os.
TRISB = 0b00000011;
TRISC = 0;	
uart_initialize();
// The Slave Select (IN2) for MDS40A set HIGH
MDS40A_L_IN2 = 1;
MDS40A_R_IN2 = 1;
// Simplified Serial Mode.
// The DIP switch must be set to 00100011.
while (1) {
// If switch 1 is pressed, forward motor at full speed.
if (SW1 == 0) {
uart_transmit(255);
}	
// If switch 2 is pressed, reverse motor at full speed.
else if (SW2 == 0) {
uart_transmit(0);
}	
// If no switch is pressed, stop the motor.
else {
uart_transmit(127);
}
}
// Infinate loop at the end of program, prevent the PIC from reset
while(1) continue;
}

**Note: The completed sample source code for simplified serial mode will be attached at the end of this tutorial.

In a nutshell, if compare to Arduino, the PIC is much difficult to be mastered, but it is cheaper. So, it is good for this tutorial since we need only do some testing on MDS40A to drive the DC motors in different mode. Meanwhile, we need some basic programming knowledge in PIC to understand well how it make the MDS40A works. Also, for the advance user, the programming technique is quite important because they need it to let the microcontroller do the right things. The learning process is time consuming, but it is worth as long as the result is meaningful for us 🙂

That’s all from me, THANK YOU! See you again next time.  😉

 

Attachments

1. Sign-magnitude PWM mode completed sample code, download from here.

2. Locked-antiphase PWM mode completed sample code, download from here.

2. Simplified Serial mode completed sample code, download from here.

Buy

, , , , ,

Related Post

20160330_085520-e1459299689529-400x323

Control and Monitor via SMS Part 2

Peak Current vs Continuous/Rated Current

gambar full robot

Raspberry Pi: Line Following Robot

Espresso Lite V2.0

Smartphone Controlled Wheel Robot

Leave a Reply

Your email address will not be published. Required fields are marked *