View previous topic :: View next topic |
Author |
Message |
csshah
Joined: 28 Feb 2005 Posts: 24
|
push buttons interfacing |
Posted: Tue Mar 08, 2005 11:49 am |
|
|
HI
I am using CCS PCH 3.219 with microchip 80 pin tqfp PIC 18F8720 board. I want to interface 3 pushbuttons with pic out of which one button is inbuilt in the board at b0(it s working fine) and I tried putting the other 2 buttons at bits b1 and b2 with required resistors but evenif I can see the correct status of pin while button is pressed it is not giving me required functionality i e it stays in the same loop evenif the button is pressed. So if somebody tell me where I m goin wrong or procedure to initialize the port at I/O for port B in CCS PCH
thanks |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Mar 08, 2005 11:54 am |
|
|
The first thing you need to due is POST A SMALL BUT COMPLETE PROGRAM that demonstrates the problem. We can tell you where you went wrong if we don't see the code! |
|
|
csshah
Joined: 28 Feb 2005 Posts: 24
|
my code |
Posted: Tue Mar 08, 2005 12:06 pm |
|
|
#if defined(__PCB__)
#include <16c56.h>
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)
#elif defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#elif defined(__PCH__)
#include <18F8720.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#endif
#define CURSOR '~'
//#define X 1
//#define Y 2
#include <stdio.h>
#include <LCD.c>
#byte TRISB = 0xFF
#byte PORTB = 0xF81
#bit PB_A = PORTB.0
#bit PB_B = PORTB.1
#bit PB_C = PORTB.2
void main() {
int i,j,X=1,Y=1,current,page=1;
lcd_init();
set_tris_b(0xFF);
for(i=0;PB_B==1;i++) <--the button is low and goes high when pushed
{
delay_ms(6);
lcd_putc("\f ** TEXT1 **\n");
lcd_putc(" TEXT2");
delay_ms(800);
lcd_putc('\f');
delay_ms(800);
}
lcd_putc(" TEXT3\n");
lcd_putc(" TEXT4");
delay_ms(2000);
lcd_putc('\f');
evenif I press the button it doesnt go out of for loop. I also tried all the combinations of conditions and using while loop instead of for. |
|
|
fuzzy
Joined: 26 Feb 2005 Posts: 64
|
|
Posted: Tue Mar 08, 2005 3:56 pm |
|
|
HI
I can't understand why you are using a FOR or While instruction to interface a pushbutton? I normally use an IF and usually i use a debouncing system to avoid error in pushing or multiple switching.
Code: |
if (input(PIN_B3)) P0SW = 1;
if(!input(PIN_B3) && (P0SW==1))
{
premuto=17;
P0SW=0;
delay_ms(100);
};
|
this code works very good, in thise case PIC can decode that I pushed button only when I release it.
i also use a shifted byte as debounce.
Code: |
laterale=input(PIN_A5);
if(!laterale && checklat==0b11111111) //laterale chiusa
{ start=startofftx;
premuto=80;
};
if(laterale && checklat==0b00000000) //laterale aperta
{ start=startontx;
premuto=81;
};
checklat<<=1;
checklat^=laterale;
|
I must say that my pushbutton are always high on PIC port and when i press they goes down.
In this case after 8 cycles PIC can decode if i pushed button. |
|
|
csshah
Joined: 28 Feb 2005 Posts: 24
|
still same!! |
Posted: Wed Mar 09, 2005 10:20 am |
|
|
I am still not getting buttons working
here I am checking the button status inside the loop because I want to display same text till the button is pressed. I think I am making mistake in initializing ports. when i use the push button(b0) given on microchip 80 pin tqfp pic18f8720 demo board everything works fine but when i connect external push buttons to some other port pins, it s not working and givin me pulses while checking using CRO.
Please reply ....
thanks |
|
|
csshah
Joined: 28 Feb 2005 Posts: 24
|
still same!! |
Posted: Wed Mar 09, 2005 10:21 am |
|
|
I am still not getting buttons working
here I am checking the button status inside the loop because I want to display same text till the button is pressed. I think I am making mistake in initializing ports. when i use the push button(b0) given on microchip 80 pin tqfp pic18f8720 demo board everything works fine but when i connect external push buttons to some other port pins, it s not working and givin me pulses while checking using CRO.
Please reply ....
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 09, 2005 12:35 pm |
|
|
Quote: | when i use the push button(b0) given on microchip 80 pin tqfp
pic18f8720 demo board everything works fine but when i connect
external push buttons |
Post a link to the web page for this demo board. Then we can look
at the schematic of the board and see what's happening. |
|
|
csshah
Joined: 28 Feb 2005 Posts: 24
|
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Mar 09, 2005 12:54 pm |
|
|
Hi csshah,
Your code don't reflect what you expect.
If you want to read a pushbutton connected to an input, the code would look like this:
Code: |
main()
{
set_tris_b(0xFF);
while(true)
{
if(input(PIN_B1))
{
..... do stuff A
}
else
{
..... do stuff B
}
}
}
|
Once you get this running, add other functions.
Humberto |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 09, 2005 1:09 pm |
|
|
I looked at the schematic. On that board, pins RB1 and RB2 are used
for RTS and CTS on the serial port. I wouldn't use those pins for
your experiments. Use pins RB3 and RB4 instead. They don't have
any other circuits connected to them.
That board has got LEDs on Port D. So, here's a simple program
that will turn on a LED when you press the switch.
You need to connector your switch (with the pull-up resistor) to Pin B3.
Then compile the following program and try it.
Code: | #include <18F8720.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
void main()
{
while(1)
{
if(input(PIN_B3)) // Read the switch pin.
output_low(PIN_D3); // If it's not pressed, turn off the LED
else
output_high(PIN_D3); // If it's pressed, turn on the LED
}
} |
|
|
|
csshah
Joined: 28 Feb 2005 Posts: 24
|
tried |
Posted: Wed Mar 09, 2005 1:14 pm |
|
|
Code: |
void main() {
int i,X=1,Y=1,current,page=1;
lcd_init();
SET_TRIS_B(0xFF);
delay_ms(800);
X: while(TRUE)
{
delay_ms(600);
lcd_putc("\f text 1\n");
lcd_putc(" text2");
delay_ms(600);
if(input(PIN_B4)==1)
{ delay_ms(100);
lcd_putc('H');
delay_ms(600);
PB_menu=0; }
else{goto X;}
|
I already tried doing this but this gives me output as
text1
text2H <---H is blinking so i think this is not checking the status of the
button pins ( I dont know how). here I am using ccs PCH compiler with pic18f8720 board. couldnt figure out why this is happening??
thanks for help
reply soon |
|
|
csshah
Joined: 28 Feb 2005 Posts: 24
|
continue |
Posted: Wed Mar 09, 2005 1:22 pm |
|
|
in the above posted code i used RB3 and RB4 but when i run the code, without pressing any button it shows me the output
text1
text2H
H is blinking
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 09, 2005 1:42 pm |
|
|
I don't know. You didn't run my test program, so I guess somebody
else will help you. |
|
|
guest Guest
|
|
Posted: Wed Mar 09, 2005 2:04 pm |
|
|
I think you just need modifiy one line in your code:
for(i=0;PB_B==0;i++)
{
delay_ms(6);
lcd_putc("\f ** TEXT1 **\n");
lcd_putc(" TEXT2");
delay_ms(800);
lcd_putc('\f');
delay_ms(800);
}
PB_B stays high by your initialization and goes to low when the button is pressed. |
|
|
csshah
Joined: 28 Feb 2005 Posts: 24
|
PCM Programmer |
Posted: Wed Mar 09, 2005 2:38 pm |
|
|
hey thanks for the program. i tried ur program and it s working but when i try to display text using same logic whether the key is pressed or not. evenif i dont press key it shows me text inside the pressed key if loop.
[#include <18F8720.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
#include <stdio.h>
#include <LCD_4bit.c>
setup_adc_ports(NO_ANALOGS);
void main() {
int i,X=1,Y=1,current,page=1,P0SW,premuto;
lcd_init();
//SET_TRIS_B(0xFF);
delay_ms(800);
while(1)
{
delay_ms(800);
lcd_putc("\f text1\n"); //<-- i want to display this text constantly
lcd_putc(" text2");
delay_ms(800);
if(input(PIN_B4))
{
delay_ms(800);
lcd_putc("\f pressed");
delay_ms(800);}
else{
lcd_putc("\f text1\n");
lcd_putc(" text2");
delay_ms(800);
}}
}
]
Quote: | it shows me
text1
text2
and
pressed intermittently. I think this is due to noise signals.
so what should i do now!! |
|
|
|
|