اذهب إلى المحتوى
  • 0

مشروع ultrasonic ب microcontroller

Ahmed Ebrahim11

السؤال

الان اعمل عل مشروع لقياس مسافات باستخدام سينسور ultrsonic ولكن دائما" ما يعطي قراءه صفر مع العلم قمنا بعمل محاكاه وقد عمل ولكن عند تجربته لم يعمل 

وهذا الكود

void display_digit(int n);

void display_number (int a);

char calc_dist(void);

int main(void){



    // Create Distance Variable (char: 0->255)

    unsigned char dist = 0;

    TRISD = 0x00;

    TRISA = 0x00;

    // Set PORTB To Be Output Port (All The 8 Pins)

    TRISB = 0x00;

    // Set PORTB To Be LOW For initial State

    PORTB = 0x00;

    // Set RC2 To Be Output Pin ( Trigger )

    TRISC &= ~(1<<2);

    PORTC &= ~(1<<2);

    // Set RC3 To Be Input Pin ( Echo )

    TRISC |= (1<<3);

    //--[ Configure Timer Module To Operate in Timer Mode ]--

    // Clear The Pre-Scaler Select Bits

    T1CON &= ~(1<<4);

    T1CON &= ~(1<<5);

    // Choose The Local Clock As Clock Source

    T1CON &=~(1<<1);

    // Write The System's Main Routine !

    while(1)

    {

        dist = calc_dist()/5;

        display_number(dist);

        if(dist==1)

        {PORTB = 0x01; delay_ms(100);}

        if(dist==2)

        {PORTB = 0x03; delay_ms(100);}

        if(dist==3)

        {PORTB = 0x07; delay_ms(100);}

        if(dist==4)

        {PORTB = 0x0F; delay_ms(100);}

        else

        {PORTB = 0x00; delay_ms(100);}

    }



    return 0;

}

void display_digit(int n )

{

switch (n){

        case 0 :

        PORTA = 0b0111111;

        break;

        case 1 :

        PORTA = 0b0000110;

        break;

        case 2 :

        PORTA = 0b1011011;

        break;

        case 3 :

        PORTA = 0b1001111;

        break;

        case 4 :

        PORTA = 0b1100110;

        break;

        case 5 :

        PORTA = 0b1101101;

        break;

        case 6 :

        PORTA = 0b1111101;

        break;

        case 7 :

        PORTA = 0b1000111;

        break;

        case 8 :

        PORTA = 0b1111111;

        break;

        case 9 :

        PORTA = 0b1101111;

        break;

        default :

            PORTA = 0b1111001;

            break ;

    }

     }

void display_number (int a)

{

int L;

int i = 0;

while(a>0){

    L = a % 10;

    display_digit(L);

    PORTD &= (1<<i);

    i = i + 1;

}

}

char calc_dist(void)

{

    unsigned char distance=0; // For Precision Critical Situations, You'll have to make it a ((Floating-Point)) variable

    TMR1L=0;

    TMR1H=0;

    PORTC |= (1<<2);

    delay_us(10);

    PORTC &= ~(1<<2);

    T1CON |= (1<<0);

    while(!(PORTC && (1<<3)));

    T1CON &= ~(1<<0);

    distance = ((TMR1L |(TMR1H<<8))/58.82); // Note: fractions will be discarded. But it's OK in our basic tutorial

    if(distance>=2 && distance <150)

         return distance;

    else



        return 0;

}

IMG-20211217-WA0106.jpg

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...