PIC8-Bit Trainer - DRV8833 - Dual H-Bridge Motor Driver.

0.Contents

PIC18F Trainer : PIC18F2xJ13 @8MHz Internal oscillator.

1.DRV8833 - BDC - 2xPWM Half Bridge with Auto-Shutdown.

// Configuration registers.
#pragma config WDTEN=OFF, PLLDIV=1, CFGPLLEN=OFF, STVREN=ON, XINST=OFF
#pragma config CP0=OFF, OSC=INTOSCO, SOSCSEL=HIGH, CLKOEC=ON, FCMEN=OFF
#pragma config IESO=OFF, WDTPS=32768, DSWDTOSC=INTOSCREF, RTCOSC=T1OSCREF
#pragma config DSBOREN=ON, DSWDTEN=ON, DSWDTPS=G2
#pragma config IOL1WAY=ON, ADCSEL=BIT10, PLLSEL=PLL4X, MSSP7B_EN=MSK7
#pragma config WPFP=PAGE_63, WPCFG=OFF, WPDIS=OFF, WPEND=PAGE_WPFP

#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 8000000
// PIC18FxxJ13 - Compile with XC8(v2.05).
// PIC18FxxJ13 - @8MHz Internal oscillator.

// DRV8833 - BDC - 2xPWM Half Bridge with Auto-Shutdown.

// Timer2 - ECCP1 - BDC Motor AIN1/AIN2.
// Timer2 - ECCP2 - BDC Motor BIN1/BIN2.

// Pinout.
// MCU.RB5 - DRV8833 nSLEEP.
// MCU.RB4 - DRV8833 BIN2.
// MCU.RB3 - DRV8833 BIN1.
// MCU.RB2 - DRV8833 AIN2.
// MCU.RB1 - DRV8833 AIN1.
// MCU.RB0 - DRV8833 nFAULT.

// Definitions.
#define DRV8833_NSLEEP			LATBbits.LATB5
#define DRV8833_PWM_FREQUENCY	0x85	// ~15kHz @8MHz.
#define DRV8833_DUTY_CYCLE_AIN	CCPR1L
#define DRV8833_DUTY_CYCLE_BIN	CCPR2L

// Main.
int main(void)
{
	// MCU Initialization.
	// Oscillator Settings.
	// Internal Oscillator 8MHz.
	OSCCONbits.IRCF = 0x7;
	// Ports Initialization.
	// Open-drain Output Settings.
	ODCON3 = 0b00000000;
	ODCON2 = 0b00000000;
	ODCON1 = 0b00000000;
	// Analog Input Settings.
	ANCON0 = 0b00011111;
	ANCON1 = 0b00011111;
	// Port A Settings.
	PORTA = 0b00000000;
	TRISA = 0b00000000;
	// Port B Settings.
	PORTB = 0b00000000;
	TRISB = 0b00000001;
	// Port C Settings.
	PORTC = 0b00000000;
	TRISC = 0b00000000;
	// PPS Settings.
	PPSCONbits.IOLOCK = 0;
	// PPS Inputs.
	RPINR24 = 0x03;	// RB0 - RP3 - FLT0 - DRV8833 nFAULT.
	// PPS Outputs.
	RPOR4 = 0xe;	// RB1 - RP4 - ECCP1 P1A - DRV8833 AIN1.
	RPOR5 = 0xf;	// RB2 - RP5 - ECCP1 P1B - DRV8833 AIN2.
	RPOR6 = 0x12;	// RB3 - RP6 - ECCP2 P2A - DRV8833 BIN1.
	RPOR7 = 0x13;	// RB4 - RP7 - ECCP2 P2B - DRV8833 BIN2.
	PPSCONbits.IOLOCK = 1;
	// Timer2 Settings.
	T2CON = 0b00000000;
	// PWM Period.
	PR2 = DRV8833_PWM_FREQUENCY;
	// ECCP1 Settings.
	// Half-bridge P1A active High, P1B active High.
	CCP1CONbits.P1M = 0b10;
	CCP1CONbits.DC1B = 0b00;
	CCP1CONbits.CCP1M = 0b1100;
	// Shutdown state SET for P1A & P1B.
	ECCP1ASbits.PSS1AC = 0b01;
	ECCP1ASbits.PSS1BD = 0b01;
	// Auto-shutdown VIL on FLT0 Pin.
	ECCP1ASbits.ECCP1AS = 0b100;
	// Auto-restart disable.
	ECCP1DELbits.P1RSEN = 0;
	// Dead-band delay = 1*(FOSC/4).
	ECCP1DELbits.P1DC = 0b0000001;
	// ECCP2 Settings.
	// Half-bridge P2A active High, P2B active High.
	CCP2CONbits.P2M = 0b10;
	CCP2CONbits.DC2B = 0b00;
	CCP2CONbits.CCP2M = 0b1100;
	// Shutdown state SET for P2A & P2B.
	ECCP2ASbits.PSS2AC = 0b01;
	ECCP2ASbits.PSS2BD = 0b01;
	// Auto-shutdown VIL on FLT0 Pin.
	ECCP2ASbits.ECCP2AS = 0b100;
	// Auto-restart disable.
	ECCP2DELbits.P2RSEN = 0;
	// Dead-band delay = 1*(FOSC/4).
	ECCP1DELbits.P1DC = 0b0000001;
	// PWM Select Timer2 for ECCP1 & ECCP2.
	CCPTMRS0bits.C2TSEL = 0x0;
	CCPTMRS0bits.C1TSEL = 0x0;
	// Timer2 Start.
	T2CONbits.TMR2ON = 1;

	//   0 - 100% - Forward Full.
	//  33 -  75% - Forward.
	//  67 -  50% - Motors Stop.
	// 100 -  25% - Backward.
	// 135 -   0% - Backward Full Speed.
	DRV8833_DUTY_CYCLE_AIN = 67;
	DRV8833_DUTY_CYCLE_BIN = 67;
	DRV8833_NSLEEP = 1;
	__delay_ms(1);
	ECCP1ASbits.ECCP1ASE = 0;
	ECCP2ASbits.ECCP2ASE = 0;

	uint8_t i;
	while(1){
		for(i=0; i<DRV8833_PWM_FREQUENCY; i++){
			DRV8833_DUTY_CYCLE_AIN = i;
			DRV8833_DUTY_CYCLE_BIN = i;
			__delay_ms(200);
		}
	}
	return(0);
}

06.2019