Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

8051 assembly code for counter

Status
Not open for further replies.

Parth86

Member
I want make counter program for 8051 controller in assembly that count 0 to 9 I know I have to use counter, timer, interrupt routine , instruction set ( mov, add INC DEC....) now problem is that I have no idea how to start please anyone will help me to write code
 

Parth86

Member
Right O... I'll be at home in about an hour...

I'll put a skeletal ASM program together ( actually I already have one ) and post it soon..
ok can you tell me what is the use of register bank and SFR in ram i think register bank is used to store location of data
and don't know about SFR
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
I just put this together

Its a start at least..
Code:
RS     BIT P3.1        ; LCD
RW     BIT P3.2        ; Constants
E     BIT P3.0        ;
BZ    BIT P1.7        ; busy flag

    org     0h        ; reset vector
    sjmp    Start

    org    20h        ; jump over vectors

;
; Start of code
;---------------------------------
Start:
    acall    LcdInit        ; called once only

While:  
    acall    LcdLine1    ; Goto Line1
    mov     DPTR,#MSG1
    acall    LcdPrint    ; Display message
    acall    LcdLine2    ; goto Line2
    mov     DPTR,#MSG2    ;
    acall    LcdPrint    ; Display message
    sjmp    While        ; forever loop

MSG1:
    db    "First Line ",0
MSG2:
    db    "Second Line ",0

;
; Initialize LCD
;---------------------------------
LcdInit:
    clr    RW
    acall    delay40Ms
    mov    P1,#33h        ; Init once
    setb    E
    clr    E
    acall    delayMs        ; Init twice
    setb    E
    clr    E
    acall    delayMs        ; Now busy can be read.
  
    mov    a,#38h        ; Function set: 8 bits: 2 Line : font 5x7.
    acall    LcdCom
    mov    a,#0Ch        ; Display On: Cursor Off : Blink Off.
    acall    LcdCom
    mov    a,#06h        ; Shift forward.
    acall    LcdCom
    mov    a,#01h        ; Clear Goto position 1.
    acall    LcdCom  
    ret
;
; Goto first line
;---------------------------------
LcdLine1:
    mov    A,#80h        ; position 1 on line 1
    acall    LcdCom
    ret
;
; Goto second line
;---------------------------------
LcdLine2:
    mov    A,#0C0h        ; position 1 on line 2
    acall    LcdCom
    ret
;
; Print a null terminated string
;---------------------------------
LcdPrint:
    mov    A,#0h
    movc    A,@A+DPTR    ; Through string until 0
    cjne    A,#0,Char    ;
    ret
Char:
    acall    LcdData        ; Print data character
    inc    dptr
    sjmp    LcdPrint
;
; Waiting for LCD module
;---------------------------------
LcdBusy:
    mov    P1,#0FFh    ; tristate the port
    clr    RS        ; command mode
    setb    RW        ; Read flags
busy:    clr    E
    setb    E
    nop
    jb    BZ, busy    ; wait until ready
    clr    E
    clr    RW        ; Back to write mode
    ret
;
; Send command to LCD module
;---------------------------------
LcdCom:                ; sends a command
    acall    LcdBusy
    clr    RW
    clr    RS
    mov    P1,A        ; Acc has command
    setb    E
    clr    E
    ret
;
; Send data to LCD module
;---------------------------------
LcdData:            ; Sends data
    acall    LcdBusy
    clr    RW
    setb    RS
    mov    P1,A        ; Acc has data
    setb    E
    clr    E
    ret  
;
; Delays
;---------------------------------
delay40Ms:
    mov    R1,#25h        ; Simple delay @ 40 milli seconds
dlyM:    acall    delayMs
    djnz    R1,dlyM
    ret

delayMs:
    mov    R0,#0h        ; Simple delay @ 1.1 milli second
dlyU:    nop
    nop
    djnz    R0,dlyU
    ret

    end
[code]

We'll do some interrupts later..
 

Parth86

Member
I just put this together

Its a start at least..
Code:
RS     BIT P3.1        ; LCD
RW     BIT P3.2        ; Constants
E     BIT P3.0        ;
BZ    BIT P1.7        ; busy flag

    org     0h        ; reset vector
    sjmp    Start

    org    20h        ; jump over vectors

;
; Start of code
;---------------------------------
Start:
    acall    LcdInit        ; called once only

While: 
    acall    LcdLine1    ; Goto Line1
    mov     DPTR,#MSG1
    acall    LcdPrint    ; Display message
    acall    LcdLine2    ; goto Line2
    mov     DPTR,#MSG2    ;
    acall    LcdPrint    ; Display message
    sjmp    While        ; forever loop

MSG1:
    db    "First Line ",0
MSG2:
    db    "Second Line ",0

;
; Initialize LCD
;---------------------------------
LcdInit:
    clr    RW
    acall    delay40Ms
    mov    P1,#33h        ; Init once
    setb    E
    clr    E
    acall    delayMs        ; Init twice
    setb    E
    clr    E
    acall    delayMs        ; Now busy can be read.
 
    mov    a,#38h        ; Function set: 8 bits: 2 Line : font 5x7.
    acall    LcdCom
    mov    a,#0Ch        ; Display On: Cursor Off : Blink Off.
    acall    LcdCom
    mov    a,#06h        ; Shift forward.
    acall    LcdCom
    mov    a,#01h        ; Clear Goto position 1.
    acall    LcdCom 
    ret
;
; Goto first line
;---------------------------------
LcdLine1:
    mov    A,#80h        ; position 1 on line 1
    acall    LcdCom
    ret
;
; Goto second line
;---------------------------------
LcdLine2:
    mov    A,#0C0h        ; position 1 on line 2
    acall    LcdCom
    ret
;
; Print a null terminated string
;---------------------------------
LcdPrint:
    mov    A,#0h
    movc    A,@A+DPTR    ; Through string until 0
    cjne    A,#0,Char    ;
    ret
Char:
    acall    LcdData        ; Print data character
    inc    dptr
    sjmp    LcdPrint
;
; Waiting for LCD module
;---------------------------------
LcdBusy:
    mov    P1,#0FFh    ; tristate the port
    clr    RS        ; command mode
    setb    RW        ; Read flags
busy:    clr    E
    setb    E
    nop
    jb    BZ, busy    ; wait until ready
    clr    E
    clr    RW        ; Back to write mode
    ret
;
; Send command to LCD module
;---------------------------------
LcdCom:                ; sends a command
    acall    LcdBusy
    clr    RW
    clr    RS
    mov    P1,A        ; Acc has command
    setb    E
    clr    E
    ret
;
; Send data to LCD module
;---------------------------------
LcdData:            ; Sends data
    acall    LcdBusy
    clr    RW
    setb    RS
    mov    P1,A        ; Acc has data
    setb    E
    clr    E
    ret 
;
; Delays
;---------------------------------
delay40Ms:
    mov    R1,#25h        ; Simple delay @ 40 milli seconds
dlyM:    acall    delayMs
    djnz    R1,dlyM
    ret

delayMs:
    mov    R0,#0h        ; Simple delay @ 1.1 milli second
dlyU:    nop
    nop
    djnz    R0,dlyU
    ret

    end
[code]

We'll do some interrupts later..
 

Parth86

Member
hello ion, this code is very difficult to understand me where you used interrupt subroutine where is timer mode i have read to write counter program we need interrupt timer mode
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
I said we'll do that later... I need you to understand the code for the LCD first..
As we introduce interrupts I can explain the timer and the external count!

Which assembler do you want to use... I would recommend ASEM51...

I have purposely used the INT pins so you can see what happens when you design systems... You can have a PCB made, then find out you need to use alternate pins for something else...

I will show you an interrupt routine first.... Then I will see if you can integrate the two

Code:
countl    equ    20h
counth    equ    21h

    org     0h        ; reset vector
    sjmp    Start

    org    03h        ; Ex0 interrupt
    sjmp    ISR

    org    20h        ; jump over vectors
;
; Start of code
;---------------------------------
Start:
    setb    P3.2        ; INT0 as input
    setb    IT0        ; Set level of interrupt
    mov    IE,#081h    ; Global On : INT0 On..
  
While:  

    sjmp    While        ; forever loop

ISR:
    mov    A,countl    ; get counter low byte
    mov    B,counth    ; get counter high byte
    add    A,#1        ; increase counter low
    jnc    non        ; are we at 255
    inc    b        ; yes! increase Counter high
non:    mov    countl,A    ; store Low
    mov    counth,B    ; store High
    clr    IE0        ; ensure flag off.
    reti            ; return from interrupt..

    end
Tell me if you understand this.... If not I'll go through it with you..
 

Parth86

Member
first thanks for help' can you explain with theory I want to understand some basic things that is used in every counter program I want to understand what basic instruction I need to use to create every counter program in theory I have read loading data transfer arithmetic logic instruction I know how to make loop, labels directives I want to create some basic algorithm for counter program for begainer
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Well just look at the code I posted..

The counter is incremented on the high to low transition on P3.2 (INT0) pin.
If the countl variable overflows, the counth variable is incremented, thus giving a 16 bit counter.

Next we need a BCD routine to be able to print the count to a screen..

You can also use a built in timer as a counter... If you just want to count with a register then the simplest command is inc A This just increases Acc by one...
 

remon7

Active Member
As Far as I know,
.MODEL SMALL
.STACK 100H

.DATA
PROMPT DB 'The counting from 0 to 9 is : $'

.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
LEA DX, PROMPT
MOV AH, 9
INT 21H
MOV CX, 10
MOV AH, 2
MOV DL, 48
loop:
INT 21H
INC DL
DEC CX
JNZ loop
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAI
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top