In sdcc, the interrupt handling function is identified by the keyword __interrupt, followed by the interrupt number. The list of 8051 interrupt numbers is as follows:
Interrupt # Description Vector Address 0 External 0 0x0003 1 Timer 0 0x000b 2 External 1 0x0013 3 Timer 1 0x001b 4 Serial 0x0023 5 Timer 2 (8052) 0x002b
Although it looks very simple, there is a small problem in the process of using it. Because I divided the program into two parts, main.c and timer.c, and the timer interrupt processing function timer0_isr in timer.c can never be executed. I checked the program and found no reason. By checking the source code in sdcc and searching the timer part code on the Internet, I did not find any problems with the initialization of timer0, which made me depressed. But I also found a difference between the two, that is, a declaration of the interrupt processing function was made before the main function, so I added this declaration in my program, and the program finally called the interrupt processing function, so I record it here. The complete program is as follows:
/* timer.c */
#include
#define OSC_FREQ 12000000UL
#define TIMER_MODE0 0x00
#define TIMER_MODE1 0x01
#define TIMER_MODE2 0x02
#define TIMER_MODE3 0x03
volatile unsigned int count = 0;
void timer0_isr(void) __interrupt 1
{
count++;
TH0 = (65536-1000*(OSC_FREQ/12000000)) >> 8;
TL0 = (65536-1000*(OSC_FREQ/12000000)) & 0x00FF;
}
void timer0_init(void)
{
TMOD &= 0xF0;
TMOD |= TIMER_MODE1; /* Timer 0 work on 16-bit timer mode */
TH0 = (65536-1000*(OSC_FREQ/12000000)) >> 8;
TL0 = (65536-1000*(OSC_FREQ/12000000)) & 0x00FF;
ET0 = 1; /* Enable timer 0 interrupt */
EA = 1;
TR0 = 1; /* Turn on timer 0 */
}
void delayms(unsigned int n)
{
while (count <= n);
count = 0;
}
/* timer.h */
#ifndef __TIMER_H
#define __TIMER_H
extern void timer0_init(void);
extern void delayms(unsigned int n);
#endif /* __TIMER_H */
/* main.c */
#include
#include "timer.h"
#define led1 P2_0
void timer0_isr(void) __interrupt 1;
void main(void)
{
timer0_init();
while (1) {
led1 = 0;
delayms(1000);
led1 = 1;
delayms(1000);
}
}
Note: It has been verified that this problem does not occur in Keil.
Previous article:Build msp430 development environment under ubuntu
Next article:Use of 8051 Timer
- 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
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Which boost solution is better? For example, considering efficiency, reliability, controllability, etc.
- How to use AD603 op amp
- 【GD32F310G-START】OLED HELLO EEWORLD——Hardware I2C
- Recommend several cost-effective mobile phones
- The ripple rate R of the inductor in the switching power supply is generally recommended to be around 0.4 as the best value? Then I started to ask?
- Design of high-speed counter based on FPGA.pdf
- Problems encountered in learning C programming language
- 5G Common Terms and Abbreviations
- I found a very good introductory book on 51 single-chip microcomputers and recommend it to beginners. It is suitable for self-study by novices (Wang Yun)
- Design of software and hardware C delay program for MSP430