#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;
#define RS232 Serial
PeriodicTimer TimerMoteur(PIT);
PeriodicTimer TimerCompensation(PIT);
int Num_Pointage;
byte Accu_Add;
void step_moteur(void) {
digitalWrite(Out_DIR, HIGH);
digitalWrite(Out_DIR, HIGH);
digitalWrite(Out_DIR, HIGH);
digitalWrite(Out_DIR, HIGH);
digitalWrite(Out_DIR, HIGH);
digitalWrite(Out_HorlogeMoteur, LOW);
}
/****************************************************************
Function Name: MoveNumOfUSteps
Return Value: none
Parameters: none
Description: Move motor x steps in dir Out_DIR
****************************************************************/
void MoveNumOfUSteps(void) {
int t = micros();
while (Num_Pointage > 1) {
// Calculation of a new _delay to accelerate the stepbystep motor
TimerMoteur.stop();
PIT_LDVAL0 = _delay; // Valeur du delai transferee dans le registre comparateur
PIT_TCTRL0 = PIT_TCTRL_TEN; // start Timer0 without interruption
Num_Pointage--;
Num_Pointage += Pointage_Corr; // Adjust Num_Pointage
step_moteur(); //one micro step
while (PIT_TFLG0 == 0) { // Waiting the end of timermoteur
}
PIT_TFLG0 = 1; //clear flag of timer0
}
t = micros() - t;
RS232.printf("time in micro seconde=%d\r\n", t);
}
void setup() {
pinMode(Out_HorlogeMoteur, OUTPUT_OPENDRAIN);
pinMode(Out_DIR, OUTPUT_OPENDRAIN);
RS232.begin(19200);
while (RS232);
// PIT Initialisation for the motor interrupt
TimerMoteur.begin(IntTimerMoteur, 400ms, true);
NVIC_SET_PRIORITY(IRQ_PIT, 20); //priority
TimerMoteur.start();
TimerCompensation.begin(IntTimerCompensation, 250ms, true);
TimerMoteur.stop();
Num_Pointage=10000;
MoveNumOfUSteps();
exit(0);
}
void loop() {
}
void IntTimerCompensation() {
//
// Timer PIT1 interrupt handler
// is used only during slewing to
//
// compensate for sidereal speed during the acceleration phase
//
if (!Accu_Add) { // If slewing is in anti-sidereal dir (one of SPD_*_MINUS) //
if (Num_Pointage > 1) {
Pointage_Corr--;
}
}
else {
Pointage_Corr++; // If slewing is in sidereal dir (one of SPD_*_PLUS) //
}
}
void IntTimerMoteur() {
step_moteur();
}