Topic09 The Timer Subsystem On The Freescale MC9S12X
Topic09 The Timer Subsystem On The Freescale MC9S12X
Topic09 The Timer Subsystem On The Freescale MC9S12X
Timer
The timer section of the 9S12X is based around a 16 bit free running counter driven by the system clock. The timer system provides the following basic functions:
A Timer overow
Can be used to generate interrupts at predetermined intervals.
Can be used to latch the value in the free running counter on the edges of an input signal.
Timer Overow
One of the great features of the timer subsystem is the timer overow. The timer is a free running 16 bit counter called TCNT, it increments from $0000 to $FFFF, then overows back to $0000 and continues once more. Each time TCNT resets back to $0000, the timer can trigger a Timer Overow Interrupt (TOI). The period of the TOI can be altered by changing the rate at which the counter TCNT increments. The counter increments on each rising edge of the clock source, so changing the frequency of the clock source, through prescaling would therefore change the period between TOIs.
Timer Overow
A 2bit Example
Timer Overow
A 2bit Example
Timer Overow
A 2bit Example
Timer Overow
A 2bit Example
Timer Overow
A 2bit Example
TOI
Programming the Timer Overow is done using the following registers of the Enhanced Capture Timer.
TCNTH TSCR1 TCNTL TSCR2 TFLG2
PRNT(Precision Timer)
This bit controls the behavior of the Timer. 0 means legacy timer is enabled. 1 means precision timer is enabled.
TSCR2
Example
We would like to create a program to toggle the output on pin 1 of PORTA every 131ms. Firstly, we will need to calculate the value of PR.
TOIperiod = (2PR*65536)/fbus PR = log2 ( (TOIperiod*fbus)/65536 )
Example
mainloop: MOVB #$F7,IVBR MOVB #$01,DDRA MOVB #$01,PORTA JSR Spin InitTOI: InitTOI BRA Spin MOVB #$80,TSCR1 MOVB #$84,TSCR2 CLI RTS TOI_ISR: MOVB #$80,TFLG2 ; Reset TOF Flag LDAA EORA STAA RTI PORTA #$01 PORTA ; Turn on Timer Subsystem, Fast Flag Clearing is OFF. ; Turn on TOI, TCRE is OFF and prescaler set at 4. ; Turn ON interrupts.
Example
The duty register denes the value at which the PWM channel will change its state. The period register denes the value at which the counter and channel must reset.
011
101
011
101
011
101
011
101
011 011
101
011
101
011
101
011
101
011
101
011
101 101
011
101
011
101
011
101
Interaction with the PWM subsystem is done via the following register set.
PWME PWMSCLA PWMPOL PWMSCLB PWMCLK PWMCNTx PWMPRCLK PWMDTYx PWMCAE PWMPERx PWMCTL
Friday, 5 November 2010
Prescaler for Clock SA fClockSA = Clock A / ( 2 * PWMSCLA) Note a value of $00 in PWMSCLA means 256.
Prescaler for Clock SB fClockSB = Clock B / ( 2 * PWMSCLB) Note a value of $00 in PWMSCLB means 256.
PWM Clocks
Each Channel has its own counter associated with it. These counters can be read at any time but can not be written to.
Each Channel has its own period register associated with it. These registers can be written to at any time, but it is always a good idea to turn off the channel before modifying it.
Each Channel has its own duty register associated with it. These registers can be written to at any time, but it is always a good idea to turn off the channel before modifying it.
When using the PWM system there are a few questions that you need to ask about your application.
Friday, 5 November 2010
Does the application require 8 or 16 bit PWM? What clock frequency does your application require? Does the waveform need to be center-aligned? Does the output waveform need to start at 0 or 1?
Example
We have an application that requires a 10Hz waveform with a 50% duty cycle (PWMPER = 2 * PWMDTY) on PWM0.
The PWMPER in combination with clock prescalers will enable us to create a 10 Hz waveform. Assuming we used ClockA as our clock source and also assuming our bus clock was 8MHz, we could only prescale Clock A down to 62.5kHz (8MHz/ 128). Therefore we shall require the use of clock SA.
Example
If we choose PCKA2:0 to be 7 and PWMSCLA to be 132 then PWMPER0 must be PWMPER0 = 8,000,000/(10 * 128 * 2 * 132 ) PWMPER0 = 23.67 (24) PWMDTY0 = 12 (for 50%)
Example
If we choose PCKA2:0 to be 7 and PWMSCLA to be 132 then PWMPER0 must be PWMPER0 = 8,000,000/(10 * 128 * 2 * 132 ) PWMPER0 = 23.67 (24) PWMDTY0 = 12 (for 50%)
Example
If we choose PCKA2:0 to be 7 and PWMSCLA to be 132 then PWMPER0 must be PWMPER0 = 8,000,000/(10 * 128 * 2 * 132 ) PWMPER0 = 23.67 (24) PWMDTY0 = 12 (for 50%)
Example
ClockSA The problem can be reduced mathematically Prescaler to 10Hz = fbus /( 2PCKA2:0 * 2 * PWMSCLA * PWMPER0)
If we choose PCKA2:0 to be 7 and PWMSCLA to be 132 then PWMPER0 must be PWMPER0 = 8,000,000/(10 * 128 * 2 * 132 ) PWMPER0 = 23.67 (24) PWMDTY0 = 12 (for 50%)
Example
If we choose PCKA2:0 to be 7 and PWMSCLA to be 132 then PWMPER0 must be PWMPER0 = 8,000,000/(10 * 128 * 2 * 132 ) PWMPER0 = 23.67 (24) PWMDTY0 = 12 (for 50%)
Example
If we choose PCKA2:0 to be 7 and PWMSCLA to be 132 then PWMPER0 must be PWMPER0 = 8,000,000/(10 * 128 * 2 * 132 ) PWMPER0 = 23.67 (24) PWMDTY0 = 12 (for 50%)
Example
If we choose PCKA2:0 to be 7 and PWMSCLA to be 132 then PWMPER0 must be PWMPER0 = 8,000,000/(10 * 128 * 2 * 132 ) PWMPER0 = 23.67 (24) PWMDTY0 = 12 (for 50%)
Example
mainloop: Spin InitPWM: JSR InitPWM BRA Spin MOVB #$00,PWME MOVB #$00,PWMPOL MOVB #$01,PWMCLK MOVB #$00,PWMCAE MOVB #$00,PWMCTL MOVB #24,PWMPER0 MOVB #12,PWMDTY0 MOVB #$01,PWME RTS ; Turn OFF PWM Channels. ; All PWM channels start low. ; Clock SA chosen for PWM0. ; All PWM channels are left-justied. ; All PWM channels are independent. ; Set the period to 24 (10Hz). ; Set the duty cycle to 12 (50%). ; Enable PWM0