Startup.h
#ifndef _Startup_H_
#define _Startup_H_
void PortInit(void);
/*Sets door-lock motor ports to outputs and all other ports as inputs.*/
void TimerInit(void);
/*Starts two min timer*/
void delay(unsigned int length);
/*Delays for given number of miliseconds*/
#endif
Startup.c
#include "Startup.h"
#include <hidef.h> /* common defines and macros */
#include <mc9s12e128.h> /* derivative information */
#include <S12E128bits.h> /*E128 bit definitions */
#include <bitdefs.h> /* BIT0HI, BIT0LO definitions */
#include <stdio.h>
#include <TIMERS12.h>
#include "MotorControl.h"
static unsigned int NumOverFlows=0;
const unsigned int MaxOverFlow=344;
//~~~~~~~~~~~~~~~~~ PortInit ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*Sets door-lock motor ports as outputs and all other ports as inputs */
void PortInit(void)
{
DDRT=0x00;
DDRU=(BIT7HI|BIT6HI);
PTU=(PTU&(BIT7LO&BIT6LO));
return;
}
//~~~~~~~~~~~~~~~ TimerInit ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*Starts 2 min timer */
void TimerInit(void)
{
//enable Timer 0
TIM0_TSCR1 = _S12_TEN;
//scale timer
TIM0_TSCR2 =( _S12_PR2|_S12_PR1|_S12_PR0); //divide M by 128
//Setup T6 as Output Compare (1)
TIM0_TIOS = (TIM0_TIOS | _S12_IOS6);
//Set T6 to not be connected to a pin
TIM0_TCTL1 = (TIM0_TCTL1 & ~(_S12_OL6 | _S12_OM6));
//reset flag
TIM0_TFLG1 = _S12_C6F;
//reset overflow flag
TIM0_TFLG2 = _S12_TOF;
//enable overflow interrupt
TIM0_TSCR2 = (TIM0_TSCR2 | _S12_TOI);
return;
}
//~~~~~~~~~~~~~~ delay ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*Waits for a given number of milliseconds to pass.
This function assumes a timer has been initialized with a rate of 1ms */
void delay(unsigned int length)
{
//variable for tracking time
unsigned int Tstart;
Tstart = TMRS12_GetTime();
while(TMRS12_GetTime() < Tstart + length/2); //loop to create delay
return;
}
//~~~~~~~~~~~ Overflow Interrupt ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*Updates overflow count each time there is an overflow
After enough overflows (2 mins pass) it stops the motors and hangs*/
void interrupt 16 TimerOverflow(void)
{
NumOverFlows++;
//if enough overflows have occured
if (NumOverFlows>=MaxOverFlow)
{
//stop motors
StopRight();
StopLeft();
//hang
while(1)
{
printf("DONE!");
}
}
//reset flag
TIM0_TFLG2 = _S12_TOF;
return;
}