View previous topic :: View next topic |
Author |
Message |
kishen
Joined: 16 Apr 2015 Posts: 8
|
HELP |
Posted: Mon Apr 20, 2015 6:32 am |
|
|
I am using 2 push buttons to do two different things, when i compile my code and press the first push button then the second one, first again then the second one it works, i.e when i press in a sequence (1,2,1,2,1...) but when i compile and press the second push button it does not work till i press the first button or pressing the first button again after pressing the first button does not work as well.
I am using if statement, so if the first button is not pressed than it should go to the second if. That should work theoretically but does not work practically
#include <18F8722.h> //select proper pic header file
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#define ONE PIN_F0 // key is connected to port F pin 0.
#define TWO PIN_F1 // key is connected to port F pin 1.
int i=0, a=0, b=0;
#INT_TIMER0
void interrupt_tim0() // program automatically jump on ISR when overflow occurred
{
i++;
// compartment 1 Timng
if(i==2 & a==1) // left
{
output_high(PIN_J4);
output_high(PIN_J6);
}
if(i==3 & a==1) // top
{
output_low(PIN_J4);
output_high(PIN_J6);
}
if(i==5 & a==1)
{
output_high(PIN_J4);
output_high(PIN_J6);
}
if(i==6 & a==1) // front
{
output_low(PIN_J4);
output_high(PIN_J6);
}
if(i==8 & a==1)
{
output_high(PIN_J4);
output_high(PIN_J6);
disable_interrupts(INT_RTCC);//disable timer0
i=0;
a=0;
}
// compartment 2 timing
if(i==2 & b==1) // left
{
output_high(PIN_J4);
output_high(PIN_J6);
}
if(i==3 & b==1) // top
{
output_high(PIN_J4);
output_low(PIN_J6);
}
if(i==8 & b==1)
{
output_high(PIN_J4);
output_high(PIN_J6);
disable_interrupts(INT_RTCC);//disable timer0
i=0;
b=0;
}
}
wait_for_press() { //function to detect key pressed
while(1) // Loop to infinity
{
// Compartment 1
if(input(ONE)) // if button one is pressed then
{while (input(ONE));
{
a=1;
output_low(PIN_J4); // on motor
output_high(PIN_J6);
enable_interrupts(INT_RTCC);//enable timer0; // key being pressed is one
}
}
// Compartment 2
if(input(TWO)) // if button two is pressed then
{while (input(TWO));
{
b=1;
output_high(PIN_J4); // on motor
output_low(PIN_J6);
enable_interrupts(INT_RTCC);//enable timer0; // key being pressed is one
}
}
}
}
main(void) {
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);//Timer 0 setup
enable_interrupts(GLOBAL);
wait_for_press();
while(1);
} |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Apr 20, 2015 7:04 am |
|
|
might you mean :
???
you would profit from reading the othe recent posts about detecting button presses - re: real world glitchiness of the contacts in the time domain. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Mon Apr 20, 2015 7:12 am |
|
|
also
please use the 'code' button' ( just under the 'subject entry' as it makes seeing errors like yours a lot easier.
thanks
jay |
|
|
kishen
Joined: 16 Apr 2015 Posts: 8
|
RE: |
Posted: Mon Apr 20, 2015 7:37 am |
|
|
Thanks, but it still does not solve my problem
Code: |
#include <18F8722.h> //select proper pic header file
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#define ONE PIN_F0 // key is connected to port F pin 0.
#define TWO PIN_F1 // key is connected to port F pin 1.
int i=0, a=0, b=0;
#INT_TIMER0
void interrupt_tim0() // program automatically jump on ISR when overflow occurred
{
i++;
// compartment 1 Timng
if(i==2 && a==1) // left
{
output_high(PIN_J4);
output_high(PIN_J6);
}
if(i==3 && a==1) // top
{
output_low(PIN_J4);
output_high(PIN_J6);
}
if(i==5 && a==1)
{
output_high(PIN_J4);
output_high(PIN_J6);
}
if(i==6 && a==1) // front
{
output_low(PIN_J4);
output_high(PIN_J6);
}
if(i==8 && a==1)
{
output_high(PIN_J4);
output_high(PIN_J6);
disable_interrupts(INT_RTCC);//disable timer0
i=0;
a=0;
}
// compartment 2 timing
if(i==2 && b==1) // left
{
output_high(PIN_J4);
output_high(PIN_J6);
}
if(i==3 && b==1) // top
{
output_high(PIN_J4);
output_low(PIN_J6);
}
if(i==8 && b==1)
{
output_high(PIN_J4);
output_high(PIN_J6);
disable_interrupts(INT_RTCC);//disable timer0
i=0;
b=0;
}
}
wait_for_press() { //function to detect key pressed
while(1) // Loop to infinity
{
// Compartment 1
if(input(ONE)) // if button one is pressed then
{while (input(ONE));
{
a=1;
output_low(PIN_J4); // on motor
output_high(PIN_J6);
enable_interrupts(INT_RTCC);//enable timer0; // key being pressed is one
}
}
// Compartment 2
if(input(TWO)) // if button two is pressed then
{while (input(TWO));
{
b=1;
output_high(PIN_J4); // on motor
output_low(PIN_J6);
enable_interrupts(INT_RTCC);//enable timer0; // key being pressed is one
}
}
}
}
main(void) {
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);//Timer 0 setup
enable_interrupts(GLOBAL);
wait_for_press();
while(1);
} |
|
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Apr 20, 2015 9:31 am |
|
|
get the string of IF tests OUT of the ISR and INTO MAIN().
use a flag inside the ISR to tell MAIN() that a rollover has occurred.
it is bad form to stuff that mess of code into the ISR.
the logical construction of the string of tests looks suspect anyway.
start by making IF (A==1)
its OWN test for the I states that follow
THEN consider a SWITCH() statement for handling I within it
i'm not going to do your homework for you though. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19492
|
|
Posted: Mon Apr 20, 2015 11:08 am |
|
|
Also look at actually typing the code logically. Look at how code is normally laid out using _indentation_ to show what is meant to be executed by a test etc.. This is what the 'code' block retains, making code readable. Yours is not to start with. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Apr 20, 2015 2:46 pm |
|
|
I've no idea what your code is trying to do.
You've shown very little by way of comments.
I don't have your PIC to test.
#defining J4 J6 functions would be useful.
I've indented your code (with no other editing) to try to help.
N.B. Each closing } is immediately below its opening { !!!
I hope this enables you to SEE where the problem is.
Please take note of the other responses you've had.
Mike
Code: | #include <18F8722.h> //select proper pic header file
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay (clock=20000000)
#define ONE PIN_F0 // key is connected to port F pin 0.
#define TWO PIN_F1 // key is connected to port F pin 1.
int i=0, a=0, b=0;
#INT_TIMER0
void interrupt_tim0() // program automatically jump on ISR when overflow occurred
{
i++;
// compartment 1 Timng
if(i==2 && a==1) // left
{
output_high(PIN_J4);
output_high(PIN_J6);
}
if(i==3 && a==1) // top
{
output_low(PIN_J4);
output_high(PIN_J6);
}
if(i==5 && a==1)
{
output_high(PIN_J4);
output_high(PIN_J6);
}
if(i==6 && a==1) // front
{
output_low(PIN_J4);
output_high(PIN_J6);
}
if(i==8 && a==1)
{
output_high(PIN_J4);
output_high(PIN_J6);
disable_interrupts(INT_RTCC);//disable timer0
i=0;
a=0;
}
// compartment 2 timing
if(i==2 && b==1) // left
{
output_high(PIN_J4);
output_high(PIN_J6);
}
if(i==3 && b==1) // top
{
output_high(PIN_J4);
output_low(PIN_J6);
}
if(i==8 && b==1)
{
output_high(PIN_J4);
output_high(PIN_J6);
disable_interrupts(INT_RTCC);//disable timer0
i=0;
b=0;
}
}
wait_for_press()
{ //function to detect key pressed
while(1) // Loop to infinity
{
// Compartment 1
if(input(ONE)) // if button one is pressed then
{
while (input(ONE));
{
a=1;
output_low(PIN_J4); // on motor
output_high(PIN_J6);
enable_interrupts(INT_RTCC);//enable timer0; // key being pressed is one
}
}
// Compartment 2
if(input(TWO)) // if button two is pressed then
{
while (input(TWO));
{
b=1;
output_high(PIN_J4); // on motor
output_low(PIN_J6);
enable_interrupts(INT_RTCC);//enable timer0; // key being pressed is one
}
}
}
}
main(void)
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128);//Timer 0 setup
enable_interrupts(GLOBAL);
wait_for_press();
while(1);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 20, 2015 4:51 pm |
|
|
Why don't you tell us what your project is. You have 6 buttons and
they control a motor (or 2 or 4 wheel motors ?). Is this project a little
remote controlled toy, like a tractor ? You press buttons for Forward,
Reverse, Turn Left, Turn Right, Stop, and something else. Is that your
project ? If we know your project, we could offer a lot better help. |
|
|
kishen
Joined: 16 Apr 2015 Posts: 8
|
|
Posted: Tue Apr 21, 2015 4:29 am |
|
|
my project has 3(horizontal) x 2(vertical) level, 6 compartments that has a car in each. it has 3 motors to retrieve the cars, 1 for horizontal movement(left - right) , 1 for vertical movement(top - down) and 1 for(front and back) movement to pull out small cars. the program i gave was just to test if i am able to control 1 motor using 2 push buttons, so if it worked i would implement 6 push buttons to drive 3 motors to retrieve cars from each compartment.
In the code i gave, i am able to drive the motor with both the push buttons when i press it in a sequence(1 than 2 than 1 than 2...) but i cannot start my motor by pressing the second push button. because i don't want to retrieve vehicles in a sequence(1,2,3..) but which ever button is pressed it should retrieve from that compartment. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Apr 21, 2015 4:57 am |
|
|
I'm still confused.
What is the purpose of all the timing?
What is supposed to happen when a button is released before i==8 ?
You're still leaving us to guess.
What is happening in the ISR seems to be in conflict with main()
[or maybe wait_for_press() ]
You've already been advised to set a flag in the ISR, and move everything else to main.
Mike |
|
|
kishen
Joined: 16 Apr 2015 Posts: 8
|
|
Posted: Tue Apr 21, 2015 8:27 am |
|
|
It's OK i have fixed the problem, thanks anyways |
|
|
|