how to configure free running timer in HSC08 MCUs?

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

how to configure free running timer in HSC08 MCUs?

Jump to solution
3,059 Views
senthilkumar
Contributor III

Hello,

Kindly help me,

How to configure free running timer for 1us?

How to find system frequency?

Labels (1)
1 Solution
2,494 Views
Robinwithu
Senior Contributor I

I sent you one message.please have look.

Regards,

Robin

View solution in original post

21 Replies
2,469 Views
senthilkumar
Contributor III

Hello,

How to configure free running counter at 40 Mhz and how to find out each counting time?

Kindly give me related answer?

0 Kudos
2,469 Views
Robinwithu
Senior Contributor I

Hi Senthil,

Here is code which i Tested for free running counter configuration .This code toggles (On and OFF) the PORTA for half period of counter value i.e. 128.

code is working fine on PE full chip simulator.

Note: The CPU frequency is in Between 32Mhz to 40Mhz. it depends on your reference frequency so change it in your hardware programmer which is described in my previous comment link.

// prototypes

void main(void);

void COUNTER_Init(void);

void main(void) {

//  EnableInterrupts;

  /* include your code here */

   ICSC2=0x00;              // BDIV=0 (Bus Frequency is divided by one)

 

 

   PTADD =0xFF;        // Define Port A as output port

   PTAD = 0x00;       // Inilise PortA output to Zero

 

   COUNTER_Init();        // Initilise the Counter

  for(;;) {

//   __RESET_WATCHDOG();    /* feeds the dog */

  

    if (MTIMCNT>128){

        PTAD = 0xFF;

    }

  

    else  {

        PTAD = 0x00;

    }

  

  }

}

  void COUNTER_Init(void) {

    

    

      MTIMCLK=0x00;      // Bus clock (BUSCLK) is selected and Prescaler is zero

      MTIMCNT=0x00;      // Initilise your counter

      MTIMMOD=0x00;      // A value of 0x00 puts the MTIM in free-running mode

      MTIMSC=0x00;      // Start the Timer (TSTP=0 ; Timer start bit) after all above Configuration

  }

I hope, this will help you.

Regards

Robin

2,469 Views
senthilkumar
Contributor III

Hello Pradeep Singh,

Please tell me How to configure this "ICSTRM" internal clock source register ?

the free running counter automatically hardware reset or we need to reset ?

Regrads,

Senthil

0 Kudos
2,469 Views
Robinwithu
Senior Contributor I

Hi Senthil,

There are two ways to reset your Free running counter

1) as soon as you write  MTIMCNT=0x00; it will reset the counter and start counting from zero

2) when your Free Running  counter overflow i.e. you have 8 bit counter and when MTIMCNT=0xFF;

Automatically in next cycle it will reset to zero , start counting from zero till 0xFF.

can you tell me which hardware you are using to program (Flash) your chip and also your code warrior version than i will tell you how to configure "ICSTRM" register.

One more thing is that above code  is working have you tested that?

Regards

Robin

2,469 Views
senthilkumar
Contributor III

Hello Pradeep Singh,

I am using DEMO9S08SH8 development board and  Code warrior version 6.3 .Above free running code working fine .

Thank you.

Regards,

Senthil

0 Kudos
2,469 Views
Robinwithu
Senior Contributor I

Hi Senthil,

I am using Codewarrior 10.5 so for me  The location of the file  "9s08sh8.s8p"  is as follows

C:\Freescale\CW MCU v10.5\MCU\bin\plugins\support\HC08\gdi\P&E       

for you may be it'S in  "C:\Program Files\Freescale\CW06 V5.0\prog".

so  find out first this file

and at position 10  you will get following line

09BIT_TRIM=004A/004B/01/FFAF/FFAE/001E8480/

The last eight digits 001E8480 has to do with triming. if you convert this value to decimal and multiply with 8 you will get 16Mhz which is Bus frequency and your Counter frequency is double of your Bus frequency cause of the changes in code.

to get 40Mhz Counter frequecy (DCLK) you need  20mhz bus frequency i changed it to 002625A0 ; but as i don't have debugger and programmer so i can't  guarantee that's a correct value but u can try.in simulator i am getting bus frequency appx.23Mhz and Counter frequecy appx. 47Mhz which is wrong i know.

you can try it ...i am also trying as soon as i will get solution, will let you know and please tell me the result of above changes in file on controller may be their is some problem in  simulator or my interpretation is wrong ...so please let me know the result.

one more,  thing in your code add the following lines  in place of    // ICSTRM=0x50;

ICSTRM = *(unsigned char*)0xFFAE;

Happy programming :smileyhappy:

Regards

Robin

2,469 Views
senthilkumar
Contributor III

Hello ,

I am using DEMO9S08SH8 development board and  Code warrior version 6.3. I wrote a LED blink code but not Not working. Please any one can upload LED Blink working code.

My LED Blink code is,

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

#include "MC9S08SH8.h"

void main()

{

       PTCDD_PTCDD0 = 1;

       PTCDD_PTCDD1 = 1;

       for(;;)

       {

        PTCD_PTCD0 = 1;

        PTCD_PTCD1 = 1;

        for(i=0;i<50000;i++);
      

        PTCD_PTCD0 = 0;

        PTCD_PTCD1 = 0;

        for(i=0;i<50000;i++);

       }

}

0 Kudos
2,469 Views
Robinwithu
Senior Contributor I

Hi Senthil,

Here is correction in your LED Code ,it's working fine

int i=0;  

   

       PTCDD =0xFF;        // Define Port C as output port

       PTCD = 0x00;       // Inilise Port C output to Zero

             

       for(;;)

       {

      for(i=0;i<50000;i++){  

          

        PTCD_PTCD0 = 1;

        PTCD_PTCD1 = 1;

       

      }

        for(i=0;i<50000;i++)

     

        {

        PTCD_PTCD0 = 0;

        PTCD_PTCD1 = 0;

        }

   

       }

Happy Progaming :smileyhappy:

Regards

Robin

2,469 Views
senthilkumar
Contributor III

Hello Robin,

Above that code also is not working. Please give me a working code.

0 Kudos
2,469 Views
Robinwithu
Senior Contributor I

Hi Senthil,

the problem is  in "Int i" definition ,

cause "int i" varibale Range of decimal value  is "-32768..+32767" as soon as you will change it to "unsigned int i " it's  become 0..65535;

in for loop you have defined it I=50,000 which is greater than 32000

so either you can defined it "unsigned Int i"  and try the code  has to work or you can change the value in for loop instant of 50000 to 25000 .

The code must work.

Let me know after changes the code is working or not.

Regards

Robin

2,469 Views
senthilkumar
Contributor III

Hello Robin,

Hi LED code working fine . thank you.

I am using DEMO9S08SH8 development board and  Code warrior version 6.3.

I have few questions,

1. I got example code for clock initialization at bus frequency 20 Mhz but How can i ensure that.

that code is,

ICSC1_CLKS  = 0;

  ICSC1_IREFS = 1;

  ICSC1_RDIV  = 0;

  ICSC2_BDIV  = 0;

  ICSTRM      = 0x58;

2. I have configured free running timer at bus frequency 20 Mhz and pres caller divider 256.

  How to find out timing of each count ( I want know the counter incrementing time period)

freerunning code is,

Please any one help me.

MTIMCLK=0x08
MTIMCNT=0x00
MTIMMOD=0x00
MTIMSC=0x00;

Regards

Senthil

0 Kudos
2,495 Views
Robinwithu
Senior Contributor I

I sent you one message.please have look.

Regards,

Robin

2,469 Views
senthilkumar
Contributor III

Hello Robin,

With your great help, we were able to finally reach to the below working code:

void main(void)

{

    UINT16 localCntr_25ms = 0;

    UINT8  portToggle = 0;

  

    SOPT1 = 0x03;   // disbale watchdog

   

    ICSC1_CLKS  = 0;    // TODO senthil

    ICSC1_IREFS = 1;

    ICSC1_RDIV  = 0;

    ICSC2_BDIV  = 3;   // 32MHz/8 => 4MHz, fsys is 4MHz and fbus = fsys/2 => 2MHz.

    //ICSTRM      = 0x96;

 

    MTIMCLK=0x08;      // Bus clock (BUSCLK) is selected and Prescaler is 256

    MTIMCNT=0x00;      // Initilise your counter

    MTIMMOD=0x00;      // A value of 0x00 puts the MTIM in free-running mode

    MTIMSC=0x00;       // Start the Timer (TSTP=0 ; Timer start bit) after all above Configuration  

    PTBDD_PTBDD7 = 1;  // Define Port C as output port

   

    SOPT1 = 0x03;   // disbale watchdog

   

    for(;;)

    { 

        // with above settings, the MTIMCNT changes every 128us.

        if (MTIMCNT > 200)

        {

            // 25.6ms

            localCntr_25ms++;

           // MTIMCNT = 0;   

           MTIMSC_TRST=1;

        }

       

        if (localCntr_25ms > 40)

        {

            // approx. 1000ms

            portToggle++;

            PTBD_PTBD7 = (portToggle & 0x01);

            localCntr_25ms = 0;

        }

    }

}

Regards,

Senthil,

2,467 Views
Robinwithu
Senior Contributor I

Hi Senthil,

You both are welcome :smileyhappy:

Happy Progming

Regards,

Robin

0 Kudos
2,467 Views
senthilkumar
Contributor III

Hello,

I am working DEMO9S08SH8 development board  with CW V6.3.

My GPIO interrupt  is not working. my code is ,

void main()

{

    PTBSC_PTBIE = 0; // Disable interrupt

    PTBPS_PTBPS0 = 1; // PortB, bit-0 is enabled for interrupt

    PTBSC_PTBMOD = 0; // edges only

    PTBES_PTBES0 = 0; // interrupt on falling edge

    PTBSC_PTBACK = 1;    // acknowledge flag

    PTBSC_PTBIE = 1; // enable interrupt

PTBDD_PTBDD0 = 0;                  

EnableInterrupts;

for(;;)

{

     ;

}

}

interrupt 21 void PORTB_ISR(void)

{

      PTBSC_PTBACK = 1;    // acknowledge flag

}

Please any one help me

0 Kudos
2,469 Views
Robinwithu
Senior Contributor I

Hello Senthil,

Here is Timer configuration for Free running counter for S08RNA16/8/4 CONTROLLER .

FTM0_CNT = 0x0000;             // The FTM Counter is Free Running Counter by Defining MODH:L=0x0000 or MODH:L=0xFFFF;

FTM0_MOD = 0xFFFF;

FTM0_SC  =  0x0F;                     //CPWMS=0; Counter Mode;System Clock (Deafult) 16  Mhz, FEI mode Default, Prescaler Factor 1:128

Hope it will help you.

Regards

Robin

0 Kudos
2,469 Views
senthilkumar
Contributor III

Hello Pradeep Singh,

Thank you for your reply, Please tell me where can I check system frequency?

0 Kudos
2,469 Views
senthilkumar
Contributor III

Hello ,

How to configure my system frequency at 40 Mhz ?

0 Kudos
2,469 Views
Robinwithu
Senior Contributor I

Hello Senthil,

Here is System  Frequency  configuration  for S08RNA16/8/4 CONTROLLER .

ICS_C1 = 0x04;          // internal reference clock (31250to39250Hz) to FLL (512) and FLL is generating 16000-20000 Khz  ; Ref. Freqeuncy  is set at 31250-39250 Hz * 512 = Appx.16-20 Mhz

ICS_C2 = 0x00;        // BDIV = 00, Freq is now 16-20Mhz

BDIV in Default case is ICS_C2=0x02 so the system frequency is  in between 8 to 10 Mhz , but you can change the value of BDIV in ICS_C2=0x00 register as i changed above to zero, prescaler 1:1, it means that Your CPU frequency is now in between 16 to 20 Mhz depend on your Reference frequency multiplied by FLL (512).

But this is valid only for MC9S08RNA 16/8/4 type of controller Archt. In your case it may  be different. can you tell me which controller you are using?

So i can check and tell you at wht freq. ur CPU is operting.

Regards

Robin

0 Kudos
2,469 Views
senthilkumar
Contributor III

Hello Pradeep Singh,

My Controller is MC9S08SH8. Up to 40 MHz HCS08 CPU (20 MHz bus frequency) .I think default CPU frequency 20Mhz. Am I right?

0 Kudos