1

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;
 }

}

Michael
  • 948
  • 1
  • 7
  • 23
  • 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. – Chris Stratton Dec 15 '11 at 04:28
  • You are right, of course. that did work. I will delete that problem from my original thread. – Michael Dec 15 '11 at 04:49

2 Answers2

3

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.

Michael
  • 948
  • 1
  • 7
  • 23
0

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

Dave.Mech.Eng
  • 1,745
  • 3
  • 17
  • 21
  • That didnt help. Made edits as noted above with no change. – Michael Dec 15 '11 at 02:33
  • 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. – Dave.Mech.Eng Dec 15 '11 at 03:13
  • 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. – Michael Dec 15 '11 at 03:54
  • 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: http://www.cornerstonerobotics.org/curriculum/lessons_year2/erii_rmw_problem.pdf – Dave.Mech.Eng Dec 15 '11 at 04:03
  • I will look into the _config registers next. Take a look at my latest update though. Something really really weird going on. – Michael Dec 15 '11 at 04:18
  • I think two calls to _config() won't have the effect you probably are looking for. Either only the first or second one will work. But, you'll have to look into that further. As far as your experiment goes I'm not sure what to think, but, is it possible there is a lot of capacitance on the PORTD pin which causes a slow drop of its voltage?? – Dave.Mech.Eng Dec 15 '11 at 04:25