#include "sonarset.h"
/**********************************超声波操作**************************************/
//从指定超声波的指定寄存器读取数据
unsigned char i2c_read(unsigned char address, unsigned char reg)
{
unsigned char read_data = 0;
//按照i2c协议
TWCR = 0xA4; // send a start bit on i2c bus
while(!(TWCR & 0x80)); // wait for confirmation of transmit
TWDR = address; // load address of i2c device
TWCR = 0x84; // transmit
while(!(TWCR & 0x80)); // wait for confirmation of transmit
TWDR = reg; // send register number to read from
TWCR = 0x84; // transmit
while(!(TWCR & 0x80)); // wait for confirmation of transmit
TWCR = 0xA4; // send repeated start bit
while(!(TWCR & 0x80)); // wait for confirmation of transmit
TWDR = address+1; // transmit i2c address with readbit set
TWCR = 0xC4; // clear transmit interupt flag
while(!(TWCR & 0x80)); // wait for confirmation of transmit
TWCR = 0x84; // transmit, nack (last byte request)
while(!(TWCR & 0x80)); // wait for confirmation of transmit
read_data = TWDR; // and grab the target data
TWCR = 0x94; // send a stop bit on i2c bus
return read_data;
}
//向指定超声波的指定寄存器写入命令字
//参数依次为:I2C地址,设备寄存器号(0-5),写入的一个字节数据
void i2c_transmit(unsigned char address, unsigned char reg, unsigned char data)
{
TWCR = 0xA4; // send a start bit on i2c bus
while(!(TWCR & 0x80)); // wait for confirmation of transmit
TWDR = address; // load address of i2c device
TWCR = 0x84; // transmit
while(!(TWCR & 0x80)); // wait for confirmation of transmit
TWDR = reg; // load number of register
TWCR = 0x84; // transmit
while(!(TWCR & 0x80)); // wait for confirmation of transmit
TWDR = data; // load data to transmit
TWCR = 0x84; // transmit
while(!(TWCR & 0x80)); // wait for confirmation of transmit
TWCR = 0x94; // stop bit
}
//将地址值为source的超声波改为destination
//新设备的初始地址是确定的,把某个超声波设备连接后,写入唯一的目标地址。I2C总线上最多可以有16个设备,地址从0xE0-oxFE.
//该地址中,最后2位标示读或写,0标示读。
void addr_change(unsigned char source,unsigned char destination)
{
i2c_transmit(source, 0, 0xA0);
i2c_transmit(source, 0, 0xAA);
i2c_transmit(source, 0, 0xA5);
i2c_transmit(source, 0, destination);
}
//超声波的速度初始化
void i2c_init(void)
{
TWBR = 16;//TWBR为寄存器,初始化I2C的通信速度
}
void portinit(void)
{
PORTB=0XFF;
DDRB=0XFF;
}
void timerinit(void)
{
TCCR1A=0Xa2;
TCCR1B=0X19;//快速pwm,比较区配是oc0置位
ICR1=0X0019;//设置top值,决定pwm频率,此时为8mhz/32=250khz
}
void init_device(void)
{
portinit();
timerinit();
MCUCR = 0x00;
// TIMSK = 0x03; ///
}
void DelayMs(int ms)
{ int i;
for(i=0;i<ms;i++)
_delay_loop_2(8*250);
}
void main(void)
{
unsigned char i,j,m,n;
unsigned int length0,length1,length2,length4;
i2c_init(); //设定波特率
init_device();
OCR1A=0x000C; //不得超过icr1值,表示占空比
OCR1B=0x000C;
//addr_change(ADDR_0,ADDR_4); //将sonar地址改为0xE2
for(i=0;i<15;i++)
{
DelayMs(5000);
}
OCR1B=0X000E;
//i2c_transmit(ADDR_0,0,0X51); //向0号寄存器写入命令值0x51,测量单位为厘米
//i2c_transmit(ADDR_1,0,0X51);
do
{
i2c_transmit(ADDR_0,0,0X51);
i2c_transmit(ADDR_1,0,0X51);
while(0xff==i2c_read(ADDR_0,0)); //通过读取0号寄存器的值是否为0xff判断发送超声波是否结束
i=i2c_read(ADDR_0,2); // 读取高8位值
length0=0|i; //扩充为16位
length0=i<<8; // 左移八位
i=i2c_read(ADDR_0,3); //读低8位值
length0|=i; //扩充为16位
/*length0=i2c_read(ADDR_0,2);
length0=length0<<8;
length0+=i2c_read(ADDR_0,3);*/
/*m=i2c_read(ADDR_0,4);
length2=0|m;
length2=m<<8;
m=i2c_read(ADDR_0,5);
length2|=m;*/
while(0xff==i2c_read(ADDR_1,0));
j=i2c_read(ADDR_1,2); // 读取高8位值
length1=0|j; //扩充为16位
length1=j<<8; // 左移八位
j=i2c_read(ADDR_1,3); //读低8位值
length1|=j; //扩充为16位
/*n=i2c_read(ADDR_1,4);
length4=0|n;
length4=n<<8;
n=i2c_read(ADDR_1,5);
length4|=n;
while(length0>=40||length1>=40);
//OCR1A=0x000f; //不得超过icr1值,表示占空比
//OCR1B=0x0011; //同上
//DelayMs(5000);
//OCR1B=0X0011;
/*if(length0<=40||length1<=40);
{
OCR1A=0x000c;
OCR1B=0x000c;
}
*/
}
while(length0>=50||length1>=50);
OCR1A=0x000c;
OCR1B=0X000c;
while(1);
}