📄 sonar.c
字号:
// sonar.c - a simple test to check if i2c works
// this program requires a devantech ultrasound sensor and a Cerebellum
// When this program is running, every time you feed Cerebellum a byte
// on the serial port (e.g. hit the space bar) it will request a sonar
// range reading from the Devantech sonar and return that value to you
// via serial. You must have your computer connected to Cerebellum at 115200
// baud to try this...
#include "cerebv41.h"
#include "cerebv41.c"
void main()
{
char command;
char data0;
init_cerebellum(); // configure cerebellum
ser_init(SER_115200); // start up serial port handling
i2c_init(); // initialize i2c handling
// Slight start up delay to allow Cerebellum to set everything in initialization
delay_ms(100);
ser_putstring("Setting up i2c master...\r\n");
delay_ms(250);
while(1)
{
command = ser_rcv_nb();
if( command != 0 ) // if there is a byte to be read on serial line //
{
// Below is the sequence of i2c communications to ask the sonar
// to perform a ping then give us a 1-byte value for the range back
i2c_start();
i2c_send(0xe0); // Device Address
i2c_send(0); // Register Addr
i2c_send(0x51); // Command
i2c_stop();
delay_ms(200);
i2c_start();
i2c_send(0xe0); // Device Address
i2c_send(0x03); // Send Register Addr to read from
i2c_restart();
i2c_send(0xe1); //Device Address (+1 because we want to read)
data0 = i2c_receive(); // Receive the 1 data byte from the sonar
i2c_nak(); //No ack;
i2c_stop();
ser_putstring("Value is:");
ser_writechar(data0);
ser_putstring("\r\n");
} // end if (command != 0) //
delay_ms(2);
} // end while(1) //
} // end main() //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -