; ; i0.6.thtp.asm ; ; Neil Gershenfeld CBA MIT 11/27/05 ; (c) Massachusetts Institute of Technology 2005 ; Permission granted for experimental and personal use; ; license for commercial use available from MIT ; ; transmit a SLIP UDP HTTP (THTP) packet when the button is pushed ; blink the LED when the button is pushed or a click is received ; .include "tn13def.inc" .equ ledpin1 = PB1; LED pin .equ buttonbit = 2; pushbutton bit .equ buttonpin = PB2; pushbutton pin .equ clickpin = PB3; click input pin .equ ledpin2 = PB4; mosfet output pin .def bitcnt = R16; bit counter .def txbyte = R17; bit counter .def temp = R18; temporary storage .def temp1 = R19; temporary storage .def count = R20; loop counter .def check_lo = R21; lo checksum accumulator .def check_hi = R22; hi checksum accumulator .def check_carry = R23; checksum carry accumulator .def zero = R24; 0 .def temp1 = R25; .equ END = 192 .equ ESC = 219 .equ ESC_END = 220 .equ ESC_ESC = 221 .macro putslip ; ; putslip ; outputs char in txbyte, with SLIP mapping ; ldi temp, END cpse txbyte, temp rjmp putslipchar mov temp, txbyte ldi txbyte, ESC rcall putclick rcall chardelay ldi txbyte, ESC_END rcall putclick rcall chardelay mov txbyte, temp rjmp endputslip ldi temp, ESC cpse txbyte, temp rjmp putslipchar mov temp, txbyte ldi txbyte, ESC rcall putclick rcall chardelay ldi txbyte, ESC_ESC rcall putclick rcall chardelay mov txbyte, temp rjmp endputslip putslipchar: rcall putclick rcall chardelay endputslip: .endmacro .cseg .org 0 rjmp reset ; putclick ; send char in txbyte clicks ; putclick: ldi bitcnt, 8 sec; set start bit ; ; send start clicks ; sbi PORTB, ledpin2 ;sbi PORTB, ledpin2 rcall clickon cbi PORTB, ledpin2 rcall clickdelay sbi PORTB, ledpin2 rcall clickon cbi PORTB, ledpin2 ;cbi PORTB, ledpin2 rcall clickdelay ; ; send data clicks ; putclick0: lsr txbyte; get next bit brcc putclick1; if carry set, send a 1 click sbi PORTB, ledpin2 rcall clickon cbi PORTB, ledpin2 rcall clickdelay cbi PORTB, ledpin2 rcall clickon cbi PORTB, ledpin2 rcall clickdelay rjmp putclick2; otherwise ... putclick1:; ... send a 0 click cbi PORTB, ledpin2 rcall clickon cbi PORTB, ledpin2 rcall clickdelay sbi PORTB, ledpin2 rcall clickon cbi PORTB, ledpin2 rcall clickdelay putclick2: dec bitcnt; if not all bits sent brne putclick0; send next bit ; ; send stop clicks ; ;; sbi PORTB, ledpin2 ;; rcall clickon ;; cbi PORTB, ledpin2 ;;rcall clickdelay ;;sbi PORTB, ledpin2 ;;rcall clickon ;; cbi PORTB, ledpin2 ;; rcall clickdelay ret; ; ; clickdelay ; delay between clicks ; clickdelay: ldi temp, 30 clickdelayloop: dec temp brne clickdelayloop ret ; clickdelay ; delay between clicks ; clickon: ldi temp, 1 clickonloop: dec temp brne clickdelayloop ret ; ; chardelay ; delay between characters ; chardelay: ldi temp, 200 chardelayloop: ldi temp1, 200 chardelayloop1: dec temp1 brne chardelayloop1 dec temp brne chardelayloop ret ; ; main program ; reset: ldi temp, low(RAMEND) ; set stack pointer to top of RAM out SPL, temp ; clr zero sbi PORTB, buttonpin ; init pushbutton pin for input with pull-up cbi DDRB, buttonpin ; cbi PORTB, ledpin2 ; init mosfet control pin for output sbi DDRB, ledpin2 ; cbi ACSR, ACIS1 ; setup comparator to output on state change cbi ACSR, ACIS0 ; sbi ADCSRB, ACME ; setup comparator to use ADC multiplexer cbi ADCSRA, ADEN ; sbi ADMUX, MUX1 ; set channel to ADC3 sbi ADMUX, MUX0 ; loop: ; ; wait for the button to be pressed or a click to arrive ; sbi ACSR, ACI ; reset comparator waitloop: sbic PINB, buttonbit ; check for button rjmp waitloop ldi txbyte, 4 rcall putclick rcall chardelay rcall chardelay rcall chardelay rcall chardelay rcall chardelay rjmp loop