The end result of what I'm trying to do is send BCD to portA so I can make a clock. I've started small with only one digit and a few LEDs and I'd like to know why something works as it does.

The first two lines of the main program have portd.0 and portd.1 toggling LEDs outside of the for/next loop. I expected in the first go round of the program the two lights will come on and stay on until the for/next loop completes its count from 0 to 15. After the loop is completed I expected the lights to go off for the next iteration of the for/next loop count from 0 to 15.

Actually the LEDs change state each time the for/next loop counts a digit. i.e. I've noticed the LEDs are on during the display of an odd number and off during the display of an even number.

The digit displays I'm using are, I think, 8200-7300 and take BCD in and display a numeric/alpha character. The LEDs are something I had laying around and I don't know the part numbers.

My question is why do the LEDs change state outside of the for/next loop?

Other than power, the connections shown are the only things connected to the 16F877A.


Code:
'*  Notes   : Transfer of data in BCD format from a 16f877 to   *
'*          : 7300 and 7340 LED assemblies                      *
'           : has added LED on pin 19 and 20 to make sure       *
‘           :program is running                                 *        
'****************************************************************
'16f877A       7300/7340
'port/pin          pin/BCD digit
'A0/pin 2----------8/A
'A1/pin 3----------1/B
'A2/pin 4----------2/C
'A3/pin 5----------3/D
'E0/pin 8----------5/Latch Enable
'D0/pin 19 +LED to GND -LED
'D1/pin 20 +LED to GND -LED
'OSC1/pin13 Crystal to pin 14 and 22pf cap to ground
'OSC2/pin14 Crystal to pin 13 adn 22pf cap to ground
 
clear 
define osc4 ' set oscillator to 4 MHz
 
'----------------------------------------------------------------fuses
 
cmcon = 7 ' comparators off
adcon0 = %11000000 ' adc off
adcon1 = %10000111 ' sets ports to digital
'-----------------------------------------------------------set ports
 
trisa = %00000000 ' sets trisa as output
trise = %11111110 ' sets bit0 of trise as output - only has 3 ports on trise
trisd = %00000000 ' sets portd as output
'---------------------------------------------------------------variables
 
outputnumber var byte
displaydigit var byte
max_number var byte
min_number var byte
latch_enable var porte.0 'pin 8 on 16f877a
 
max_number = 15
min_number = 0
main:
toggle portd.1
toggle portd.0 
for outputnumber = min_number to max_number ' generate a number to be displayed
    displaydigit = outputnumber ' give that number to the display variable
    porta = displaydigit ' set port output to effectively BCD
    toggle latch_enable 'toggle display to make it work
    pause 500 ' wait 1/2 second
next outputnumber
 
if outputnumber = max_number then outputnumber = min_number
 
goto main
 
end