Hello!
Can you help me please.
I am a student and i have some problems with one task.
I had to write a program for an embedded system which will have 8 buttons and will show on 7-segment display the number of the button pressed last.
I wrote the program but the teacher said that i must use external interrupts and that i can't use polling.I can't quite figure out what should i do next and how to fix my code so that it will fit the teachers demands.
Here is the code that i wrote and the scheme in proteus.
Thanks in advance,hope you can help me.

#include <reg51.h>

sbit in1=P1^0;        // input pins
sbit in2=P1^1;
sbit in3=P1^2;
sbit in4=P1^3;
sbit in5=P1^4;
sbit in6=P1^5;
sbit in7=P1^6;
sbit in8=P1^7;


sbit ou1=P2^0;        // output pins
sbit ou2=P2^1;
sbit ou3=P2^2;
sbit ou4=P2^3;
sbit ou5=P2^4;
sbit ou6=P2^5;
sbit ou7=P2^6;

void main()
{
  in1 = in2 = in3 = in4 = in5 = in6 = in7 = in8 = 1;  // Initialize the input pin
  ou1 = ou2 = ou3 = ou4 = ou5 = ou6 = ou7 = 1;
  while(1)
    {
        if (in1==0)
        {
        ou2 = ou3 = 0;
        ou1 = ou4 = ou5 = ou6 = ou7 = 1;
        }
        if (in2==0)
        {
        ou1 = ou2 = ou4 = ou5 = ou7 = 0;
        ou3 = ou6 = 1;
        }     
        if (in3==0)
        {
        ou1 = ou2 = ou4 = ou3 = ou7 = 0;
        ou5 = ou6 = 1;
        }
        if (in4==0)
        {
        ou3 = ou2 = ou6 = ou7 = 0;
        ou1 = ou4 = ou5 = 1;
        }
        if (in5==0)
        {
        ou1 = ou3 = ou4 = ou6 = ou7 = 0;
        ou2 = ou5 = 1;
        }


        if (in6==0)
        {
        ou1 = ou5 = ou3 = ou4 = ou6 = ou7 = 0;
        ou2 = 1;
        }

        if (in7==0)
        {
        ou1 = ou3 = ou2 = 0;
        ou4 = ou5 = ou6 = ou7 = 1;
        }

        if (in8==0)
        {
        ou1 = ou2 = ou5 = ou3 = ou4 = ou6 = ou7 = 0;
        }

    }
}

on the link bellow you can see the proteus simulation

https://scontent-b-fra.xx.fbcdn.net/hphotos-xpa1/v/t1.0-9/1620544_865262753486429_7528027219030798071_n.jpg?oh=92c0cb4217d2d501f645b2df7f30474d&oe=54B87314

I saw this, but i dont know how to implement this in my code, if you know help me please

I did smth like this, but it doesn't work anyway. What i did wrong?

#include<reg51.h>


sbit in1=P1^0;        // input pins
sbit in2=P1^1;
sbit in3=P1^2;
sbit in4=P1^3;
sbit in5=P1^4;
sbit in6=P1^5;
sbit in7=P1^6;
sbit in8=P1^7;


sbit ou1=P2^0;        // output pins
sbit ou2=P2^1;
sbit ou3=P2^2;
sbit ou4=P2^3;
sbit ou5=P2^4;
sbit ou6=P2^5;
sbit ou7=P2^6;
           // Pin P1.0 is named as LED

//Function declarations
void cct_init(void);
void InitINT0(void);

// Main function
void main(void)
{
  in1 = in2 = in3 = in4 = in5 = in6 = in7 = in8 = 1;  // Initialize the input pin
  ou1 = ou2 = ou3 = ou4 = ou5 = ou6 = ou7 = 1;

   cct_init();      // Make all ports zero
   InitINT0();      // Intialize INT0 interrupts

   while(1)
   {}
}

// Init CCT function
void cct_init(void)
{
    P0 = 0x00;    // Make all pins zero
    P1 = 0x00;    // Make all pins zero
    P2 = 0x00;    // Make all pins zero
    P3 = 0x04;    // Make P3.2 (INT0) pin high only
}

// External INT0 pin interrupt init function
void InitINT0(void)
{
    IT0 = 1;      //Edge triggered interrupt mode (Neg Edge)
    EX0 = 1;      //Enable external interrupt INT0
    EA  = 1;      //Enable global interrupts
}

// INT0 ISR
void external0_isr(void) interrupt 0
{
            if (in1==0)
        {
        ou2 = ou3 = 0;
        ou1 = ou4 = ou5 = ou6 = ou7 = 1;
        }
        if (in2==0)
        {
        ou1 = ou2 = ou4 = ou5 = ou7 = 0;
        ou3 = ou6 = 1;
        }
        if (in3==0)
        {
        ou1 = ou2 = ou4 = ou3 = ou7 = 0;
        ou5 = ou6 = 1;
        }
        if (in4==0)
        {
        ou3 = ou2 = ou6 = ou7 = 0;
        ou1 = ou4 = ou5 = 1;
        }
        if (in5==0)
        {
        ou1 = ou3 = ou4 = ou6 = ou7 = 0;
        ou2 = ou5 = 1;
        }


        if (in6==0)
        {
        ou1 = ou5 = ou3 = ou4 = ou6 = ou7 = 0;
        ou2 = 1;
        }

        if (in7==0)
        {
        ou1 = ou3 = ou2 = 0;
        ou4 = ou5 = ou6 = ou7 = 1;
        }

        if (in8==0)
        {
        ou1 = ou2 = ou5 = ou3 = ou4 = ou6 = ou7 = 0;
        }

    }

}

Hello Iana264,

t the moment, I am so busy so that I have no time to test your code. However, you should try a simple code first, one DI, one DO, then debug by yourself that will make you feel fantastic at the end.

If you really want to learn on how to program microcontrollers, please be passion, and pay attention, try step-by-step.

Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.