interrupt in project

JohnInTX

Joined Jun 26, 2012
4,787
On every interrupt, it will check the button once and turn the LED on or off accordingly. It will do this as long as the interrupt is enabled and the timer is running.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
On every interrupt, it will check the button once and turn the LED on or off accordingly. It will do this as long as the interrupt is enabled and the timer is running.
Thanks I have written code for hardware interrupt. I have tested on simulator it's work for me

Please check below program and if you find any logic error please let me know

Problem statement : When button pressed , LED will toggle
C:
#include<reg51.h>

#define  Pressed  1

sbit LED    = P1^0;           // LED connected to P1.0
sbit Button = P3^2;           // Switch button connected to P3.2

void initialize_Port(void);  //Function declarations

void initialize_Port(void)  // Make all ports zero
{
       P0 = 0x00;
       P1 = 0x00;
       P2 = 0x00;
       P3 = 0x00;
}

void main(void)      // Main function
{
     initialize_Port();   

     Button = Pressed ;

     EX0 = 0;      //Disable external interrupt INT0

     IT0 = 1;      //Edge triggered interrupt mode (Neg Edge)
     EX0 = 1;      //Enable external interrupt INT0
     EA  = 1;      //Enable global interrupts
    
      while(1)
         {
         }
}

void Hardware_ISR(void) interrupt 0 
{
    LED = ~LED;   // Toggle LED pin
}
Do I need to disable external interrupt in ISR ?
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
Do I need to disable external interrupt in ISR ?
No. When interrupted, the processor disables all other interrupts of the same priority. Priority is set by the IP register. If all sources of interrupts are at the same priority, only one at a time is processed.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
If all sources of interrupts are at the same priority, only one at a time is processed.
Did you check my program?. I think both of my programs are correct Do I need to improve in program somewhere ?

I have read there are two types of delay. but I don't know exact exactly about meaning

Hardware delay and software Delay

When I need software delay in program I just write program like below
C:
void delay(void)      
    {
          int x = 10500;   11.89uS 
           while(x--);        // So 10500 * 11.89uS = nearly 125mS
    }
I have doubt on hardware delay. What is hardware delay ?

I think If I use timer of microcontroller to generate delay it's called hardware delay. like I was doing in timer interrupt
 

JohnInTX

Joined Jun 26, 2012
4,787
In #22, why are you doing button=pressed if the LED is toggled by interrupt?

Hardware vs. software timers can have different meanings but for your purposes:
Software timers are what you are using in your delay functions. Hardware timers use the chip’s built-in timers, usually with interrupts, frequently using additional counting variables to count periodic interrupts for longer times.
 

JohnInTX

Joined Jun 26, 2012
4,787
I have set all the ports to 0 in main function at starting. Then I am setting button port 3.2. I think if I don't set port Pin 3.2 then MCU will not get the signal.
OK. But in that case, you should just unit the port with:
P3 = 0x04; // make P3.2 input, others output.
You momentarily made P3.2 an output. The circuit expects it to always be an input.

Also note how the lack of comments caused confusion.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
.Priority is set by the IP register. If all sources of interrupts are at the same priority, only one at a time is processed.
In below program I have set two hardware interrupts. Which of these two interrupts is responded to first?
C:
#include<reg51.h>

sbit LED1    = P1^0;           // LED 1 connected to P1.0
sbit LED2    = P1^1;           // LED 2 connected to P1.1

sbit Button1 = P3^2;           //Switch button 1 connected to P3.2
sbit Button2 = P3^3;           // Switch button connected to P3.3

void initialize_Port(void);

void initialize_Port(void)
{
       P0 = 0x00;
       P1 = 0x00;
       P2 = 0x00;
       P3 = 0xOC;   // Make P3.2 and P3.3 pin Highy only
}

void main(void)
{
     initialize_Port();

     EX0 = 0;      //Disable external interrupt INT0
     EX1 = 0;      //Disable external interrupt INT1

     IP  = 0x01;   // assign High priority

     IT0 = 1;      //Edge triggered interrupt mode (Neg Edge)

     EX0 = 1;      //Enable external interrupt INT0
     EX1 = 1;      //Enable external interrupt INT1

     EA  = 1;      //Enable global interrupts

     while(1);
 
}

void Hardware1_ISR(void) interrupt 0
{
    LED1 = ~LED1;   // Toggle LED1 pin
}

void Hardware2_ISR(void) interrupt 1
{
    LED2 = ~LED2;   // Toggle LED2 pin
}

Does it look's like batter? Is there any mistake ?

Note : I haven't tested this code. I will test it later on simulator
Note 2 I have replaced value P3 = 0xOC;
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
Recheck the init for P3.

The higher priority interrupt takes precidence over the low priority one. If it comes when servicing the low priority interrupt, the low priority interrupt processing will be suspended until the high priority interrupt service is complete. Also, a low priority interrupt will not be serviced if any high priority interrupts are being serviced.
 

philba

Joined Aug 17, 2017
959
Simulators are fine but going to real hardware you will find that the interrupt pins need to be tied to +V and pulled to gnd by the switches. Also you will discover contact bounce will make your switches appear to work randomly and need to handle that. In general, interrupts from contacts take some care - I just avoid them. Easier to debounce switches by using a timer interrupt to read their state.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Recheck the init for P3.

The higher priority interrupt takes precidence over the low priority one. If it comes when servicing the low priority interrupt, the low priority interrupt processing will be suspended until the high priority interrupt service is complete. Also, a low priority interrupt will not be serviced if any high priority interrupts are being serviced.
It would be P3 = 00001100 binary = 0x0c Hexa decimal

In my program the high priority interrupt will first serviced because IP = 1 if it is would be IP = 0 then low priority interrupt will be first serviced
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
Simulators are fine but going to real hardware you will find that the interrupt pins need to be tied to +V and pulled to gnd by the switches..
I would also like to work on real hardware but I don't have 8051 board right now. I'm trying to test 8051's program on simulator. I am planning to purchase ARM Board. Very soon I will start working on real hardware
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I have tested code interrupt on P3.2 is working but interrupt on P3.3 is not working (post #28). I am trying to find out what could be wrong. I am posting code with circuit design
C:
#include<reg51.h>

sbit LED  = P1^0;  // LED connected to P1.0

sbit Button = P3^3;  // Switch button connected to P3.3

void initialize_Port(void);  //Function declarations

void initialize_Port(void)
{
  P0 = 0x00;
  P1 = 0x00;
  P2 = 0x00;
  P3 = 0x08;  // Make P3.3 pin high
}
void main(void)
{
  initialize_Port();

  EX1 = 0;  //Disable external interrupt INT1

  IT0 = 1;  //Edge triggered interrupt mode (Neg Edge)

  EX1 = 1;  //Enable external interrupt INT1
  EA  = 1;  //Enable global interrupts

  while(1)
  {
  }
}
void Hardware_ISR(void) interrupt 1
{
  LED = ~LED;  // Toggle LED pin
}
What could be wrong ?

Ext1.jpg
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
You have specified the wrong interrupt vector for IE1.
EXTERNAL INT 1 2

I replaced code with this part Now problem is that when I press button LED start to blink for few times. It doesn't toggle. Why it's happening ?
C:
void Hardware_ISR(void) interrupt 2
{
  LED = ~LED;  // Toggle LED pin
}
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Post #33, line 23.
Here is complete code.
When I press button 1, LED1 toggle and when I press button 2 LED2 toggle.
C:
#include<reg51.h>

sbit LED1    = P1^0;           // LED 1 connected to P1.0
sbit LED2    = P1^1;           // LED 2 connected to P1.1
sbit Button1 = P3^2;           //Switch button 1 connected to P3.2
sbit Button2 = P3^3;           // Switch button connected to P3.3

void initialize_Port(void);

void initialize_Port(void)
{
       P0 = 0x00;
       P1 = 0x00;
       P2 = 0x00;
       P3 = 0x0c;   // Make P3.2 and P3.3 pin Highy only
}
void main(void)
{
     initialize_Port();

     EX0 = 0;      //Disable external interrupt INT0
     EX1 = 0;      //Disable external interrupt INT1

     IP  = 0x01;   // assign High priority

     IT0 = 1;      //Edge triggered interrupt mode (Neg Edge)
     IT1 = 1;      //Edge triggered interrupt mode (Neg Edge)

     EX0 = 1;      //Enable external interrupt INT0
     EX1 = 1;      //Enable external interrupt INT1

     EA  = 1;      //Enable global interrupts

     while(1);
}

void Hardware1_ISR(void) interrupt 0
{
    LED1 = ~LED1;   // Toggle LED1 pin
}

void Hardware2_ISR(void) interrupt 2
{
    LED2 = ~LED2;   // Toggle LED2 pin
}
if all look's like correct then Next I will try to write code for serial interrupt
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
if all look's like correct then Next I will try to write code for serial interrupt
If the goal was to play with the external interrupts and it works in the sim then OK by me.

So about the serial interrupt. Before going on, tell us why you would want to do that. The answer shouldn't be just ‘ to read GPS strings’.
Put another way, interrupt-driven serial comms are more complex to code and use up precious RAM yet many experienced programmers would not think of doing serial comms any other way (I am one of those). So.. what is it about serial comms that would make a lazy guy like me go to all of that trouble?

Next.
The answer to that question brings up these questions:
How often would you expect an interrupt at 9600 baud?
How would handle the incoming data and what sort of data structure(s) would you use?
How much processing of the data would you want to do in the interrupt routine?
How would the main program access that data?

Once you can answer those, a rough idea of how to design the program should emerge. If you can't answer those you won't be able to design a solution and you can't proceed with coding.

Give it some thought.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
So about the serial interrupt. Before going on, tell us why you would want to do that.
Give it some thought.
I am not thinking about the GPS programming. I am trying to do something simple like I will send the data from computer terminal to microcontroller and this data will display on LCD screen. I have to use uart and I want use serial interrupt. because my goal is to learn about serial interrupt. this is what I am thinking. should I do this. Is there any other way easier than this ?

today I am little bit busy with some work so I will take some time
 

JohnInTX

Joined Jun 26, 2012
4,787
I am not thinking about the GPS programming. I am trying to do something simple like I will send the data from computer terminal to microcontroller and this data will display on LCD screen. I have to use uart and I want use serial interrupt. because my goal is to learn about serial interrupt. this is what I am thinking. should I do this. Is there any other way easier than this ?
Sure. Implement the UART receiver then make your main routine just send the characters back to the terminal. Simple and that avoids the additional complexity of the LCD code. That can come later.

Once that is working, you can add a few things like changing lower case to upper case to show that you can process the characters. Once you can reliably communicate both ways, then you can change your simple echo processing to something more complex like writing to the LCD.

Writing the LCD would be a good final test and vindication of your decision to use an interrupt-driven UART. Those delays in your LCD routines will foul up communications if you don't have the UART right.

Think about how you'd implement the basic receive function first, though. When you get that interrupt and character in, the next one is arriving in about 1msec at 9600 baud. What do you do with the first character to be ready for the next one arriving? And the next one.. Consider what would happen if you had a 5ms dumb delay in your main routine. What are you going to do with those 5 characters that arrive while you are whiling away your CPU time in a delay loop??
 
Last edited:
Top