Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Firing external 8051 interrupts by keyboard

Status
Not open for further replies.

bitsurfer

Member level 3
Joined
Jul 19, 2012
Messages
56
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Croatia
Activity points
1,734
Hello, need (beginners level) help on this code.

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<AT89X51.H>
 
sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^2;
sbit led4=P1^3;
sbit key1=P3^7;
sbit key2=P3^6;
long count=0;
void delay(void);
 
void intrupt0(void) interrupt 0 
{
  led1=0;
  delay();
  led1=1;
}
 
void intrupt1(void) interrupt 2
{
  led2=0;                                  
  delay();
  led2=1;
}
 
void main()
{
  P1=0xFF;
  P3=0xFF;
 
  EA=1;
  IE=0x85; // enable both ext interrupts
  while(1)
  { 
      if(key1==1)
      {
            // fire interrupt0
      }
 
      if(key2==1)
      {
            // fire interrupt1
      }
      
      count++;
      if (count>10000) 
      {
        led3=~led3;
        count=0;
      }
  }
}
 
void delay()
{
  int x;
  for(x=0;x<10000;x++);
}


Question is how to properly fire interrupt0 and interrupt1 isr when key1 or key2 is pressed.
 
Last edited by a moderator:

Why do you want to use interrupts for keyboard service? Your code is permanently scanning the keyboard, it doesn't need interrupts at all. For more exact debounce timing and defined behaviour, you would want to perform the scan action timer based (possibly timer interrupt), e.g. with 15 to 20 Hz scan rate.
 

Hi Fvm. Yes, you right my main loop pool's fast keys all the time.
But I would like to teach more about interrupts so I would like to see first how they works on such simple example.
This days I experimenting with putting my basic hardware to very different situations, that's all.
 

Code:
// USE IT1 and IT2 if your keys give you 0 when pressed

#include<at89x51.h>

sbit led1=P1^0;
sbit led2=P1^1;
sbit led3=P1^2;
sbit led4=P1^3;
sbit key1=P3^2;                                                 // you need to place key at P3.2 & P3.3 as they are the External Int pins of 8051 !
sbit key2=P3^3;
long count=0;

void delay(unsigned char a)					 //1ms delay for a=1
{
	unsigned int m,n;
	for(m=0;m<a;m++)
	for(n=0;n<1275;n++);
}

void external1() interrupt 0
{
        led1=0;
        delay(1);
        led1=1;
}

void external2() interrupt 2
{
        led2=0;
        delay(1);
        led2=1;
}

void main()
{
        P1=0xFF;
        P3=0xFF;
        EA=1;
//     IT1 = 1;						// TCON.2 : set by software to specify falling edge triggered external interrupt
	EX1 = 1;						// Enables External Interrupt 1 (P3.3)
//	IT2 =1;                                             // set by software to specify falling edge triggered external interrupt
        EX1 = 1;                                           // Enables External Interrupt 1 (P3.3)
        
       while(1)
       {
                count++;
                if (count>10000)
                {
                       led3=~led3;
                       count=0;
                }
       }
}

// Hope this help you... All the Best
 
Last edited:

Of course this helps something Jigar.
But still have some basic misunderstanding of this, very interesting thema.

At the moment I have ready maded board with few peripherials and simulator chip and not able to do electronic job at the moment.
As I can understand you enable interrupts at startup that pins 3.2 and 3.3 would be able to take signal when to go to isr (external1 and external2).
So, I woud need to mount my keys (or some other device) to those pins to "activate" interrupt.
But, on my board I have two keys connected to pins 3.7 and 3.6. which connects those pins to ground (-) when pressed.

Now, the basic of my question is:
Is it possible to fire external interrupt by software.
Say, If I press key at pin 3.7 that this invokes external interrupt like key on 3.3 was pressed?
As a beginner I still can't decide if my idea is funny or not, so sory if it is.
 

Hi Bitsurfer,

That's not possible dear, 8051 has 2 external interrupts and that too on P3.3 and P3.2 ONLY.
If you're willing to use P3.7 that invokes external interrupt like on P3.3 then you can't use the feature of Interrupt but you can do it using Pooling method which you know very well.

And if you're not willing to change your circuit then you can just short P3.2 with P3.6 and P3.3 with P3.7 and can go with the program which I've post.

In short ONLY P3.3 and P3.2 is your option to do so with Interrupt feature.
 
OK Jigar,
I understand now better how "this thing" works.

Thank you very much for helping.
 

After all, I take a wire and connect crystal's case with physical pin 13 at chip
and I can say that everything works as expected.
After delay main loop continues.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top