I created a simple programme to view the registers in a PIC16F877A with each of the PORTB pins connected to a LED:
#define _XTAL_FREQ 12000000
#include <xc.h>
#pragma config FOSC = HS
#pragma config WDTE = ON
#pragma config PWRTE = OFF
#pragma config BOREN = ON
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF
int x = 1;
void main()
{
TRISB = 0;
x = x - 1;
PORTB = STATUS;
while(1) {}
}
I noted that bit4 (TO) was initially set then went low as the WDT came in, as expected. Then I noticed that bit2 (zero bit) was set. The relevant bits of STATUS register are:
bit 4 TO: Time-out bit
1 = After power-up, CLRWDT instruction or SLEEP instruction
0 = A WDT time-out occurred
bit 3 PD: Power-down bit
1 = After power-up or by the CLRWDT instruction
0 = By execution of the SLEEP instruction
bit 2 Z: Zero bit
1 = The result of an arithmetic or logic operation is zero
0 = The result of an arithmetic or logic operation is not zero
bit 1 DC: Digit carry/borrow bit
1 = A carry-out from the 4th low order bit of the result occurred
0 = No carry-out from the 4th low order bit of the result
bit 0 C: Carry/borrow bit
So, I added a simple line of arithmetic with x = 1 to begin and got the following, surprising (to me), results [Z, DC, C]:
x = x - 1 the answer is 0 but STATUS reg says [0, 0, 0]
x = x + 10 the answer is 11 but STATUS reg says [1, 0, 0]
x = x - 10 the answer is -9 but STATUS reg says [1, 1, 1]
Therefore, this is the exact opposite from what I would expect. If the arthimetic operation produces a zero then Z should be set!
Any help with where I went wrong this time please?
Cheers.