PIC8 Trainer - L298.

0.Contents

PIC18F Trainer : PIC18F2620 @8MHz Internal oscillator.

1.L298 - 2x BDC Motors - Basic Logic Full ON/OFF

// Configuration registers.
#pragma config FOSC = INTIO67, PLLCFG = OFF, PRICLKEN = OFF, FCMEN = OFF, IESO = OFF
#pragma config PWRTEN = OFF, BOREN = OFF, BORV = 190, WDTEN = OFF, WDTPS = 32768
#pragma config CCP2MX = PORTC1, PBADEN = OFF, CCP3MX = PORTB5, HFOFST = OFF
#pragma config T3CMX = PORTC0, P2BMX = PORTB5, MCLRE = EXTMCLR
#pragma config STVREN = ON, LVP = ON, XINST = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF, CPB = OFF, CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTC = OFF, WRTB = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF, EBTRB = OFF

#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 1000000
// PIC18F26K22 - Compile with XC8(v2.00).
// PIC18F26K22 - @1MHz Internal oscillator.

// L298 - 2x BDC Motors - Basic logic command - Full ON/OFF.

// Definitions.
#define L298P_IN1		LATAbits.LATA0
#define L298P_IN1_TRIS	TRISAbits.TRISA0
#define L298P_IN2		LATAbits.LATA1
#define L298P_IN2_TRIS	TRISAbits.TRISA1
#define L298P_IN3		LATAbits.LATA2
#define L298P_IN3_TRIS	TRISAbits.TRISA2
#define L298P_IN4		LATAbits.LATA3
#define L298P_IN4_TRIS	TRISAbits.TRISA3
#define L298P_ENA		LATCbits.LATC2
#define L298P_ENA_TRIS	TRISCbits.TRISC2
#define L298P_ENB		LATCbits.LATC1
#define L298P_ENB_TRIS	TRISCbits.TRISC1

// Function prototypes.
void l298p_backward(void);
void l298p_brake(void);
void l298p_coast(void);
void l298p_forward(void);
void mcu_initialize(void);

// Main.
int main(void)
{
	mcu_initialize();

	l298p_forward();
	__delay_ms(3000);

	l298p_brake();
	__delay_ms(1000);

	l298p_backward();
	__delay_ms(3000);

	l298p_coast();
	
	while(1){
	}
	return(0);
}

// Functions.
void l298p_backward(void){
	L298P_IN1 = 0;
	L298P_IN2 = 1;
	L298P_ENA = 1;
	
	L298P_IN3 = 0;
	L298P_IN4 = 1;
	L298P_ENB = 1;
}

void l298p_brake(void){
	L298P_IN1 = 1;
	L298P_IN2 = 1;
	L298P_ENA = 1;
	
	L298P_IN3 = 1;
	L298P_IN4 = 1;
	L298P_ENB = 1;
}

void l298p_coast(void){
	L298P_ENA = 0;
	L298P_ENB = 0;
}

void l298p_forward(void){
	L298P_IN1 = 1;
	L298P_IN2 = 0;
	L298P_ENA = 1;
	
	L298P_IN3 = 1;
	L298P_IN4 = 0;
	L298P_ENB = 1;
}

void mcu_initialize(void)
{
	// Internal Oscillator 1MHz.
	OSCCON = 0b00110010;
	OSCCON2 = 0b00000000;
	OSCTUNE = 0b00000000;
	if(!OSCCONbits.HFIOFS){};
	// PortA.
	LATA = 0x00;
	TRISA = 0x00;
	// PortB.
	LATB = 0x00;
	TRISB = 0x00;
	// PortC.
	LATC = 0x00;
	TRISC = 0x00;
}

10.2018