Continue to Site

# 16F886 Pin Input issue

Status
Not open for further replies.

#### lmichals80

##### New Member
Hello everyone again,

I am having a problem reading input pins. using the code listed below(but changing (1<<1), to (1<<0)), I am able to light an LED on RB2 when RA0 is driven high with a separate 5V input and ~1.6K pull down resistor(2 800ohms in series, it was all I had) when I attempt the same with any of the RA pins(specifically RA1) I cannot get the LED to light. The 5V input is a Radio controlled vehicle receiver, but connected to the constant on wire(when power is supplied, voltage is a constant 5v). The supply voltage from the receiver comes from a DC regulated power supply. I tried using the 1.6k as a pull up resistor instead(although this seemed backwards) and it still did not work. As far as I can see from the datasheet, I have turned off all peripherals that would contradict using digital I/O pins. Any help would be greatly appreciated!!!

Setup:

PIC: 16F886
Compiler: MikroC
Config: WDT off, MCLR off, LVP off
Code:

Code:
#define setbit(var,bitnum)(var) |=(1<<(bitnum))

void lighton(void){
while(porta&(1<<1) == 1)setbit(portb,2);
portb = 0x00;
}

void main(void){
cm1con0 = 0x00;   //comparator 1 disabled
cm2con0 = 0x00;   //comparator 2 disabled
osccon = 0x61;    //set OSC freq 4mhz
ansel = 0x00;     // set all pins digital
trisb = 0x00;     // set PORTB as output
trisa = 0x03;     //set PORTA pins 1 and 2 as input
portb = 0x00;     //no output on PORTB
while (1) {
lighton();
}
}

Thanks!!!

Luke

Last edited:

#### eng1

##### New Member
Code:
void lighton(void){
while(porta&(1<<1) == 1)setbit(portb,2);
portb = 0x00;

When you change the code to use RA1 as input, you should change the condition of the while loop above. Specifically, I would write

Code:
void lighton(void){
while(porta&(1<<1) == 0b00000010) setbit(portb,2);
portb = 0x00;

The result of porta&(1<<1) is not a single bit.

#### lmichals80

##### New Member
Code:
void lighton(void){
while(porta&(1<<1) == 0b00000010) setbit(portb,2);
portb = 0x00;

when I enter this, the result is the LED lights up as soon as the chip is powered up. the input has no effect on the LED, even with a direct connection to Vss on RA1.

Last edited:

#### lmichals80

##### New Member
ok so this works which is good!!!:
Code:
void lighton(void){
while(porta = 0b00000010)setbit(portb,2);
portb = 0x00;

I am confused now with the logic I guess. why would porta&(1<<1) == 0b00000010 not work? is 0b00000010 the only way to reference that pin? This means also means that if I want to change another pin on PORTA to input, that I have double check that any other references to PORTA are correct...more if statements etc...

#### kpatz

##### New Member
You can reference individual pins with ra0-7, rb0-7 as well, so instead of

Code:
  while(porta == 0b00000010) ...

you can use

Code:
  while (ra1 == 1) ...

And then the other bits/pins won't affect your comparison and you won't have to mask out the other bits. It also compiles to fewer machine instructions, saving memory.

You can set bits with the ra#/rb# variables as well, so you don't need your setbit macro.

Code:
  while (ra1 == 1) rb2 = 1;

Last edited:

#### lmichals80

##### New Member
from what I can tell the mikroC version I am using does not use that format...but does use something similar: PORTA.F0 to PORTA.F7. I need to look at the help file in mikroC more to expand on this. It also referenced bit fields, but I have not had time to investigate further as to what those are yet. I was too busy trying to get my code to work

Status
Not open for further replies.

Replies
10
Views
3K
Replies
30
Views
2K
Replies
5
Views
683
Replies
3
Views
2K
Replies
5
Views
1K