View previous topic :: View next topic |
Author |
Message |
pokiko
Joined: 27 Jul 2007 Posts: 33
|
Any ideas on push buttons? |
Posted: Fri Aug 24, 2007 7:29 am |
|
|
Can anybody give me an idea about how can i make a button control algoritm that will scan if any of push buttons pressed then will take a return value which will be used for another function.
(it will be better for me to make this by interrupt)
buttons will be configured as follows;
#define BUTON_UP PIN_C0
#define BUTON_DOWN PIN_C3
#define BUTON_ENTER PIN_C4
#define BUTON_BREAK PIN_C5 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
pokiko
Joined: 27 Jul 2007 Posts: 33
|
|
Posted: Sun Aug 26, 2007 9:10 am |
|
|
I am newbie in ccs. I looked at the 18f2520.h file but i couldn't understand how can i use above pins as interrupt. If i can, please tell me how? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Aug 26, 2007 10:10 am |
|
|
It's not interrupt-driven code. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Aug 26, 2007 2:38 pm |
|
|
Here's an interrupt driven version of the button code. I just made it.
Because the button code uses a 10 ms debounce delay, it can easily
be done with a Timer interrupt instead of using delay_ms(10). So in
the code below, I setup Timer0 to interrupt every 10 ms. Then I call
the button routines inside the Timer0 isr. If a button has been pressed
then I set a global flag, which can be polled in a loop in main(). The
following program was tested on a PicDem2-Plus, and it works well.
Here's the output in a terminal window, as I press one button, then the
other, and then hold both of them down at the same time for a few seconds.
Quote: |
Start: AAABBBBBAAAABBBBABABABABABABABABABABABABABAB |
The Button.c routine comes from here:
http://www.ccsinfo.com/forum/viewtopic.php?t=23837
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
// These are the "Bvar" variables required by Button.c.
int8 A4 = 0; // For the button on pin A4
int8 B0 = 0; // For the button on pin B0
// Global "button pressed" variables.
int8 A4_pressed = FALSE;
int8 B0_pressed = FALSE;
#include <Button.c>
// This gives an RTCC interrupt rate of 100.16 Hz
// (approx. 10 ms) to sample and debounce the buttons.
#define RTCC_PRELOAD (256 - 39)
// The buttons are read in this Timer0 (RTCC) isr.
#int_rtcc
void rtcc_isr(void)
{
set_rtcc(RTCC_PRELOAD); // Reload the Timer.
if(button(PIN_B0, 0, 50, 10, B0, 1))
B0_pressed = TRUE;
if(button(PIN_A4, 0, 50, 10, A4, 1))
A4_pressed = TRUE;
}
//================================
void main()
{
printf("\n\rStart: ");
// Setup the RTCC for a 10 ms interrupt rate.
setup_counters(RTCC_INTERNAL, RTCC_DIV_256);
set_rtcc(RTCC_PRELOAD);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
while(1)
{
if(A4_pressed == TRUE)
{
A4_pressed = FALSE;
printf("A");
}
if(B0_pressed == TRUE)
{
B0_pressed = FALSE;
printf("B");
}
}
} |
|
|
|
pokiko
Joined: 27 Jul 2007 Posts: 33
|
|
Posted: Mon Aug 27, 2007 9:55 am |
|
|
Thank you so much, PCM programmer. Let me ask you a last question. I want a large resolution. If we select timer2 as 8 bit so we can write maximum 255 as prescaler. Is there any ways we can write a 16bit number such as 65535 like below?
setup_timer_2(T2_DIV_BY_1,65535,1); |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Aug 27, 2007 10:59 am |
|
|
What PIC are you using ? Timer2 is only 8-bits, as far as I know.
You can't load an 8-bit timer with a 16-bit value. |
|
|
pokiko
Joined: 27 Jul 2007 Posts: 33
|
|
Posted: Tue Aug 28, 2007 12:07 am |
|
|
i am using pic18f2520 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 28, 2007 12:12 am |
|
|
If you want a 16-bit timer, you have a choice of Timer0, 1, or 3,
but not Timer2. |
|
|
pokiko
Joined: 27 Jul 2007 Posts: 33
|
|
Posted: Tue Aug 28, 2007 1:05 am |
|
|
THANK YOU |
|
|
|