Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
01/09/05 12:20
Read: times


 
Msg Score: +1
 +1 Good Answer/Helpful
#84600 - More explanation...
Responding to: ???'s previous message
[pre]
STATE_TABLE:
DW MAIN_STATE_0 ; pointer to State 0 routine
DW MAIN_STATE_1 ; pointer to State 1 routine
DW MAIN_STATE_2 ; pointer to State 2 routine
DW MAIN_STATE_3 ; pointer to State 3 routine
STATE_CNT EQU ($ - STATE_TABLE)/2 ;number of states
[/pre]

The '$' symbol means the current address. For example if STATE_TABLE was at address 2000h then $ = 2000h + 8 = 2008h. Each DW (define word allocates two bytes of code space). So STATE_CNT = 2008h - 2000h / 2 = 4. STATE_CNT is an abreviation for state count, ie the number of states. If you added DW MAIN_STATE_4, the assembler calculate STATE_CNT to equal 5 and so on.


In the start of the code at PWR_UP low is send to bits and bytes where the switch status and state is to be placed using indirect addressing wy simply zero is not moved on the bytes ?


[pre]
MOV R0, #SwitchStates ; clear all switch states to 0
MOV R2, #8
SwStInit:
MOV @R0, #0
INC R0
DJNZ R2, SwStInit
[/pre]

Why does the programmer not just move zeroes to the bytes? This code is more generic - if you make a change as in add more switches etc the assembler makes the changes for you. It is a little more work up front, but makes the code easier to maintain.

In MAIN_LOOP: MOV A, SwitchNo ; is the switch number to be processed.
on next line ADD A,#SwitchStates ; this for fetching the state for that switch , can we use MOV A,#SwitchStates ?


[pre]
MOV A, SwitchNo ; fetch the state for the current
ADD A, #SwitchStates ; switch
MOV R0, A
MOV A, @R0
[/pre]

Ok, we're indexing a table here. SwitchNo is the current switch we're processing, SwitchStates is the table in memory where the states for each switch is stored. In 'c' this would be something like a = SwitchStates[SwitchNo];

For switch 0, the address would be SwitchStates+0
For switch 1, the address would be SwitchStates+1
For switch 2, the address would be SwitchStates+2
and so on.

So we calculate the address we want to read by #Switchstates which is the address of the table (note #) and add the SWitchNo as each entry is one byte. This address is put into R0,and the mov a,@r0 reads from that address thus retrieving the state value we want.

the table of SWT_MSK telling the controller that there is High on the switch to be processed or something else?

SWT_MSK translates to 'switch mask' - we give it a bit value (0..7) and it returns us a mask value with the bit number set - for bit 0 it returns us 1, for bit 7 it returns us 80h. We use this value 'anded' with the port value to isolate the switch bit we want to look at. The result after the 'and' is either zero or non-zero.


5 sec counter is equated to 5*100 can ASM51 read this as 5 seconds counter or what it suppose to mean, explain the 5 second counter & 10 msec relation shown in teh start of state 1 ?

FIVE_SEC_CNT (five second count) equals 5 * 100 = 500 - the assembler does the calculation for you. If each timer tick is 10mS there is 100 * 10mS per second. This value is obviously a word value (byte only is 0 -255) so the HIGH and LOW operators get the high or low byte of the word value.


How the counter State 1 works specially the part where CLR C and RLC A is used ? where in comments it is written make*2 word type index to counter table


Shifting left multiplies the value by 2. Shifting right divides by two. Think of the binary sequence : 1,2,4,8,16....


In State 2 ADD A,#Switchcounters+1 offset what does this means i have gone through scott's book but no clue.


The Switchcounters table is a word table (two bytes for each entry) so +0 is the high byte, +1 is the low byte


what is the function of CALL_Table ?


The description of the routine tells you exactly what it does. The DPTR is loaded with the address of a table of word addresses. A has the index into this table. The routine figures out which jump address from the table to use,pushes this address onto the stack then does a RET which effectively jumps to that address. You will see STATE_TABLE has 4 routines it can jump to: MAIN_STATE_0...3. Therefore A can only have the value 0..3 when passed to CALL_Table. Put simply, if you call CALL_TABLE with DPTR loaded with STATE_TABLE and A = 0, then you will jump to MAIN_STATE_0, if A= 3 you jump to MAIN_STATE_3


Hopefully this answers all your questions. Again, a step through with the simulator would have told you all this.

This code does a lot of indexing that can be confusing for a beginner. The code was written to be flexible as well as understandable. The techniques used are very common. I had not seen this code before and was able to understand what it was supposed to do, so therefore the code was written in a manner that is easily maintainable. This is what professional programmers such as myself are paid to do!

List of 9 messages in thread
TopicAuthorDate
State Machine example on this web            01/01/70 00:00      
   Link to state Machine Example?            01/01/70 00:00      
      I just wondered the same            01/01/70 00:00      
         link            01/01/70 00:00      
      State machine            01/01/70 00:00      
   any explaination            01/01/70 00:00      
      More explanation...            01/01/70 00:00      
         Excellent Description            01/01/70 00:00      
            Excellent!            01/01/70 00:00      

Back to Subject List