/*
I am using SG8 device 16-PIN(TSSOP),CW 6.2v.
In my application i want use capture,compare module.
So pin no 16 (PTA0/TPM1CH0) is used for capture i/p ,
the square wave (50 Hz,5v amplitude) is given to this
pin & TPM1CH1 is used only for output compare.
Once the capture is happened i am reseting TPM1 & putting
some value in channel 1 valure register (TPM1C1V) & making
PTBD_PTBD1 = 1.So once the TPM1 matched with channel 1
value register (TPM1C1V) interrupt will generate & in ISR
i am making PTBD_PTBD1 = 0;
This is happening if i enabled TPM1 (TPM1SC_CLKSA = 1) in "main()" function.
But if i disabled it no capture or compare is happened.
& in my applicationi want to start TPM1 timer after capture happened.
Please tell me the solution to overcome this.Below is my source code.
*/
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#define PULSEHIGHTIME FALSE //time for pulse high, or from input pulse low to
#define CH1INTERRUPT FALSE //defines if TPM1CH1 interrupt is needed //output pulse low.
#define LED1 PTBD_PTBD6
#define LED1MOD PTBDD_PTBDD6
#define LED2 PTBD_PTBD7
#define LED2MOD PTBDD_PTBDD7
#define ON 0
#define OFF 1
#define US100 0x18
void MCU_init(void);
unsigned char flag;
interrupt 26 void MTIM_ISR(void) {
MTIMSC_TOF = 0;
}
/*interrupt 11 void Timovr(void){
TPM1SC_TOF=0;
}
*/
#if CH1INTERRUPT
//...........................................................
/**********************************************************************
*
* TPM1CH1 interrupt
*
***********************************************************************/
void interrupt VectorNumber_Vtpm1ch1 TPM1Ch1int(void){
TPM1C1SC_CH1F=0;
PTBD_PTBD1 = 0; // Make pin output low once Compare happened.
}
//...........................................................
#endif
/**********************************************************************
*
* TPM1CH0 interrupt
*
***********************************************************************/
void interrupt VectorNumber_Vtpm1ch0 TPM1Ch0int(void){
// ISR for TPM1 channel 0
unsigned char x;
LED2 = ON;
TPM1C0SC_CH0F=0; //clear interrupt flag
//start the output pulse on TPM1CH1
TPM1SC_CLKSA = 0; // Stop TPM1 Counter incrementing
TPM1C1SC = TPM1C1SC_MS1A_MASK| //pull TPM1CH1 high
TPM1C1SC_ELS1B_MASK|
TPM1C1SC_ELS1A_MASK;
TPM1C1V = TPM1CNT+1; //get current time, add 1
TPM1SC_CLKSA = 1; // Start TPM1 Counter incrementing
//wait at least 1 clock cycle (250 kHz)
for (x = 0;x < 25;x++); //short delay
PTBD_PTBD1 = 1; // Make pin output high once Capture happened.
PTBD_PTBD2 = ~PTBD_PTBD2; // Check capture is happened.
//now its time to store the TPM1CH1 stop address
TPM1SC_CLKSA = 0; // Stop TPM1 Counter again
#if PULSEHIGHTIME
//...........................................................
TPM1C1V = TPM1C0V+US100; // Change value in compare register.
//...........................................................
#else
//...........................................................
TPM1C1V = TPM1CNT+US100; // Change value in compare register.
//...........................................................
#endif
TPM1C1SC =
#if CH1INTERRUPT
//...........................................................
TPM1C1SC_CH1IE_MASK| //enable the TPM1CH1 interrupts (***)
//...........................................................
#endif
TPM1C1SC_MS1A_MASK| //pull TPM1CH1 low at the trig time
TPM1C1SC_ELS1B_MASK;
TPM1SC_CLKSA = 1; // Start TPM1 Counter incrementing
flag = 1;
LED2 = OFF;
}
/**********************************************************************
*
* main()
*
***********************************************************************/
void main(void) {
MCU_init();
LED1MOD = 1;
LED2MOD = 1;
LED1 = OFF;
LED2 = OFF;
flag = 0;
EnableInterrupts; /* enable interrupts */
/* include your code here */
for(;;) {
LED1 = ON;
LED1 = OFF;
} /* loop forever */
/* please make sure that you never leave main */
}
void MCU_init(void)
{
/******************** LVI**************************/
SPMSC1 = 0b00110100; //0x34=> Enable LVI
SPMSC2 = 0x00;//0b00100000;//0x20 =>LVDV = 1;LVWV = 0; LVD Trip Point = 4.0v
SOPT1 = 0x04; // COP Disabled,STOP mode Disabled,SDA on PTB6, SCL on PTB7.
SOPT2 = 0x00; //TPM1CH1 on PTB5.TPM1CH0 on PTA0.
/******************** PORT A ********************/
/*PTAD = 0x00; // Port A Data register (PTAD_PTAD _PTAD.MergedBits.grpPTAD)
PTADD = 0x02; // 0b00000010 =>Port A Data Direction register ,I/P= PTA1/PTA2/PTA3,O/P = PTA0
PTAPE = 0x00; // Internal pull-up register disabled
*/
/******************** PORT B ********************/
/*PTBD = 0x54; // Port B Data register
PTBDD = 0xD6; //0b11010110 => Port B Data Direction register, I/P= PTA1/PTA2/PTA3,O/P = PTA0
PTBPE = 0x00; // Port B Data Direction register ,I/P= PTB0/PTB3/PTB5,O/P = PTB1/PTB2/PTB4/PTB6/PTB7
PTBD_PTBD6 = 1; //Discharge the Ign coil.
*/
/*************** Internal Clock Source Select *************************/
ICSSC = *(unsigned char*)0xFFAE;
ICSTRM = *(unsigned char*)0xFFAF;
ICSC1 = 0x04; //Output of FLL is selected.
ICSC2 = 0x00;
while(!ICSSC_IREFST) { /* Wait until the source of reference clock is internal clock */
}
/*************** Timer/PWM Module **********************************************************/
//TPM1SC = 0x46; //TPM counter disable,TPM/64 = 16MHz/64 = 250 KHz,So 1 Count = 4 usec.
TPM1SC = TPM1SC_PS2_MASK|TPM1SC_PS1_MASK;
TPM1C0SC = TPM1C0SC_CH0IE_MASK|TPM1C0SC_ELS0B_MASK;
//no interupts for channel 0 so far
TPM1C1SC = 0;
TPM1SC_CLKSA =1; // Start TPM1 Counter incrementing
}