距离期末考试还有十天!!!!!!!!!!
感觉到一股杀气。。。。。。
超声波:
额,,,
有四个口: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;
}