Tutorial: DC Motor Control
DC Motor Control Software
Having seen how the hardware will be connected, we must now take a look at the software perspective. The last section showed us that only 6 pins will be used by the PIC. Those pins are on PORTD & PORTC. Take a look below and you'll see how easy it is to set the digital 0's or 1's on those ports.
Follow the comments in the program below & you should be able to gather a simple understanding of how the system will function.
« End Code »
Download the code here: bridge.c
The code seen above is actually an abriged version of the code available for download. I urge you to look at the downloadable code. The functionality is the same, but there are more comments which describe PORTC's part better than the comments in the code above.
If you have read the previous PIC Microcontroller tutorials I've done then this code should make a decent amount of sense. We're just flipping bits, turning on an led & waiting.
Now we'll enable the system, see it work and explain why & how it works.
Having seen how the hardware will be connected, we must now take a look at the software perspective. The last section showed us that only 6 pins will be used by the PIC. Those pins are on PORTD & PORTC. Take a look below and you'll see how easy it is to set the digital 0's or 1's on those ports.
Follow the comments in the program below & you should be able to gather a simple understanding of how the system will function.
« Begin Code »
/*
Written by: Chris
Date: 10/21/2007
Purpose & Description: This program will send control
signals to the attached LMD18245 via PORTD & PORTC.
The Olimex P-40 provides an LED on PORTA which we will
turn on or off inbetween motor speed changes.
*/
#include <p18f452.h> //We're using the PIC18F452
#include <delays.h> //We'll need the delays library
void main(void){
TRISA = 0x00; //Initialize these Ports as Outputs
TRISC = 0x00; //This one too
TRISD = 0x00; //This one toooo
PORTA = 0x00; //Initialize PORTA...so the LED is off
PORTDbits.RD0 = 0; //Brake Off
PORTDbits.RD1 = 1; //Direction Forward
while(1){
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250); //4 * 0.5 seconds = 2 Second Pause
PORTA = 0x01; //Turn LED On
PORTC = 0b00001111; // 0x0F Full Speed
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250); //4 * 0.5 seconds = 2 Second Pause
PORTA = 0x00; //Turn LED Off
PORTC = 0b00001011; //0X0B 3/4 Full Speed
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250); //4 * 0.5 seconds = 2 Second Pause
PORTA = 0x01; //Turn LED On
PORTC = 0b00000111; //0x07 1/2 Full Speed
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250); //4 * 0.5 seconds = 2 Second Pause
PORTA = 0x00; //Turn LED Off
PORTC = 0b00000011; //0x03 1/4 Full Speed
PORTDbits.RD0 = 1; //Turn Brake On
Delay10KTCYx(250); //Pause 0.5 Seconds
PORTDbits.RD0 = 0; //Turn Brake Off
}
}
« End Code »
The code seen above is actually an abriged version of the code available for download. I urge you to look at the downloadable code. The functionality is the same, but there are more comments which describe PORTC's part better than the comments in the code above.
If you have read the previous PIC Microcontroller tutorials I've done then this code should make a decent amount of sense. We're just flipping bits, turning on an led & waiting.