Forum: Mikrocontroller und Digitale Elektronik SRF04 Ultraschallmodul in AVR Assembler-suche Programm


von Tilman W. (tilman)


Lesenswert?

Halli-Hallo,
ich möchte ein SRF04 Ultraschallmodul an einen AT Tiny 2313
anschliessen. Der Controller soll in Assembler programmiert werden.
Ich weiss, es ist nicht besonders heldenhaft, hier nach einem fertigen
Programm zu fragen, aber mir rennt die Zeit davon und ich komme nicht
weiter. Hat jemand schon mal das SRF04 in Assembler programmiert und
kann mir hier mit einem Code weiterhelfen ?

Wenn nicht, ich habe einen Code von Steven Theriault im Netz gefunden
"Sonartest.asm", der als HEX Zahl über UART die Anzahl der Taktzyklen
ausgibt, die der Ausgang vom SRF04 high ist
(http://www.mil.ufl.edu/imdl/papers/IMDL_Report_Fall_02/theriault_steven/sonar.pdf).
Der Code ist ursprünglich für den AT Mega 163. Ich habe ihn (leicht
angepasst, Registernamen und so) auf einem AT Mega 16 ausprobiert.
Funktioniert zwar scheinbar (Das SRF04 rattert leise vor sich hin) ,
aber leider wird keine Hex Zahl auf meinem Terminalprogramm angezeigt.
Tabula Rasa. Ich habe den Trigger-Puls Eingang des SRF04 an PB0 des M16
angeschlossen, den Echo-Puls Ausgang an PD6 (ICP).

Für jeden Ratschlag bin ich dankbar,
Gruß,
Tilman

Hier der Theriault-Code:

.include "m16def.inc"

.def temp = r16
.def capturel = r1
.def captureh = r2
.def temp1 = r17
.def temp2 = r18
.def temp3 = r19

.org0
    jmp main

.org $24

main:
  ldi temp, high(ramend)  ;Stack Pointer Setup
  out SPH, temp
  ldi temp, low(ramend)
  out SPL,temp

;set data direction
  ldi temp, $01
  out DDRB, temp
  ldi temp, 0b10110000  ;pins 7,5,4 as outputs
  out DDRD, temp

;init UART
  ldi temp,0      ;setup UART baud rate 9600
  out UBRRH, temp
  ldi temp, 38
  out UBRRL, temp

  sbi UCSZ2,TXEN    ;enable UART transmit

;init T1
  ldi temp,0b10000010    ;prescaler clk/8
  out WGM12,temp      ;enable input noise filter
              ;falling edge input capture

mainloop:
  sbi PORTB,0      ;initiate sonar ping
  ldi temp,20      ;wait 10us

wait10us:
  dec temp
  brpl wait10us

  ldi temp,0b00100100    ;clear overflow flag 1
  out TIFR, temp      ;clear IC flag

  in capturel,TCNT1L    ;store timer1 value
  in captureh,TCNT1H

  cbi PORTB,0        ;Start sonar ping

chk_ic_flg:
  in temp,TIFR
  sbrs temp,ICF1      ;wait for IC flag
  rjmp chk_ic_flg

  in temp,ICR1l      ;load capture registers
  in temp1,ICR1h

  cp temp, capturel    ;test original time with new capture time
  cpc temp1,captureh

  brsh captSub      ;if new>original branch

;$FFFF - original time + new time
  ldi temp2,$ff      ;use temp3:temp2 for accumulator
  ldi temp3,$ff

  sub temp2, capturel    ;$FFFF - original time
  sbc temp3, captureh

  add temp2, temp      ; +new time
  adc temp3, temp1

  mov temp,temp2      ;move accumulator back to temp,temp1 to print via
UART
  mov temp1,temp3

  jmp printResults

captSub:
  sub temp,capturel    ;subtract 16bit time
  sbc temp1,captureh

printResults:
  push temp1        ;push capture time for UART
  rcall sendHEX

  push temp
  rcall sendHEX
  rcall sendEOL

  ldi temp,$4F    ;wait 10 ms
wait10ms:
  ldi temp1,$FF
wait10msa:
  dec temp1
  brne wait10msa
  dec temp
  brne wait10ms

  jmp mainloop


;Method to send hex number to UART
;gets number from stack
;stack is emptied after routine call
sendHEX:
;UART TX most significant nibble
  pop temp2    ;save return addr from stack
  pop temp3
  pop temp    ;pop off stack to temp reg
  push temp    ;push temp back to stack

  swap temp    ;make most sig nibble the least sig nibble

  andi temp,$0F  ;zero upper nibble

  cpi temp,$a    ;test for correcting a-f
  brmi addThirty1  ;between 0 and 9

  ldi temp1,7
  add temp,temp1  ;add 7 to temp

addThirty1:
  ldi temp1,$30
  add temp,temp1  ;add $30 to temp

waitTX1:
  sbis DOR,UDRE  ;wait until tx reg is empty
  rjmp waitTX1

  out UDR,temp  ;send data

;UART TX least significant nibble
  pop temp    ;pop off stack to temp

  andi temp,$0F  ;Zero most sig nibble

  cpi temp,$a    ;test for correcting a-f
  brmi addThirty  ;between 0 and 9

  ldi temp1,7
  add temp,temp1  ;add 7 to temp

addThirty:
  ldi temp1,$30
  add temp,temp1  ;add$30 to temp

waitTX:
  sbis DOR,UDRE  ;wait until tx reg is empty
  jmp waitTX

  out UDR,temp  ;send data

  push temp3    ;push return addr
  push temp2

  ret        ;return from subroutine

;Method to send EOL to UART
;
;
sendEOL:
  ldi temp,$a    ;send Line Feed

waitTX2:
  sbis DOR,UDRE  ;wait until tx reg is empty
  jmp waitTX2

  out UDR,temp  ;send data

  ldi temp,$d    ;send carriage return

waitTX3:
  sbis DOR,UDRE  ;wait until tx reg is empty
  jmp waitTX3

  out UDR,temp  ;send data

  ret


Das Programm sollte wenn möglich

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.