# Timers and internal clock generator on Freescale MCU

I am using MC9S08AW60A with DEMO9S08AW60E Board (Freescale). I have studied about the timers given with the MCU. I wish to know how exactly to Implement timers by code. The Freescale suite also comes with Processor Expert (a GUI to configure the MCU).

I have written the following code to light-up the LED's in a sequential manner WITHOUT using a DELAY function i.e only by altering/using TIMERS and INTERNAL CLOCK GENERATOR.

//------------CODE BEGINS------------

PTFDD = 0xFF;
TPM1SC = 0x0E;//configures the Timer 1 Status and Control Register
TPM1C2SC = 0x98;//configures the TImer 1 CHANNEL 2 Status and Control Register
ICGC1= 0xE8; //Have altered the ICG with respect to TPM.
ICGC2= 0x8E;
//As you can see i have not altered the ICG in any way.And the same values as above
//(for TPM) i have emulated using the Processor Expert.
for (i=1;i<=8;i++)
{
if (i==8) i = 1;
if (i==1) PTFD = 0x01;
if (i==2) PTFD = 0x02;
if (i==3) PTFD = 0x04;
if (i==4) PTFD = 0x08;
if (i==5) PTFD = 0x10;
if (i==6) PTFD = 0x20;
if (i==7) PTFD = 0x40;
}

//----------CODE ENDS-----------------


But all I get:

1. LEDs are on but very dim, I think they're toggling very quickly. Am I right?
2. Also, Do I need to change the ICG settings if using TImer?
3. Also, i have initialized the device with Processor Expert and written the same as code. does it interfere. (i have made sure both represent the same values,else it would have shown me an error.

On the data sheet, refer to page 165 for the timer/PWM and page 129 for Inter Clock Generator.

• For your first question, do you have an oscilloscope you can check with? If not, try setting a breakpoint and stepping through. If you do that, each LED should light up full brightness and should only change when you click to the next step. – embedded.kyle Dec 14 '12 at 12:58
• @embedded.kyle: I checked that. Interestingly, the leds dont light up. And i also just realised the LED's and TImer share the same pins. Could tat be the reason?? – sheetansh Dec 14 '12 at 13:10
• @embedded.kyle: ya, i can get an oscilloscope. Which pins do i check for? – sheetansh Dec 14 '12 at 13:20
• They don't light up when you step through? That's curious. I'm not familiar with the chip so I'll have to check the datasheet a bit. But under normal run, use the oscilloscope to check the pins that drive each LED. You should be able to access them from the headers. Check the frequency of the square wave that drive them. – embedded.kyle Dec 14 '12 at 13:26
• Try setting TPM1SC = 0x10. ELSnB:ELSnA need to both be 0 so that the timer doesn't toggle the output on compare. – embedded.kyle Dec 14 '12 at 13:45

Apparently, as the timer frequency is very high, to set it up we would require a large Prescaler Setting(i maxed out at 128). After having had a talk with my colleague, he says that i need to generate a Periodic "Tick" with the flash period for each LED set accordingly. So as to make sure that LED(ON or OFF) corresponds to the TICK rather than the Bus Clock Frequency.Ya, this works but i am still confused.

1. If you are able to use the datasheet to understand the various register functions, then why, setting the frequency of the timer as per requirement, doesnt work?

2. Moreover, isnt this "Tick" the same as the Delay funciton??

here is the code for the same(by the way this code was suggested to me at the freescale forum, but it might also help someone else; i take no credit for this code) :

// Assumed bus frequency: 8.0 MHz
// TPM prescale divisor:  4

// Global variables:
byte tic_cnt;             // Timer tick counter
byte ch_nbr;              // LED channel number

#define mLED0    0x01
#define mLED1    0x02
#define mLED2    0x04
#define mLED3    0x08
#define mLED4    0x10
#define mLED5    0x20
#define mLED6    0x40
#define mLED7    0x80

#define TIC_INCR 20000    // 10 ms tick period, with prescale 4

//***************************************************************

void LED_poll( void)

{

if (!tic_cnt)

{        // Timeout occurred

tic_cnt = 50;       // Start new 500ms timing interval

ch_nbr++;

if (ch_nbr == 8)  ch_nbr = 0;

if (ch_nbr == 0)  PTFD = mLED0;  // Turn LED0 on, other LEDs off
if (ch_nbr == 1)  PTFD = mLED1;  // Turn LED1 on, other LEDs off
if (ch_nbr == 2)  PTFD = mLED2;  // Turn LED2 on, other LEDs off
if (ch_nbr == 3)  PTFD = mLED3;  // Turn LED3 on, other LEDs off
if (ch_nbr == 4)  PTFD = mLED4;  // Turn LED4 on, other LEDs off
if (ch_nbr == 5)  PTFD = mLED5;  // Turn LED5 on, other LEDs off
if (ch_nbr == 6)  PTFD = mLED6;  // Turn LED6 on, other LEDs off
if (ch_nbr == 7)  PTFD = mLED7;  // Turn LED7 on, other LEDs off
}
}

__interrupt VectorNumber_Vtpm1ch0 void ISR_TPM1_ch0( void)

{

TPM1C0_CH0F = 0;       // Clear TPM channel flag

TPM1C0V += TIC_INCR;   // Set next software compare

if (tic_cnt)  tic_cnt--;
}

• "If you are able to use the datasheet to understand the various register functions, then why, setting the frequency of the timer as per requirement, doesnt work?" It is possible that the timer hardware doesn't come with a hardware pre-scaler large enough to create the desired delays. – Lundin Dec 17 '12 at 13:33
• "Moreover, isnt this "Tick" the same as the Delay funciton??" No, far from it. A delay function is something that just burns away CPU cycles in a loop, with little care about real time accuracy. With an interrupt solution such as the one you posted, the CPU can do meaningful tasks while waiting for your timer. For example, it can run the main loop of the program and blink the LED as a "background task". And at the same time you get much better real time accuracy. – Lundin Dec 17 '12 at 13:36
• @Lundin : thansk mate. that clarified a lot. – sheetansh Dec 17 '12 at 14:08