Jump to content
Sign in to follow this  
Onka

(noob)16f628a - Input Button Just Lowered To Gnd

Recommended Posts

Hi !

 

Im new to PIC programming and here goes my headache :

as topic says im trying to use PIC 16F628A to detect a push-button being pressed, which (when pressed) just lowers corresponding IC pin to ground without any pullup or pulldown resistors involved. This means that when button is not pressed, corresponding IC pin is "floating", not connected to anything.

 

Problem is - I get random activity even if i dont push the button - i dont actually have to touch it. I know that there is a way to read info properly since i have taken schematics from a real world example. Maybe there are some config bits i missed ?

 

Anyway here goes the code which is running fine if i use those pullup/pulldown resistors.

 

 

 

#include <system.h>

#pragma CLOCK_FREQ 4000000

 

#pragma DATA _CONFIG, _XT_OSC & _PWRTE_ON & _LVP_OFF & _MCLRE_OFF & _WDT_OFF &_CP_OFF & _BODEN_ON

 

 

void flash (int cnt) //function to flash a LED, cnt times

{

porta.1=0;

for (int k=0; k<cnt; k++)

{

porta.1=1;

delay_ms(200);

porta.1=0;

delay_ms(200);

}

 

}

 

void main()

{

 

cmcon = 7; // set analog comparators off

 

// output bit

trisa.1 = 0;

 

// input bits

trisa.5 = 1;

//trisb.3 = 1;

trisb.5 = 1;

 

 

while( 1 ) //endless loop

{

 

if (!porta.5) {flash(1);} // this is connected via pullup resistor to +5v all the time

if (!portb.5) {flash(2);} // this one is left just floating. if i comment this line out, i get proper response from porta.5 button presses

 

}

 

 

 

}

Share this post


Link to post
Share on other sites

Without knowing your hardware design I am not sure if this will cause a problem or not but Port B has built in pull-up resistors. option_reg.RBPU = 0 will force the bits (pins) on port B high (logical 1) and then when the button is pressed the input will go low (logical 0). If that is not an option use 10K resistors to VDD on the pin with the button this will have the same effect.

 

Also you may want to consider using a 1K resistor to GND from the button. You can get away without doing this but I always feel safer limiting the current below the 20ma max for a pin in case there is a short elsewhere.

 

RJS

Share this post


Link to post
Share on other sites

Hi Noob - Nope, can't do it like that. "Float" means just that - you are detecting the stray voltage on the pin drifting up and down. You have to either ground it and pull it high with the button or vice-versa. I guess what your schematic didn't show was that all the portB pins can have a "weak pullup" applied in software to all those pins on portB that are set as inputs.

Do that by clearing RBPU in the option register (ie: "option.RBPU=0")

(See p34 of the Microchip datasheet for the for the 16F628A)

Then you can detect your grounded button by either polling or interrupt as you like.

Best of luck .. we've all been there!

Share this post


Link to post
Share on other sites
Without knowing your hardware design I am not sure if this will cause a problem or not but Port B has built in pull-up resistors. option_reg.RBPU = 0 will force the bits (pins) on port B high (logical 1) and then when the button is pressed the input will go low (logical 0). If that is not an option use 10K resistors to VDD on the pin with the button this will have the same effect.

 

Also you may want to consider using a 1K resistor to GND from the button. You can get away without doing this but I always feel safer limiting the current below the 20ma max for a pin in case there is a short elsewhere.

 

RJS

 

 

This is good advice. An input pin should never be allowed to float, it can actually dammage the PIC. An input should be driven perhaps from an impedance of around 10K, if an analog input the impedance should probably be much lower.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...