PDA

View Full Version : How to create 6 buttons?



tacbanon
- 6th December 2012, 23:59
Hello everyone!
I'm trying to develop a basic queuing system, using 2 digit 7 segment. Currently I have 3 buttons to trigger a bell and increment the value display. So far the main functionality are working...but at the moment buttons are on PORTA. The problem I'm facing is when buttons are pressed long enough it continues to increment in which I do not want. My solution is to use DT's interrupt library, using external interrupt (on Pic18F4550).
For testing I use the following codes...
asm
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
;__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
Define OSC 20


INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; Include if using PBP interrupts
TRISD = %00000000
PORTD = %00000000
Led1 Var POrtD.5
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT0_INT, _ToggleLED1, PBP, yes
INT_Handler INT1_INT, _ToggleLED1, PBP, yes
INT_Handler INT2_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

@ INT_ENABLE INT0_INT ; enable external (INT) [email protected] INT_ENABLE INT1_INT ; enable external (INT) interrupts
@ INT_ENABLE INT1_INT
@ INT_ENABLE INT2_INT


Main:
PAUSE 100
GOTO Main

'---[INT - interrupt handler]---------------------------------------------------
ToggleLED1:
TOGGLE LED1
@ INT_RETURN


Works perfectly for my project...now my question is, I know PORTB has 3 external interrupt. Is there a way I can make the same functionality for 6 buttons? I've read some about interrupt on ANY pins but not really sure how to implement it. Hope anyone can give me an idea/help ..appreciate it very much.
6760


Thanks in advance,
tacbanon

pedja089
- 7th December 2012, 09:34
Here is example how to use 1 int to serve button on any input pin of PIC.
6763
One button also can be attached to INTX pin.
In your ISR just look which pin is 0, then do whatever you want.
If all pin is 1, then button on INTX is pressed...

tacbanon
- 7th December 2012, 11:09
@Pedja089
Thanks so much for sharing...got to try it...feedback here my output later.
---------------------------------------
I have a follow up question, I've tried before to create a 4x4 keypad using Mr_e' s keypad routine...do you think the same concept can be applied to my 6 buttons?

regards,
tacbanon

pedja089
- 8th December 2012, 02:46
It probably could...
Try to pull down all "drive line" And on the top of ISR scan whole keyboard... Then again pull down all "drive lines".

mister_e
- 8th December 2012, 11:19
You could still read the I/O in a Timer based ISR... done fast enough it "give" the feeling of an interrupt. You can even use the Time base as debounce delay... you could even trick the PIC and generate a false INT0 interrupt by writing directly to the according bit.

No extra hardware needed, free up one I/O.

tacbanon
- 8th December 2012, 11:41
Hi..something not right, I'm trying to toggle led on PORTB.7 but no success....can you check my code what I'm doing bad?
6766

asm
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
;__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
Define OSC 20
TRISB.7 = 0
PORTB.7 = 0
TRISD = %00000000
PORTD = %00000000
LedD Var POrtD.5
Led1 var PortB.7

INCLUDE "DT_INTS-18.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP interrupts

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

@ INT_ENABLE INT_INT ;RB Port Change Interrupt
PORTB = %00000000
Main:
pause 500
Toggle LedD
GOTO Main

'---[INT - interrupt handler]---------------------------------------------------
ToggleLED1:
'TOGGLE LED1
If PortB.1 = 0 then
Toggle Led1
else
If PortB.2 = 0 then
Toggle Led1
else
If PortB.3 = 0 then
Toggle Led1
else
If PortB.4 = 0 then
Toggle Led1
else
If PortB.5 = 0 then
Toggle Led1
else
If PortB.6 = 0 then
Toggle Led1
else
Endif:Endif:Endif:Endif:Endif:Endif
@ INT_RETURN


thanks in advance
tacbanon

mister_e
- 8th December 2012, 12:12
Check the default interrupt edge....that's not what you need ;)

INTCON2

tacbanon
- 8th December 2012, 18:07
Hi mister_e,
Thanks for the time responding...I think the default is INTCON2.6 = 1 so I need to make INTCON2.6 = 0 'interupt on falling edge.
But either one for me is not working on my setup.

regards,
tacbanon

mister_e
- 8th December 2012, 18:32
Have you tried with real hardware or your life depends on How proteus decide to work (assuming it works at all)

From your schematic you're using led instead of diode and i'm pretty confident proteus is not smart enough to understand this.

pedja089
- 8th December 2012, 19:06
Maybe problem is in D1 - D6.
Check state of int pin when you press button...
It square on INT0 should be blue if button is pressed...
If that isn't case, replace D1 - D6 with 1N4148.

tacbanon
- 8th December 2012, 22:59
Thank you pedja089 and mister_e...I got it now.

/tacbanon