SCGC1_TPM1 = 1;TPM1MOD = 0x3F;TPM1SC = 0b01001111;
void interrupt VectorNumber_Vtpm1ovf commutationTime(void) { TPM1SC_TOF = 0; SCGC1_TPM1 = 0; // stop clock TPM1SC = 0b01000111; // stop clock TPM1CNT = 0; // reset timer TPM1MOD = 0x2; // change mod value TPM1SC = 0b01001111; // start clock SCGC1_TPM1 = 1; // start clock PTGD_PTGD7 ^= 1; // here I toggle one pin to see what timer does
}
void interrupt VectorNumber_Vtpm1ch0 zeroCrossing(void) { // TPM1CH0 interrupt service // another code here TPM1CNT = 0; TPM1SC = (unsigned char) (TPM1SC & 0b00000111); // stop counter, but keep clock divider
if(TPM2CNT == 0) { // here I test TPM2CNT for 0, and it is 0 } TPM2CNT = 0; // this is THE line
TPM2MOD = (unsigned short) (someVar); // set new modulo value for TPM2 TPM2SC = (unsigned char) (0b01001000 | TPM1SC); // set TPM2 clock divider the same as TPM1 clock divider, start timer
// another code here
TPM1SC = (unsigned char) (TPM1SC | 0b01001000); start timer
// another code here
TPM2SC; TPM2SC_TOF; TPM1C0SC; TPM1C0SC_CH0F = 0;}
void interrupt VectorNumber_Vtpm2ovf commutationTime(void) { // TPM2 overflow interrupt service TPM2SC = 0b00000111; // stop counter
// another code hereTPM2SC;
void interrupt VectorNumber_Vtpm2ovf commutationTime(void) { TPM2SC = 0b00000111; TPM2CNT = 0; // another code here TPM2SC; TPM2SC_TOF = 0;}
#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */ #define SAMPLES 3
word samplesHistory[SAMPLES];unsigned char samplePosition = 0;char waitingForZeroCrossing = 1; // 1 = zero crossing expected ; 0 = commutation planned
/********************************************/Commutation routine/********************************************/
void commutate(void) { PTGD_PTGD7 ^= 1; // toggle this pin }
/********************************************/TPM1CH0 ISR/********************************************/void interrupt VectorNumber_Vtpm1ch0 zeroCrossing(void) { samplesHistory[samplePosition] = TPM1C0V; // store timer value when input capture occurs if(++samplePosition > SAMPLES) { // calculate next position in the samplesHistory array samplePosition = 0; } TPM1SC = (unsigned char) (TPM1SC & 0b00000111); // stop timer but keep divider TPM1CNT = 0; // reset counter int sum = 0; unsigned char i; if(!waitingForZeroCrossing) { // there was a commutation planned but zero crossing occured earlier commutate(); } for(i = 0 ; i < SAMPLES ; i++) { // calculate sum of last samples sum += samplesHistory[i]; } TPM2MOD = (unsigned short) (sum / (2*SAMPLES)); // TPM2MOD = half of last samples average, this makes 90 degrees shift of output signal TPM2SC = (unsigned char) (0b01001000 | TPM1SC); // start TPM2 with the same clock divider as TPM1 TPM2CNT = 0; waitingForZeroCrossing = 0; TPM1SC = (unsigned char) (TPM1SC | 0b01001000); // start TPM1 TPM1SC; TPM1SC_TOF = 0; TPM1C0SC; TPM1C0SC_CH0F = 0; }/********************************************/TPM1 overflow ISR/********************************************/void interrupt VectorNumber_Vtpm1ovf zeroCrossingOverflow(void) { TPM1SC; TPM1SC_TOF = 0;
// I don't use this ISR but if I disable TPM1 overflow interrupts, the program doesn't work...why?}
/********************************************/TPM2 overflow ISR/********************************************/void interrupt VectorNumber_Vtpm2ovf commutationTime(void) { TPM2SC = 0b00000111; // stop counter TPM2CNT = 0; // reset counter commutate(); // make commutation waitingForZeroCrossing = 1; TPM2SC; TPM2SC_TOF = 0;}/********************************************/MAIN/********************************************/void main(void) { PTGDD_PTGDD7 = 1; // output signal will be here ICSC2_BDIV = 0; // divider = 1 // TPM1CH0 will be input capture // TPM1MOD = 0xFFFF; TPM1C0SC = 0b01001100; TPM1SC = 0b01001111; EnableInterrupts; /* enable interrupts */ for(;;) { __RESET_WATCHDOG(); /* feeds the dog */ } /* loop forever */}
#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */ void commutate(void) { PTGD_PTGD7 ^= 1; }void interrupt VectorNumber_Vtpm1ch0 zeroCrossing(void) { TPM1SC = (unsigned char) (TPM1SC & 0b00000111); TPM1CNT = 0; TPM2MOD = (unsigned short) (TPM1C0V/2); TPM2SC = (unsigned char) (0b01001000 | TPM1SC); TPM2CNT = 0; // without this line it works TPM1SC = (unsigned char) (TPM1SC | 0b00001000); TPM1SC; TPM1SC_TOF; TPM1C0SC; TPM1C0SC_CH0F = 0; }void interrupt VectorNumber_Vtpm2ovf commutationTime(void) { TPM2SC = 0b00000111; TPM2CNT = 0; commutate(); TPM2SC; TPM2SC_TOF = 0;}void main(void) { PTGDD_PTGDD7 = 1; ICSC2_BDIV = 0; // bus clock /= 1 // TPM1CH0 will be input capture // TPM1MOD = 0xFFFF; TPM1C0SC = 0b01001100; TPM1SC = 0b00001111; EnableInterrupts; /* enable interrupts */ for(;;) { __RESET_WATCHDOG(); /* feeds the dog */ } /* loop forever */}