Hello every one. i need some help regarding timer of 8 bit freescale microcontroller

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

Hello every one. i need some help regarding timer of 8 bit freescale microcontroller

Jump to solution
862 Views
gauravgarg
Contributor I

hello everyone,

i am using freescale MC9S08GT16A microcontroller. i am beginner of using this microcontroller. could any one explain me how to give 30 sec delay in this microcontroller. during device initilization i have given delay of 2 sec. but i am confusing about delay of 30 sec. i am using code warrior software. my values for during device initilization is ..

 

prescaler   128

clk            4 mhz

modulo value  31249...

 

TPM1SC = 0x00;                       /* Stop and reset counter */

  TPM1MOD = 0x7A11;                    /* Period value setting */

  (void)(TPM1SC == 0);                 /* Overflow int. flag clearing (first part) */

  /* TPM1SC: TOF=0,TOIE=1,CPWMS=0,CLKSB=0,CLKSA=1,PS2=1,PS1=1,PS0=1 */

  TPM1SC = 0x4F;                       /* Int. flag clearing (2nd part) and timer control register setting */

  /* ### Init_GPIO init code */

  /* PTCDD: PTCDD1=1,PTCDD0=1 */

  PTCDD |= (unsigned char)0x03;

 

__interrupt void TIMER_OVF(void)

{

  /* Write your interrupt code here ... */

  PTCD_PTCD0=~PTCD_PTCD0;

  PTCD_PTCD1=~PTCD_PTCD1;

  PTCD_PTCD2=~PTCD_PTCD2;

 

 

 

 

 

  for (temp=0;temp<15;temp++) {

   

  TPM1SC; /* Clear the TOF flag */

  TPM1SC_TOF = 0;

  }

 

Thank you....

Labels (1)
Tags (1)
0 Kudos
1 Solution
457 Views
weapon
Senior Contributor I

Hi Grag,

You can try to count the TPM interrupt in the ISR, for example, the TPM overflow time is 3s, then you can define a global variables to count the overflow interrupt, when the counter reach 10, that means 30s is passed.

unsigned int tpm_overflow_count = 0;

unsigned char flag_30s = 0;

__interrupt void TIMER_OVF(void)

{

  /* Write your interrupt code here ... */

TPM1SC_TOF = 0;

  PTCD_PTCD0=~PTCD_PTCD0;

  PTCD_PTCD1=~PTCD_PTCD1;

  PTCD_PTCD2=~PTCD_PTCD2;

tpm_overflow_count++;

  if( tpm_overflow_count ==10)

  {

      tpm_overflow_count = 0;

      flag_30s = 1; // reach 30s

  }

}

Hope it is helpful.

B.R

XWP

View solution in original post

0 Kudos
4 Replies
457 Views
bigmac
Specialist III

Hello,

I would further suggest that the TPM module remains free-running with TPM1MOD = 0 (or 65535), and that the prescale division be reduced from 128 to 1.  This will greatly improve the timing resolution, and also the delay uncertainty when the delay period commences.  The period resolution would become 16.384 ms instead of 2097.2 ms.  For a 30 second delay, the count value would be 1831, rather than 15.

Additionally, by decrementing the counter variable within the ISR, until zero is reached, an additional flag variable is unnecessary - simply test for zero counter value.

unsigned int counter;

__interrupt void TPM1_OVF(void)

{

   TPM1SC_TOF = 0;  // Clear TPM overflow flag

   if (counter)  counter--;

}

...

void wait_delay( unsigned int delay)  // Multiples of 16.384 ms

{

   counter = delay;

   while (counter);  // Wait for timeout

   PTCD_PTCD0 = ~PTCD_PTCD0;

   PTCD_PTCD1 = ~PTCD_PTCD1;

   PTCD_PTCD2 = ~PTCD_PTCD2;

}

Regards,

Mac

0 Kudos
457 Views
gauravgarg
Contributor I

Than you mac.... i got result....

0 Kudos
458 Views
weapon
Senior Contributor I

Hi Grag,

You can try to count the TPM interrupt in the ISR, for example, the TPM overflow time is 3s, then you can define a global variables to count the overflow interrupt, when the counter reach 10, that means 30s is passed.

unsigned int tpm_overflow_count = 0;

unsigned char flag_30s = 0;

__interrupt void TIMER_OVF(void)

{

  /* Write your interrupt code here ... */

TPM1SC_TOF = 0;

  PTCD_PTCD0=~PTCD_PTCD0;

  PTCD_PTCD1=~PTCD_PTCD1;

  PTCD_PTCD2=~PTCD_PTCD2;

tpm_overflow_count++;

  if( tpm_overflow_count ==10)

  {

      tpm_overflow_count = 0;

      flag_30s = 1; // reach 30s

  }

}

Hope it is helpful.

B.R

XWP

0 Kudos
457 Views
gauravgarg
Contributor I

Thank you  ,

problem had solved.

~Garg

0 Kudos