Interrupts using MC9s08GB60A

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

Interrupts using MC9s08GB60A

1,491 Views
LTi
Contributor I
I am new at this so any help and as detailed as possible would be great.  I am using the MC9S08GB chip with a 32.769K crystal as the reference for a clock.  I set the ICGC1 to FEE mode and crystal reference.  Then I have the ICGC2 to set the Ficgout to ICGCE = 0x00 so the Ficgout = 8.39MHz.  So then Fbus = Ficgout/2 = 4.19MHz.  The next step i did was to set up my TPM2SC = 0x4A, which divides Fbus, as its source, by 4 to  create  a timmer with a frequency of 1048576 Hz (essentially an interrupt is set for every .954 usec)  then i have IRQSC = 0x3A for the TPM1SC which is another interrupt for writing to the EEPROM.
 
I am trying to create a clock with good accuracy (internal clock will not suffice).  So what my questions is how do I set up to have an interrupt at every 100usec instead of possibly .954 usec?  I believe i have to configure the TPM2MOD but im not quick sure how to or how it works.  If anyone has a better way of doing this, please let me know.  I appreciate it. 
Labels (1)
0 Kudos
Reply
3 Replies

457 Views
bigmac
Specialist III
Hello,
 
There seems to be a misunderstanding about the interrupt operation of the TPM module.  For a TPM clock frequency of 1.048576 MHz (after pre-scaling), this will give a clock period of 0.954 us, however, interrupts do not occur at this rate.  You have the choice of using an interrupt on each TPM module overflow (by default each 65536 TPM clock cycles), or setting up a TPM channel for output compare operation after each 100 us increment.
 
With the TPM clock you are using, an interrupt will need to occur each 105 TPM clock cycles (or 420 bus cycles).  To use the TPM overflow method, you would need to change TPMxMOD to a value of 104.  Alternatively, you could enable output compare operation for one of the TPM channels (software compare only), and then simply increment the current channel value by 105, on the occurrence of each output compare interrupt.
 
Personally, I would usually prefer the latter method because the TPMxMOD value is unaffected, therefore not disrupting the operation of the other channels for the TPM module.  For either method, you will need to ensure that the interrupt processing code within lthe ISR occupies considerably less than 420 bus cycles to execute, and the allowable time is likely to be affected by any other interrupts that may also occur.
 
If you are attempting to provide an accurate real time clock,  100 us interrupts may not be the best choice.  With the watch crystal you are using, the interrupt period will actually be 100.1358...us.  A better choice might be 122.0703...us (1/8192 seconds).
 
Regards,
Mac
 
0 Kudos
Reply

457 Views
LTi
Contributor I
So looking at your advice, i tried coming up with different system configuration of the clock and all and came up with the following.  With a 32768Hz crystal, I have set:

Ficgout = 2097152
Fbus = Ficgout/2 = 1048576
Ftpm = Fbus/R = Fbus/128 = 8192

So now i can create an interrupt every 122.0703...us by setting TPMxMOD = 1?  and i'll have a counter which after 8192x will increment a 'seconds' variable.  Does anyone see a problem with this or bad programming/issues?  Would there be better implementations?  Using the Timmer interrupt, i also increment other variables, but as far as that, nothing long and complicated. 
0 Kudos
Reply

457 Views
bigmac
Specialist III
Hello,
 
Actually, setting TPMxMOD to a value of 1 will not give the result you require.  I suspect that the interrupt period would be 244 us.  It is unnecessary to use large prescale values, and extremely small modulo settings.  A prescale value of 1, and a modulo setting of 127 should give the correct interrupt period.
 
However, unless there are specific reasons not to do so, there are advantages for using of a much higher bus frequency - it provides many more bus cycles between interrupts.  For example, for a bus frequency of 8.388608 MHz, the modulo setting would be 1023, or for a frequency of 16.777216 MHz, the modulo setting would be 2047.
 
Regards,
Mac
 
0 Kudos
Reply