Discussion in 'Programmer's Corner' started by cjvanr10, Oct 30, 2010.

  1. cjvanr10

    Thread Starter New Member

    Oct 30, 2010
    Hi there,
    Just joined a moment. I'm doing some PIC projects for my college and I'm having trouble with this subtraction program I've written. Project works fine on the simulator, but when I load it on the PIC it behaves differently. I didn't think I had a hardware fault as other projects worked fine. The problem arises when a negative number is produced form the subtraction. I've dealt with a negative result by switching one of my inputs to output to display the 9 bit 2's complement negative number, then swapping it back again with a reset button. The program and hardware work fine with a positive number result and also on switch on when you do a calc with a negative result. If you try to do another negative result calc errors occur, but regular errors always the same. Is there any thing I have to consider when switching an input to an output then back again with the PIC16F84? Any help would be great. If additional info is needed let me know.
  2. Markd77

    Senior Member

    Sep 7, 2009
    When you switch from input to output, it will output whatever the input value was.
    If you post the code that would probably help.
  3. John P

    AAC Fanatic!

    Oct 14, 2008
    This isn't the source of your problem, but don't use the PIC16F84. It's outdated; you can get a cheaper processor with more features that has the same pinout. Of course if your college has a box of PIC16F84's and that's what they give the students, that's what you'll use.

    If you're getting a different result according to whether you just reset the processor or not, my guess is that each time the calculation runs, it uses a variable that only gets zeroed on processor reset.

    Are the variables int8's or int16's, or possibly floats? It might make a difference how many bytes are involved storing the data.
  4. DonQ

    Active Member

    May 6, 2009
    You don't say how much of an error. Completely the wrong number? or just off by 1.

    If it is off by one, it may be something as simple as the difference between a subtract instruction, and a subtract with carry instruction. I'm not sure of the mnemonic in PIC assembly but on a CISC processor I use it is the difference between SUB and SBC. Subtract with carry is so that single word subtracts can be chained together to make subtracts for more bits than in a single word.

    Not sure how a PIC does this since I mostly use C and refer to the manual when I need to do assembly. But if this is the problem, the first subtract may include the carry, which would be zero after a reset, but then would be set after the first subtract. The second subtract would include the value of the carry from the first subtract and offset the answer by 1. If this is the case, either clear the carry manually, or use an instruction that does not include it in the lowest word of the subtract.

    If it is off by more than 1, then... never mind.
  5. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    Please post your source code, as well as more details of the error. Error Displaying on the LCD, calculation error (what amount), etc?