6
Using PIC18F448 in wireless DC motor control Abstract – Design of the drive circuitry of a DC motor using Microchip’s MRF24J40MA wireless module combined with a PIC18F448 microcontroller and a MOSFET H-bridge. The wireless transmission is achieved in the 2.4 Ghz frequency band and the above mentioned microcontroller communicates with the MRF module via SPI interface. Keywords: microcontroller, wireless, H- bridge I. INTRODUCTION A host computer will generate command words to drive the DC motor which are decrypted at reception by the PIC microcontroller. Thus one can implement actuation based upon a predefined command table stored locally in the PIC’s Data EEPROM. The transmission is achieved using a PIC18F448 communicating with the host PC via RS232 serial interface and connected to the MRF24J40MA wireless module provided by Microchip. Reception is implemented with a similar MRF24J40MA module connected to a PIC18F448 microcontroller using the SPI interface. Also this unit drives a full MOSFET H-bridge suited for a DC motor control thus implementing a forward or reverse mode. II. SYSTEM OVERWIEV Basically, the entire device can be illustrated as it is shown in Fig.1. Fig.1. System diagram Fig.2. indicates a detailed bloc diagram of the transmission unit (T.U.). Fig.2. Transmission unit diagram A serial level converter (S.L.C.) is needed to match the voltage levels of the serial RS232 interface of the host PC to the TTL logic levels of the PIC microcontroller’s similar interface. A MAX232 circuitry will do the job. According to drive the MRF wireless module, some control signals are needed to be connected to the PIC18F448, stated as “Control” in Fig.2., beside the usual SPI interface signals [1]. The receiving unit (R.U.) contains a similar MRF module and a PIC18F448 microcontroller which not only controls the wireless module but stores the DC motor control data sent by the host PC, decrypts the values for spin direction and speed, drives the MOSFET H-bridge and processes the feedback signals from the DC motor. Fig.3. depicts the bloc diagram of the receiving unit Host PC T.U. Wireless RF R.U. H Bridge M Feedback Wireless RF RS232 S.L. C. RS232 PIC 18F44 8 MRF SPI Contr ol RF

PIC18F448 in Wireless DC Motor Control

Embed Size (px)

Citation preview

Page 1: PIC18F448 in Wireless DC Motor Control

Using PIC18F448 in wireless DC motor control

Abstract – Design of the drive circuitry of a DC motor using Microchip’s MRF24J40MA wireless module combined with a PIC18F448 microcontroller and a MOSFET H-bridge. The wireless transmission is achieved in the 2.4 Ghz frequency band and the above mentioned microcontroller communicates with the MRF module via SPI interface. Keywords: microcontroller, wireless, H-bridge

I. INTRODUCTION

A host computer will generate command words to drive the DC motor which are decrypted at reception by the PIC microcontroller. Thus one can implement actuation based upon a predefined command table stored locally in the PIC’s Data EEPROM. The transmission is achieved using a PIC18F448 communicating with the host PC via RS232 serial interface and connected to the MRF24J40MA wireless module provided by Microchip. Reception is implemented with a similar MRF24J40MA module connected to a PIC18F448 microcontroller using the SPI interface. Also this unit drives a full MOSFET H-bridge suited for a DC motor control thus implementing a forward or reverse mode.

II. SYSTEM OVERWIEV

Basically, the entire device can be illustrated as it is shown in Fig.1.

Fig.1. System diagram

Fig.2. indicates a detailed bloc diagram of the transmission unit (T.U.).

Fig.2. Transmission unit diagram

A serial level converter (S.L.C.) is needed to match the voltage levels of the serial RS232 interface of the host PC to the TTL logic levels of the PIC microcontroller’s similar interface. A MAX232 circuitry will do the job. According to drive the MRF wireless module, some control signals are needed to be connected to the PIC18F448, stated as “Control” in Fig.2., beside the usual SPI interface signals [1].The receiving unit (R.U.) contains a similar MRF module and a PIC18F448 microcontroller which not only controls the wireless module but stores the DC motor control data sent by the host PC, decrypts the values for spin direction and speed, drives the MOSFET H-bridge and processes the feedback signals from the DC motor. Fig.3. depicts the bloc diagram of the receiving unit combined with the motor driving circuitry.

Fig.3. Receiving and driving unit

Signals from the four PWM outputs are forwarded to the MOSFET H-bridge through adequate gate drivers. We will use TC428 dual high-speed power MOSFET drivers [2].The driving algorithm can use a current feedback to regulate the motor torque and a mechanical feedback to control speed or position. These feedbacks are optional.

III. DEVICE INTERFACING

The PIC18F448 communicates with the MRF wireless module as it is shown in Fig.4.

Fig.4. Connecting PIC18F448 with the MRF24J40MA

Host PC

T.U. Wireless RF

R.U. HBridge M

Feedback

Wireless RF

RS232

S.L.C.RS232

PIC18F448

MRFSPI

Control

RF

Vcc

Gate driversMRFPIC

18F448

SPI

Control

RF

H-Bridge

Current Feedback

Mechanical Feedback

PWM x4

R

DC Motor

Page 2: PIC18F448 in Wireless DC Motor Control

The MRF24J40MA module operates in the 2.4 GHz frequency band and is FCC, IC and ETSI compliant. The integrated module design allows quick and simple usage without any supplementary regulatory compliance test, thus allowing quicker product development and time to market [1]. The module interfaces to the PIC18F448 via 3-wire serial SPI, using Serial interface data input (SDI), Serial interface data output (SDO) and Serial interface clock (SCK). In addition a serial interface enable pin is used (/CS), connected to a usual I/O line. Furthermore, interrupt, wake and Reset signals are required to fully operate the module. Excepting the interrupt, all other lines are driven by usual I/O microcontroller pins. Table 1 indicates the pin assignment to the MRF and PIC18F448 interfacing.

Table 1PIC RB0 RB1 RB2 RB3 RC3 RC4 RC5

MRF INT /CS Wake /Reset SCK SDI SDO

In order to command the MOSFET H-bridge, we will use the built in DC motor control options of the PIC’s ECCP module (Enhanced Capture/Compare Module) to provide a simple and efficient driving method. P1A, P1B, P1C and P1D are the outputs from the ECCP module (lines RD4 – RD7) and they are configured as Full Bridge mode (Forward or Reverse) [2]. Actually, lines RD4 – RD7 are directly connected to the inputs of four gate drivers to assure level compatibility and proper current drain from the port lines. Two TC428 integrated drivers with complementary outputs are used as shown in Fig.5. A TTL/CMOS input voltage level is translated into a rail-to-rail output voltage level swing. The CMOS output is within 25 mV of ground or positive supply [3].

Fig.5. Interfacing PIC18F448 to the H-bridge

According to Fig.5. all PWM outputs are active high thus turning on the respective MOSFET due to complementary outputs of TC428. Input logic signals may equal the power supply voltage and the input current is 1mA, making direct interface to CMOS microcontrollers simple and easy [3]. The H-bridge used hereby is a new generation of MOSFETs from Zetex, utilizing a unique structure that combines the benefits of low on-resistance with fast switching speed, making them ideal for high efficiency, low voltage, power management applications [4].

ZXMHC6A07T8 is a complementary 60V enhancement mode MOSFET H-bridge with low gate drive, suitable for this application. Fig.6. gives the circuit configuration and Fig.7. the pinout diagram.

Fig.6. ZXMHC6A07T8 configuration

Fig.7. ZXMHC6A07T8 pinout diagram

To implement a bidirectional DC motor control, a classic H-bridge wiring configuration is used in addition with a current feedback actually proportional with the voltage on the resistor R as depicted in Fig.8.

Fig.8. H-bridge interfacing

Pin RA1 of PIC18F448 is used as an analog input and should be configured accordingly. Optional mechanical feedback can be added to the system using pins RA2, RA3 and RA4 set as digital inputs depending on the chosen transducer (i.e. incremental). Any DC motor up to 60V/1A can be used due to the integrated ZXMHC6A07T8 H-bridge. According to the used DC motor the R resistor must be dimensioned properly regarding power dissipation.

(RD4)P1A

(RD5)P1B

(RD6)P1C

(RD7)P1D

PIC8F448

InA /OutA

InB OutB

TC428

InA /OutA

InB OutB

TC428

nMOS

pMOS

nMOS

pMOS

PIC18F448

RA1 P1A P1B P1C P1D

InA

InB

TC428/OutA

OutB

InA

InB

TC428/OutA

OutB

G1 G2 G4 G3

S2,S3 S1,S4 D1,D2 D3,D4

MR Vcc

ZXMHC6A07T8

Page 3: PIC18F448 in Wireless DC Motor Control

IV. SOFTWARE CONFIGURATION

System operation starts when the host PC is sending 8 bit command words to the transmission unit (T.U.) via the RS232 serial interface. Therefore, the T.U.’s PIC18F448 USART (Universal Synchronous Asynchronous Receiver Transmitter) is used in synchronous slave reception. Pin RC6 is assigned for the serial clock and pin RC7 for the receive Rx signal. The following steps should be achieved in software initialization:-setting the bits SYNC, SPEN;-clearing the bit CSRC from the TXSTA register;-clearing the bits CREN, SREN;-enabling USART interrupts by setting the bit RCIE from the PIE1 register.Testing if the USART Receive Interrupt Flag bit, RCIF is set, will show the end of transmission [5]. Then an interrupt request is generated and a propper subroutine will read the value from RCREG which gives the wanted command word. Each received word is stored afterwards in the data memory of the receiving unit. Table 2 shows the layout used for the command word.

Table 2Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0Direction Speed in % of the maximum rpm of the motor

“Direction” can be “0” for clockwise and “1” for counterclockwise and speed a value between 0 – 100%, encoded binary on 7 bits. The sequence 10001111 means a counterclockwise rotation with 15% of the maximum motor speed.In order to operate the MRF24J40MA, the MSSP (Master Synchronous Serial Port) module of the PIC18F448 must be initialized and entered in SPI (Serial Peripheral Interface) communication mode. Pins RC3, RC4 and RC5 are used for the SPI. Also RB0, RB1, RB2 and RB3 control the MRF, according to Table 1. Therefore PIC’s TRISB register should be loaded with the hexadecimal value 0x01 and TRISC with 0xD0. To write or read data with the MRF, SPI mode 0 must be used. The maximum SPI clock frequency must not exceed 10 MHz. Therefore if we use a 20 MHz oscillator as clock for the PIC18F448, a software division with 4 is required. To enable the port, SSPEN bit from SSPCON1 register must be set. This configures the SDI, SDO, SCK, and SS pins as serial port pins [2]. Next, bits SSPM<3:0> must be cleared (in SSPCON1 register) to achieve a division by 4 of the Fosc (20 MHz). The clock polarity must be selected by programming the CKP bit from SSPCON1, the CKE (Clock Edge) and SMP (Sample) from SSPSTAT register according to SPI waveforms illustrated in Fig.9. That means, for SPI mode 0 CKP and CKE will be cleared and the SMP set. When receiving data, bit BF (Buffer Full) from SSPSTAT should be flagged. If 0 it means empty and only if it turns to 1 indicates that SSPBUF has been loaded with the received data (transmission completed).

When the SSPBUF is read, the BF bit is cleared which is relevant only when the SPI is a receiver (used for the receiving unit).

Fig.9. SPI Master mode waveforms

Before operating with the MRF24J40MA, the init routine should perform a high level setting on /CS (by setting the RB1 pin) and also the same for the /RESET (RB3 pin). SPI setup routines must be run on both microcontrollers (on transmission and receiving units) to initialize the respective wireless modules. Initializing the MRF24J40MA means to issue a sequence of auxiliary operations like setting the transmission channel and enabling or disabling the interrupts. Also, on start-up, certain control register values must be initialized for basic operations [1]. First initialization steps are shown as follows.

1. SOFTRST (0x2A) = 0x07 – Perform a software Reset;2. PACON2 (0x18) = 0x98 – Initialize FIFOEN = 1 and TXONTS = 0x6;3. TXSTBL (0x2E) = 0x95 – Initialize RFSTBL = 0x9;4. RFCON1 (0x201) = 0x01 – Initialize VCOOPT = 0x01;5. RFCON2 (0x202) = 0x80 – Enable PLL (PLLEN = 1);6. RFCON6 (0x206) = 0x90 – Initialize TXFIL = 1 and 20MRECVR = 1;7. RFCON7 (0x207) = 0x80 – Initialize SLPCLKSEL = 0x28. RFCON8 (0x208) = 0x10 – Initialize RFVCO = 1;9. SLPCON1 (0x220) = 0x21 – Initialize CLKOUTEN = 1 and SLPCLKDIV = 0x01;10. BBREG2 (0x3A) = 0x80 – Set CCA mode to ED;11. RSSITHCCA (0x3F) = 0x60 – Set CCA ED threshold;12. BBREG6 (0x3E) = 0x40 – Set appended RSSI value to RXFIFO.

The next step (13) is to enable interrupts, on pin INT of the MRF24J40MA to signal the microcontroller when a successful transmission or reception have been made. Interrupts are enabled via the INTCON register (0x32) and the INTSTAT (0x31) indicates the status of operation and it is automatically cleared when read. Thus, the INT pin will continue to signal an interrupt until the INSTAT register is read. Also the edge polarity of the INT pin should be configured accordingly to the interrupt input of the PIC18F448. In our case we choose interrupt on falling edge this being the default on the MRF module after RESET.

Page 4: PIC18F448 in Wireless DC Motor Control

That means INTEDG0 will be cleared in the INTCON2 register of the PIC18F448 microcontroller.

A channel selection follows (14). The MRF24J40MA is capable of selecting one of sixteen channel frequencies in the 2.4 GHz band [5], listed in Table 3.

Table 3

Bits RFCON0<7:4> (address 0x200) select the desired channel. The current application will use channel 11. After channel selection we perform as follows:15. RFCTL (0x36) = 0x04 – Reset RF state machine;16. RFCTL (0x36) = 0x00;17. Delay at least 192 μs.Step 17 is necessary to allow the RF circuitry to recalibrate after the RF State Machine Reset [5]. A simple function to receive a word from the MRF module via SPI interface, after it is set in receiving mode is written bellow:

void receive(int rvalue){ while (!SSPSTAT.F0) {} rvalue=SSPBUF;}

where rvalue will be loaded with the SSPBUF value after a successful wireless transmission. When transmitting a value a simple routine should be used, like the following:

void transmit(int tvalue){ SSPBUF=tvalue;}

where tvalue is the word issued by the host PC and to be transmitted to the receiving unit via the MRF. Based upon the transmitted command word, the

PIC18F448 from the receiving unit generates four PWM signals using the built in ECCP module. The PWM period is specified by writing in PR2 register. The value to be written is given by:

PWM period = [(PR2) + 1] • 4 • TOSC • (TMR2 p.v.) (1)

where TMR2 p.v. is the prescale value set by TMR2 and Tosc is the period of the clock oscillator according to a 20 MHz frequency that is used. The PWM duty cycle is calculated using:

PWM duty cycle = (ECCPR1L:ECCP1CON<5:4>) •

TOSC • (TMR2 p.v.) (2)

The ECCPR1L contains the eight MSbs and the ECCP1CON<5:4> contains the two LSbs thus making a 10 bit wide word. The EPWM1M<1:0> bits in the ECCP1CON register allows to switch the ECCP operation between Full – bridge output forward or reverse mode. In order to use the ECCP, bits TRISD<7:4> should be cleared thus making pins RD7:4 outputs. Table 4 shows the operating modes.

Table 4

MODE ECCP1CON

Forward ‘b01xx1100’

Reverse ‘b11xx1100’

In order to use the current feedback on pin RA1 we use a initializing routine of PORTA as follows:

void analog_init(){

PORTA=0;LATA=0;ADCON1=0x80;TRISA=0xFF;

}

When acquiring an analog value, the integrated A/D converter is used and the conversion is started by setting bits ADON and GO/Done of ADCON0 register.

V. REMARKS

Combining known DC motor driving methods with new concepts regarding wireless communication this approach is intended to offer support to further development of automation devices. Focused mainly on achieving the wireless communication the proposed method can be used to implement a complex wireless robotic drive system.

REFERENCES

[1] Microchip Technology Inc., “MRF24J40MA Datasheet”, 2008 [2] Microchip Technology Inc., “PIC Microcontroller CCP and ECCP Tips’n Tricks”, 2008[3] Telcom Semiconductor Inc., “TC428 1.5A dual high-speed power MOSFET drivers”1996[4] Zetex Semiconductors, “ZXMHC6A07T8 complementary 60V enhancement mode MOSFET H-bridge Datasheet”, 2005

Page 5: PIC18F448 in Wireless DC Motor Control

[5] Microchip Technology Inc., “PIC18F448 Datasheet”, 2001