1. Timer structure
Check the chip manual to find the following timer structure diagram
From left to right, analyze the picture: (not considered)
Prescaler: Timers 0 and 1 share an 8-bit divider, while timers 2, 3, and 4 share another 8-bit divider. The divider divides the input PCLK into: PCLK/(prescaler+1).
Clock divider & MUX: Each timer has a clock divider, which generates 5 different divided signals (1/2, 1/4, 1/8, 1/16, and TCLK). Each timer block receives its own clock signal from the clock divider, and the clock divider receives the clock from the corresponding 8-bit divider. The 8-bit divider is programmable and divides PCLK according to the loaded value, which is stored in the TCFG0 and TCFG1 registers. At this time, the clock frequency of the timer is: Timer input clock Frequency = PCLK / (prescaler+1) / divider value.
Control logic: The count buffer register (TCNTBn) has an initial value that is loaded into the down counter when the timer is enabled. The compare buffer register (TCMPBn) has an initial value that is loaded into the compare register to be compared with the value of the down counter. The double buffering feature of TCNTBn and TCMPBn enables the timer to produce a stable output when changing the frequency and duty cycle. Each timer has its own 16-bit down counter, which is counted by the Timer input clock Frequency. When the down counter reaches 0, a timer interrupt request is generated to notify the CPU of the timer interrupt. When an interrupt occurs, the value of TCNTBn is automatically loaded into the next counter to continue the next operation. The timer can be stopped by clearing the timer enable TCONn bit during the timer operation mode, and the value of TCNTBn will not be reloaded into the counter.
TOUTn & Dead Zone Generator: S3C2440 has five 16-bit timers. Timers 0, 1, 2, and 3 have pulse width modulation (PWM) functions. Timer 4 is only an internal timer without output pins. Timer 0 has a dead zone generator.
*PS:
The compare buffer register (TCMPBn) is used for PWM generation
The timer reload operation occurs automatically when the down counter reaches 0.
prescaler value = 0~255; divider value = 2, 4, 8, 16
The content about dead zone will be discussed in the following blog post (digging pit)
2. Timer interrupt program design
I. Initialize timer interrupt
This can be compared to the external interrupt operation (because it is also in irq mode), so the specific role of the duplicate register is mentioned in the previous blog S3C2440 Development Board Practice (4): External Interrupt.
Here, we set the ultimate goal to execute the timer interrupt function once every 0.5 seconds.
Setp 1: Turn on the interrupt master switch in CPSR
mrs r0, cpsr
bic r0, r0, #0xf //Set the mode to user mode
bic r0, r0, (1<<7) //external interrupt IRQ open
msr cpsr, r0 //user mode
ldr sp, =0x33f00000 //Set the user stack (useless, just compare with the future interrupt address)
Step 2: INTERRUPT MASK (INTMSK) REGISTER
void init_EINT(void)
{
INTMSK &= ~(1<<10);
// Interrupt corresponding to the button: TIMER -> The corresponding bit is: 10
}
Step 3: Configure timer interrupt parameters
Here we take PCLK=50Mhz as an example. For 2440, the clock configuration is very simple. For the specific clock settings, please refer to the second part of S3C2440 Development Board Practice (2): start.S Initial Understanding + SDRAM Configuration + Relocation.
According to the formula: Timer input clock Frequency = PCLK / (prescaler+1) / divider value. We set prescaler = 99; divider value = 16. It can be calculated that the frequency of the timer clock is 31250hz. So if you want to time 1s, the initial value of the counter should be 31250. According to this parameter configuration, set the timer register as follows:
1. TIMER CONFIGURATION REGISTER0 (TCFG0)
Function: Configure two 8-bit prescalers and configure the dead zone length
Do: Set prescaler0 to 99
2. TIMER CONFIGURATION REGISTER1 (TCFG1)
Function: 5-MUX & DMA mode selection
Do: Set the Clock divider corresponding to the Timer to 16 divisions
3. TIMER 0 COUNT BUFFER REGISTER (TCNTB0)
Function: Set the initial value of the down counter
Do: From the ultimate goal, we can see that the initial value is 31250 / 2 = 15625
4. TIMER CONTROL (TCON) REGISTER
Function: Update the initial value of the down counter, set to automatic loading, start, output inverter
Do: Manually update the initial value of the down counter first, then set it to automatically load and start
* The manual update bit needs to be cleared to 0 after use (cleared to 0 before the next write)
In summary, the timer initialization configuration function is as follows:
void init_timer(void)
{ //TCLK = PCLK/(99+1)/16 = 31250
TCFG0 = 0x63; //prescaler = 99
TCFG1 &= ~(0xf<<0);
TCFG1 |= (3<<0); //1/16 divider
TCNTB0 = 15625; // count 15625 (0.5s)
TCON |= (1<<1); // update TCNTB0 to coutor
TCON &= ~(1<<1); // CLEAR
TCON |= ((1<<0) | (1<<3)); // AUTO RELOAD & START
}
II. Interrupt entry function
Same as the previous article, let’s go straight to the code!
do_irq:
/* Before executing here:
* 1. lr_und holds the address of the next instruction to be executed in interrupt mode
* 2. SPSR_irq saves the CPSR of the interrupt mode
* 3. M4-M0 in CPSR is set to 10010, entering irq mode
* 4. Jump to 0x18 to execute the program
*/
// 1. Protect the scene
ldr sp, =0x33d00000
/* r0-r12 may be modified in the irq exception handling function, so save it first*/
/* lr-4 is the return address after exception handling, also need to be saved*/
sub lr, lr, #4
stmdb sp!, {r0-r12, lr}
// 2. Processing interrupt function
bl Find_interrupt_source
/* 3. Restore the scene*/
ldmia sp!, {r0-r12, pc}^ /* ^ will restore the value of spsr to cpsr*/
III. Set up the timer interrupt function
In the previous article, we designed a function template for external interrupts, so we use it here to extend it. So the timer interrupt function
void Find_interrupt_source(void)
{
puts("begin_interruptnr");
int bit = INTOFFSET;
if(bit == 10)
timer_interrupt_fun();
/* Clear interrupts: clear from the source*/
SRCPND = (1< void timer_interrupt_fun(void) { led_control_NOT(4); } void led_control_NOT(int val) { unsigned int val_DATF = GPFDAT; if(val_DATF & (1 << val)) GPFDAT &= ~(1 << val); else GPFDAT |= (1 << val); } After running, you can see a light flashing. This is the first time I have solved the bug by hand and achieved the goal directly. Although there are program modifications for external interrupts, isn't it exciting to do it yourself?
Previous article:S3C2440 development board practice (6): network configuration + setting up NFS
Next article:S3C2440 development board practice (3): Compilation concept + LED lighting and flashing
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- The New Year is coming soon. What activities and benefits are there in the forum? Is there a year-end bonus? Haha
- [Zero Knowledge ESP8266 Tutorial] Quick Start 26 Blynk Mobile APP Displays Indoor Temperature and Humidity
- Voltage signal amplification and waveform conversion
- During the epidemic prevention and control, don’t forget to have a date with spring!
- Academician criticizes the challenges faced by commercialization of electric vehicles with a range of 1,000 kilometers
- TB5128 circuit file
- "Power amplifier experimental case" Application of power amplifier in magnetoacoustic imaging method of ultrasonic detection
- The use and introduction of Crazy Shell AI open source drone ground station host computer
- Please recommend a single-supply precision dual op amp, millivolt level, preferably in SOP-8 package
- Solution sharing: Type-C fast charging solution for fascia gun