
;8052 Serial port HELLO WORLD program
;Ran on a ATMEL AT89C52 at 11.059MHz wired to a MAX232CPE chip
;output to PC in HyperTerminal at 300 baud 8/N/1 with no flow control  

.org	0x0000

mov PCON, #00H  ;clear SMOD - baud rate NOT doubled
                ; so K = 1 in baud equation below

mov TMOD, #22H  ;set Timers 1 and 0 to 8-bit auto-reload

mov TH1, #0xA0H ;sets timer 1 for 300 baud assuming 11.059MHz clock
                ; TH1 = 256 - ((K * OscFreq)/(384 * BaudRate))
                ;     = 256 - ((1 * 11059000)/(384 * 300))
                ;     = 256 - 95.99826388888
                ;     = 160 = #0xA0H 
                
setb TCON.6     ;same as setb TR1 - starts timer 1 running                

mov SCON, #50H  ;Serial port mode 1 - 8bit with variable baud
                ;receive enabled
                ;clears Transmit interrupt flag

start:
  mov dptr, #message  ;point the data pointer to the start of the message
  clr a
  movc a, @a+dptr     ;get first letter to display

loop:
  clr TI       ;clear the "transmitted" bit
  mov SBUF, a  ;send the letter to the serial port
  acall wait   ;wait for letter to finish sending
  inc dptr     ;increment data pointer
  clr a
  movc a, @a+dptr ;get next letter to display
  jz start     ;jump back to the start of the message if we hit the end
  sjmp loop    ;otherwise send the next letter  

wait:
jnb TI, wait   ;loop until TI is set
ret

message:
 .db  "HELLO WORLD! ", 0   ;the zero is an end-of-string marker
 
