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

ccp2 --> 1 btn to do 2 functions

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



Joined: 07 Feb 2011
Posts: 26

View user's profile Send private message

ccp2 --> 1 btn to do 2 functions
PostPosted: Thu Mar 24, 2011 10:56 am     Reply with quote

I want to use 1 button to do 2 functions at separate occassion.

button -->RC1 (ccp2) with ccp2 interrupt
counter = 0;
button press --> check counter=0, it is 1st pressed --> turn LED on PIN_B2, counter++

if button press --> check counter=1, it is 2nd pressed --> turn LED on PIN_B3, reset counter = 0;

see my code below

Code:


#include <18F4620.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOMCLR
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) 


unsigned char counter = 0;

long rise,fall,pulse_width;

#int_ccp1
void isr()
{
      output_high(pin_b1);
      clear_interrupt(INT_CCP1);
}

#int_ccp2
void iss2()
{
   if(counter == 0)
   {
      output_high(pin_b2);
      counter++;
   }
   if(counter == 1)
   {
      output_high(pin_b3);
      counter = 0;
   }
   clear_interrupt(INT_CCP2);
      
}




void main()
{
   printf("\n\rHigh time (sampled every second):\n\r");
   setup_ccp1(CCP_CAPTURE_FE);    // Configure CCP1 to capture rise
   setup_ccp2(CCP_CAPTURE_FE);    // Configure CCP2 to capture fall
   setup_timer_1(T1_INTERNAL);    // Start timer 1

   enable_interrupts(INT_CCP1);   // Setup interrupt on falling edge
   enable_interrupts(INT_CCP2);   // Setup interrupt on falling edge
   enable_interrupts(GLOBAL);

   while(TRUE) {
     
   }
}


I couldn't get it to do 2 functions separately, it does both functions the same time.

What can I do to get it to work? is there any flag or else to do this?
Please help.
temtronic



Joined: 01 Jul 2010
Posts: 9173
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 24, 2011 11:28 am     Reply with quote

Among other things...

Buttons, switches, etc. are electrically noisey...

You MUST add a 'debounce' HW circuit or SW routine to get 'clean' transitions from your switch.

How you do it, is up to you as only you know the switch,it's characteristics,wiring, required speed, etc.
pic_micro



Joined: 07 Feb 2011
Posts: 26

View user's profile Send private message

PostPosted: Thu Mar 24, 2011 11:54 am     Reply with quote

Is there any instruction how to add the debounce to it?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 24, 2011 4:47 pm     Reply with quote

What is the purpose of your program ? Do you need the CCP ?
pic_micro



Joined: 07 Feb 2011
Posts: 26

View user's profile Send private message

PostPosted: Thu Mar 24, 2011 8:02 pm     Reply with quote

I am working on a project(custom camera) that interface to user by using 3 buttons. This is an existing custom board that all other I/O pins are being used.

btn1 ---connect to RB0 (INT0) //ONOFF switch
btn2 ---connect to RC2 (ccp1) //Video Zoom
btn3 ---connect to RC1 (ccp2) //Video Capture

all 3 buttons require interrupt because there are other tasks such as ADC,
RS232, SPI, etc running simultaneously.

btn1 control 2 functions (power on/off, 1st pressed = on, 2nd pressed = off)

btn2 will control zoom, this will only require to have outputs high at the same time for 500ms.

btn3 control 2 functions (start capture, stop capture , 2 different output)

The original design in mind was to use ccp1 and ccp2 to capture the external inputs how long the button pressed, but the timer1 and timer3 are already used for something else so I can not use it to measure a button press how long it being pressed, so I plan to use the interrupt but as soon as the ccp1 and ccp2 see the buttons pressed, jump to an interrupt routine, and determine btn3 --1st pressed is start capture, 2nd pressed is stop capture.

same for btn1

btn2 is to output 2 low output at same time for 500ms.

I guess you get the idea. This is just a test program to test the switch function.

I couldn't get btn1 and btn3 to do 2 functions each.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 24, 2011 10:55 pm     Reply with quote

Can you allocate a timer to interrupt every 10ms ? If so, you can poll
the buttons in the isr, and create any kind of code that you need to
handle button events. Examples:
http://www.ccsinfo.com/forum/viewtopic.php?t=39585
http://www.ccsinfo.com/forum/viewtopic.php?t=42027
http://www.ccsinfo.com/forum/viewtopic.php?t=31849
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