; ; hello.serial.45.asm ; ; 9600 baud serial hello-world program ; ; Neil Gershenfeld ; CBA MIT 7/27/07 ; ; (c) Massachusetts Institute of Technology 2007 ; Permission granted for experimental and personal use; ; license for commercial sale available from MIT. ; .include "tn45def.inc" .equ tx_pin = PB2; transmit pin .equ led_src = PB3; led source .equ led_gnd = PB4; led ground .def bit_count = R16; bit counter .def temp = R17; temporary storage .def temp1 = R18; temporary storage .def txbyte = R19; data byte ; ; print ; .macro print ldi zl,low(@0*2) ldi zh,high(@0*2) rcall print_db .endmacro .cseg .org 0 rjmp reset ; ; bit delay ; serial bit delay ; .equ b = 13 ; 9600 baud (8 MHz clock /8) bit_delay: ldi temp, b bitloop: dec temp brne bitloop ret ; ; putchar ; assumes no line driver (doesn't invert bits) ; .equ sb = 1; number of stop bits putchar: ldi bit_count, 9+sb; 1+8+sb com txbyte; invert everything sec; set start bit putchar0: brcc putchar1; if carry set sbi PORTB, tx_pin; send a '0' rjmp putchar2; else putchar1: cbi PORTB, tx_pin ; send a '1' nop ; even out timing putchar2: rcall bit_delay; one bit delay rcall bit_delay lsr txbyte; get next bit dec bit_count; if not all bits sent brne putchar0; send next bit ret; ; ; print_db ; prints a null-terminated .db string ; print_db: print_loop: lpm mov txbyte,R0 cpi txbyte,0 breq return rcall putchar adiw zl, 1 rjmp print_loop return: ret ; ; main program ; reset: ; ; set stack pointer to top of RAM ; ldi temp, high(RAMEND) out SPH, temp ldi temp, low(RAMEND) out SPL, temp ; ; init comm pin for output ; sbi PORTB, tx_pin sbi DDRB, tx_pin ; ; turn on LED ; sbi PORTB, led_src sbi DDRB, led_src sbi DDRB, led_gnd cbi PORTB, led_gnd loop: print message message: .db "IHTFP Hello World! IHTFP",10,0 rjmp loop