The S3C2440 has five 16-bit timers, among which the timer 0, 1, 2, and 3 have the pulse width modulation (PWM) function. The timer 4 is an Internal timer with no external output pins. Here, we do not detail the working principle of the write timer, but only operate on the timer 0. Other timer operations are similar. The program analysis is as follows:
1. Overall Program Framework
The program is still composed of two parts. startcode is the startup code (in fact, it does not use all the startup code, but it is added together here .), Source is your own program. 2. The program under source parses the main. c file
# Include "timer. H "# include" IRR. H "# include" LED. H "extern int flag; int main () {timer0_init (); // timer 0 initialization function isr_init (); // interrupt initialization function led1_init (); // led initialization function while (1) {; // while (1) loop. this parameter is null, indicating that no command is executed; from this point, we can also see that led flashing is the result of the interruption of the service program due to timer 0 .} Return 0 ;}
Timer. h file
#ifndef_EIMER0_H_#define_TIMER0_H_#include"2440addr.h"void Timer0_Init(void);#endif
Timer. c file
# Include "2440addr. H" Void timer0_init (void) {rt1_0 & = ~ (0xff); // reset the low 8 bits of the t00000 register (t00000 is used to configure the first-level divider) rt00000 | = 99; // write the Division coefficient-1 to the low 8-bit Tsung 0 (Division coefficient = prescaler // value + 1, Here prescaler = 99) rtcfg1 & = ~ (0xf); // reset the tcfg1 low 4 bits (configure the second divider) rtcfg1 | = 0x02; // assign the tcfg1 low 4 bits to 0x02, that is, select the 8-frequency output rtcmpb0 = 0; // load the initial value to tcmpb0. When tcntb0 is reduced to this value, the rtcntb0 = 62500 is interrupted; // The input clock frequency of the timer 0 is 62.5 kHz, that is, the timer counts 62500 times per second. Therefore, 62500 is loaded during initialization, 1 s generates an interruption rtcon | = (1 <1); rtcon = 0x09; // sets the automatic loading bit and enables the timer .}
IRR. h file
#ifndef _ISRSERVICE_H_#define _ISRSERVICE_H_void Isr_Init(void) ;void __irq Timer0_Isr(void);#endif
IRR. c file
# Include "config. H" # include "IRR. H" # include "led. H" int flag = 0; void isr_init (void) {rintmsk & = ~ (1 <10); // remember to open the interrupt .... Pisr_timer0 = (unsigned INT) timer0_isr; // load the endpoint address of the interrupted service program to the memory address // 0x33ffff20} void _ IRQ timer0_isr () // interrupt the service function, when the timer 0 is interrupted. {Flag =! Flag; If (flag = 1) {ledbench on ();} else {ledbench off ();} rsrcpnd | = 1 <10; // after an interruption occurs, clear the two digits into rintpnd |=1 <10 ;}
Led. h file
#ifndef_LED_H_#define_LED_H_extern void Led1_Init(void);extern void Led1_On(void);extern void Led1_Off(void);
The LED. c file initializes the LED, lights up four LEDs, and disables four LEDs.
#include"2440addr.h"#include"led.h"void Led1_Init(void){rGPBCON &=~(3<<10 |3<<12 | 3<<14 | 3<<16);rGPBCON |=(1<<10 | 1<<12 |1<<14 |1<<16);rGPBUP &=~(1<<5 | 1<<6 | 1<<7 | 1<<8);rGPBDAT |=(1<<5 | 1<<6 | 1<<7 | 1<<8);}void Led1_On(void){ rGPBDAT &= ~(1<<5 | 1<<6 | 1<<7 | 1<<8);}void Led1_Off(void){rGPBDAT |=(1<<5 | 1<<6 | 1<<7 | 1<<8);}
The above program enables four LEDs to flash at a frequency of 1 second (four simultaneously, not a fl lamp ).