When explaining the system clock and timer earlier, an experiment was given. The function implemented was: using the function of timer 0 to make the LED flash once per second. At that time, it was implemented using the query method, and now the interrupt method is used to implement the above function.
The following figure shows the layout of my project files:
I'll post my code below, you can also download it here.
http://download.csdn.net/detail/mybelief321/5457371 After downloading, compile directly, click Flash/Download, download to nor flash and run.
main.c file
#include "led.h"
#include "timer.h"
#include "isrservice.h"
#include "interrupt.h"
unsigned int flag = 0;
int main()
{
Led_Init(); //Initialize LED
Timer0_Init(); //Timer 0 initialization
Timer0_Interrupt_Init(); //Timer 0 interrupt initialization
while(1) //Loop, waiting for an interrupt to occur
{
if(flag)
{
Led2_On();
}
else
{
Led2_Off();
}
}
}
led.c file
/****************************************************
* The GPIO ports corresponding to the 4 LED lights on my mini2440 development board
* LED1---GPB5 LED2---GPB6
* LED3---GPB7 LED4---GPB8
*********************************************************/
#include /**************************************************** * Function name: void Led_Init(void) * Global variables: None * Parameter Description: None * Return value; None * Function: Set GPB5-8 as output function, initialize 4 LED lights off *********************************************************/ void Led_Init(void) { GPBCON&=~((3<<10)|(3<<12)|(3<<14)|(3<<16)); GPBCON|=((1<<10)|(1<<12)|(1<<14)|(1<<16)); //Set GPB5-8 ports to output function GPBUP&=~((1<<5)|(1<<6)|(1<<7)|(1<<8)); //Pull-up resistor enabled GPBDAT|=(1<<5)|(1<<6)|(1<<7)|(1<<8); //Set GPBDAT5-8 to high level, which means all 4 LEDs will be off } led.h file #ifndef __LED_H__ #define __LED_H__ #include #define Led1_On() {GPBDAT&=(~(1<<5));} #define Led1_Off() {GPBDAT|=(1<<5);} #define Led2_On() {GPBDAT&=(~(1<<6));} #define Led2_Off() {GPBDAT|=(1<<6);} #define Led3_On() {GPBDAT&=(~(1<<7));} #define Led3_Off() {GPBDAT|=(1<<7);} #define Led4_On() {GPBDAT&=(~(1<<8));} #define Led4_Off() {GPBDAT|=(1<<8);} /**************************************************** * Function name: void Led_Init(void) * Global variables: None * Parameter Description: None * Return value; None * Function: Set GPN5-8 as output function, initialize 4 LED lights off *********************************************************/ void Led_Init(void); #endif timer.c file #include #include "timer.h" /******************************************************************* * Function name: void Timer0_Init(void) * Parameter Description: None * Global variables: None * Return value: None * Function: For 50MHz PCLK, obtain 62.5KHz timer 0 after frequency division * Input clock. *******************************************************************/ void Timer0_Init(void) { TCFG0&=~(0xff); //Set the first level frequency division coefficient to 99 TCFG0|=99; TCFG1&=~(0xf); //Set the second level frequency division coefficient to 8 TCFG1|=0x02; //62.5KHz=50MHz/(99+1)/8 TCNTB0=62500; //1s interrupt. After the above divider, the input clock frequency of timer 0 is 62.5kHz, that is, timer //Count 62500 times per second. Therefore, when initialized, the initial count value of timer 0 is 62500 //Here we can see that TCMPBn is not set, because we use its default value 0, so there is no need to set it TCON|=(1<<1); //Turn on the manual update bit, that is, when the timer is turned on, the values in TCMPB0 and TCNTB0 will be loaded into registers TCMP0 and TCNT0 TCON = 0x09; // Turn off the manual update bit, set the automatic load bit, and start the timer at the same time. In this way, TCNT0 counts down by 1. When the count in TCNT0 is //When the value is reduced to 0, the data in TCNTB0 and TCMPB0 will be automatically loaded into TCNT0 and TCMP0 respectively and a new round of subtraction counting will be performed. } timer.h file #ifndef __TIMER_H__ #define __TIMER_H__ /******************************************************************* * Function name: void Timer0_Init(void) * Parameter Description: None * Global variables: None * Return value: None * Function: For 50MHz PCLK, obtain 62.5KHz timer 0 after frequency division * Input clock. *******************************************************************/ void Timer0_Init(void); #endif interrupt.c file #include #include "interrupt.h" /**************************************************** * Function name: void Timer0_Interrupt_Init(void) * Global variables: None * Parameter Description: None * Return value; None * Function: Set the timer 0 interrupt mask bit to invalid *********************************************************/ void Timer0_Interrupt_Init(void) { INTMSK&=~(1<<10); //Set the timer 0 interrupt mask bit to invalid, so that when //When timer 0 is interrupted, the interrupt request signal can } //Smoothly reach the CPU interrupt.h file #ifndef __INTERRUPT_H__ #define __INTERRUPT_H__ /**************************************************** * Function name: void Timer0_Interrupt_Init(void) * Global variables: None * Parameter Description: None * Return value; None * Function: Set the timer 0 interrupt mask bit to invalid *********************************************************/ void Timer0_Interrupt_Init(void); #endif isrservice.c file #include #include "isrservice.h" extern unsigned int flag; //Declare the external variable flag, which is defined in the main.c file //When 1s arrives, the interrupt response function inverts the variable value and //The program implements different operations by detecting the value of this variable /**************************************************** * Function name: void __irq IRQ_Handler(void) * Global variables: None * Parameter Description: None * Return value; None * Function: Timer 0 interrupt service function, must add __irq *********************************************************/ void __irq IRQ_Handler(void) //Note that this function name must be the same as the jump label at S3C2440.s { flag=!flag; //1s time is up, the flag value is inverted SRCPND|=1<<10; //Clear interrupt flag INTPND|=1<<10; } isrservice.h file #ifndef __ISRSERVICE_H__ #define __ISRSERVICE_H__ /**************************************************** * Function name: void __irq IRQ_Handler(void) * Global variables: None * Parameter Description: None * Return value; None * Function: Timer 0 interrupt service function, must add __irq *********************************************************/ void __irq IRQ_Handler(void); #endif Be sure to remember to download it to NOR flash. If you don't know how, please read the blog: http://blog.csdn.net/mybelief321/article/details/8954788
Previous article:Let's learn mini2440 bare metal development (XII) -- mini2440 serial port interruption experiment
Next article:Let's learn mini2440 bare metal development (Part 9) --ARM interrupt control system
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- How is the circuit of the LED aging rack connected? Series or parallel?
- VICOR "Bidirectional 48V/12V Converter Module" evaluation board is now available for free!
- Sorting out the msp430f149 port functions and settings
- Grounding of electronic equipment
- What is the difference between the full-port and half-port of Zhongying SH367309 5-16 series lithium battery protection chip?
- 【DIY Creative LED】The soldered board has normal charging function
- ESP32 SD/MMC hardware connection method
- Domestic chips are amazing. I just applied for a board. CH579 is so small but it integrates network, USB, and Bluetooth.
- Embedded System Design and Examples.pdf
- [GD32L233C-START Review] Development environment construction and download test