6月20日

距离期末考试还有十天!!!!!!!!!!
感觉到一股杀气。。。。。。
超声波:
额,,,
有四个口:Vcc/Trig/Echo/GND
当然啦,Vcc 和 GND 依旧是电源两枚
Trig/Echo 是输入输出。
输入输出:
(我是不是回了太多行)
输入就像在听别人说,默默记下来。
输出就像在对别人说,要传递信息。

恩恩
Trig/Echo要连GPIO口
SS send signal //输出 接Echo
RS receive signal //输出 接Trig
后面要加全称


// input.c
//
// Example program for bcm2835 library
// Blinks a pin on an off every 0.5 secs
//
// After installing bcm2835, you can build this
// with something like:
// make or gcc -o led led.c -lbcm2835
// sudo ./led
/*
             define from bcm2835.h                       define from Board DVK511
                 3.3V | | 5V               ->                 3.3V | | 5V
    RPI_V2_GPIO_P1_03 | | 5V               ->                  SDA | | 5V 
    RPI_V2_GPIO_P1_05 | | GND              ->                  SCL | | GND
       RPI_GPIO_P1_07 | | RPI_GPIO_P1_08   ->                  IO7 | | TX
                  GND | | RPI_GPIO_P1_10   ->                  GND | | RX
       RPI_GPIO_P1_11 | | RPI_GPIO_P1_12   ->                  IO0 | | IO1
    RPI_V2_GPIO_P1_13 | | GND              ->                  IO2 | | GND
       RPI_GPIO_P1_15 | | RPI_GPIO_P1_16   ->                  IO3 | | IO4
                  VCC | | RPI_GPIO_P1_18   ->                  VCC | | IO5
       RPI_GPIO_P1_19 | | GND              ->                 MOSI | | GND
       RPI_GPIO_P1_21 | | RPI_GPIO_P1_22   ->                 MISO | | IO6
       RPI_GPIO_P1_23 | | RPI_GPIO_P1_24   ->                  SCK | | CE0
                  GND | | RPI_GPIO_P1_26   ->                  GND | | CE1

::if your raspberry Pi is version 1 or rev 1 or rev A 加全称
RPI_V2_GPIO_P1_03->RPI_GPIO_P1_03
RPI_V2_GPIO_P1_05->RPI_GPIO_P1_05
RPI_V2_GPIO_P1_13->RPI_GPIO_P1_13
::
*/
#include <bcm2835.h>
#include <stdio.h>
#include <sys/time.h>

#define SS    RPI_V2_GPIO_P1_13      //send signal 13输出
#define RS  RPI_GPIO_P1_12        //receive signal 12输入
int getdistance()
{
    bcm2835_gpio_write(SS,LOW);
    bcm2835_delay(100);

    bcm2835_gpio_write(SS,HIGH);
    bcm2835_delayMicroseconds(15);
    bcm2835_gpio_write(SS,LOW);

    while(bcm2835_gpio_lev(RS)==0)//回波开始计时
    {

        bcm2835_delayMicroseconds(1);
    }

    struct timeval tv_begin, tv_end;
    gettimeofday(&tv_begin, NULL);
    int count=0;

    //最远探测约5米,无回波返回
    while(bcm2835_gpio_lev(RS)==1 && count<=30000)
    {

        bcm2835_delayMicroseconds(1);
        count++;
    }


    if(count>=30000)//回波若太长直接退出
        return 0;
    gettimeofday(&tv_end, NULL);//取当前时间
    int timebetween;
    if(tv_end.tv_sec>tv_begin.tv_sec)//取时间差
    {

        timebetween=tv_end.tv_usec+1000000-tv_begin.tv_usec;
    }
    else
        timebetween=tv_end.tv_usec-tv_begin.tv_usec;

    int distance=timebetween*17150/1000000;
    return distance;
    //bcm2835_delay(500);
}
int main(int argc, char **argv)
{
    uint8_t value,i;

    if (!bcm2835_init())//初始化
    {
        printf("init error\n");
        return 1;
    }

    bcm2835_gpio_fsel(SS, BCM2835_GPIO_FSEL_OUTP);//要输出
    bcm2835_gpio_fsel(RS, BCM2835_GPIO_FSEL_INPT);//要输入


    while (1)//输出打印
    {
        printf("distance %d cm\n",    getdistance());
        bcm2835_delay(1000);
    }
    bcm2835_close();
    return 0;
}
距离期末考试还有十天!!!!!!!!!!
感觉到一股杀气。。。。。。
超声波:
额,,,
有四个口:Vcc/Trig/Echo/GND
当然啦,Vcc 和 GND 依旧是电源两枚
Trig/Echo 是输入输出。
输入输出:
(我是不是回了太多行)
输入就像在听别人说,默默记下来。
输出就像在对别人说,要传递信息。

恩恩
Trig/Echo要连GPIO口
SS send signal //输出 接Echo
RS receive signal //输出 接Trig
后面要加全称


// input.c
//
// Example program for bcm2835 library
// Blinks a pin on an off every 0.5 secs
//
// After installing bcm2835, you can build this
// with something like:
// make or gcc -o led led.c -lbcm2835
// sudo ./led
/*
             define from bcm2835.h                       define from Board DVK511
                 3.3V | | 5V               ->                 3.3V | | 5V
    RPI_V2_GPIO_P1_03 | | 5V               ->                  SDA | | 5V 
    RPI_V2_GPIO_P1_05 | | GND              ->                  SCL | | GND
       RPI_GPIO_P1_07 | | RPI_GPIO_P1_08   ->                  IO7 | | TX
                  GND | | RPI_GPIO_P1_10   ->                  GND | | RX
       RPI_GPIO_P1_11 | | RPI_GPIO_P1_12   ->                  IO0 | | IO1
    RPI_V2_GPIO_P1_13 | | GND              ->                  IO2 | | GND
       RPI_GPIO_P1_15 | | RPI_GPIO_P1_16   ->                  IO3 | | IO4
                  VCC | | RPI_GPIO_P1_18   ->                  VCC | | IO5
       RPI_GPIO_P1_19 | | GND              ->                 MOSI | | GND
       RPI_GPIO_P1_21 | | RPI_GPIO_P1_22   ->                 MISO | | IO6
       RPI_GPIO_P1_23 | | RPI_GPIO_P1_24   ->                  SCK | | CE0
                  GND | | RPI_GPIO_P1_26   ->                  GND | | CE1

::if your raspberry Pi is version 1 or rev 1 or rev A 加全称
RPI_V2_GPIO_P1_03->RPI_GPIO_P1_03
RPI_V2_GPIO_P1_05->RPI_GPIO_P1_05
RPI_V2_GPIO_P1_13->RPI_GPIO_P1_13
::
*/
#include <bcm2835.h>
#include <stdio.h>
#include <sys/time.h>

#define SS    RPI_V2_GPIO_P1_13      //send signal 13输出
#define RS  RPI_GPIO_P1_12        //receive signal 12输入
int getdistance()
{
    bcm2835_gpio_write(SS,LOW);
    bcm2835_delay(100);

    bcm2835_gpio_write(SS,HIGH);
    bcm2835_delayMicroseconds(15);
    bcm2835_gpio_write(SS,LOW);

    while(bcm2835_gpio_lev(RS)==0)//回波开始计时
    {

        bcm2835_delayMicroseconds(1);
    }

    struct timeval tv_begin, tv_end;
    gettimeofday(&tv_begin, NULL);
    int count=0;

    //最远探测约5米,无回波返回
    while(bcm2835_gpio_lev(RS)==1 && count<=30000)
    {

        bcm2835_delayMicroseconds(1);
        count++;
    }


    if(count>=30000)//回波若太长直接退出
        return 0;
    gettimeofday(&tv_end, NULL);//取当前时间
    int timebetween;
    if(tv_end.tv_sec>tv_begin.tv_sec)//取时间差
    {

        timebetween=tv_end.tv_usec+1000000-tv_begin.tv_usec;
    }
    else
        timebetween=tv_end.tv_usec-tv_begin.tv_usec;

    int distance=timebetween*17150/1000000;
    return distance;
    //bcm2835_delay(500);
}
int main(int argc, char **argv)
{
    uint8_t value,i;

    if (!bcm2835_init())//初始化
    {
        printf("init error\n");
        return 1;
    }

    bcm2835_gpio_fsel(SS, BCM2835_GPIO_FSEL_OUTP);//要输出
    bcm2835_gpio_fsel(RS, BCM2835_GPIO_FSEL_INPT);//要输入


    while (1)//输出打印
    {
        printf("distance %d cm\n",    getdistance());
        bcm2835_delay(1000);
    }
    bcm2835_close();
    return 0;
} 距离期末考试还有十天!!!!!!!!!!
感觉到一股杀气。。。。。。
超声波:
额,,,
有四个口:Vcc/Trig/Echo/GND
当然啦,Vcc 和 GND 依旧是电源两枚
Trig/Echo 是输入输出。
输入输出:
(我是不是回了太多行)
输入就像在听别人说,默默记下来。
输出就像在对别人说,要传递信息。

恩恩
Trig/Echo要连GPIO口
SS send signal //输出 接Echo
RS receive signal //输出 接Trig
后面要加全称


// input.c
//
// Example program for bcm2835 library
// Blinks a pin on an off every 0.5 secs
//
// After installing bcm2835, you can build this
// with something like:
// make or gcc -o led led.c -lbcm2835
// sudo ./led
/*
             define from bcm2835.h                       define from Board DVK511
                 3.3V | | 5V               ->                 3.3V | | 5V
    RPI_V2_GPIO_P1_03 | | 5V               ->                  SDA | | 5V 
    RPI_V2_GPIO_P1_05 | | GND              ->                  SCL | | GND
       RPI_GPIO_P1_07 | | RPI_GPIO_P1_08   ->                  IO7 | | TX
                  GND | | RPI_GPIO_P1_10   ->                  GND | | RX
       RPI_GPIO_P1_11 | | RPI_GPIO_P1_12   ->                  IO0 | | IO1
    RPI_V2_GPIO_P1_13 | | GND              ->                  IO2 | | GND
       RPI_GPIO_P1_15 | | RPI_GPIO_P1_16   ->                  IO3 | | IO4
                  VCC | | RPI_GPIO_P1_18   ->                  VCC | | IO5
       RPI_GPIO_P1_19 | | GND              ->                 MOSI | | GND
       RPI_GPIO_P1_21 | | RPI_GPIO_P1_22   ->                 MISO | | IO6
       RPI_GPIO_P1_23 | | RPI_GPIO_P1_24   ->                  SCK | | CE0
                  GND | | RPI_GPIO_P1_26   ->                  GND | | CE1

::if your raspberry Pi is version 1 or rev 1 or rev A 加全称
RPI_V2_GPIO_P1_03->RPI_GPIO_P1_03
RPI_V2_GPIO_P1_05->RPI_GPIO_P1_05
RPI_V2_GPIO_P1_13->RPI_GPIO_P1_13
::
*/
#include <bcm2835.h>
#include <stdio.h>
#include <sys/time.h>

#define SS    RPI_V2_GPIO_P1_13      //send signal 13输出
#define RS  RPI_GPIO_P1_12        //receive signal 12输入
int getdistance()
{
    bcm2835_gpio_write(SS,LOW);
    bcm2835_delay(100);

    bcm2835_gpio_write(SS,HIGH);
    bcm2835_delayMicroseconds(15);
    bcm2835_gpio_write(SS,LOW);

    while(bcm2835_gpio_lev(RS)==0)//回波开始计时
    {

        bcm2835_delayMicroseconds(1);
    }

    struct timeval tv_begin, tv_end;
    gettimeofday(&tv_begin, NULL);
    int count=0;

    //最远探测约5米,无回波返回
    while(bcm2835_gpio_lev(RS)==1 && count<=30000)
    {

        bcm2835_delayMicroseconds(1);
        count++;
    }


    if(count>=30000)//回波若太长直接退出
        return 0;
    gettimeofday(&tv_end, NULL);//取当前时间
    int timebetween;
    if(tv_end.tv_sec>tv_begin.tv_sec)//取时间差
    {

        timebetween=tv_end.tv_usec+1000000-tv_begin.tv_usec;
    }
    else
        timebetween=tv_end.tv_usec-tv_begin.tv_usec;

    int distance=timebetween*17150/1000000;
    return distance;
    //bcm2835_delay(500);
}
int main(int argc, char **argv)
{
    uint8_t value,i;

    if (!bcm2835_init())//初始化
    {
        printf("init error\n");
        return 1;
    }

    bcm2835_gpio_fsel(SS, BCM2835_GPIO_FSEL_OUTP);//要输出
    bcm2835_gpio_fsel(RS, BCM2835_GPIO_FSEL_INPT);//要输入


    while (1)//输出打印
    {
        printf("distance %d cm\n",    getdistance());
        bcm2835_delay(1000);
    }
    bcm2835_close();
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值