程序代写代做 C clock Timing Peripherals

Timing Peripherals
• Every microcontroller has at least one timer function.
• In most cases, there are several timer/counter units, each one with a special purpose. The basic operation of a timer/counter is as follows

Timing Peripherals (cont)
• Timers may count UP only, DOWN only, or UP/DOWN.
• Timers can also be STARTED or STOPPED.
• Control register bits control this operation.

Timing Peripherals (cont)
• The counter frequency is typically some multiple of the system clock.
• Usually control register bits allow various divisions of the clock.
• A T-flip-flop acts as a simple clock divide circuit.

Watchdog Timers
• Embedded systems must be able to run
forever without user interaction.
• The watchdog (computer operating properly) timer is intended as a check to ensure software doesn’t “go crazy”.
• Typically, if the watchdog ever wraps around, the CPU resets. Software must clear it back to zero before that happens.

Watchdog Timers (cont)
• For example, suppose the system clock is 1MHz, and the watchdog is a 16-bit counter.
• The watchdog will wrap around after approximately 65ms.
• At least once every 65ms, software must reset the watchdog.
• If it jumps to the wrong place, gets hung up in a loop, or otherwise fails, this will not happen, and the system will reset.

System Clock Timers
• The most fundamental use of time is to do something at regular intervals. For this, we have the system clock.
• Typically, the system clock sets a flag (status register bit) when something interesting happens.
• Software can monitor this bit and react.

Up-counting System Clock
• An up-counting system clock would set its flag on each wrap from FFFF to 0000.
• To set a specified time interval, a programmer would write 10000-TIME to the data register, then wait for wrap around.
• Eg: 1MHz clock. A write of (decimal) 64536 will cause a wrap around event in 1000us = 1ms.

Down-counting System Clock
• A down-counting system clock would set its flag on each wrap from 0000 to FFFF.
• To set a specified time interval, a programmer would write TIME to the data register, then wait for wrap around.
• Eg: 1MHz clock. A write of 1000 will cause a wrap around event in 1000us = 1ms.

Repeating System Clock
• In the simple examples given, the needed value must be written each time the event is desired.
• More sophisticated timers will let you write only once, leading to repeated events.
• Eg: write (decimal) 1000 to some register once and get an event each 1ms.
• It is usually also possible to enable or disable counting.

Our System Clock
• For the purposes of this course, we will use the following, very simplified model:
• This counter always counts DOWN
• Statusbit0turnsONwhenthetimerreaches0 • NotethatthisisNOThowtheARMworks

Output Compare Unit
• Output compare is used to generate output
events at specific, precise times.
• Essentially, you can give the microcontroller an instruction like “I want bit 4 of port C to turn on in exactly 100 microseconds from now”.

Output Compare Unit
• A typical output compare requires the following steps:
1) Enable a timer. Usually this is NOT the same as the
system clock timer
2) Specify a time. Usually this is some number of ticks from NOW.
3) Specify an event. Set/clear/toggle some bit. Sometimes the bit is fixed. Sometimes you can specify it.

Output Compare Unit
• The unit will then set a status flag telling you it is finished, in case you want to do something else.

Output Compare Unit • Our simple output compare unit:

Output Compare Unit
• The timer register ($180) counts up at 1MHz
• Control register bit 7 clears the timer
• Control register bit 6 enables/disables the timer
• The output pin takes on the value of Control register bit 0 when the Compare register ($102) value exceeds the timer ($180) value
• Status register bit 0 indicates that a compare event has happened.

Input Capture Unit
• An Input Capture unit is the mirror image of
the Output Compare
• In this case, we are saying to the microcontroller, “Watch bit 4 of port C. Tell me exactly when it goes high”

Input Capture Unit
• Input Capture is associated with an up timer. Usually, this is the same as the Output Compare timer
• Specify a pin to watch
• Specify a state to watch for (high/low/toggle)
• Set some status bit to indicate the event has happened.
• Set a data register to indicate WHEN the event happened.

Input Capture Unit • Our simple input capture unit:

Input Capture Unit
• Hereisaneffectivecircuitthat(sortof)captures how it works:

Input Capture Unit
• The timer register ($180) counts up at 1MHz
• Control register bit 0 selects HIGH or LOW to detect
• Control register bit 1 starts a capture
• The data register takes on the value of the
timer the first time the input is matched.
• Status register bit 0 indicates that a capture event has happened.