;**************************************************************************** ;TITLE: Program to recover one sample from MAX186 ADC ; ;Format of instruction : R ; where, R : instruction to read the ADC ; ;Crystal frequency : 3.58 MHz. ;Baud rate : 2400 ; ;FUNCTIONS ;transmit : transmit the 2 packed digits to comp ;hextoasc : converts hex to ascii ;rxcomp : receive ascii digit from comp ;txcomp : transmit ascii digit to comp ;rxadc : receive a byte from ADC ;txadc : transmit a byte to ADC ;adcread : receive data from ADC ;**************************************************************************** .include "2313def.inc" .def rtemp = r16 .def treg = r17 ; data (in ASCII) to be transmitted to computer .def rreg = r18 ; data (in ASCII) received from computer .def cnt = r19 ; counter 0 .def datmsb = r20 ; msb of data received from ADC .def datlsb = r21 ; lsb of data received from ADC .def datbyt = r22 .cseg .org 0 rjmp RESET RESET: ldi rtemp, $a0 ; initialize stack out SPL, rtemp ldi rtemp, $5A ; Setting baud rate to 2400 out UBRR,rtemp ; at crystal frequency of 3.58MHz ldi rtemp,0b11111101 ; Configure PB1 as input out DDRB,rtemp rcall rxcomp ; get command from user cpi rreg,$52 brne nxt1 sbi PORTB,PINB3 ; set CS cbi PORTB,PINB2 ; clear sclk cbi PORTB,PINB0 ; clear Din cbi PORTB,PINB3 ; clear CS ldi datbyt,0b10000111 ; load control word rcall txadc rcall adcread ; get data (2 bytes) from ADC ldi treg,$20 ; leave a space rcall txcomp mov datbyt,datmsb ; display msb of obtained data rcall transmit mov datbyt,datlsb ; display lsb of obtained data rcall transmit sbi PORTB,PINB3 ; set CS nxt1: ldi treg,$0a ; leave a line after a command rcall txcomp rjmp RESET ; loop again ;**************************************************************************** ;'transmit' ;Aim ; Transmit data to computer - 8 bits ;Registers used ; treg : contains data to be transmitted ; datbyt : contains packed hex data from calling function ;Functions called ; hextoasc ; txcomp ;**************************************************************************** transmit: mov treg,datbyt swap treg ; swap nibbles rcall hextoasc ; convert one digit to ASCII rcall txcomp ; and transmit mov treg,datbyt rcall hextoasc ; convert next digit to ASCII rcall txcomp ; and transmit ret ; return if complete data transmitted ;***************************************************************************** ;'hextoasc' ;Aim ; Convert hex to ASCII. Hex value is passed from & ASCII value is passed ; back to calling function in register treg. ;Registers used ; rtemp temporary register ; treg register containing data ;Functions called ; none ;***************************************************************************** hextoasc: andi treg,$0f cpi treg,$0a brsh henxt1 ldi rtemp,$30 rjmp henxt2 henxt1: ldi rtemp,$37 henxt2: add treg,rtemp ret ;***************************************************************************** ;'rxcomp' ;Aim ; Function to receive data (1 digit) from computer. Value received is ; passed to calling function through register rreg. ;Registers used ; rtemp temporary register ; rreg register containing data received ;Functions called ; none ;***************************************************************************** rxcomp: ldi rtemp, $10 ; Enable receiver out UCR,rtemp rxpoll: sbis USR,7 ; Poll to check if char received rjmp rxpoll in rreg,UDR ; Put received data in rreg ret ;***************************************************************************** ;'txcomp' ;Aim ; Function to transmit data (1 digit) to computer. The value to be ; transmitted is passed from calling function into register treg. ;Registers used ; rtemp temporary register ; treg register containing data to be transmitted ;Functions called ; none ;***************************************************************************** txcomp: ldi rtemp, $08 ; Enable transmitter out UCR,rtemp out UDR,treg txpoll: sbis USR,5 ; Poll to check end of transmission rjmp txpoll ret ;**************************************************************************** ;'txadc' ;Aim ; Function to write a byte to adc. Value to be written is passed from ; calling function in register datbyt ;Registers used ; cnt to store no of bits ; datbyt to store the data ;Functions called ; none ;***************************************************************************** txadc: ldi cnt, $08 ; load counter txbck: rol datbyt ; rotate data left through carry cbi PORTB,PINB2 ; clear SCL brcc txnxt ; if carry set sbi PORTB,PINB0 ; set Din rjmp txnxt1 ; else txnxt: cbi PORTB,PINB0 ; clear Din txnxt1: sbi PORTB,PINB2 ; set SCL dec cnt ; decrement counter brne txbck ; if counter not zero, go back nop cbi PORTB,PINB2 ; clear SCL ret ;****************************************************************************** ;'rxadc' ;Aim ; Function to read a byte from adc. Value read is returned in register ; datbyt ;Registers used ; cnt to store no of bits ; datbyt to store the data read ;Functions called ; none ;***************************************************************************** rxadc: ldi cnt,$08 ; load counter ldi datbyt, 0 rxbck: sbi PORTB,PINB2 ; set scl sbic PINB,PINB1 ; if Dout set then sec ; set carry sbis PINB,PINB1 ; else clc ; clear carry rol datbyt cbi PORTB,PINB2 ; clear scl dec cnt ; decrement counter brne rxbck ; if counter equals zero, goto calling function ret ; else jump back ;****************************************************************************** ;'adcread' ;Aim ; Complete function to read data from ADC. Data returned to calling ; function in datmsb & datlsb. ;Registers used ; datbyt register in which data read is stored ; rtemp temporary register ; datmsb,datlsb data registers (msb,lsb) ;Functions called ; rxadc ;***************************************************************************** adcread: rcall rxadc ; get msb from ADC mov datmsb,datbyt rcall rxadc ; get lsb from ADC mov datlsb,datbyt subi datlsb,$08 ; convert from 2's complement to binary brcc adnxt subi datmsb,$01 adnxt: com datlsb com datmsb ret ; go back to calling function