__interrupt void TC7x(void)
{ // minimum timer interrupt handling code.
// You really don't need to test this bit.
TFLG1 = 0x80; // Clear the interrupt.
TC7 += t7delay; // reset the register, if you want a specific rate.
// This also resets the interrupt if you set the TFFCA bit.}TSCR1 = 0x90; // Enable TCNT and fast clear TSCR2 = 0x03; // Set prescaler to 1:8 (or what you need) TIOS |= TIOS_IOS7_MASK; // Enable OC7 TC7 = TCNT + t7delay; // Init the compare registers TIE |= TIOS_IOS7_MASK ; // Enable the timer interrupt bit.
TSCR1 = 0x90; TSCR2 = 0x02; TIOS = 0x01; TCTL2 = 0x02; //interrupt stuff TCTL3 = 0x80; TIE = 0x80;
void Motor_Run(unsigned int motor_dir) {
if(motor_dir == 0)
PORTB_BIT0 = 0;
else
PORTB_BIT0 = 1;
TC0 = TCNT + PULSE;
MOTOR1_RUN_FLAG = 1;
while (PBMCUSLK_PB2 && (MOTOR1_RUN_FLAG == 1)){//run motor until pushed or interrupt
while (TFLG1_C0F == 0){}
TCTL2 = 0x03;
TC0 = TC0 + PULSE;
while (TFLG1_C0F == 0){}
TCTL2 = 0x02;
TC0 = TC0 + PULSE;
}
return;
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED //JCB
__interrupt void TC7x(void)
{
TFLG1_C7F = 1;
MOTOR1_RUN_FLAG = 0;
}
#pragma CODE_SEG DEFAULTTFLG1_C7F = 1; // read/write to do an or. or's the bit, and you are not supposed to that. It should be just as I had it: TFLG1 = 0x80; // Clear the interrupt.
As I said, this is a special register and writing a 1 in the bit position of the channel will not affect other channels. If the motor is going the be hooked up to the output if timer channel 0 then you should set it up for output compare toggle mode and write an interrupt handler for that. This will cause a square to be generated on the pin. It' very simple to do. If this is what you want, I will post some sample code for that.
// near the top of the file Add this:
// Enable channel 0 interrupts.
#define MOTOR_ON() TC0 = TCNT + PULSE; TIE |= 1
// Disable channel 0 interrupts.
#define MOTOR_OFF() TIE &= ~1
int StepCounter = 0; // for testing, may be useful later.
// In main:
(other thing in main you need)
//interrupt stuff TSCR1 = 0x90; // Enable TCNT and fast clear
TSCR2 = 0x02; // Set the prescaler
// Withe the TIE, TCTLX and TOS registers, we DO need to 'or' things in
// 'and' and things out.
TIOS |= 0x01; // set channel 0 to output compare
TCTL2 |= 0x01; // set channel 0 for toggle mode.
// forget about channel 7 for now...
EnableInterrupts; // Don't do this until you have things set up right..
DDRB_BIT0 = 1; //set pin for output
PORTB_BIT0 = 0; //start out with pin at 0
MOTOR_ON(); // Start channel 0 interrupts
// Let the motor run for 100 steps then stop it.
// After this works, change the code to step in one direction,
// then step in the other direction for so many pulses.
for(;;)
{
if( StepCounter > 100 ) // change 100 if this is too short or long.
MOTOR_OFF();
}
} // end main
// Make sure you add this to the vector table in the slot for
// timer channel 0.
__interrupt void Timer0Interrupt(void)
{
++StepCounter;
TC0 += PULSE; // update the register and clear the int.
}