;Attiny12 7 bit Barker code beacon. Code supports up to three phases. ;Brkshf061015B (Version). Set for punctual output. ;Copyright 2006 Dick Cappels. http://www.projects.cappels.org ;Three sets of time-shifted barker codes are generated, and one is output: early, punctual, ;and late, separated by 1/2 of a chip width. A sync pulse for triggering ;the scope is also output. The start pulse fires when the first bit a code sequence ;is emitted. ;This code expects a 4 Mhz clock and can run on an ATtiny12.The timer 0 interrupt does not ;preserve processor status, thus interrupts would need to be stopped if forground compuing ;is needed. ;Note on Barkder code encoding: The register "barker" may contain a pattern up to 8 bits in length. ;The pattern is shiftet out msb first. When the register conatins all zeros, it is reloaded with the ;constant "barkercode". Thus, only patterns ending with a logic "1" will be shifted out properly. ;BIT PATTERNS ;Barker Code Output 0010111,,,0010111 ;Barker Code Sync 0000010...0000010... ;The sync output is only 1/2 chip wide and occurs during the first half chip in ;second chip the group of three chips of the same value. .nolist ; ;****************************************************** .include "tn12def.inc"; ;****************************************************** ;PORTB Assignments ;0 Barker code sync output (PIN 5) ;1 Output Barker inverted (PIN 6) ;2 Barker code output (PIN 7) ;3 Reserved for 4 MHz crystal (PIN 2) ;4 Reserved for 4 Mhz crystal (PIN 3) ;5 Reset with external pullup. (PIN 1) ;6 ;7 .equ PORTBInitData = 0b11111111 ;Initialization data for PORTB .equ PORTbInitDDR = 0b11111111 .equ barkercode = 0b00101110 ;Barker code. Zero at end is not part of the code.Shifted lsb first. .equ timerload = 220 ;Number of prescaled cycles to count before next interrupt. .def barker = r2 ;Register contains Barkercode being shifted. .def temp = r16 ;Scratch register for non-interrupt time only. .def flagreg = r17 ;Internal flags (see bel0w) .def bout = r18 ;Buffering output data for PORTB. Write to this instead of port. .def quadreg = r19 ;Code from Barker generator is shifted 1/2 chip at a time in this register. ;Flagreg bit assignments ;0 Emission mode bit 0 * ;1 Emission mode bit 1 * ;2 Value of bit to be sent via modulated Barker code (if modulation is used). ;3 ;4 ;5 ;6 Odd/Even intterupt. Toggled by tovflo routine. ;7 State of Barker Code bit out of barker register. ;* Emission mode bits values: ;00 No code emitted ;01 Emit punctual (a logic one) ;10 Emit 1/2 chip early ;11 Emit 1/2 chip late .ORG $0000 rjmp Start ;ATtiny12 vectors reti ;external interrupt handler reti ;pin change interrupt -return rjmp tovflo ;timer overflow interrupt handler Start: ;Well, no stack to initiate! ldi temp,PORTBInitData ;Set PORTB as all bits otuputs out PORTB,temp ldi temp,PORTBInitData out DDRB,temp ldi temp,$80 out ACSR,temp ;Turn off comparitor (save a little power). ldi temp,$02 out TCCR0,temp ;Set timer prescaler $03 = Fclk/8 ldi temp,timerload ;Set number of prescaled clocks until next interrupt.(was 247d on generator) out TCNT0,temp ldi temp,$02 ;enable timer overflow interrupts out TIMSK,temp ldi temp,barkercode ;load the Barker Code. mov barker,temp ldi flagreg,1 sei main: ;Note: Interrupt routine currently does not save anything on ATtiny12. rjmp main ;All the action is during interrupts. tovflo : ;Overflow interrupt tasks ;Reset Timer ;Output Port B ;If even field shift the code byte and set flag to odd field ;Else set flag to even field, ;Shift Barker bit into quardature shift register (1/2 chip per shift) and ;output to Port B buffers. ldI temp,timerload ;Set number of prescaled clocks until next interrupt. out TCNT0,temp mov temp,bout ;Set bout bit 1 equal to bout bit 2. asr temp com temp andi temp,0b00000010 andi bout,0b11111101 or bout,temp out portb,bout ;Transfer portb buffer to port b. out portb,bout ;Output Port B. Transfer portb buffer to PORTB. andi bout,0b11111010 ;Clear sync bit and barker output bits. sbrs flagreg,6 rjmp Oddfield andi flagreg,0b01111111 ;Set state of barker bit flag in flagreg to zero. andi flagreg,0b00111111 ;Clear odd/even flag and clear barker bit flag. lsl barker ;Shift barker right until register is all zeros,then reload. brcc BbitIsLow ori flagreg,0b10000000 ;If carry is not low from shift then set the barker bit in flagreg. BbitIsLow: tst barker ;Check to see if barker is zero yet. brne Encoded ;If barker is not zero, then don't reload the register. ldi temp,barkercode ;Reload the Barker Code. mov barker,temp ori bout,0b00000001 ;Set the sync bit high. Encoded: rjmp Endodd Oddfield: ori flagreg,0b01000000 Endodd: ;Shift Barker bit into quardature shift register (1/2 chip per shift) and ;output to Port B buffers. mov temp,flagreg ;Get copy barker state (bit 7) into temp. rol temp ;Move barker state into carry. ror quadreg ;Shift barker code into quadreg. mov temp,flagreg ;Get flagresiter into temp. andi temp,0b00000011 ;Mask off all but Emission Mode bits breq BitsetDone ;If Emission Mode is zero, don't generate an output bit. cpi temp,2 breq EmitTooEarly cpi temp,3 breq EmitTooLate ;Emission Mode bits do not equal zero 2 or 3, so email punctual code. ;Shift bit six (punctual bit) into bit 2 position (code output on port B). mov temp,quadreg ;Copy quadreg into temp. ror temp ror temp ror temp ror temp rjmp Endshifts EmitTooEarly: mov temp,quadreg ;Copy quadreg into temp. ror temp ror temp ror temp ror temp ror temp rjmp Endshifts EmitTooLate: mov temp,quadreg ;Copy quadreg into temp. ror temp ror temp ror temp rjmp Endshifts Endshifts: andi temp,0b00000100 ;Mask off all but bit 1. or bout,temp ;Move the three msb of quadreg into previously cleared msb of bout. BitsetDone: ; rcall XNORData ;Call XNORData is polarity modulation is needed.here if polarity modulation is needed. reti ;Return from interrupt. XNORData: ;Modulate polarity of Barker Code output with data bit. mov temp,flagreg eor temp,bout com temp andi temp,0b00000100 andi bout, 0b11111011 or bout,temp .exit