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

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

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

ソリューションへジャンプ
972件の閲覧回数
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....

ラベル(1)
タグ(1)
0 件の賞賛
返信
1 解決策
567件の閲覧回数
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 件の賞賛
返信
4 返答(返信)
567件の閲覧回数
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 件の賞賛
返信
567件の閲覧回数
gauravgarg
Contributor I

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

0 件の賞賛
返信
568件の閲覧回数
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 件の賞賛
返信
567件の閲覧回数
gauravgarg
Contributor I

Thank you  ,

problem had solved.

~Garg

0 件の賞賛
返信