; definitions ; .include "tn13def.inc" .equ chargepin = PB1 ; charging pin .equ txpin = PB4 ; serial transmit pin .def bitcnt = R16 ; bit counter SERIAL PORT STUFF .def temp = R17 ; temporary storage .def txbyte = R18 ; data byte .def delaycnt = R19 ; delay counter ;storage that we dont realy need .def lo = R20 ; low byte .def hi = R21 ; high byte ;counters for sleep4ever .def counter1 = R22 .def counter2 = R23 ; ; start of code ; .cseg .org 0 rjmp reset ; jump to reset routine ; ; putchar routine ; assumes no line driver (doesn't invert bits) ; .equ sb = 1 ; number of stop bits putchar: ldi bitcnt, 9+sb ; 1+8+sb com txbyte ; invert everything sec ; set start bit putchar0: brcc putchar1 ; if carry set sbi PORTB, txpin ; send a '0' rjmp putchar2 ; else putchar1: cbi PORTB, txpin ; send a '1' nop putchar2: rcall bitdelay ; one bit delay rcall bitdelay lsr txbyte ; get next bit dec bitcnt ; if not all bits sent brne putchar0 ; send next ret ; else return ; ; serial bit delay routine ; .equ b = 17 ; 9600 bps bitdelay: ldi temp, b bitloop: dec temp brne bitloop ret ; ; routine to wait for sample to settle ; .equ delay = 255 settle: ldi temp, delay settleloop: dec temp brne settleloop ret sleep4ever: ldi counter1, 255 ldi counter2, 32 loop1: loop2: dec counter2 brne loop2 ldi counter2, 32 dec counter1 brne loop1 ret ; ; main program ; reset: ldi temp, low(RAMEND) ; set stack pointer to top of RAM out SPL, temp ; ; ; init output pins ; sbi PORTB, txpin ; comm sbi DDRB, txpin ; " sbi PORTB, chargepin ; charging PB1/Pin 6 sbi DDRB, chargepin ; set bit to 1 to make it output ; ; init A/D ; cbi ADMUX, REFS0 ; use Vcc as reference cbi ADMUX, ADLAR ; right-adjust result sbi ADCSRA, ADEN ; enable A/D cbi ADCSRA, ADATE ; disable auto-trigger cbi ADCSRA, ADPS2 ; set prescaler for /2 cbi ADCSRA, ADPS1 ; " cbi ADCSRA, ADPS0 ; " cbi ADMUX, MUX1 ; input on ADC1 sbi ADMUX, MUX0 ; " ; ; infinite main loop ; loop: ;sleep for a long time cbi PORTB, chargepin rcall sleep4ever sbi PORTB, chargepin ; turn on the juice ldi delaycnt, 40 ;sleep for CPU wait21cycles: dec delaycnt ;1 cycle brne wait21cycles ;1 cycle false, 2 true ;read AD sbi ADCSRA, ADSC ; start conversion adloopup: sbic ADCSRA, ADSC ; loop until complete rjmp adloopup ;save our values in lo, ADCL ; get low byte in hi, ADCH ; get high byte cbi PORTB, chargepin ; turn off juice ;send the 2 bytes to le serial port mov txbyte, lo ; low byte rcall putchar mov txbyte, hi ; high byte rcall putchar ;framing bullshit must be a better way.... ldi txbyte, 1 rcall putchar ldi txbyte, 2 rcall putchar ldi txbyte, 3 rcall putchar ldi txbyte, 4 rcall putchar rjmp loop; back to where we started life