CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Any ideas on push buttons?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pokiko



Joined: 27 Jul 2007
Posts: 33

View user's profile Send private message Send e-mail

Any ideas on push buttons?
PostPosted: Fri Aug 24, 2007 7:29 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 24, 2007 1:12 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=23837
pokiko



Joined: 27 Jul 2007
Posts: 33

View user's profile Send private message Send e-mail

PostPosted: Sun Aug 26, 2007 9:10 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Aug 26, 2007 10:10 am     Reply with quote

It's not interrupt-driven code.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 26, 2007 2:38 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Mon Aug 27, 2007 9:55 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 27, 2007 10:59 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 28, 2007 12:07 am     Reply with quote

i am using pic18f2520
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 28, 2007 12:12 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 28, 2007 1:05 am     Reply with quote

THANK YOU
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group