We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi. I'm doing my thesis on 8051 and i'm programming interrupt routines in Keil C and simulate them. But I can't use variables inside interrupt routines because they generate unpredictable effects on simulations. Does anybody can help me please?
"But I can't use variables inside interrupt routines because they generate unpredictable effects on simulations" Darn! There goes the entire fabric of embedded systems programming - no more variables in interrupt routines! :-( Clearly, you are doing something seriously wrong - I don't think I've ever seen a non-trivial interrupt routine that doesn't use variables! What "simulations" are you talking about - the Keil uVision Simulator, or someting else? Did you read the uVision Getting Started guide, and work through the examples in it? Have you tried downloading and using the interrupt-driven serial IO example from this site? It shows you how interrupts and variables can be used. "Does anybody can help me please?" Not on the information provided so far! Can you post an example? (not forgetting the 'pre' and '/pre' tags - see the Notes when you post) Explain what sort of "unpredictable results" you are seing.
Darn! There goes the entire fabric of embedded systems programming - no more variables in interrupt routines! :-( There's always polled I/O. I seem to recall hearing that variables could be used with that. ;-) Jon
I use Modeltech for simulate the program. For unpredictable effects I mean that things happen like address bus for reading ROM that goes much much ahead the real limit of the program. This is the situation: this part works correctly: #include <reg51.h> /* define 8051 registers */ #include <stdio.h> /* define I/O functions */ #include <stdlib.h> /* Main - Program entry point * * INPUT: N/A * RETURNS: N/A */ unsigned int i; void external_int (void) interrupt 0 using 0 { /* The interrupt was generated. */ P1 = P1 + 1; /* Reset Interrupt */ } void main(void) { /*----------------------------------------------- Configure INT0 (external interrupt 0) to generate an interrupt on the falling-edge of /INT0 (P3.2). Enable the EX0 interrupt and then enable the global interrupt flag. -----------------------------------------------*/ IT0 = 1; // Configure interrupt 0 for falling edge on /INT0 (P3.2) EX0 = 1; // Enable EX0 Interrupt EA = 1; // Enable Global Interrupt Flag PS = 0; /* Interrupt Seriale */ PT1 = 0; /* BBh Priority Interrupt Timer 1 */ PX1 = 0; /* BAh Priority Interrupt extern INT 1 */ PT0 = 0; /* B9h Priority Interrupt Timer 0 */ PX0 = 1; /* B8h Priority Interrupt extern INT 0 */ P2 = 0; /* Port 2 will be initialized */ /* * Now the counter loop begins. Because this program is intended * to run in an embedded system with no user interaction, and will * run forever, the rest of the program is placed in a non-exiting * while() loop. */ while (1) { /* * This is a very unpredictable method of implementing a delay * loop, but remains the simplest. More reliable techniques * can be done using the using the built-in timers. In this * example, though, the for() loop below will run through 60000 * iterations before continuing on to the next instruction. * The amount of time required for this loop varies with the * clock frequency and compiler used. */ for (i = 0; i < 5; i++) /* Increment Port 0 */ P2 = P2 + 1; } } If I change the value of the i variable (or use another variable) inside the interrupt routine suddenly nothing works and strange things happen in the simulation.
Now I have also noticed that only Port 1 can be used in the interrupt routine. The other three ports generate several problems. The simulation doesn't represent the program.
Uhhh, are you using an 8051 with an external data/address bus that is driven by P0 and P2? If so, you can't use P0 and P2 for general purpose I/O. Since the serial port and RD/WR pins are provided on P3, you can't modify P3 the way you modify P1. Since I don't really have details of the device or hardware you are using and since I really don't know what a Modeltech is, it's impossible for me to help you. If you're using the Keil tools, tho, why don't you use the simulator in uVision2? Jon
This could be an exmplanation for ports but what is strange is that also register as Accumulator and so on cannot be used inside an interrupt routine: using them make simulation non corresponding to reality. Beside of this I can't use variables inside interrupt routines. Can you help me please?
Sounds like it's time to contact technical support. The things you are having trouble with are very basic. And, no one else seems to have trouble with them. Jon
Have you tried it in the Keil uVision simulator?