SG8 capture,compare module query

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SG8 capture,compare module query

1,258 Views
Deepak1
Contributor IV
Dear Sir,
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 getnerate & 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.
 
Code:
#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */void MCU_init(void);unsigned char flag;interrupt 26 void MTIM_ISR(void) {MTIMSC_TOF = 0;}interrupt 11 void Timovr(void){        TPM1SC_TOF=0;}interrupt 6 void TPM1CH1(void){        TPM1C1SC_CH1F=0;    PTBD_PTBD1 = 0;   // Make pin output low once Compare happened.}interrupt 5 void TPM1CH0(void){ // ISR for TPM1 channel 0TPM1SC_CLKSA =0;     // Stop TPM1 Counter incrementing(void)TPM1C0SC;TPM1C0SC_CH0F=0;TPM1CNT = 0x0000;TPM1SC_CLKSA = 1;       // Start TPM1 Counter incrementingTPM1C1V = 100;          // Put value in compare register.PTBD_PTBD1 = 1;         // Make pin output high once Capture happened.PTBD_PTBD2 = ~PTBD_PTBD2; // Check capture is happened.flag = 1;}void main(void) {MCU_init();TPM1SC_CLKSA = 1; flag = 0;  EnableInterrupts; /* enable interrupts */  /* include your code here */      for(;;) {  while(flag == 0) ;      flag = 0;      } /* loop forever */  /* please make sure that you never leave main */}void MCU_init(void) {/******************** LVI**************************/SPMSC1 = 0b00110100; //0x34=> Enable LVISPMSC2 = 0x00;//0b00100000;//0x20 =>LVDV = 1;LVWV = 0; LVD Trip Point = 4.0vSOPT1 = 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 = PTA0PTAPE = 0x00; // Internal pull-up register disabled/******************** PORT B ********************/PTBD = 0x54;  // Port B Data registerPTBDD = 0xD6; //0b11010110 => Port B Data Direction register, I/P= PTA1/PTA2/PTA3,O/P = PTA0PTBPE = 0x00; // Port B Data Direction register ,I/P= PTB0/PTB3/PTB5,O/P = PTB1/PTB2/PTB4/PTB6/PTB7PTBD_PTBD6 = 1; //Discharge the Ign coil./************* Analog Comparator ***************************/ACMPSC = 0x00;//Analog Comparator Disabled/************* Analog To Digital Conversion ***************************/ ADCCFG = 0xD8; //Clock Divide by 4,MODE=>10-bit conversion,Input Clock=>Bus clock. ADCSC2 = 0x00; ADCSC1 = 0x1F;//ADC Module disabled,Interrupt disable APCTL1 = 0x9c; // Analog I/p => PTA2,PTA3,PTB0,PTB3/*************** 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 */  }/****I2C ***********/// IICC1 = 0x00; /**************** Modulo Timer *********************************************************///Reset clears TOIE. Do not set TOIE if TOF = 1. Clear TOF first, then set TOIE.MTIMSC = 0x70;  //MTIM Status and Control Register//MTIM Overflow Interrupt Enable,MTIM counter is reset to $00,MTIM counter is stopped,MTIMCLK = 0x08; //MTIM Clock Configuration Register //Bus Clock Source Select,Clock Source Prescaler=> ÷ 256 //1 tik count = 16 usec,So MTIM will overflow after 4.096 msecMTIMMOD = 0x00;     //MTIM Modulo Register/*************** Timer/PWM Module **********************************************************/TPM1SC  = 0x46; //TPM counter disable,TPM/64 = 16MHz/64 = 250 KHz,So 1 Count = 4 usec. TPM2SC  = 0x46; //TPM counter disable,TPM/64 = 16MHz/64 = 250 KHz,So 1 Count = 4 usec. TPM2MOD = 0x18;// TPM2 will overflow every 100 usec overflow.TPM1C0SC = 0x48;//Channel 0 for capture on falling edge.TPM1C1SC = 0x50;}

 
Regards,
Deepak.
Labels (1)
0 Kudos
Reply
1 Reply

320 Views
Ake
Contributor II
Hi,
I took your code and rewrote some parts of it.
 
1) You should never reset the TPMCNT. It is only reset when coming out of reset.
This means that all the timing parameter that you enter should be relative, not absolute.
 
2) Your application, if I have understood it, only requiers 1 interrupt. It is the interrupt from the falling 100 Hz input signal.
In this interrupt , there are two ways of setting the desired pulse width.
Either the outgoing pulse width is important, then read the TPMCNT, add 0x18 and store it in the TPM1C1V register.
Or if the time from the falling edge to the trailing edge of the pulse width is vital, then read the TPM1C0V, add 0x18 and store it in TPMC1V.
Use the PULSEHIGHTIME macro to select either method.
 
3) The interrupt at the trailing edge of the outgoing pulse is not important.
You can select to have them with the code using the CH1INTERRUPT macro.
 
4) Sorry, I did not have the time to make a beautiful source code. Hope it is readable anyhow.
I ran the code on a DEMO9S08SH8, which is slightly different than  the 9S08SG8. I think that the code will run just the same on a 9S08SG8 MCU.
 
 
Regards,
Ake
 
0 Kudos
Reply