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
}