ATMEL programming

please help me with the logic of this program.
i have problems analyzing the commands and different variables.

this is a project called the room light controller with person counter. the uC used was ATMEL AT89S52

i badly need help. please

 

 

******program starts here*******

RB0     EQU     000H    ; Select Register Bank 0
RB1     EQU     008H    ; Select Register Bank 1  ...poke to PSW to use


DIS_A    EQU    P0.2       
DIS_B    EQU    P0.3       
DIS_C    EQU    P0.4
;DIS_D    EQU    P0.6
DIS_D    EQU    P2.2
DIS_E    EQU    P0.5
DIS_F    EQU    P0.1
DIS_G    EQU    P0.0

DIS1    EQU    P0.7
;DIS2    EQU    P2.6  
DIS2    EQU    P0.6

LIGHT    EQU    P2.7

SEN1    EQU    P1.0
SEN2    EQU    P1.1
test    EQU P1.2 
test2    EQU P1.3


        UP      BIT     0H 
        DWN     BIT     1H
        COUNT   EQU     30H
        SPEED   EQU     31H
        VALUE_1 EQU     32H
        VALUE_2 EQU     33H
        NUMB1 EQU     34H
        NUMB2 EQU     35H
        NUMB3 EQU     36H
        VISITOR EQU    37H

        STACK   EQU     50H


;---------==========----------==========---------=========---------
;              PROCESSOR INTERRUPT AND RESET VECTORS
;---------==========----------==========---------=========---------
  
    ORG     00H            ; Reset
    JMP     MAIN
    ORG     000BH        ;Timer Interrupt0
    JMP     REFRESH
   
;---------==========----------==========---------=========---------
;  Main routine. Program execution starts here.
;---------==========----------==========---------=========---------
MAIN:   
    MOV PSW,#RB0          ; Select register bank 0
   MOV SP,STACK
    CLR LIGHT
    CLR test2
    CLR DIS_D
    MOV VISITOR,#00H
    MOV SPEED,#00H
    MOV COUNT,#00H
    MOV VALUE_1,#15H
    MOV VALUE_2,#15H
    CLR DIS1
    CLR DIS2
   
    MOV TMOD,#01H        ;enable timer0 for scanning
    MOV TL0,#00H
    MOV TH0,#0FFH
    SETB ET0
    SETB EA
    SETB TR0        ;Start the Timer


        MOV VALUE_1,#00H
        MOV VALUE_2,#00H
        SETB SEN1
        SETB SEN2
UPP:    JNB SEN1,UP_COUNT
        CPL test
        JB SEN2,UPP
       
        MOV A,VISITOR            ;DOWN COUNTING
        CJNE A,#00,UAPS
        CLR LIGHT
        JNB SEN2,$
        CALL DELAY
        JB SEN1,$
        CALL DELAY
        JNB SEN1,$
        CALL DELAY
        AJMP UPP           
UAPS:    DEC VISITOR   
        MOV A,VISITOR
        CJNE A,#00,UAPA
        CLR LIGHT
UAPA:    MOV R2,VISITOR
      MOV R1,#00H   
        MOV R3,#00D
      MOV R4,#00D
      MOV R5,#00D
      MOV R6,#00D
      MOV R7,#00D      
        CALL HEX2BCD
        MOV VALUE_2,R3
       MOV VALUE_1,R4
        JNB SEN2,$
       CALL DELAY
       JB SEN1,$
       CALL DELAY
       JNB SEN1,$
       CALL DELAY
        AJMP UPP
   
UP_COUNT:
        SETB LIGHT
        INC VISITOR       
        MOV A,VISITOR
        CJNE A,#99,UPPS
        MOV VISITOR,#98
        JNB SEN1,$
        CALL DELAY
        JB SEN2,$
        CALL DELAY
        JNB SEN2,$
        CALL DELAY
        AJMP UPP
UPPS:    MOV R2,VISITOR
      MOV R1,#00H   
        MOV R3,#00D
      MOV R4,#00D
      MOV R5,#00D
      MOV R6,#00D
      MOV R7,#00D      
        CALL HEX2BCD
        MOV VALUE_2,R3
       MOV VALUE_1,R4
        JNB SEN1,$
        CALL DELAY
       JB SEN2,$
       CALL DELAY
        JNB SEN2,$
        CALL DELAY
        AJMP UPP


;**************************************************   
HEX2BCD:
          MOV B,#10D
        MOV A,R2
        DIV AB
        MOV   R3,B              ;  
        MOV   B,#10             ; R7,R6,R5,R4,R3
        DIV   AB
        MOV   R4,B
        MOV   R5,A
        CJNE R1,#0H,HIGH_BYTE   ; CHECK FOR HIGH BYTE
        SJMP ENDD
 
HIGH_BYTE:
            MOV   A,#6
            ADD   A,R3
            MOV   B,#10
            DIV   AB
            MOV   R3,B
            ADD   A,#5
            ADD   A,R4
            MOV   B,#10
            DIV   AB
            MOV   R4,B
            ADD   A,#2
            ADD   A,R5
            MOV   B,#10
            DIV   AB
            MOV   R5,B
            CJNE R6,#00D,ADD_IT
            SJMP CONTINUE
ADD_IT:
            ADD A,R6
CONTINUE:
            MOV R6,A
            DJNZ R1,HIGH_BYTE
            MOV B, #10D
           MOV A,R6
            DIV AB
            MOV R6,B
            MOV R7,A
ENDD:       RET
;***************************************************************************
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;     7 SEGMENT DISPLAY ROUTINE
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
DISP:
    MOV R2,SPEED
    CJNE R2,#00H,AAS1
    CLR DIS_A
    CLR DIS_B
    CLR DIS_C
    CLR DIS_D
    CLR DIS_E
    CLR DIS_F
    SETB DIS_G
    RET
AAS1:    CJNE R2,#01H,AS2
    CLR DIS_B
    CLR DIS_C
    SETB DIS_A
    SETB DIS_D
    SETB DIS_E
    SETB DIS_F
    SETB DIS_G
    RET
AS2:    CJNE R2,#02H,AS3
    CLR DIS_A
    CLR DIS_B
    CLR DIS_D
    CLR DIS_E
    CLR DIS_G
    SETB DIS_C
    SETB DIS_F
    RET
AS3:    CJNE R2,#03H,AS4
    CLR DIS_A   
    CLR DIS_B
    CLR DIS_C
    CLR DIS_D
    CLR DIS_G
    SETB DIS_E
    SETB DIS_F
    RET
AS4:    CJNE R2,#04H,AS5
    CLR DIS_B
    CLR DIS_C
    CLR DIS_F
    CLR DIS_G
    SETB DIS_A
    SETB DIS_D
    SETB DIS_E
    RET
AS5:    CJNE R2,#05H,AS6
    CLR DIS_A
    CLR DIS_C
    CLR DIS_D
    CLR DIS_F
    CLR DIS_G
    SETB  DIS_B
    SETB DIS_E
    RET
AS6:    CJNE R2,#06H,AS7
    CLR DIS_A
    CLR DIS_C
    CLR DIS_D
    CLR DIS_E
    CLR DIS_F
    CLR DIS_G
    SETB DIS_B
    RET
AS7:    CJNE R2,#07H,AS8
    CLR DIS_A
    CLR DIS_B
    CLR DIS_C
    SETB DIS_D
    SETB DIS_E
    SETB DIS_F
    SETB DIS_G
    RET
AS8:    CJNE R2,#08H,AS9
    CLR DIS_A
    CLR DIS_B
    CLR DIS_C
    CLR DIS_D
    CLR DIS_E
    CLR DIS_F
    CLR DIS_G
    RET
AS9:    CJNE R2,#09H,AS10
    CLR DIS_A
    CLR DIS_B
    CLR DIS_C
    CLR DIS_D
    CLR DIS_F
    CLR DIS_G
    SETB DIS_E
    RET
AS10:    CJNE R2,#15H,AS11        ;symbol for -
    SETB DIS_A
    SETB DIS_B
    SETB DIS_C
    SETB DIS_D
    SETB DIS_E
    SETB DIS_F
    CLR DIS_G
    RET
AS11:    CJNE R2,#16H,AS12        ;switch off all disp
    SETB DIS_A
    SETB DIS_B
    SETB DIS_C
    SETB DIS_D
    SETB DIS_E
    SETB DIS_F
    SETB DIS_G
    RET
AS12:    MOV SPEED,#00H
    AJMP DISP
;**********************************************************   
;        INTRRUPT ROUTINE TO REFRESH THE DISPLAY
;**********************************************************   
REFRESH:
    PUSH   PSW           ; save current registerset
   MOV    PSW,#RB1
   PUSH   ACC
    INC COUNT
    MOV R4,COUNT
QA1:    CJNE R4,#01H,QA2
    MOV SPEED,VALUE_1
    CLR DIS2
    SETB DIS1
    CALL DISP
    AJMP DOWN  
QA2:    CJNE R4,#02H,QA3
    MOV SPEED,VALUE_2
    CLR DIS1
    SETB DIS2
    CALL DISP
    AJMP DOWN
QA3:    MOV COUNT,#01H
    MOV R4,COUNT
    AJMP QA1
DOWN:    MOV TL0,#0FFH
    MOV TH0,#0F0H
    POP    ACC
   POP    PSW
    RETI
;**********************************************************
DELAY:
    MOV R1,#4FH
REP2:    MOV R2,#0FFH
REP1:    NOP
    DJNZ R2,REP1
    DJNZ R1,REP2
    RET
;**********************************************************   




END


   
   
   
   
   
   
   
   

do you have any suggestions

do you have any suggestions of websites for the C version of this program?

Google?

Where did you get this program?  If you did not write it, then maybe you need to start over and write the program in C or whatever language you know to start getting things working.  I know that sounds like an excuse, but unless you program it yourself and run into issues that way, how do you know the code you copied will do what you want it to do?

Maus

heres a beginners guide to

heres a beginners guide to assembler:

http://www.avr-asm-download.de/beginner_en.pdf

this + your UC’s datasheet may shed some light on that program there.

 

well, i just downloaded the

well, i just downloaded the program and im trying to understand it, but it seems im having no progress. i dont really know how to program an atmel uC. please help me Maus

oh yeah thanks. can you

oh yeah thanks. can you somehow understand the program?

I have commented your code

Get yourself a decent editor also and you will have some colour to make it more readable as well. I use programmers notepad if not a particular IDE for brand of micro. Copy and paste it into a new file called .asm

For a list describing the micros instruction set. Look here.


; The interrupt routine called everytime timer0 overflows updates the 2x7seg led displays with the results from the last HEX2BCD conversion results
; left in registers R3 and R4. They are transferred to VALUE_1 and VALUE_2 and Then finally transferred to SPEED to update either display
; UP_COUNT increases the count by one everytime a person enters room
; UPP      decreases the count by one everytime somebody leaves the room


program starts here

RB0     EQU     000H    ; Select Register Bank 0
RB1     EQU     008H    ; Select Register Bank 1  …poke to PSW to use


DIS_A    EQU    P0.2    ; pin assignments look at datasheet for these numbers  
DIS_B    EQU    P0.3    ; and connect segments of display to the corresponding pin  
DIS_C    EQU    P0.4    ;
;DIS_D    EQU    P0.6
DIS_D    EQU    P2.2    ;
DIS_E    EQU    P0.5    ;
DIS_F    EQU    P0.1    ;
DIS_G    EQU    P0.0    ; letters correspond to the seven segments driven

DIS1    EQU    P0.7        ; common for 7-seg led1 common anode display
;DIS2    EQU    P2.6 
DIS2    EQU    P0.6        ; common for 7-seg led2 "       "          "

LIGHT    EQU    P2.7    ; this pin controls the light in the room

SEN1    EQU    P1.0        ; sensor 1 of two sensors
SEN2    EQU    P1.1        ; sensor 2 - having 2 sensors can tell the direction of travel so we decrement the count or increment    
test    EQU P1.2         ; These pins don’t appear to be doing anything as the only reference below is test2 pin is cleared once
test2    EQU P1.3        ;


        UP      BIT     0H     ; Not used anywhere in code
        DWN     BIT     1H    ; Nor this
        COUNT   EQU     30H ; used to determine which display digit to refresh
        SPEED   EQU     31H ; holds the digit value to update the display being pointed to
        VALUE_1 EQU     32H ; holds one of the display digits
        VALUE_2 EQU     33H    ; holds the other display digit
        NUMB1 EQU     34H    ; also not used
        NUMB2 EQU     35H    ; "           "
        NUMB3 EQU     36H    ; "           "
        VISITOR EQU    37H    ; keeps the count of number of people in room at anytime

        STACK   EQU     50H ; is temporary storage during interrupt routine


;---------==========----------==========---------=========---------
;              PROCESSOR INTERRUPT AND RESET VECTORS
;---------==========----------==========---------=========---------
 
    ORG     00H          ; Reset < when the processor starts this directs it to the starting point in code
    JMP     MAIN
    ORG     000BH        ; Timer Interrupt0
    JMP     REFRESH         ; each time the timer reaches end of count<overflows> the code will stop and redirect to the refresh routine below
  
;---------==========----------==========---------=========---------
;  Main routine. Program execution starts here.
;---------==========----------==========---------=========---------
MAIN:  
    MOV PSW,#RB0         ; Select register bank 0
    MOV SP,STACK
    CLR LIGHT             ; setting these pins off
    CLR test2             ;
    CLR DIS_D             ;
    MOV VISITOR,#00H     ; initializes some variables
    MOV SPEED,#00H         ; necesary at start as can have random values on startup
    MOV COUNT,#00H         ; so we make zero
    MOV VALUE_1,#15H     ; these two we start at 15 hexadecimal
    MOV VALUE_2,#15H     ; "        "        "        "        "
    CLR DIS1             ;    setting these pins to zero disables the common anode to both 7-seg displays
    CLR DIS2             ;
  
    MOV TMOD,#01H        ; enable timer0 for scanning
    MOV TL0,#00H         ;  1 byte of timer0 register
    MOV TH0,#0FFH       ; high byte of timer 0. Value placed here represents what timer0 counts to before overflowing
    SETB ET0             ; required bit settings to configure timer 0
    SETB EA
    SETB TR0             ;Start the Timer


        MOV VALUE_1,#00H    ; set to zero
        MOV VALUE_2,#00H    ; set to zero
        SETB SEN1            ; set sensor1 pin
        SETB SEN2            ; set sensor2 pin
UPP:    JNB SEN1,UP_COUNT   
        CPL test            ; toggle the test pin - unless this is connected to another led or component it would appear this does nothing.
        JB SEN2,UPP            ; if sensor2 pin set goto UPP
      
        MOV A,VISITOR            ; DOWN COUNTING
        CJNE A,#00,UAPS             ; jump to UAPS if count not equal to zero
        CLR LIGHT                 ; turn off light because the people count is zero. That is no one is in room
        JNB SEN2,$                 
        CALL DELAY
        JB SEN1,$
        CALL DELAY
        JNB SEN1,$
        CALL DELAY
        AJMP UPP          
UAPS:    DEC VISITOR          
        MOV A,VISITOR   
        CJNE A,#00,UAPA        ; check if no one left jump to UAPA if there are still some people left
        CLR LIGHT            ; turn light off if there aren’t
UAPA:    MOV R2,VISITOR       
      MOV R1,#00H             ; clear these cpu registers prior to calling bcd converter 
        MOV R3,#00D
      MOV R4,#00D
      MOV R5,#00D
      MOV R6,#00D
      MOV R7,#00D     
        CALL HEX2BCD        ; convert our count from hexadecimal to bcd
        MOV VALUE_2,R3
       MOV VALUE_1,R4
        JNB SEN2,$
       CALL DELAY
       JB SEN1,$
       CALL DELAY
       JNB SEN1,$
       CALL DELAY
        AJMP UPP
  
UP_COUNT:                        ; another person has entered
        SETB LIGHT                ; turn on light
        INC VISITOR               ; increment counter
        MOV A,VISITOR
        CJNE A,#99,UPPS            ; compare count to 99 if not 99 jump to UPPS
        MOV VISITOR,#98            ; if it is 99 then make the count 98
        JNB SEN1,$                ; loop here until sensor1 pin is activated
        CALL DELAY                ; wait
        JB SEN2,$                ; test sensor2 pin and loop here until clear
        CALL DELAY                ; wait
        JNB SEN2,$                ; loop here until sensor2 pin is set
        CALL DELAY                ; wait
        AJMP UPP                ; Ajmp is like goto UPP
UPPS:    MOV R2,VISITOR            ; move the value of visitor into register 2 for conversion below
      MOV R1,#00H               ; As Hex2BCD is a 16bit converter R1 must be made 0 as we can only display up to 99 on 2x7seg leds
        MOV R3,#00D
      MOV R4,#00D
      MOV R5,#00D
      MOV R6,#00D
      MOV R7,#00D     
        CALL HEX2BCD
        MOV VALUE_2,R3            ; the results from Hex2Bcd left a number from 0-9 in R3
       MOV VALUE_1,R4            ; and also in R4 . Here they are transferred to VALUE_1 and VALUE_2
        JNB SEN1,$
        CALL DELAY
       JB SEN2,$
       CALL DELAY
        JNB SEN2,$
        CALL DELAY
        AJMP UPP


;

  
HEX2BCD:                        ; as the counter is at present in hexadecimal
          MOV B,#10D            ; it must be converted so we can display the number of people in decimal on
        MOV A,R2                ; the 7-seg led display
        DIV AB                    ; this is what this whole routine does down to endd
        MOV   R3,B              ; 
        MOV   B,#10             ; R7,R6,R5,R4,R3
        DIV   AB
        MOV   R4,B
        MOV   R5,A
        CJNE R1,#0H,HIGH_BYTE   ; CHECK FOR HIGH BYTE
        SJMP ENDD
 
HIGH_BYTE:
            MOV   A,#6
            ADD   A,R3
            MOV   B,#10
            DIV   AB
            MOV   R3,B
            ADD   A,#5
            ADD   A,R4
            MOV   B,#10
            DIV   AB
            MOV   R4,B
            ADD   A,#2
            ADD   A,R5
            MOV   B,#10
            DIV   AB
            MOV   R5,B
            CJNE R6,#00D,ADD_IT
            SJMP CONTINUE
ADD_IT:
            ADD A,R6
CONTINUE:
            MOV R6,A
            DJNZ R1,HIGH_BYTE
            MOV B, #10D
           MOV A,R6
            DIV AB
            MOV R6,B
            MOV R7,A
ENDD:       RET
;

;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
;     7 SEGMENT DISPLAY ROUTINE
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
DISP:
    MOV R2,SPEED            ; checks if value in register R2 = 0
    CJNE R2,#00H,AAS1        ; if not then goto AAS1
    CLR DIS_A
    CLR DIS_B
    CLR DIS_C
    CLR DIS_D
    CLR DIS_E
    CLR DIS_F
    SETB DIS_G                ; display a zero
    RET                        ; return to the line after where DISP was called
AAS1:    CJNE R2,#01H,AS2    ; the rest of this section checks if r2 is = values 1-9
    CLR DIS_B                ; and switches on the segments to correspond to these values
    CLR DIS_C
    SETB DIS_A
    SETB DIS_D
    SETB DIS_E
    SETB DIS_F
    SETB DIS_G
    RET
AS2:    CJNE R2,#02H,AS3
    CLR DIS_A
    CLR DIS_B
    CLR DIS_D
    CLR DIS_E
    CLR DIS_G
    SETB DIS_C
    SETB DIS_F
    RET
AS3:    CJNE R2,#03H,AS4
    CLR DIS_A  
    CLR DIS_B
    CLR DIS_C
    CLR DIS_D
    CLR DIS_G
    SETB DIS_E
    SETB DIS_F
    RET
AS4:    CJNE R2,#04H,AS5
    CLR DIS_B
    CLR DIS_C
    CLR DIS_F
    CLR DIS_G
    SETB DIS_A
    SETB DIS_D
    SETB DIS_E
    RET
AS5:    CJNE R2,#05H,AS6
    CLR DIS_A
    CLR DIS_C
    CLR DIS_D
    CLR DIS_F
    CLR DIS_G
    SETB  DIS_B
    SETB DIS_E
    RET
AS6:    CJNE R2,#06H,AS7
    CLR DIS_A
    CLR DIS_C
    CLR DIS_D
    CLR DIS_E
    CLR DIS_F
    CLR DIS_G
    SETB DIS_B
    RET
AS7:    CJNE R2,#07H,AS8
    CLR DIS_A
    CLR DIS_B
    CLR DIS_C
    SETB DIS_D
    SETB DIS_E
    SETB DIS_F
    SETB DIS_G
    RET
AS8:    CJNE R2,#08H,AS9
    CLR DIS_A
    CLR DIS_B
    CLR DIS_C
    CLR DIS_D
    CLR DIS_E
    CLR DIS_F
    CLR DIS_G
    RET
AS9:    CJNE R2,#09H,AS10
    CLR DIS_A
    CLR DIS_B
    CLR DIS_C
    CLR DIS_D
    CLR DIS_F
    CLR DIS_G
    SETB DIS_E
    RET
AS10:    CJNE R2,#15H,AS11        ;symbol for -
    SETB DIS_A
    SETB DIS_B
    SETB DIS_C
    SETB DIS_D
    SETB DIS_E
    SETB DIS_F
    CLR DIS_G
    RET
AS11:    CJNE R2,#16H,AS12        ;switch off all disp
    SETB DIS_A
    SETB DIS_B
    SETB DIS_C
    SETB DIS_D
    SETB DIS_E
    SETB DIS_F
    SETB DIS_G
    RET
AS12:    MOV SPEED,#00H
    AJMP DISP
;
  
;        INTERRUPT ROUTINE TO REFRESH THE DISPLAY
;
  
REFRESH:
    PUSH   PSW           ; save current registerset
   MOV    PSW,#RB1
   PUSH   ACC
    INC COUNT
    MOV R4,COUNT
QA1:    CJNE R4,#01H,QA2 ; do we want to display on DIS1 led?
    MOV SPEED,VALUE_1   
    CLR DIS2             
    SETB DIS1             ; switch on dis1 7seg-led
    CALL DISP
    AJMP DOWN 
QA2:    CJNE R4,#02H,QA3 ; do we want to display on DIS2 led?
    MOV SPEED,VALUE_2
    CLR DIS1
    SETB DIS2             ; switch on dis2 7seg-led
    CALL DISP
    AJMP DOWN
QA3:    MOV COUNT,#01H
    MOV R4,COUNT
    AJMP QA1
DOWN:    MOV TL0,#0FFH    ; restore registers
    MOV TH0,#0F0H
    POP    ACC
   POP    PSW
    RETI                ; return from interrupt
;

DELAY:                    ; delay routine used in various sections to wait a set time
    MOV R1,#4FH
REP2:    MOV R2,#0FFH
REP1:    NOP
    DJNZ R2,REP1
    DJNZ R1,REP2
    RET
;
*****************************  




END

that really helps. thanks

that really helps. thanks you.
btw, the programmer’s notepad is great. thanks alot
all i have to do now is to study the commands.
God bless

This is the first tutorial I

This is the first tutorial I used to teach myself AVRs and it was really basic but easy to follow:

http://imakeprojects.com/Projects/avr-tutorial/