//*******************************************************
//* INITIALIZE CLOCK AND IO PORTS
//*******************************************************
OSCCON = 0x70; //run at 8MHz
PLLEN = 1; //enable 4x PLL
TRISA = 0b01010111;
TRISB = 0b11111111;
TRISC = 0b10001010;
For now, we won’t be employing the services of the PIC
‘s analog-to-digital converter (ADC) subsystem. So, we’ll just
turn it off. If you want to use the ADC in your version of this
project, you can do so as portions of the PIC18F2620’s
PORTA and PORTB I/O subsystem can become ADC inputs.
Since we’re not using the ADC, here’s the ADC “OFF” code:
//*******************************************************
//* CONFIGURE A2D AND COMPARATORS
//*******************************************************
ADCON1 = 0b00001111; //all port I/O is digital
ADON = 0; //ADC off
CMCON = 0x07; //comparators off
The ADC OFF code also turns off the PIC18F2620
comparators with a write to the CMCON register.
The PIC is endowed with a multitude of timers/counters.
So, why not use them? I’ve coded up timer routines for
TIMER1 and TIMER3. Each of the aforementioned timers will
trigger an interrupt every millisecond. I’ve set up TIMER1 as
a general-purpose timer that can resolve milliseconds,
seconds, and minutes. TIMER3 leans towards being a
real-time clock and if you add an LED to RA7, TIMER3 will
drive the LED at one blink per second. The timer setup code
is pretty simple and looks like this for TIMER1:
//************************************************
//* CONFIGURE AND START TIMER1
//* SET TO OVERFLOW EVERY 1mS
//************************************************
TIMER1OFF;
T1CON = 0b00000000;
TMR1H = 0xE0;
TMR1L = 0xC1;
TIMER1ON;
I’ve provided the full code package for the A3979 motor
driver board via the SERVO website. If you’re interested in the
way the timers interact during an interrupt, you can peruse
the download package source code to study the timer
interrupt service routines in detail.
The final steps of the A3979 motor driver board
initialization process include activating the interrupts and
setting up the stepper motor:
//*******************************************************
//* CONFIGURE EXTERNAL INTERRUPTS
//*******************************************************
enable_TMR1int;
enable_TMR3int;
enable_GLOBALint;
//*******************************************************
//* INITIALIZE STEPPER MOTOR DRIVER HARDWARE
//*******************************************************
quarter_step; //quarter-step mode
step_HALT; //stop the motor
rst_step = 0; //reset the A3979
mdelay1(100); //delay 100mS
rst_step = 1; //bring A3979 out of reset
slp_step = 1; //put A3979 to sleep
step_HALT; //make sure motor is stopped
The millisecond delay routine (mdelay1()) is actually a C
macro and its source code can also be found in the download
code package. The same can be said of the interrupt enable
macros. There are a couple of additional macros (quarter_step
and step_HALT) in the stepper motor initialization code that
need to be defined for you at this time. Check this set of
A3979 translator input definitions against Schematic 1:
#define rst_step
#define slp_step
#define dir_step
#define ms1
#define ms2
LATC5
LATC4
LATC0
LATA3
LATA5
Let’s see, there’s a RESET translator input, a SLEEP translator input, a DIRection translator input, and the step resolution selection (ms1-ms2) translator inputs. Now you can go
And, like this for TIMER3:
//************************************************
//* CONFIGURE AND START TIMER3
//* SET TO OVERFLOW EVERY 1mS
//************************************************
ihours3 = 12;
imins3 = 0;
isecs3 = 0;
imsecs3 = 0;
T3CON = 0x00;
TMR3H = 0xE0;
TMR3L = 0xC1;
TMR3ON = 1;
Screenshot 3 is a CleverScope capture of the
TIMER3 1 ms clock driving I/O pin RA7. In the TIMER3
interrupt code, I count 1,000 of these to mark seconds,
which clocks the RA7 LED if it is present in your design.
SCREENSHOT 2. To get this shot, I simply toggled the RA7 line every time
a TIMER3 interrupt occurred. Using the 1 mS interrupt clock as a time
base, I can count the 1 mS pulses to create timings for seconds, minutes,
hours, and days.
SERVO 01.2008 35