View previous topic :: View next topic |
Author |
Message |
newbie1000 Guest
|
start button question |
Posted: Sat Nov 14, 2009 7:44 pm |
|
|
Hey I am new to pic programming,
I would like to know what code can I use so that when I press a button my code will start to execute until that button is pressed again?
I would appreciate any help.
I am using the PIC18f4520 trainer board and the "wait for one press" tutorial is not helping much. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Nov 15, 2009 1:16 am |
|
|
Quote: | when I press a button my code will start to execute |
Describe the code that is to be executed. What does it do and how
long does it take to execute ? Your answer will determine the type
of button code that can be used.
Also, rather than using push-buttons, it might easier to use the
RS-232 serial port on the board and a terminal program on your PC
to detect keypresses. The tools to do that are already built-in to the
compiler. You don't need to add push-button code. It would be a
lot easier for you.
Instructions for installing the TeraTerm terminal program:
http://www.ccsinfo.com/forum/viewtopic.php?t=39388&start=18 |
|
![](templates/subSilver/images/spacer.gif) |
newbie1000 Guest
|
|
Posted: Sun Nov 15, 2009 8:23 am |
|
|
Well..............I want when I push a button, two motors begin to run.
So it is an external circuit......a stand alone. I don't want it where it comes on as soon as I put on the power. I would like to start the operation with the press of a button after I put on the power.
I am varying the speed of the motor using a pot. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Nov 15, 2009 11:57 am |
|
|
What drives the motor ? Is the hardware PWM module in the PIC ?
Is the PWM module connected to an external H-Bridge chip that drives
the motor ? |
|
![](templates/subSilver/images/spacer.gif) |
newbie1000 Guest
|
|
Posted: Mon Nov 16, 2009 10:00 am |
|
|
It is the PWM from the pic18f4520.
I just wanted to know how to have a push button to turn on the program verses turning on the power and everything comes on. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 16, 2009 1:17 pm |
|
|
Here is a demo program that will toggle an LED on/off when a
push-button is pressed.
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)
// A push-button switch must be connected to the following
// pin on the PIC. The switch must connect the pin to
// ground when it's pressed. The PIC pin must have a
// pull-up resistor on it (Example: 10K).
#define BUTTON_PIN PIN_A4
// This pin must have an LED with a series resistor connected
// to it. (Example: 470 ohms). The cathode of the LED should
// connect to ground.
#define LED_PIN PIN_B1
void wait_for_button(void);
//====================================
void main()
{
output_low(LED_PIN);
while(1)
{
wait_for_button();
output_toggle(LED_PIN);
}
}
//==================================
void wait_for_button(void)
{
char count;
// Wait for the button to be released.
// The button must be in the "up" state for two
// consecutive samples, at 10 ms intervals.
count = 0;
while(1)
{
if(input(BUTTON_PIN))
count++;
else
count = 0;
if(count == 2)
break;
delay_ms(10);
}
// Now that the button is up, wait until the
// user presses it. For the keypress to be
// considered valid, the button must be held
// down for two consecutive samples, taken at
// 10 ms intervals.
count = 0;
while(1)
{
if(input(BUTTON_PIN) == 0)
count++;
else
count = 0;
if(count == 2)
break;
delay_ms(10);
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
newbie1000 Guest
|
|
Posted: Tue Nov 17, 2009 8:11 am |
|
|
THANK YOU A MILLION............I REALLY APPRECIATE IT!!!!!!!! |
|
![](templates/subSilver/images/spacer.gif) |
entah
Joined: 23 Sep 2010 Posts: 7
|
|
Posted: Sun Oct 03, 2010 7:45 am |
|
|
how to add the switch...plzzzz... help!!!! _________________ helpme!! Im the blind IT person!! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19381
|
|
Posted: Sun Oct 03, 2010 10:15 am |
|
|
Read the comments in the code, on lines 7 to 10. Tells you how to connect the switch....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
serabut
Joined: 03 Oct 2010 Posts: 1
|
|
Posted: Mon Oct 04, 2010 3:44 am |
|
|
tq for comment.....but i means if want add some switch and led in program this not at board...
so how....can ur explain to me??coz i already try but has error at
void main().. ![Question](images/smiles/icon_question.gif) |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 04, 2010 2:04 pm |
|
|
Does serabut = entah ?
Please don't use two different identities on the same thread.
If you are a different person, please don't "hijack" someone else's thread. |
|
![](templates/subSilver/images/spacer.gif) |
entah
Joined: 23 Sep 2010 Posts: 7
|
|
Posted: Thu Oct 07, 2010 4:42 am |
|
|
no same person la....but same team.....we have project for last sem lorrr......we student lorrrr....we dont know about this program so just ask k....dont treat we like you pcm....ur pro about this but we new....aiyoooo.....!!!so help lorrr.... _________________ helpme!! Im the blind IT person!! |
|
![](templates/subSilver/images/spacer.gif) |
|