Continue to Site

# Problem in Reading input in 16F72

Status
Not open for further replies.

#### GreenBeret

##### New Member
I am facing some problem while reading input through PORTB in 16F72 PIC.

I wrote a simple code which shows a number between 0-9 in a seven-segment. When i press button connected to RB5, then number should increment to a different number. It works fine in Simulator. But when I tried in PIC, it shows digit 8, and have a flickering too....

Below given is my source code written in HiTech C. Please help. I am stuck with this problem for a long perios....

//include file
#include <pic.h>

//16F72 configuration word according to the features required.
__CONFIG(0x3FB1);

int i;

// This macro is to make a delay after showing a digit in a segment.
#define my_delay() for ( i = 0; i <= 20; i++ );

void showNumber ( unsigned char *num )
{
switch ( (*num) )
{
case 0:
PORTC = 0b11111100;
PORTB = 0b00000000;
break;
case 1:
PORTC = 0b00011000;
PORTB = 0b00000000;
break;
case 2:
PORTC = 0b01101100;
PORTB = 0b00000001;
break;
case 3:
PORTC = 0b00111100;
PORTB = 0b00000001;
break;
case 4:
PORTC = 0b10011000;
PORTB = 0b00000001;
break;
case 5:
PORTC = 0b10110100;
PORTB = 0b00000001;
break;
case 6:
PORTC = 0b11110100;
PORTB = 0b00000001;
break;
case 7:
PORTC = 0b00011100;
PORTB = 0b00000000;
break;
case 8:
PORTC = 0b11111100;
PORTB = 0b00000001;
break;
case 9:
PORTC = 0b10111100;
PORTB = 0b00000001;
break;
case 10:
PORTC = 0b0;
PORTB = 0b0;
PORTA = 0b0;
break;
}
}

//main function
void main ( void )
{
unsigned int noShow = 10;
unsigned int temp = 0;

unsigned long swith_1_Count = 0;
unsigned int flag1 = 0;

TRISB = 0b00100000;
TRISA = 0x00;
TRISC = 0x00;

PORTC = 0;
PORTB = 0;
PORTA = 0;

while ( 1 )
{
if ( ( PORTB & 0b00100000 ) == 0b00100000 )
{
temp = ( temp +1 ) % 10;
}

PORTA = 0b00000001;
showNumber(&temp);
my_delay();
}
}

#### Pommie

##### Well-Known Member
I would guess that it is showing all numbers very fast and so it looks like an 8. Try increasing your delay lots.

Also, use the correct labels for the config. No one knows what CONFIG(0x3FB1); does.

Mike.

#### GreenBeret

##### New Member

Its not showing all numbers very fast. even if i increase my delay it seems to be like that... But still according to the source code, how will it behaves like this?

#### GreenBeret

##### New Member
Now here one basic doubt...

Can i use some of the pins in PORTB as Input and some of the pins in PORTB as output at the same time?...
This problem happens when i am doing so...

#### Pommie

##### Well-Known Member
Yes, you can set any combination of input/output.

Mike.

#### eng1

##### New Member
I wrote a simple code which shows a number between 0-9 in a seven-segment. When i press button connected to RB5, then number should increment to a different number. It works fine in Simulator. But when I tried in PIC, it shows digit 8, and have a flickering too....

A difference between simulation and real circuits is that switches 'bounce' for milliseconds. Consider adding software de-bouncing in your code.

Last edited:

#### GreenBeret

##### New Member
Hi eng1, thanx for your reply. I am new to PIC. Can u brief on wat is software de-bouncing or could pls give any link abt this... ?

#### eng1

##### New Member
Each time a button is pressed (or released), this doesn't result in a clean transition from Vdd to GND (from GND to Vdd). Lots of transitions between the two states happen for millisenconds. Since the PIC run fast, it could read this 'noise', causing unpredictable behaviour.
De-bouncing allows you to interpret the change in switch state correctly.
With the most basic implementation in software, you check the switch and when its state has changed, you generate a 20 ms delay (this is considered enough for most switches); then, you check again the state of the switch (assuming that it's staying in the same state), and take actions accordingly.

Since you have already a delay elsewhere in your code, that might work if it is increased to about 20 ms; how much delay do you have now?

Code:
if ( ( PORTB & 0b00100000 ) == 0b00100000 )
How is the switch connected? It is common to have a pull-up resistor (from RB5 to Vdd) and the switch to ground; if this is the case, you should be checking for a logic 0 at the PIC input (switch closed).

Last edited:
Status
Not open for further replies.

Replies
10
Views
11K
Replies
11
Views
2K
Replies
4
Views
6K
Replies
3
Views
2K
Replies
1
Views
676