Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;Have buffer in out routines going. Save this then On to flash programming.
- ;Serial85_2. Now starting get a string from terminal at 600 baud ending with CR
- ; and get string with len in front. Both strings go into 'table'.
- ; Now doing showBuf stuff. And fillBuff
- ;Buffers in and out of serial streams
- ;serial85_1. Going to try to get both tx and rx going using software loops for delays.
- ; appears to be working, both tx and rx'
- ;serial85_0. Is just the tiny85 version of m328p below.Works for tn85 tx
- ;Serial01. Got both Tx and Rx routines working out of Arduino
- ; pins 8 and 10 resp.Bit holdup due to not noticing PINB was the
- ; place to look for inputs, not PORTB. Now going to send and receive longer strings
- ; of bytes. 22June14, 15:13.
- ;SerialInOut0. Builds on SerialOut below
- ;Serial Out0. Send byte in r16 out soft serial TX_PIN in port b
- ; out at 600 baud.
- ; get a delay of exactly 1/1200 sec for 600 baud softserial
- ;initially for Arduino then for Tiny85
- ;1/1200 sec is 833.3 uSec
- ;use r20,21,22
- ;This subroutine works for delay of 833.13uS
- ;defs, then equ then inc, theb .dseg and eseg then eseg
- .def serialByteReg = r16
- .def rxByte = r18
- .def counterReg = r17
- .set testing = 1 ;comment out if not testing
- .equ tab_size = 64
- .equ TX_PIN = 0
- .equ RX_PIN = 2 ; Tx,Rx pins are PB0 and PB2 resp
- .include "tn85def.inc"
- ;.include "m328pdef.inc"
- .DSEG
- var1: .BYTE 1 ; reserve 1 byte to var1
- table: .BYTE tab_size ; reserve tab_size bytes
- .ESEG
- .DB "PeterB's serial program"
- .CSEG
- ldi r30,low(var1) ; Load Z register low
- ldi r31,high(var1) ; Load Z register high
- ld r1,Z ; Load VAR1 into register 1
- ;.DB 1,2,3, 4
- test:
- ldi r16, low(RAMEND)
- out SPL, r16
- ldi r16,high(RAMEND)
- out SPH, r16
- ldi r16, 0xf9
- out DDRB,r16 ;
- nop
- ldi r16, $ff
- out PORTB,r16
- ;===============================================================
- ;rjmp test2_rxStrEndCR
- rcall test_rsStrWithLen
- ;rjmp waitForDDump
- ;rjmp test_dumpTable
- ;rjmp waitForCharD
- ; rjmp serialTest1
- ; rjmp serialTest0
- ; rjmp fillBuf
- ;rjmp test_serialStrOut
- here1:
- rjmp here1
- ;----------------------------------------------------------------
- halfBitTime: ;better name for this delay. Half of 1/600
- ;myDelay1200:
- ;ldi r21,13 ; 13 works for m328 at 16Mhz
- ldi r21,7 ;try 7 for tiny85 at 8Hmz
- ldi r20,130 ;r20,21 at 130,7 give 833uS. Good for 600baud at 8Mhz
- start:
- inc r20
- nop
- brne start
- dec r21
- brne start
- ret
- ;--------------------------------------------------
- oneBitTime:
- rcall halfBitTime
- rcall halfBitTime
- ret
- ;-------------------------------------------------
- sendAZero:
- ;output 0 on Tx pin
- cbi PORTB,TX_PIN ; send a zero out PB0
- ret
- ;-----------------------------------------------------
- sendAOne:
- ;output 1 on Tx pin
- sbi PORTB,TX_PIN ; send a zero out PB0
- ret
- ;-----------------------------------------------------
- sendStartBit:
- ; send a 0 for one bit time
- rcall sendAZero
- rcall oneBitTime
- ret
- ;-------------------------------------------------------
- sendNextDataBit: ;main output routine for serial tx
- lsr serialByteReg ;push high bit into carry flag then inspect it
- ;originally did lsl but found lsb first.
- brcc gotzero ;if it's a 0 do nothing
- rcall sendAOne ;must have been a 1 in carry
- rjmp down
- gotzero:
- rcall sendAZero ;if here carry was a zero
- down:
- rcall oneBitTime ;so that 1 or 0 lasts 1/600 sec
- ret
- ;-------------------------------------------------------------
- send8DataBits: ; send all bits in serialByteReg
- ldi counterReg,8 ;8 data bits
- sendBit:
- rcall sendNextDataBit
- dec counterReg
- brne sendBit
- ret
- ;--------------------------------------------------------
- sendStopBit:
- ; send a 1 for one bit time
- rcall sendAOne
- rcall oneBitTime
- ret
- ;--------------------------------------------------------
- sendSerialByte: ;main routine. Byte in serialByteReg = r16
- push counterReg
- rcall sendStartBit
- rcall send8DataBits
- rcall sendStopBit
- rcall sendStopBit ;two stops
- pop counterReg
- ret
- ;**************************************************************
- serialTest0: ;output series of 'AAAA..'s
- ldi serialByteReg, 0x42 ;0x41
- rcall sendSerialByte
- rcall oneBitTime ; take a rest
- rjmp serialTest0 ;continue forever
- ;---------------------------------------------------------
- ;---------Now do SerialRx routines-------------------
- waitForHigh: ;loop til RX is high
- sbis PINB,RX_PIN ;test that pin for set (PB2)
- rjmp waitForHigh ; loop if rx pin is low
- ret
- ;-----------------------------------------------
- waitForLow: ;PRONBLEMs loop til RX is low. FIXED.
- sbic PINB,2 ;test that pin for set (PB2)
- rjmp waitForLow ; loop if rx pin is high
- ret
- ;---------------------------------------------------
- waitForStartBit: ;loop til get a real start bit
- rcall waitForHigh ;should be marking at start
- rcall waitForLow ;gone low. might be noise
- rcall halfBitTime ;is it still low in middle of bit time
- sbic PINB,RX_PIN ;..well, is it?
- rjmp waitForStartBit ;loop if level gone back high. Not a start bit.
- ret ;we've got our start bit
- ;----------------------------------------------------
- checkForStopBit: ;at end, get carry flag to reflect level. Prob if c=0
- rcall oneBitTime ; go into stop bit frame, halfway
- sec ;should stay a 1 in C if stop bit OK
- sbis PINB,RX_PIN ;don't clc if bit is high
- clc ;but only if we have a weird low stop bit
- ret ;with carry flag = stop bit. Should be a 1
- ;-------------------------------------------------------------
- get8Bits: ;get the 8 data bits. No frame stuff
- clr rxbyte ;this will fill up with bits read from RX_PIN
- push counterReg ;going to use this so save contents for later
- ldi counterReg,8 ;because we're expecting 8 databits
- nextBit:
- rcall oneBitTime ;first enter here when mid-startbit
- rcall rxABit ;get one bit
- dec counterReg ;done?
- brne nextBit ;no, round again
- pop counterReg ;yes, finished, restor counter and get out
- ret
- ;---------------------------------------------------------------
- rxABit: ;big serial input routine for one bit
- clc ;assume a 0
- sbic PINB,RX_PIN ; skip nxt if pin low
- sec ;rx pin was high
- ror rxbyte ;carry flag rolls into msb first
- ret
- ;********************************
- getSerialByte: ;big routine. Serial ends up in rxByte
- push counterReg
- rcall waitForStartBit ;**change
- rcall get8Bits
- rcall checkForStopBit
- pop counterReg
- ret ;with rxByte containing serial bye
- ;----------------------------------------------------
- serialTest1: ;output A then reflect input. Worked OK
- ldi serialByteReg, 0x36 ;0x41
- rcall sendSerialByte
- rcall oneBitTime ; take a rest
- rcall getSerialByte
- mov serialByteReg,rxByte ;output what's been read
- rcall sendSerialByte
- rjmp serialTest1
- ;--------------------------------------------------------
- ;----------Now doing buffer work. Want to and from 64 bytes----------
- fillBuf:
- ldi ZL,low(table) ;table is my buffer
- ldi ZH, high(table) ;Z now points to table
- ldi counterReg,64 ;64 bytes in buffer
- ldi r16,$30
- storeB0:
- st z+,r16
- inc r16
- dec counterReg
- brne storeB0
- here:
- ; rjmp here
- ret
- ;----------------------------------------------------------
- serialStrOut: ;X points to start of string,r17 has length
- ld serialByteReg, x+
- rcall sendSerialByte
- dec r17 ;got to end of string?
- brne serialStrOut
- ret
- ;----------------------------------
- test_serialStrOut:
- rcall fillBuf
- ldi XL,low(table) ;table start of str
- ldi XH, high(table)
- ldi r17,64 ;going to send len=r17 bytes
- rcall serialStrOut
- here2:
- rjmp here2
- ;--------------------------------------
- waitForCharD: ;wait til eg a 'D' is pressed then do something.
- ldi serialByteReg, '>' ;0x41
- rcall sendSerialByte
- rcall oneBitTime ; take a rest
- rcall getSerialByte
- mov serialByteReg,rxByte ;output what's been read
- cpi rxByte, 'D'
- brne waitForCharD
- ldi serialByteReg, '*'
- rcall sendSerialByte
- rjmp waitForCharD
- ;-----------------------------------------------------------
- dumpTable:
- ldi XL,low(table) ;table start of str
- ldi XH, high(table)
- ldi r17,64 ;going to send len=r17 bytes
- rcall serialStrOut
- ret
- ;-------------------------------------------------------------
- test_dumpTable:
- rcall fillBuf
- rcall getSerialByte ;any one will do.
- rcall dumpTable
- rjmp test_dumpTable
- ;----------------------------------------------------------
- waitForDDump: ;wait til eg a 'D' is pressed then dump table
- ldi serialByteReg, '>' ;0x41
- rcall sendSerialByte
- rcall oneBitTime ; take a rest
- rcall getSerialByte
- mov serialByteReg,rxByte ;output what's been read
- cpi rxByte, 'D'
- brne waitForDDump
- rcall dumpTable
- rjmp waitForCharD
- ;---------------------------------------------------------------
- rxStrEndCR: ;get a serial string that ends with CR
- clr counterReg
- ldi XL,low(table) ;table is where str will go
- ldi XH, high(table)
- upsec:
- rcall getSerialByte
- cpi rxByte,$0d ;is it CR = end of string?
- breq fin
- st x+, rxByte ;char goes into buffer="Table"
- inc counterReg ;don't go over 64 bytes
- cpi counterReg,64
- brne upsec ;not too long and not CR so keep going
- fin:
- ret
- ;---------------------------------------------
- test_rxStrEndCR: ;just a test of above
- rcall rxStrEndCR
- rcall waitForDDump
- rjmp test_rxStrEndCR
- ;------------------------------------------------------
- test2_rxStrEndCR: ;want a diagnostic dump if testing. Works with .IFDEF
- rcall rxStrEndCR
- .IFDEF testing
- rcall dumpTable
- .ENDIF
- rjmp test2_rxStrEndCR
- ;------------------------------------------------------------
- rxStrWithLen: ;expect len char char char.. for len chars
- push counterReg
- ldi XL,low(table) ;table is where str will go
- ldi XH, high(table)
- rcall getSerialByte ; get length bye Must be less than 65
- mov counterReg, rxByte ;save len in counter
- cpi counterReg,65 ;
- brlo allOK ;less than 65 so carry on. Branch if Lower
- ldi counterReg,64 ; if len>64 then len=64. Buffer = table only 64 bytes
- allOK:
- tst counterReg ;zero yet?
- breq finrs
- rcall getSerialByte ;next serial input byte
- st x+, rxByte ;put into buffer
- dec counterReg ;have we done len=counterReg bytes?
- rjmp allOK
- finrs:
- pop counterReg
- ret
- ;---------------------------------------------------------------
- test_rsStrWithLen: ;works ok with macro $05GHIJKLM. Sends GHIJK
- ldi r16, '#'
- rcall sendSerialByte
- rcall rxStrWithLen
- rcall dumpTable
- rjmp test_rsStrWithLen
Advertisement
Add Comment
Please, Sign In to add comment