I2C na atmega128

Diskuze a poradna o programátorech a programování různých obvodů

Moderátor: Moderátoři

Odpovědět
Zpráva
Autor
Uživatelský avatar
endthe
Příspěvky: 56
Registrován: 07 bře 2009, 01:00
Bydliště: Brno
Kontaktovat uživatele:

I2C na atmega128

#1 Příspěvek od endthe »

Zdravim vas a prosim o radu

snazim se rozjet ultrazvukovy dalkomer pres I2C na procaku atmega128.

info o senzoru: http://www.robot-electronics.co.uk/htm/srf02techI2C.htm

pouzivam kod:

Kód: Vybrat vše

i2c_init();
i2c_start_wait(0xE0+I2C_WRITE);
i2c_start(0xE0);
i2c_write(0x51);	 
i2c_stop();
i2c_start(0xE0);
i2c_start_wait(0xE0+I2C_WRITE);
i2c_rep_start(0xE0+I2C_READ);
i2c_stop();
a pouzite knihovny prikladam v prilohach

poradi mi nekdo co s tim.
problem je ze mi to porad nechce komunikovat

dekuji predem za rychle rady
Přílohy
twi_lib_h.txt
(5.52 KiB) Staženo 135 x
twi_lib_c.txt
(6.07 KiB) Staženo 98 x

Andrea
Příspěvky: 9340
Registrován: 07 zář 2007, 02:00

#2 Příspěvek od Andrea »

Proč posíláš za sebou dva starty a proč při tom čtení nic nečteš?

V popisu píšou, že komunikace probíhá jako u EEPROM 24xx. Takže je potřeba poslat start, adresu čipu+READ/WRITE, pak adresu registru a pak buď příkaz (když se zpisuje příkaz) nebo opakovaný start s READ a pak přečíst obsah registru. Zakončit to stopem. Po spuštění měření vzdálenosti trvá 70ms než je k dispozici změřená vzdálenost.

Příklad komunikace s EEPROM 24xx je i v tom Hčkovém souboru.

Uživatelský avatar
endthe
Příspěvky: 56
Registrován: 07 bře 2009, 01:00
Bydliště: Brno
Kontaktovat uživatele:

#3 Příspěvek od endthe »

nakonec sem to poresil takto:

Kód: Vybrat vše

// ******************************************
#include "main.h"
#include <avr/interrupt.h>
#include <inttypes.h>                                            // these four library headers are part of WinAVR compiler
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/delay.h>
#include "lcd_led_matrix__lcd.h"
#include "delay_1ms_u16.h"

volatile u08 key_counter = 0;
volatile u08 key = 0;
volatile u08 old_key = 0;
// ******************************************
#define DevSRF02  0xE0


void setup(void);
void i2c_transmit(char,char,char);

char i2c_read(char,char);
char s[21];





// ******************************************
SIGNAL(SIG_OUTPUT_COMPARE0)		// signal handler for Timer/Counter-0 compare match
{
	u08 tmp;
	
	if (!lcd_port_locked()) {	// test if no LCD write operation is in progress
		LCD_DATA_DDR = 0x0F;
		LCD_DATA_PORT = (~(1 << key_counter) & 0x0F);
		_delay_loop_2(10e-6 * F_CPU);		// 10us
		tmp = ~LCD_DATA_PIN;
		LCD_DATA_DDR = 0xFF;
		if ((tmp & 0xF0) != 0) {
			if (tmp != old_key) {
				old_key = tmp;
				key = tmp;
				key_counter = 0;
			}
		}
		else {
			key_counter++;
			if (key_counter >= 4) {
				key_counter = 0;
				old_key = 0;
			}
		}
	}
}
// ******************************************
void init(void)
{
	// disable JTAG On-chip debug system
	MCUCSR = 0x80;
	MCUCSR = 0x80;

	// Timer/counter-0 .. CTC mode, prescaler=64, top=230 => 1ms
	TIMSK = (1<<OCIE0);
	TCNT0 = 0;
	OCR0  = 230;
	TCCR0 = (1<<WGM01) | (1<<CS02) | (1<<CS00);

	sei();
}

void main(void)
{
char x;
unsigned int range,angle;
long delay;
u08 line = 0;
setup();

	init();
	delay_1ms_u16(20);
	init_lcd();
	clear_disp();
	put_string_xy(3,0,"I2C TESTER");
	//put_string_xy(3,1,"ULTRASONIC");
 	_delay_ms(2000);
while(1){
      for(delay=0; delay<30000L; delay++);                       // just a refresh rate delay
      i2c_transmit(0xE0,0,0x51);                                 // srf02 ping command, result in cm
		_delay_ms(100);
	  range = i2c_read(0xE0,2) <<8;                              // read srf02 range, high byte
      range += i2c_read(0xE0,3);                                 // read srf02 range, low byte
      sprintf(s,"SRF02 = %d cm",range);     // fills char string s with srf02 line text and data
 		//clear_disp();				
		put_string_xy(0, 0, s);
		cursor_disp_ctrl(0x04);
		//_delay_ms(20);
 }
}

char i2c_read(char address, char reg){
char read_data = 0;

   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 address of i2c device 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;

}

void i2c_transmit(char address, char reg, 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;
   TWCR = 0x84;                                                  // transmit
   while(!(TWCR & 0x80));                                        // wait for confirmation of transmit
   TWDR = data;
   TWCR = 0x84;                                                  // transmit
   while(!(TWCR & 0x80));                                        // wait for confirmation of transmit
   TWCR = 0x94;                                                  // stop bit
}

void setup(void)
{
   TWBR = 12;   //32                                                 // 100khz i2c bus speed
}



Andrea
Příspěvky: 9340
Registrován: 07 zář 2007, 02:00

#4 Příspěvek od Andrea »

Prostě jsi to pajcnul.

Uživatelský avatar
endthe
Příspěvky: 56
Registrován: 07 bře 2009, 01:00
Bydliště: Brno
Kontaktovat uživatele:

#5 Příspěvek od endthe »

jo priznavam tu komunikaci jo jelikoz sem to potreboval rychle rozjet
jen to jaksi hapruje

Odpovědět

Zpět na „Programování PIC, ATMEL, EEPROM a dalších obvodů“