Cảm biến siêu âm

Collapse
This is a sticky topic.
X
X
 
  • Giờ
  • Show
Clear All
new posts
  • fidel
    Nhập môn đệ tử
    • Nov 2010
    • 4

    #61
    Đây là code của mình, SRF05/mode1/Atmega128.



    #define F_CPU 14745600UL
    #define STOP_CLK 0x08 // Stop clock
    #define START_CLK 0x0A // Start clock with 1:8 prescaler CTC mode
    #define START_CLK_N 0x02 // Start clock running 1:8 prescaler in normal mode
    #include <avr/io.h>
    #include <string.h>
    #include <stdio.h>
    #include <util/delay.h>
    #include <stdint.h>
    #include "knihovnaLCD.c"

    float s;
    char buffer [50];
    void setup(void)
    {
    DDRA = 0x00; // Port A asIN
    DDRC = 0xFF; // Port C as OUT
    TCCR1A = 0x00; // Set timer up in CTC mode
    TCCR1B = 0x08;
    TWBR = 0x20; // 100MHz I2C clock frequency
    }
    void waitForTimer(void)
    {
    while(!(TIFR&0x10)); // wait for timer to set compare match flag
    TCCR1B = STOP_CLK; // Stop the timer
    }
    void startTimer(unsigned int time)
    {
    OCR1A = time; // Time set to count to
    TIFR = 0x10; // Clear timer campare match flag
    TCNT1 = 0x00; // Clear timer to zero
    TCCR1B = START_CLK; // Start timer running 1:8 prescaler
    }

    unsigned int getEcho(void)
    {
    unsigned int range;

    TCNT1 = 0x00; // Clear timer to zero
    TCCR1B = START_CLK_N;
    while(bit_is_clear(PINA,0) && TCNT1 < 0xC350) // Wait for echo pin to go high, this indicates the start of the incoming pulse
    {
    }
    if (TCNT1 > 0xC300) //Time over
    {
    TCCR1B = STOP_CLK;
    return(999);
    }
    TCCR1B = STOP_CLK;
    TCNT1 = 0x00; // Clear timer to zero
    TCCR1B = START_CLK_N; // Start timer running 1:8 prescaler in normal mode
    while(bit_is_set(PINA,0)); // Echo pin goes high
    { // Just wait
    }
    TCCR1B = STOP_CLK; // Echo pin has just fallen down. Stop the timer
    range = TCNT1/107; // Read back value in the timer counter register
    return(range);
    }

    int main(void)
    {
    setup();
    startTimer(0xFFFF);
    waitForTimer();
    LCD_Init();
    LCD_Clear();

    while (1)
    {
    PORTC=255;
    _delay_us(10);
    PORTC=0;
    s = getEcho();
    sprintf (buffer, "s =%u ", (int)s);
    LCD_Position(1 , 1);
    LCD_WriteString(buffer);
    _delay_ms(100);
    };
    }

    Comment

    • navy
      Đệ tử 2 túi
      • Jan 2007
      • 34

      #62
      Nguyên văn bởi fidel View Post
      Đây là code của mình, SRF05/mode1/Atmega128.



      #define F_CPU 14745600UL
      #define STOP_CLK 0x08 // Stop clock
      #define START_CLK 0x0A // Start clock with 1:8 prescaler CTC mode
      #define START_CLK_N 0x02 // Start clock running 1:8 prescaler in normal mode
      #include <avr/io.h>
      #include <string.h>
      #include <stdio.h>
      #include <util/delay.h>
      #include <stdint.h>
      #include "knihovnaLCD.c"

      float s;
      char buffer [50];
      void setup(void)
      {
      DDRA = 0x00; // Port A asIN
      DDRC = 0xFF; // Port C as OUT
      TCCR1A = 0x00; // Set timer up in CTC mode
      TCCR1B = 0x08;
      TWBR = 0x20; // 100MHz I2C clock frequency
      }
      void waitForTimer(void)
      {
      while(!(TIFR&0x10)); // wait for timer to set compare match flag
      TCCR1B = STOP_CLK; // Stop the timer
      }
      void startTimer(unsigned int time)
      {
      OCR1A = time; // Time set to count to
      TIFR = 0x10; // Clear timer campare match flag
      TCNT1 = 0x00; // Clear timer to zero
      TCCR1B = START_CLK; // Start timer running 1:8 prescaler
      }

      unsigned int getEcho(void)
      {
      unsigned int range;

      TCNT1 = 0x00; // Clear timer to zero
      TCCR1B = START_CLK_N;
      while(bit_is_clear(PINA,0) && TCNT1 < 0xC350) // Wait for echo pin to go high, this indicates the start of the incoming pulse
      {
      }
      if (TCNT1 > 0xC300) //Time over
      {
      TCCR1B = STOP_CLK;
      return(999);
      }
      TCCR1B = STOP_CLK;
      TCNT1 = 0x00; // Clear timer to zero
      TCCR1B = START_CLK_N; // Start timer running 1:8 prescaler in normal mode
      while(bit_is_set(PINA,0)); // Echo pin goes high
      { // Just wait
      }
      TCCR1B = STOP_CLK; // Echo pin has just fallen down. Stop the timer
      range = TCNT1/107; // Read back value in the timer counter register
      return(range);
      }

      int main(void)
      {
      setup();
      startTimer(0xFFFF);
      waitForTimer();
      LCD_Init();
      LCD_Clear();

      while (1)
      {
      PORTC=255;
      _delay_us(10);
      PORTC=0;
      s = getEcho();
      sprintf (buffer, "s =%u ", (int)s);
      LCD_Position(1 , 1);
      LCD_WriteString(buffer);
      _delay_ms(100);
      };
      }
      Không biết Topic này có chuyển sang Forum nào không mà không thấy tiếp tục? Bạn có thể cho mình địa chỉ mail để liên lạc hỏi về vấn đề này nhé? Mình đang quan tâm! Thanks!
      lanhnavy@gmail.com
      Mobile phone: 0974711766
      Automation

      Comment

      • spkt_kdd95
        Nhập môn đệ tử
        • May 2006
        • 2

        #63
        Mình đang muốn làm con robo dùng cảm biến siêu âm Sr04 để tìm vật cản, mà viết code hoài mà nó không chạy theo ý muốn mong các sư huynh chỉ giúp. Đây là code mình viết.

        ;================================================= =======
        ; Ten chuong trinh : Mach phat xung 10us-Dung Cho Cam Bien Sieu Am Hc-SR04
        ; Nguoi thuc hien : Duong Van Bong
        ; Ngay thuc hien : 04/03/2012
        ; Phien ban : 1.0
        ; Mo ta phan cung : Dung PIC16F84A - thach anh 16MHz
        ; : HC-SR04 giao tiep voi PORTB
        ; : RB0 - RB7 la cac chan output
        ;----------------------------------------------------------------
        ; Ngay hoan thanh : 04/03/2012
        ; Ngay kiem tra : 04/03/2012
        ; Nguoi kiem tra : Duong Van Bong
        ;----------------------------------------------------------------
        ; Chu thich : Dung Pic tao xung cho cam bien
        ; :
        ;================================================= =======
        TITLE "Mach phat xung 10us"
        PROCESSOR P16F84A
        INCLUDE <P16F84A.inc>
        __CONFIG _CP_OFF & _PWRTE_ON & _WDT_OFF &_HS_OSC
        ;================================================= ======
        ; Phan Dat Bien
        ;-------------------------------------
        ; Bien o bang 0
        ;-------------------------------------
        ORG 0x020
        Delay_reg1 EQU 0x020
        Delay_reg2 EQU 0x021
        Delay_reg3 EQU 0x022
        Delay_reg4 EQU 0x023
        Delay_reg5 EQU 0x024
        Rb1 EQU 1
        ;------------------------------------
        ; Bien o bang 1
        ;------------------------------------
        ORG 0x0A0
        ;------------------------------------
        ; Bien o bang 2
        ;------------------------------------
        ORG 0x120
        ;================================================= ========================
        ; Phâ`n chuong trình chính
        ;================================================= ========================
        ORG 0x0000
        GOTO MAIN
        ORG 0x0005
        MAIN
        BANKSEL TRISB ; bank select
        MOVLW b'00000010'
        MOVWF TRISB
        CLRF TRISA ; trisa = 00000000
        CLRF PORTA ; porta = output
        BCF STATUS,RP0
        LOOP BSF PORTB,0 ; rb0 = 1
        CALL DELAY_10US ; Delay times 10us
        BCF PORTB,0 ; Rb0=0
        CALL DELAY_1740US ; Delay times 1740us
        MOVFW PORTB
        BTFSS PORTB,Rb1
        CALL BONG
        BTFSC PORTB,Rb1
        GOTO $-1
        GOTO LOOP
        ;================================================= ============================
        ; Các chuong trình con
        ;================================================= ============================
        DELAY_10US
        MOVLW d'13'
        MOVWF Delay_reg1
        LOOP1 DECFSZ Delay_reg1
        GOTO LOOP1
        RETURN
        ;------------------------------------
        DELAY_1740US
        MOVLW d'3'
        MOVWF Delay_reg2
        MOVLW d'25'
        MOVWF Delay_reg3
        LOOP2 DECFSZ Delay_reg2
        GOTO LOOP3
        GOTO EXIT
        LOOP3 DECFSZ Delay_reg3
        GOTO LOOP3
        GOTO LOOP2
        EXIT
        RETURN
        ;------------------------------------
        DELAY_50MS
        MOVLW d'17'
        MOVWF Delay_reg4
        MOVLW d'232'
        MOVWF Delay_reg5
        LOOP4 DECFSZ Delay_reg4
        GOTO LOOP5
        GOTO EXIT_1
        LOOP5 DECFSZ Delay_reg5
        GOTO LOOP5
        GOTO LOOP4
        EXIT_1
        RETURN
        ;------------------------------------
        BONG
        BSF PORTA,0
        CALL DELAY_50MS
        BCF PORTA,0
        RETURN
        ;------------------------------------
        END
        ;================================================= ==================
        duongvanbong@yahoo.com
        Mong các sư huynh chỉ giúp

        Comment

        • fire_dragon
          Nhập môn đệ tử
          • Aug 2012
          • 3

          #64
          Hiện giờ, anh Hùng có bán loại cảm biến siêu âm SRF05 còn mới.
          Giá bộ cảm biến 550000 VND. Liên hệ anh Hùng qua email:
          hungmn30@yahoo.com

          Comment

          • matong999
            Nhập môn đệ tử
            • Mar 2014
            • 1

            #65
            cảm biến này hay thật
            hạt điều mật ong

            Comment

            Working...