Continue to Site

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.

ATMEL89S8253, assembler

Status
Not open for further replies.

siusia

New Member
Hello,
I need to write a program in assembler for a microcontroller ATMEL89S8253 as a college job. The microcontroller is connected to a DOS computer with rs232. The program is supposed to rely on autorepets, after pressing the button is to send many characters, but no more than 2 per second. I tried to use delays, but nothing works, please help me
 
Your code should look something like this. Baud rate values are assuming a 11.059MHz crystal and 9600bps.

Code:
#define   BUTTON   P1.0   ; change to port pin that button is on

   org   0x0000
   ajmp   START

   org   0x0023
SerRx:   ; place serial interrupt handler here

   org   0x0100
START:
   mov   TMOD,#0x21   ; timer 1 auto-reload mode (baud rate generator)
   setb   TR1       ; timer 1 run
   mov   TH1,#0xFA   ; 9600 w/11.059MHz xtal
   setb   PCON.SMOD1   ; double baud rate bit
   mov   SCON,#0x50   ; serial port mode 1 (8-bit UART)
   setb   TI       ; invoke tx complete

MAIN:
   setb   BUTTON       ; P1.0 input
PRESS:
   jb   BUTTON,MAIN   ; wait for button press
RELEASE:
   jnb   BUTTON,RELEASE   ; wait for button release

sendChar:
   jnb   TI,sendChar   ; wait for clear buffer
   clr   TI           ; acknowledge tx interrupt
   mov   SBUF,#0x41       ; send 'A'
   acall   delay2sec       ; 2 second wait
   ajmp   sendChar       ; send again (forever)

delay2sec:
   push   ACC       ; store accumulator
   mov   A,R0       ; store R0 register
   push   ACC       ;
   mov   R0,#0x0D   ; load delay counter
   mov   TH0,#0x00   ; reset timer 0
   mov   TL0,#0x00   ;
   setb   TR0       ; timer 0 run
delayReset:
   clr   TF0       ; clear timer 0 interrupt flag
WAIT:  
   jnb   TF0,WAIT       ; wait for timer interrupt
   djnz   R0,delayReset   ; decrement delay counter
   clr   TR0       ; timer 0 stop
   pop   ACC       ; restore R0 register
   mov   R0,A       ;
   pop   ACC       ; restore accumulator
   ret           ; done

   end
 
Last edited:
; 10 MHz, 9600 baud rate
Code:
;#define   BUTTON   P1.0   ; change to port pin that button is on

       org   0
       ajmp   START

       org   23h
SerRx:                   ; place serial interrupt handler here


    setb ren              
    setb ea               
    setb es                    


       org   100h

START: 
       mov   TMOD,#21h           ; timer 1 auto-reload mode (baud rate generator)
    setb   TR1               ; timer 1 run
       mov   TH1,#0fdh           ; 9600
     
setb   PMOD.SMOD1         ; double baud rate bit  .SMOD1
       ;mov a, pcon           ;dodalam
       ;setb acc.7        ;dodalam
       ;mov pcon, a        ;dodalam
       mov   SCON,#050h            ;serial port mode 1 (8-bit UART)
       setb   TI                ;invoke tx complete

MAIN:
       setb   P1.0                ;P1.0 input
PRESS: 
       jb   P1.0, MAIN            ; wait for button press
RELEASE:
       jnb   P1.0, RELEASE            ; wait for button release

sendChar:
       jnb   TI,sendChar            ; wait for clear buffer
       clr   TI                         ; acknowledge tx interrupt
        mov   SBUF,#33                   ; send 'A'
       acall   delay2sec            ; 2 second wait
       ajmp   sendChar                 ; send again (forever) 

delay2sec:
       push   ACC                 ; store accumulator
       mov   A,R0                ; store R0 register
       push   ACC                ;
        mov   R0,#00Dh             ; load delay counter #0x0D
       mov   TH0,#000h            ; reset timer 0
       mov   TL0,#000h            ;
       setb   TR0                ; timer 0 run
delayReset:
       clr   TF0                ; clear timer 0 interrupt flag
WAIT:   jnb   TF0, WAIT         ; wait for timer interrupt  DODAŁAM WAIT
       djnz   R0,delayReset            ; decrement delay counter
       clr   TR0                 ; timer 0 stop
       pop   ACC                 ; restore R0 register
       mov   R0,A                ;
       pop   ACC                 ; restore accumulator
    ret                        ; done
   end
 
I use C51ASM program and I have an include file
<code> ;*H***************************************************************************
; NAME: at89s8253.inc
;----------------------------------------------------------------------------
; PURPOSE: SFR Description file for AT89S8252/8253 products on C51ASM assembler
;*****************************************************************************

$SAVE
$NOLIST
$NOMOD51

; PORT Registers
P0 DATA 080H

P0_7 BIT 087H
P0_6 BIT 086H
P0_5 BIT 085H
P0_4 BIT 084H
P0_3 BIT 083H
P0_2 BIT 082H
P0_1 BIT 081H
P0_0 BIT 080H

P1 DATA 090H

P1_7 BIT 097H
P1_6 BIT 096H
P1_5 BIT 095H
P1_4 BIT 094H
P1_3 BIT 093H
P1_2 BIT 092H
P1_1 BIT 091H
P1_0 BIT 090H
SCK BIT 097H
MISO BIT 096H
MOSI BIT 095H
SSB BIT 094H
T2EX BIT 091H
T2 BIT 090H

P2 DATA 0A0H

P2_7 BIT 0A7H
P2_6 BIT 0A6H
P2_5 BIT 0A5H
P2_4 BIT 0A4H
P2_3 BIT 0A3H
P2_2 BIT 0A2H
P2_1 BIT 0A1H
P2_0 BIT 0A0H

P3 DATA 0B0H

P3_7 BIT 0B7H
P3_6 BIT 0B6H
P3_5 BIT 0B5H
P3_4 BIT 0B4H
P3_3 BIT 0B3H
P3_2 BIT 0B2H
P3_1 BIT 0B1H
P3_0 BIT 0B0H
RD BIT 0B7H
WR BIT 0B6H
T1 BIT 0B5H
T0 BIT 0B4H
INT1 BIT 0B3H
INT0 BIT 0B2H
TXD BIT 0B1H
RXD BIT 0B0H

;------------------ Status register ---------------------
PSW DATA 0D0H

CY BIT 0D7H
AC BIT 0D6H
F0 BIT 0D5H
RS1 BIT 0D4H
RS0 BIT 0D3H
OV BIT 0D2H
UD BIT 0D1H
P BIT 0D0H

;------------------ CPU registers ---------------------
ACC DATA 0E0H
B DATA 0F0H
SP DATA 081H
DPL DATA 082H
DPH DATA 083H
DP0L DATA 082H
DP0H DATA 083H
DP1L DATA 084H
DP1H DATA 085H
AUXR DATA 08EH

;------------------ TIMERS registers ---------------------
TCON DATA 088H
TF1 BIT 08FH
TR1 BIT 08EH
TF0 BIT 08DH
TR0 BIT 08CH
IE1 BIT 08BH
IT1 BIT 08AH
IE0 BIT 089H
IT0 BIT 088H

TMOD DATA 089H

T2CON DATA 0C8H
TF2 BIT 0CFH
EXF2 BIT 0CEH
RCLK BIT 0CDH
TCLK BIT 0CCH
EXEN2 BIT 0CBH
TR2 BIT 0CAH
C_T2 BIT 0C9H
CP_RL2 BIT 0C8H

T2MOD DATA 0C9H
TL0 DATA 08AH
TL1 DATA 08BH
TL2 DATA 0CCH
TH0 DATA 08CH
TH1 DATA 08DH
TH2 DATA 0CDH
RCAP2L DATA 0CAH
RCAP2H DATA 0CBH
WDTRST DATA 0A6H
WDTCON DATA 0A7H


;------------------- UART registers ------------------------
SCON DATA 098H
SM0 BIT 09FH
FE BIT 09FH
SM1 BIT 09EH
SM2 BIT 09DH
REN BIT 09CH
TB8 BIT 09BH
RB8 BIT 09AH
TI BIT 099H
RI BIT 098H

SBUF DATA 099H
SADEN DATA 0B9H
SADDR DATA 0A9H

;-------------------- IT registers -----------------------
IE DATA 0A8H
IP DATA 0B8H
IPH DATA 0B7H

; IE
EA BIT 0AFH
ET2 BIT 0ADH
ES BIT 0ACH
ET1 BIT 0ABH
EX1 BIT 0AAH
ET0 BIT 0A9H
EX0 BIT 0A8H

; IP
PT2 BIT 0BDH
PS BIT 0BCH
PT1 BIT 0BBH
PX1 BIT 0BAH
PT0 BIT 0B9H
PX0 BIT 0B8H

;-------------------- OSC control registers ----------------------
PCON DATA 087H
CLKREG DATA 08FH

;-------------------- EEPROM Data registers-----------------------
EECON DATA 096H

;-------------------- SPI registers ------------------------------
SPDR DATA 086H
SPSR DATA 0AAH
SPCR DATA 0D5H



$RESTORE
</code>
 
Last edited:
Actually, it was for 11.0592MHz, which is the most common crystal frequency used with 9600bps. But even with that my math was off. R0 should be loaded with 0x1C, which is decimal 28. With a 11.059MHz crystal and timer 0 operating in 16-bit mode, it will interrupt 28 times in 2 seconds. Loading R0 makes it count interrupts until it reaches 28, in which 2 seconds will have lapsed.

If you're using 10MHz, you will not be able to achieve the standard 2400, 4800, and 9600bps bit rates no matter which timer you use. You'll be so far off it will never communicate properly. I highly suggest going with a 11.0592MHz crystal for this.

And yes Ian that was a typo on my part. PCON is the correct register for the double baud rate bit.
 
Last edited:
Timer 1 as baud rate generator -
PCON.SMOD1 = 0

TH1 Reload Value = 256 - ((Fosc / 384) / Baud)

10MHz / 384 = 26041.6667
26041.6667 / 9600 = 2.713
256 - 2.713 = 253.287

You'd have to go with either 253 or 254, which would result in baud rates of -

253 would result in a baud rate of 8680.556bps
254 would result in a baud rate of 13.020.833bps

PCON.SMOD1 = 1

TH1 Reload Value = 256 - ((Fosc / 192) / Baud)

10MHz / 192 = 52083.333
52083.333 / 9600 = 5.426
256 - 5.426 = 250.574

You'd have to go with either 250 or 251, which would result in baud rates of -

250 would result in a baud rate of 8680.556bps
251 would result in a baud rate of 10416.667bps

Using timer 2 would result in a bit rate of -

RCAP2H:RCAP2L Reload Value = 65536 - ((Fosc / 32) / Baud
10000000 / 32 = 312500
312500 / 9600 = 32.552
RCAP2 = 65536 - 32.552 = 65503.448

65503 would result in a bit rate of 9469.7bps
65504 would result in a bit rate of 9765.625bps

While using timer 2 MIGHT work, you'd have anywhere from a 1.3% - 1.7% BER (Bit Error Rate), which might work but I still question the reliability and the chances of frame errors. Why even deal with it when a crystal is dirt cheap and it's a commonly available crystal?

Use a 11.0592MHz crystal and you'll have 0% BER.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top