A problem in using sdcc interrupt

Publisher:梦幻之光Latest update time:2017-02-04 Source: eefocusKeywords:sdcc  interrupt Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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:


  1. /* timer.c */  

  2. #include   

  3.   

  4. #define OSC_FREQ 12000000UL  

  5.   

  6. #define TIMER_MODE0 0x00  

  7. #define TIMER_MODE1 0x01  

  8. #define TIMER_MODE2 0x02  

  9. #define TIMER_MODE3 0x03  

  10.   

  11. volatile unsigned int count = 0;  

  12.   

  13. void timer0_isr(void) __interrupt 1  

  14. {  

  15.         count++;  

  16.   

  17.         TH0 = (65536-1000*(OSC_FREQ/12000000)) >> 8;  

  18.         TL0 = (65536-1000*(OSC_FREQ/12000000)) & 0x00FF;  

  19. }  

  20.   

  21. void timer0_init(void)  

  22. {  

  23.         TMOD &= 0xF0;  

  24.         TMOD |= TIMER_MODE1; /* Timer 0 work on 16-bit timer mode */  

  25.         TH0 = (65536-1000*(OSC_FREQ/12000000)) >> 8;  

  26.         TL0 = (65536-1000*(OSC_FREQ/12000000)) & 0x00FF;  

  27.         ET0 = 1; /* Enable timer 0 interrupt */  

  28.         EA = 1;  

  29.         TR0 = 1; /* Turn on timer 0 */  

  30. }  

  31.   

  32. void delayms(unsigned int n)  

  33. {  

  34.         while (count <= n);  

  35.   

  36.         count = 0;  

  37. }  



  1. /* timer.h */  

  2. #ifndef __TIMER_H  

  3. #define __TIMER_H  

  4.   

  5. extern void timer0_init(void);  

  6. extern void delayms(unsigned int n);  

  7.   

  8. #endif /* __TIMER_H */  



  1. /* main.c */  

  2. #include   

  3.   

  4. #include "timer.h"  

  5.   

  6. #define led1 P2_0  

  7.   

  8. void timer0_isr(void) __interrupt 1;  

  9.   

  10. void main(void)  

  11. {  

  12.         timer0_init();  

  13.   

  14.         while (1) {  

  15.                 led1 = 0;  

  16.                 delayms(1000);  

  17.                 led1 = 1;  

  18.                 delayms(1000);  

  19.         }  

  20. }  



Note: It has been verified that this problem does not occur in Keil.


Keywords:sdcc  interrupt Reference address:A problem in using sdcc interrupt

Previous article:Build msp430 development environment under ubuntu
Next article:Use of 8051 Timer

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号