1
\$\begingroup\$

I need some help using a PIC16F887 and the provided 44-pin demo board from Microchip. Its being programmed with a PicKit2.

On this demo board, there is a push button connected to RB0 in such a way so that the level at the pin is high all the time, and pressing the button drops the level low. The purpose of the push button is to simulate an external interrupt signal. I am trying to experiment with interrupts on this chip but cannot read any status change on this pin.

I have a simple piece of code which tests the status of RB0 and sets RD0 accordingly. However, nothing happens whether or not the button is pressed.

Can anyone see something wrong here? I know the IC itself is fine as I can run other programs that dont use this input pin.

Here is a PDF on the demo board if anyones interested. http://ww1.microchip.com/downloads/en/devicedoc/41296b.pdf

#include <pic16f887.h>
#include <htc.h>

__CONFIG (0x20E4);
__CONFIG (0x2EFF);

void main(void){

 int i;
 int k;
     TRISB = 0xff; //set PORTB as inputs
     TRISD = 0x00;        // Set PORTD as an Output
     while(1)
 {
    RD0=RB0;
 }

I made the following changes as per the solution by Dave below with no change.

void main(void){

 int i;
 TRISB = 0xff; //set PORTB as inputs
 TRISD = 0x00;        // Set PORTD as an Output
 while(1)
 {
     i=PORTB;
     PORTD=i;
 }

}

\$\endgroup\$
2
  • \$\begingroup\$ When you remove the connection, does that mean there is nothing connected to the pin? If so, the read value may be indeterminate, unless there's an internal pullup or pulldown resistor or comparable behavior implicit in the design of the input stage. Try putting a resistor (any value between 1K and 10K) between an input pin and VCC, and then touching and removing a wire connecting the input pin to ground. If the program is working, the output you copy it to should change immediately, since in both states the pin is driven rather than left floating. \$\endgroup\$ Dec 15 '11 at 4:28
  • \$\begingroup\$ You are right, of course. that did work. I will delete that problem from my original thread. \$\endgroup\$
    – Michael
    Dec 15 '11 at 4:49
3
\$\begingroup\$

I've found my answer to this question. I had to set ANSEL and ANSELH to 0 to allow for digital I/O

Thanks for everyone's suggestions.

\$\endgroup\$
2
  • \$\begingroup\$ Great! Glad you got it working. However, you may still run into that RMW problem so keep it in mind. \$\endgroup\$ Dec 15 '11 at 16:26
  • 1
    \$\begingroup\$ @Michael, accept your own answer so that the question is removed from the unanswered list. \$\endgroup\$ Dec 15 '11 at 16:31
0
\$\begingroup\$

Instead of writing bit 0 on port B to bit 0 on port D, write the entire portB to a variable then write the variable to portD. If this works then it is probably, the "read modify write" issue. I had a similar issue and was helped by this answer: https://electronics.stackexchange.com/a/7686/1900

\$\endgroup\$
6
  • \$\begingroup\$ That didnt help. Made edits as noted above with no change. \$\endgroup\$
    – Michael
    Dec 15 '11 at 2:33
  • \$\begingroup\$ sorry, it has been a year since I dealt with this problem, however, after looking for old code I found that I was using different data types. You can try this if you are so inclined: change "int i;" to "static unsigned char i;" in your new code segment. Sorry if this doesn't work. \$\endgroup\$ Dec 15 '11 at 3:13
  • \$\begingroup\$ No that didnt work either. I think there's something fundamentally wrong with my board. Thanks for the help though. No such thing as bad suggestions. \$\endgroup\$
    – Michael
    Dec 15 '11 at 3:54
  • \$\begingroup\$ Your very welcome. Sorry I couldn't help. I'm not sure but, you may want to look into your _config( ) code there. It is pretty unusual just to have a hex value there instead of something a human has an easier time reading. Maybe the micro is not set up right. Anyway, I'm sure someone else here will be more successful in helping you. If you want to understand the RMW read-modify-write problem in more detail I found this to be the most accessible and complete explanation: cornerstonerobotics.org/curriculum/lessons_year2/… \$\endgroup\$ Dec 15 '11 at 4:03
  • \$\begingroup\$ I will look into the _config registers next. Take a look at my latest update though. Something really really weird going on. \$\endgroup\$
    – Michael
    Dec 15 '11 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.