; ; hello.button.45.asm ; ; button hello-world program ; ; Neil Gershenfeld ; CBA MIT 10/22/08 ; ; (c) Massachusetts Institute of Technology 2008 ; Permission granted for experimental and personal use; ; license for commercial sale available from MIT. ; .include "tn45def.inc" .equ tx_pin = PB2; transmit pin .equ button_pin = PB4; button pin .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 button pin for input with pull-up ; sbi PORTB, button_pin cbi DDRB, button_pin ; ; init comm pin for output ; sbi PORTB, tx_pin sbi DDRB, tx_pin main_loop: button_down_loop: sbic PINB, button_pin rjmp button_down_loop print button_down_message button_down_message: .db "button pressed",10,0 button_up_loop: sbis PINB, button_pin rjmp button_up_loop print button_up_message button_up_message: .db "button released",10,0 rjmp main_loop