Форум myROBOT.ru » Шаг за шагом » С чего начать? » как связать avr и сонар

Страниц (1): [1]
 

1. begheener - 27 Ноября, 2007 - 23:04:45 - перейти к сообщению
объясните пожалуйста,
пины авра сигналят бинарно, а надо к SRF02 отправить шестнадцатеричную цифру. как быть?
Ламер
2. VCOM - 30 Ноября, 2007 - 14:06:34 - перейти к сообщению
Ну дак вроде просто все. Он умеет цепляться по шине I2C: http://www.robot-electronics.co.uk/htm/srf02techI2C.htm. Ну и по USART
http://www.robot-electronics.co.uk/htm/srf02techSer.htm. Режим работы вибирается ногой "Mode"-
0-USART (нога "Mode" повешена на землю)
1-I2C (нога "Mode" в воздухе, там внутренний пулап к +5 В)
Вот выбирай режим, дальше смотря чего выберешь то-же могу подсказать....
3. begheener - 30 Ноября, 2007 - 19:48:39 - перейти к сообщению
пасиба.
а сколько в среднем стоит I2C bus?
4. VCOM - 02 Декабря, 2007 - 17:38:44 - перейти к сообщению
Хитрый вопрос конечно, я даже растерялся от такого вопроса Улыбка Ну шина I2C это по большому счету 2 провода с подтяжкой 2 резисторами к +5 В (ну или от скольки там авр ваш питается) . Поэтому собсно главное чтобы ваш авр имел модуль i2c на борту. Обозначается как SCL SDA. Вот, ну к примеру все меги его имеют на борту... К примеру у MEGA8 в дип 28 i2c находится на ногах 28 (SCL) и 29 (SDA). Так что сколько ваш авр в среднем стоит, столько и i2c стоит. Подмигивающий
5. Admin - 02 Декабря, 2007 - 20:03:59 - перейти к сообщению
To VCOM

Спасибо за Ваш замечательный ответ!

Я тоже растерялся.Тащусь
6. begheener - 02 Декабря, 2007 - 23:24:55 - перейти к сообщению
ухтыы! как всё несильно хитро, оказываецо!
тока я нигде не нашёл примеров, как обращацо с рабами:
не могли бы вы подсказать, что именно я должен отправить через 2 ноги и в каком порядке, чтобы устройство с адресом 0xE0 получило 0х50?
Ламер Смущение
7. VCOM - 03 Декабря, 2007 - 13:08:51 - перейти к сообщению
Да запросто, вот для MEGA32под IAR:
#include <iom32.h>

#define I2CADR 0xE0

//------------------------------ ------ * I2C * -------------------------------- ------------
void TWI_init(char bitrate, char prescaler)
//sets bitrate and prescaler
{
TWAR = 0x60;
TWBR = bitrate;
//mask off the high prescaler bits (we only need the lower three bits)
TWSR = prescaler & 0x03;
}
//------------------------------ -------------------------------- ----------------------------
char TWI_action(char command)
//starts any TWI operation (not stop), waits for completion and returns the status code
//TWINT and TWEN are set by this function, so for a simple data transfer just use TWI_action(0);
{ //make sure command is good
TWCR = (command|(1<<TWINT)|(1<<TWEN));
//wait for TWINT to be set after operation has been completed
while(!(TWCR & (1<<TWINT)));
//return status code with prescaler bits masked to zero
return (TWSR & 0xF8);
}

//------------------------------ -------------------------------- ----------------------------
char TWI_start(void)
//uses TWI_action to generate a start condition, returns status code
{ //TWI_action writes the following command to TWCR: (1<<TWINT)|(1<<TWSTA)|(1<<TWEN)
return TWI_action(1<<TWSTA);
//return values should be 0x08 (start) or 0x10 (repeated start)
}
//------------------------------ -------------------------------- ----------------------------
void TWI_stop(void)
//generates stop condition
{ //as TWINT is not set after a stop condition, we can't use TWI_action here!
TWCR = ((1<<TWINT)|(1<<TWSTO)|(1<<TWEN));
//status code returned is 0xF8 (no specific operation)
}
//------------------------------ -------------------------------- ----------------------------
char TWI_write_data(char data)
//loads data into TWDR and transfers it. Works for slave addresses and normal data
//waits for completion and returns the status code.
{ //just write data to TWDR and transmit it
TWDR = data;
//we don't need any special bits in TWCR, just TWINT and TWEN. These are set by TWI_action.
return TWI_action(0);
//status code returned should be:
//0x18 (slave ACKed address)
//0x20 (no ACK after address)
//0x28 (data ACKed by slave)
//0x30 (no ACK after data transfer)
//0x38 (lost arbitration)
}
//------------------------------ -------------------------------- ----------------------------
char TWI_read_data(char put_ack)
{ //if an ACK is to returned to the transmitting device, set the TWEA bit
if(put_ack)
return(TWI_action(1<<TWEA));
//if no ACK (a NACK) has to be returned, just receive the data
else
return(TWI_action(0));
//status codes returned:
//0x38 (lost arbitration)
//0x40 (slave ACKed address)
//0x48 (no ACK after slave address)
//0x50 (AVR ACKed data)
//0x58 (no ACK after data transfer)
}
//------------------------------ -------------------------------- ----------------------------
void TWI_wait(char slave)
{
//send slave address until a slave ACKs it. Good for checking if the EEPROM
//has finished a write operation. Use carefully! If the wrong slave address
//is being waited for, this function will end in an infinite loop.
do
{
TWI_start();
}
while(TWI_write_data(slave) != 0x18);
TWI_stop();
}
//------------------------------ ------ * 24C128 * -------------------------------- ----------
char i2c_write(unsigned int address, unsigned char data)
{
char dummy;

//we need this for the first if()
dummy = TWI_start();
//if the start was successful, continue, otherwise return 1
if((dummy != 0x08) && (dummy != 0x10))
return TWSR;
//now send the I2CADR slave address together with the address bits 8..10 for page select
//address format:
//|bit7 |bit6 |bit5 |bit4 |bit3 |bit2 |bit1 |bit0 |
//| I2CADR | page select | R/W |
if(TWI_write_data(I2CADR) != 0x18)
return TWSR;
//now send the word address byte
if(TWI_write_data((char)(address>>8)) != 0x28)
return TWSR;
if(TWI_write_data((char)(address)) != 0x28)
return TWSR;
//now send the data byte
if(TWI_write_data(data) != 0x28)
return TWSR;
//if everything was OK, return zero.
return 0;
}
void main(void)
{
TWI_init(12, 0); //Инициализировали I2C на 368640 Hz/ при кварце14745600 Hz
i2c_write(0x00, 0x50);
//Вот здесь происходит запись в устройство с адресом I2CADR
//Внутри устройства данные пишутся по адресу 0x00
//0x50 собсно сами данные

}

Ну вот так как-то примерно Подмигивающий