TIMERS AND PROCESSOR EXPERT

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

TIMERS AND PROCESSOR EXPERT

1,503 Views
sheetanshkaushi
Contributor III

HI!

 

I am using MC9S08AW60A with DEMO9S08AW60E Board. I have studied about the timers given with the MCU. I wish to know how exactly to Implement timers by code as i have tried using PROCESSOR EXPERT but no effect. I have uploaded a pic of the PE settings that i had implemented. I have also uploaded the pdf of the chip and the Board.

 

The idea of the code is to sequentially blink the LED's without using a "Delay Function", rather by modifying the TIMER functionality.

 

the code is as follows( very basic, works if i use a delay function):

------------CODE BEGINS------------

PTFDD = 0xFF;

 

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-----------------

 

for the sake of clarity, in the PE1 pic, the black dots mean 1 and white dots mean 0. And yes, i am using virtual PC. before this the codes i tried to excecute, worked perfectly fine.

 

Concurrently, According to the architecture, the TPM are on PORTF pins, as are the LED's does this affect the outocme. I guess No, but i just want to confirm. Does it?

then, if i have initialized with PE, does it mean i should also mannually code the same??

 

Thanks in advance!

 

 

 

 

 

 

 

 

 

 

Labels (1)
2 Replies

591 Views
bigmac
Specialist III

Hello,

The timing associated with the blinking of LEDs is usually of a relatively lengthy duration, but does not require critical timing.  The hardware output(s) from the TPM channel would generally not be directly used since this would require a large prescale setting, and may not even be feasible for high bus frequencies.

The usual method is to generate a periodic "tick" at a specific interval, with the flash period for each LED corresponding to an integral number of tick intervals.  A TPM channel might be utilised to generate the tick period, making use of the "software" compare mode.  A suitable tick period for this example might be 10 milliseconds. The tick timing would be handled using the TPM channel interrupt, but the LED flashing would probably use polling to test for the completion of each flash period.

The following code snippet demonstrates the process using a LED flash interval of 500 ms.  I have not shown the initialisation required for the bus frequency, the TPM, and other modules.  You could make use of the PE initialisation tool for this purpose. although my personal preference is to write my own initialisation code, including more meaningful documentation than provided by PE.

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

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

// LED mask values:
#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


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

// LED blink polling function

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--;
}

Regards,

Mac


591 Views
sheetanshkaushi
Contributor III

Thank you BigMac. i just wanted to let you know, i had also asked this question on Stackoverflow(electrical engineering), where i posted your answer to close the question. If that is in any way, a problem for you please do let me know.

You have been a great help.

0 Kudos