real time interrupts

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

real time interrupts

Jump to solution
1,069 Views
russianspy1234
Contributor I

I am using a MC9S08MP16 with an external crystal for a clock.  I am trying to get my program to hold for a very specific amount of time (3.24 seconds), but I can't seem to figure out how to use the RTI flags.

Labels (1)
Tags (1)
0 Kudos
1 Solution
730 Views
bigmac
Specialist III

Hello,

 

To achieve a timing resolution of 10ms, the output of the RTC prescaler should be 10Hz, or a multiple.  Using a 2MHz crystal, a prescale setting of 20,000 would be suitable.  However, a timeout period of 3.24 seconds would represent a total count of 324 prescaler output cycles, which cannot be directly achieved with the 8-bit modulo value of the RTC.  It would then be necessary to use a minimum of two interrupts (each with 1.62 second period) to achieve the required period.

 

The following untested code snippet attempts to demonstrate this operation.

.

 

byte RTIcount;  // Global variable

 

void timer_start( void)

{

   RTIcount = 2;

   RTCMOD = 162;

   RTCSC = 0xBC; // Clear flag and enable interrupt

 

   // Action required at start of timing period

 

}

 

interrupt void ISR_RTC( void)

{

   RTIcount--;

   if (RTIcount == 0) {

      RTCSC = 0xAC;  // Clear flag & disable further interrupts

 

      // Action required at end of timing period

 

   }

   else {

      RTCSC = 0xBC;  // Clear flag & continue interrupts

   }

}

.

Regards,

Mac

 

View solution in original post

0 Kudos
5 Replies
730 Views
bigmac
Specialist III

Hello, and welcome to the forum.

 

The suitability and approach to using the RTI (or maybe MTIM) for generating a 3.24 second delay will depend on the timing accuracy that you require, and the crystal frequency you are using.  Some crystal frequencies will be more convenient than others, and potentially produce greater accuracy.

 

For example, if you were using a 32.768 kHz watch crystal as the external reference, your required timing period would be represented by 106168 clock cycles.  This has factors of 8 x 23 x 577.  The last factor exceeds an 8-bit modulo count.

 

With this example, the approach might be to set the RTI modulo value to 184 (8 x 23), with a prescale value of 1.  This would generate an interrupt every 5.62 milliseconds.  Within the ISR code, you might decrement a global variable that starts with an initial value of 577.  When the variable decrements to zero, the required delay period would have elapsed.  Make sure that you clear the RTI flag within the ISR code.

 

With a selected higher frequency crystal, you might be able to make use of the prescaler, and possibly achieve the delay period with a single interrupt, i.e. with a prescaled clock of 50Hz, and a modulo value of 162, or maybe a prescaled clock of 75Hz, and a modulo value of 243.  Of course, the crystal frequency will also need to be suitable for the external reference range needed by the ICS module.  With this approach, crystal frequencies of 5.000 MHz or 4.9152 MHz would appear suitable.

 

For future reference, threads concerning the operation of the HCS08 MCU hardware would usually be posted in the 8-bit forum.

 

Regards,

Mac

 

0 Kudos
730 Views
russianspy1234
Contributor I

The crystal is 2Mhz, and was used for this project in the past, we are in the process of upgrading to this microprocessor, and I can't even figure out how to set up the ISR code properly.

0 Kudos
731 Views
bigmac
Specialist III

Hello,

 

To achieve a timing resolution of 10ms, the output of the RTC prescaler should be 10Hz, or a multiple.  Using a 2MHz crystal, a prescale setting of 20,000 would be suitable.  However, a timeout period of 3.24 seconds would represent a total count of 324 prescaler output cycles, which cannot be directly achieved with the 8-bit modulo value of the RTC.  It would then be necessary to use a minimum of two interrupts (each with 1.62 second period) to achieve the required period.

 

The following untested code snippet attempts to demonstrate this operation.

.

 

byte RTIcount;  // Global variable

 

void timer_start( void)

{

   RTIcount = 2;

   RTCMOD = 162;

   RTCSC = 0xBC; // Clear flag and enable interrupt

 

   // Action required at start of timing period

 

}

 

interrupt void ISR_RTC( void)

{

   RTIcount--;

   if (RTIcount == 0) {

      RTCSC = 0xAC;  // Clear flag & disable further interrupts

 

      // Action required at end of timing period

 

   }

   else {

      RTCSC = 0xBC;  // Clear flag & continue interrupts

   }

}

.

Regards,

Mac

 

0 Kudos
730 Views
russianspy1234
Contributor I

Thank you, that is really helpful.  I am not too worried about how many times I need to call the interupt, since nothing (codewise) needs to happen while the program is waiting, it's all taken care of by attached hardware.  I am really new to programming microprocessors, I am still having some trouble setting up the bits for ICSC1, ICSC2, and ICSSC to properly use my external crystal.

0 Kudos
730 Views
JorgeOchoa
Contributor II

Remember you can use for more precision the TPM module.

Regards!!!

 

0 Kudos