CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

push buttons interfacing
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
csshah



Joined: 28 Feb 2005
Posts: 24

View user's profile Send private message

push buttons interfacing
PostPosted: Tue Mar 08, 2005 11:49 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Mar 08, 2005 11:54 am     Reply with quote

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

View user's profile Send private message

my code
PostPosted: Tue Mar 08, 2005 12:06 pm     Reply with quote

#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

View user's profile Send private message

PostPosted: Tue Mar 08, 2005 3:56 pm     Reply with quote

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

View user's profile Send private message

still same!!
PostPosted: Wed Mar 09, 2005 10:20 am     Reply with quote

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

View user's profile Send private message

still same!!
PostPosted: Wed Mar 09, 2005 10:21 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 09, 2005 12:35 pm     Reply with quote

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

View user's profile Send private message

the link
PostPosted: Wed Mar 09, 2005 12:37 pm     Reply with quote

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=84&useSecondary=true&device=en010058&ds=on&uguide=on&dnotes=on&errt=on&mig=on&tut=on&refm=on&broc=on
The only difference in the buttons is the board button is always high and go to low when pushed while the buttons I'm using are opposite to that.

thanks
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Wed Mar 09, 2005 12:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 09, 2005 1:09 pm     Reply with quote

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

View user's profile Send private message

tried
PostPosted: Wed Mar 09, 2005 1:14 pm     Reply with quote

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

View user's profile Send private message

continue
PostPosted: Wed Mar 09, 2005 1:22 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Mar 09, 2005 1:42 pm     Reply with quote

I don't know. You didn't run my test program, so I guess somebody
else will help you.
guest
Guest







PostPosted: Wed Mar 09, 2005 2:04 pm     Reply with quote

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

View user's profile Send private message

PCM Programmer
PostPosted: Wed Mar 09, 2005 2:38 pm     Reply with quote

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!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group