There are two main portions of code that we are concerned with:
-The Initializations
-Motor Control Steps
The first part of the firmware program that we'll look at is the initializations. These configure the PIC's outputs and setup the PWM hardware module. PORTB and PORTC will be used as outputs, so TRISC and TRISB are cleared. Timer2 is used with the hardware PWM module, so we need to set it's pre-scalar value. Finally, the PWM modules are opened up and set to about 7.5 KHz.
PIC Initializations For Motor Control
------------« Begin Code »------------
TRISB = 0x00; PORTB = 0x01; //RED LED ON TRISC = 0x00; PORTC = 0x00; Delay10KTCYx(1); //Use x16 Pre-scalar T2CONbits.T2CKPS0 = 1; T2CONbits.T2CKPS1 = 1; //10 KHz OpenPWM1(20); OpenPWM2(20); //Disable Both Motors PORTC = 0x00;
------------« End Code »------------
The next part of code from the firmware program gives an example for how to control the PWM output on the PIC. Both PWM modules need to be changed at the same time, to keep the motors in-sync. The first example shows how the motors were turned-on and told to move full speed forward (90% Duty Cycle). The second example shows how the motors are put into a stop state (50% duty cycle + disable motors).
Pre-Programmed Motor Control Movements
------------« Begin Code »------------
/*******Move Forward***********/ SetDCPWM1(6); SetDCPWM2(6); PORTB = 0x04; //GREEN LED ON PORTCbits.RC4 = 1; PORTCbits.RC5 = 1; //1 Second Delay Delay10KTCYx(250); Delay10KTCYx(250); /***************************/ /*******Full Stop***********/ SetDCPWM1(38); SetDCPWM2(38); PORTB = 0x01; //RED LED ON PORTCbits.RC4 = 0; PORTCbits.RC5 = 0; //1 Second Delay Delay10KTCYx(250); Delay10KTCYx(250); /***************************/
------------« End Code »------------
These are the main portions of code that you will find the firmware program. Download it, compile it on your own and modify at your own desire. It's a simple program as it is meant to provide you with a stepping stone to more complicated programs.