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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Konstantinos L. Angelis
07/07/08 07:04
Read: 1266 times

#156470 - You have to enrich your library of Assembly Code
Responding to: Alan Dorman's previous message
Dear Alan,

You have to enrich your library of Assembly Code for 8051.

Here you will find most common code to
a. Init_Serial: Intitialise the Serial Port (polling method)
b. getchar: Receive a character from UART
c. putchar: Send a character from UART
d. prints: Print a string (text message) from Code memory
e. print_byte: Print a byte in Accumulator as a Hex Number
print_hex: -same as above-
f. print_bin99 Print a byte in Accumulator as a decimal 00-99d

also: a simple call from your MAIN Program
and: some definitions for CR/LF, FF, string, prompt

This code is already running in a AT89S4051 at 11.0592 MHz
maintained by IAR Assembler.

may this is helpful to you

;====================================== Init Serial Port at 9600,8,n,1,noHandShake
Init_Serial:                           ; (Assuming XTAL 11.0592MHz, 12cycles)
          clr  TCON_TR1                ; Stop Timer1
          mov  A, TMOD                 ; TMOD = (TMOD & 0x0F)|0x20
          anl  A, #0x0F                ; TMOD = 0000dddd (clear Timer1 bits)
          orl  A, #0x20                ; TMOD = 0010dddd (set M1=1 M0=0)
          mov  TMOD, A                 ; Timer1 at mode2 8bit autoreload
          mov  A, PCON                 ; PCON &= 0x7F 
          anl  A, #0x7F                ; clear PCON.7 = SMOD double rate
          mov  PCON, A                 ; 0=x1 baud rate, 1=x2 baud rate
          mov  SCON, #0x52             ; REN=1 (ready to receive)
                                       ; TI=1 (ready to transmit next char)
          mov  TH1, #0xFD              ; Reload value TH1 
          mov  TL1, #0xFD              ; Reload value TL1
          setb TCON_TR1                ; Start Timer1 as Baud Rate Generator

;====================================== getchar Returns received character in A
getchar:                               ; 
gtchr1:   jbc  SCON_RI, gtchr2         ; if  RI=1 (char received) clear RI
          sjmp gtchr1                  ; polled wait for RI=1
gtchr2:   mov  A, SBUF                 ; then return the char in A

;====================================== putchar Transmit a character in A =====
ptchr1:   jbc  SCON_TI, ptchr2         ; if TI=1 (ready for next) clear TI
          sjmp ptchr1                  ; polled wait for TI=1
ptchr2:   mov  SBUF, A                 ; then send the next char to TX

;====================================== prints DPTR points to a string ========
prints:                                ; in Code memory
prnts1:   clr  A                       ; print string Null terminated 
          movc A, @A+DPTR              ; 
          jz   prnts2                  ; Check for NULL  #0x00
          acall putchar                ; print character
          inc  DPTR                    ; get next
          sjmp prnts1                  ;
prnts2:   ret

;====================================== print_hex or print_byte ===============
print_byte:                           ;
print_hex:                            ; Print a hex value in A to stdio (ANSI)
                                      ; eg. A has 11010111  ==> D7
prh_a:                                ; uses A, B
           push B                     ; save B
           mov  B, A                  ; save A in B
           anl  A, #0xF0              ; mask upper nibble 
           swap A                     ; now A has 0000xxxx
           cjne A, #0x0A, prh_c       ; check A <> 10
prh_b:     add  A, #0x37              ; if A = 0x0A then add 0x37 ('A'-10)
           sjmp prh_d                 ; goto print
prh_c:     jnc  prh_b                 ; if A > 0x0A same a s A=0x0A
           add  A, #0x30              ; if A < 0x0A then add 0x30 ('0')
prh_d:     acall putchar              ; print high nibble

           mov  A, B                  ; get the value from B
           anl  A, #0x0F              ; mask the low nibble
           cjne A, #0x0A, prh_f       ; check A <> 10
prh_e:     add  A, #0x37              ; if A = 0x0A then add 0x37 ('A'-10)
           sjmp prh_g                 ; goto print
prh_f:     jnc  prh_e                 ; if A > 0x0A 
           add  A, #0x30              ; if A < 0x0A then add 0x30 ('0')
prh_g:     acall putchar              ; print high nibble
           pop  B                     ; restore B
           mov  A, B                  ; restore A

;====================================== print_bin99 prints a byte 00h-63h ASCII
           push A                      ; A holds the byte to be printed
           push B                      ;
           push PSW                    ;
prb99_a:                               ; while (A>=100) A-=100
           cjne A, #100, prb99_b       ;
             sjmp prb99_c              ; A=100
prb99_b:   jc prb99_d                  ; if A<100 then exit
prb99_c:      clr C                    ; A>=100
              subb A, #100             ; A-=100
           sjmp prb99_a                ;

           mov  B, #10                 ; divider by decimal 10
           div  AB                     ; quotiend=piliko in A (dekades)
                                       ; remainder-ypoloipo in B (monades)
           add  A,#'0'                 ; make quotiend 0h-9h ASCII 30h-39h
           acall putchar               ; dekades first
           mov  A, B                   ;
           add  A, #'0'                ; make remainder ASCII 30h-39h
           acall putchar
           pop PSW
           pop B
           pop A

;====================================== PART of MAIN =======================
          acall Init_Serial

          mov  DPTR, #string1          ; Send String1
          acall prints                 ;
          mov  DPTR, #CRLF             ; Send CRLF
          acall prints                 ; 
          mov  DPTR, #prompt           ; Prompt "0>"
          acall prints                 ;
          sjmp $

CRLF:     DB 0x0D, 0x0A, 0x00          ; Carriage Return, Line Feed, Null term.
FF:       DB 0x0C                      ; FormFeed
string1:  DB "String 1 to print"       ; Double quotes = NULL terminated string
prompt:   DB "0>"                      ; Prompt

List of 32 messages in thread
loading the SBUF shortcut?      Alan Dorman      07/06/08 18:46      
   Printing a string      Roger Blott      07/06/08 22:40      
   Shortcut...NOT        Michael Karas      07/06/08 22:46      
   You have to enrich your library of Assembly Code      Konstantinos L. Angelis      07/07/08 07:04      
      that is not a shortcut      Erik Malund      07/07/08 07:27      
         Library of Assembly Code      Konstantinos L. Angelis      07/07/08 08:31      
            the key word      Erik Malund      07/07/08 08:50      
               polled serial      Konstantinos L. Angelis      07/07/08 09:06      
                  "living with interrupts "      Erik Malund      07/07/08 09:24      
                     what I got out of all of this      Alan Dorman      07/07/08 21:35      
                        DPTR .... Yes!!      Michael Karas      07/07/08 21:54      
                        Check out DPTR      Konstantinos L. Angelis      07/08/08 00:51      
                     I AGREE      Mahesh Joshi      07/07/08 23:43      
                        agree with WHAT?      Erik Malund      07/08/08 06:40      
                            I AGREE with ...      Mahesh Joshi      07/08/08 22:46      
   C shortcut      Oliver Sedlacek      07/08/08 01:11      
      'C' confusion      Alan Dorman      07/10/08 03:35      
         Confused      Oliver Sedlacek      07/11/08 01:56      
            Maybe, maybe not      Craig Steiner      07/11/08 22:56      
               printf baggage      Oliver Sedlacek      07/14/08 04:07      
                  This should come as no surprise      Andy Neil      07/15/08 10:20      
               27K lots of memory      Alan Dorman      07/15/08 04:39      
                  "C baggage"      Erik Malund      07/15/08 06:26      
                     Getting off-topic        Craig Steiner      07/15/08 08:56      
                        I wholehardely agree      Erik Malund      07/15/08 09:02      
         Shortcuts are for wimps!      Richard Erlacher      07/12/08 19:27      
            Disagree        Craig Steiner      07/12/08 23:33      
            Half a million Lines?      Neil Kurzman      07/13/08 14:05      
               I didn't use 'C' or other HLL's on the old PC      Richard Erlacher      07/13/08 16:54      
   Poor choice of terms!      Andy Neil      07/09/08 13:45      
      A subroutine?      Alan Dorman      07/10/08 03:33      
         more code-efficient that macros      Richard Erlacher      07/12/08 19:29      

Back to Subject List