Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;serial85_0. Is just the tiny85 version of m328p below. Works for tx. Got halfbittime delay going.
- ;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
- .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 serialTest1
- rjmp serialTest0
- 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
- rcall sendStartBit
- rcall send8DataBits
- rcall sendStopBit
- rcall sendStopBit ;two stops
- ret
- ;**************************************************************
- serialTest0: ;output series of 'AAAA..'s
- ldi serialByteReg, 0x41 ;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
- rcall waitForStartBit ;**change
- rcall get8Bits
- rcall checkForStopBit
- 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 fro 64 bytes----------
- showBuf:
- ldi ZL,low(table) ;table is my buffer
- ldi ZH, high(table) ;Z now points to table
- ldi counterReg,64 ;64 bytes in buffer
- ; finish later
- ret
Advertisement
Add Comment
Please, Sign In to add comment