| View previous topic :: View next topic |
| Author |
Message |
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Mon May 09, 2005 3:59 pm |
|
|
Hi DragonPIC,
Take in mind that the poster problem was that he canīt detect the pushbutton
action at all and I focused the help in the hardware and not in the best way to
debounce a switch contact.
If you want to know my opinion regarding code for debouncing, by far the best way is
using interrupts:
The level change impossed by the switch action is detected by any of the uC external
hardware interrupts, inside the interrupt clear a global counter and enable one timer,
wich every time it overflows increment the counter, after n counts bit test if the switch
had changed to confirm that the toggle was done, if yes just disable the timer interrupt.
You get a clean and efective debouncing technique without overhead nor wasting
time in delays while you have been running other code in the main.
Best wishes,
Humberto |
|
 |
Guest
|
|
Posted: Tue May 10, 2005 1:53 am |
|
|
______________ +5V
|
|
_
| ° Button
| °
|
|_____________ PIN_A3
|
|
|
|
|_____________ Gnd |
|
 |
Tester21 Guest
|
|
Posted: Tue May 10, 2005 1:56 am |
|
|
| Code: | ______________ +5V
|
|
\
/ R 10K
\
/
|
|_____________ PIN_A3
|
|
|
| ° Button
| °
|
|_____________ Gnd |
This is schematic for my button... |
|
 |
Tester21 Guest
|
|
Posted: Tue May 10, 2005 6:12 am |
|
|
Hmmmm...
The situation is the same.
When programm hits the "first option" there is no way out.
The button function is called ok but i cant break out, to start something else...
| Code: | short button(void)
{
int cnt;
output_high(PIN_B2);
while(intr)
{
delay_us(5); //
cnt++;
if(cnt == 25)
output_high(PIN_B1);
return(1);
}
return(0);
}
...
lcd_init();
sec=0;
while(sec < 15)
{
printf(lcd_putc,"\f First option");
delay_ms(200);
if (button()) break;
}
printf(lcd_putc,"\f Second option");
} |
|
|
 |
Guest
|
|
Posted: Tue May 10, 2005 10:51 am |
|
|
| Code: | short button(void)
{
int cnt;
output_high(PIN_B2);
while(intr)
{
delay_us(5); //
cnt++;
if(cnt == 25)
{
output_high(PIN_B1);
return(1); //if you reach this point, after return you should break from loop
}
}
return(0);
}
...
lcd_init();
sec=0;
while(sec < 15)
{
printf(lcd_putc,"\f First option, sec = %u",sec);
delay_ms(900);
if (button())
break;
}
printf(lcd_putc,"\f Second option");
|
try that |
|
 |
Tester21 Guest
|
|
Posted: Thu May 12, 2005 3:26 am |
|
|
| Quote: | | //if you reach this point, after return you should break from loop |
No, i can't reach this point.
Practicly, no response on clicks...
p.s.
What is the difference between #define intr pin A_3 and
Byte, bit definitions ? |
|
 |
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
|
Posted: Thu May 12, 2005 10:02 am |
|
|
It would be helpful if you displayed your whole program now since I do not know how you are initializing your port. You should have a "set_tris_a(0x08)" to make the first bit an input on that port (this is the pin you are using the button on right?). _________________ -Matt |
|
 |
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
|
Posted: Thu May 12, 2005 10:16 am |
|
|
[quote="Tester21"] | Quote: |
p.s.
What is the difference between #define intr pin A_3 and
Byte, bit definitions ? |
PIN_A3 id usually used with the functions like "nput(PIN_A3);" so you could use:
| Code: | #define PIN_A3 43
while(input(PIN_A3))
{
...
} |
which you may also use. I do not like using these functions and not to fond of letting a function handle my port directional control. See your CCS manual for information on the difference. _________________ -Matt |
|
 |
Tester21 Guest
|
|
Posted: Fri May 13, 2005 7:30 am |
|
|
Button is connected like in the shematics, on the PIN A3.
Maybe i have problems with port definitions... This i the code:
| Code: |
...
#BYTE port_a = 3
#bit intr = port_a.0
set_tris_a(0x08);
short button(void)
{
int cnt;
while(intr)
{
delay_us(5); //
cnt++;
if(cnt == 25)
{
output_high(PIN_B1);
return(1);
}
}
return(0);
}
main()
{
lcd_init();
sec=0;
while(sec < 15)
{
printf(lcd_putc,"\f First option, sec = %u",sec);
delay_ms(900);
if (button()) break;
}
printf(lcd_putc,"\f Second option");
} |
|
|
 |
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Fri May 13, 2005 8:46 am |
|
|
| Quote: |
Maybe i have problems with port definitions... This i the code:
...
#BYTE port_a = 3
#bit intr = port_a.0
set_tris_a(0x08);
|
For sure.
#BYTE port_a = 5
If you have the pushbutton connected in port_a.0, this PIN must be an input.
set_tris_a(0x01); // 0b00000001
Humberto |
|
 |
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
|
Posted: Fri May 13, 2005 12:21 pm |
|
|
if your button is on pin A3 and not pin A0 then you should have
#bit intr_button = port_a.3
and
set_tris_a(0x08);
| Code: | #BYTE port_a = 5
#BIT intr = port_a.3
short button(void)
{
int cnt;
while(intr_button)
{
delay_us(5); //
cnt++;
if(cnt == 25)
{
output_high(PIN_B1);
return(1);
}
}
return(0);
}
main()
{
set_tris_a(0x08);
lcd_init();
for(sec = 0;sec < 15,sec++)
{
printf(lcd_putc,"\f First option, sec = %u",sec);
delay_ms(900);
if (button()) break;
}
printf(lcd_putc,"\f Second option");
} |
_________________ -Matt |
|
 |
Tester21 Guest
|
|
Posted: Fri May 13, 2005 12:26 pm |
|
|
| Quote: | | if your button is on pin A3 and not pin A0 then you should have |
It is on A3 ... it was my typing error.
But the program is still not working...
Is there anything else which could make a problem ? |
|
 |
Guest
|
|
Posted: Mon May 16, 2005 12:20 pm |
|
|
Yeah:
1) check your hardware to make sure connections are made right. Use an oscilloscope at the pin of the PIC to make sure the signal is going up and down with each button press.
2) Use a debugging program to make sure bit A3 in the internal register is being set and cleared and make sure the port is being configured correctly.
Just so I unserstand the problem fully, you do see the LCD count up the sec from 0 - 15 with no problem from the
printf(lcd_putc,"\f First option, sec = %u",sec);
then you see the message from
printf(lcd_putc,"\f Second option");
then program end. right?
The problem is that the button on A3 will not interrupt the 0-15 count. right? |
|
 |
Tester21 Guest
|
|
Posted: Tue May 17, 2005 4:58 am |
|
|
| Code: | | you do see the LCD count up the sec from 0 - 15 with no problem from the |
Yes i see the count but i can't break it...
The hardware connection is ok and cheked.
To you have some sample program so i can test it on my board? |
|
 |
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
|
Posted: Tue May 17, 2005 1:03 pm |
|
|
see modifications
Do you have a debugger? That is the only way to be sure the PIC is reading bits from port A correctly. Or you could try printing the port information out to LCD.
I also added code to make sure the ADC was disabled.
| Code: |
#BYTE port_a = 5
#BIT intr_button = port_a.3
int sec;
int button(void)
{
int cnt;
printf(lcd_putc,"\f %u",port_a);
while(intr_button == 0) // since you have a pullup with button grounding
{
delay_us(5); //
cnt++;
if(cnt == 25)
{
output_high(PIN_B1);
return(1);
}
}
return(0);
}
main()
{
setup_adc_ports(ADC_OFF); // just in case
set_tris_a(0x08);
lcd_init();
for(sec = 0;sec < 15;sec++)
{
printf(lcd_putc,"\f First option, sec = %u",sec);
delay_ms(450);
if (button()) break;
delay_ms(450);
}
delay_ms(450);
printf(lcd_putc,"\f Second option");
} |
_________________ -Matt |
|
 |
|