View previous topic :: View next topic |
Author |
Message |
Guest
|
Switch toggle High/Low |
Posted: Thu Nov 02, 2006 3:05 pm |
|
|
Hi i want to to toggle push button high and low but i can't make it what's wrong with code
Code: | while(true){
static int mm = 0;
printf(lcd_putc,"\f Status ");
switch (input(pin_C0)== mm){
case 0:
printf(lcd_putc,"\n Low ");
delay_ms(500);
break;
case 1:
printf(lcd_putc,"\n High ");
delay_ms(500);
mm = 0 ; // reset variable mm
break;
} |
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Nov 02, 2006 3:26 pm |
|
|
Code: |
while(true)
{
printf(lcd_putc,"\f Status ");
if (!input(pin_C0))
printf(lcd_putc,"\n Low ");
else
printf(lcd_putc,"\n High ");
delay_ms(500);
} |
|
|
|
Guest
|
|
Posted: Thu Nov 02, 2006 3:34 pm |
|
|
thanks but i want the status stays for each press like when i press first time the status goes high and stay high when i press another time to go low and stay low like normal switch but in push button |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Nov 02, 2006 4:04 pm |
|
|
Code: |
int state = 0;
while(true)
{
while (!input(pin_C0));
printf(lcd_putc,"\f Status ");
if (state)
{
state = 0;
printf(lcd_putc,"\n Low ");
}
else
{
state = 1;
printf(lcd_putc,"\n High ");
}
} |
|
|
|
Guest
|
|
Posted: Thu Nov 02, 2006 4:19 pm |
|
|
it dosen't work |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Nov 02, 2006 5:10 pm |
|
|
post your complete program |
|
|
Guest
|
|
Posted: Fri Nov 03, 2006 7:36 am |
|
|
it's a test program for toggling a button this whole program except the headers |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri Nov 03, 2006 7:58 am |
|
|
The code posted by Mark should work.
What about your hardware:
does the uC oscillator is running ?
can you blink a LED to see that it is alive ?
can you write a single word in the LCD ?
did you connect pull up resistors in the push buttons ?
Humberto |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Nov 03, 2006 8:07 am |
|
|
Anonymous wrote: | it's a test program for toggling a button this whole program except the headers |
Obivously then you do not want any help since you cannot do what is asked. I'm done....Bye |
|
|
Guest
|
|
Posted: Fri Nov 03, 2006 8:11 am |
|
|
Humberto all thing is allright i can do anything with lcd the xtal is ok and the board is ok
Mark i can't understand you this is the main of my program so it's my whole program i use PIC 16F876A and 20 Mhz xtal anyway thanks .. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Nov 03, 2006 9:25 am |
|
|
Anonymous wrote: | Humberto all thing is allright i can do anything with lcd the xtal is ok and the board is ok
Mark i can't understand you this is the main of my program so it's my whole program i use PIC 16F876A and 20 Mhz xtal anyway thanks .. |
What can't you understand about posting a COMPLETE program that compiles. I don't want bits and pieces or psudeo code or even something typed out which is close to the test program. I want a cut and paste of your program. Is that clear enough? Did ya understand that? I've been at this quite a while and have wasted plenty of time because of other problems else where in the code. |
|
|
Guest
|
|
Posted: Sat Nov 04, 2006 3:55 am |
|
|
Ok here's the Code
Code: | #include <16F876A.h>
#fuses XT,NOWDT
#use delay(clock=2000000)
#include "LCD.c"
void main()
{
lcd_init();
while(true){
static int mm = 0;
printf(lcd_putc,"\f Status ");
switch (input(pin_C0)== mm){
case 0:
printf(lcd_putc,"\n Low ");
delay_ms(500); //Debounce
break;
case 1:
printf(lcd_putc,"\n High ");
delay_ms(500); //Debounce
mm = 0 ; // reset variable mm
break;
}
}
}
|
|
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Sat Nov 04, 2006 4:45 am |
|
|
I don't think you're using the code that was written for you.
I don't follow your logic in using a SWITCH statement for an either/or condition?
Also:
!=
Code: | #use delay(clock=2000000) |
That is one very good reason to post the WHOLE program.
If you'd like the pros to help you, let them. |
|
|
Guest
|
|
Posted: Sat Nov 04, 2006 4:53 am |
|
|
jecottrell Thanks For Xtal note really i was working on the program without notice the xtal value
Thanks again but i use the same technique in Borland C++ Builder to perform two action with same button !! and here it's not working |
|
|
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
|
Posted: Sat Nov 04, 2006 4:57 am |
|
|
You need to figure out what you're doing. Are you trying to get code working on a PIC or what?
If you're using a 20MHz crystal should also use the HS fuse and not the XT.
A 500mS debounce delay is too long also.
Make some effort here and give a detailed account of what you are doing. Otherwise you're going to on your own. |
|
|
|