Mam taki kod (głównie skopiowany z TEJ strony). Chcę się skomunikować z modułem MPU6050.
main.cpp:
#ifndef __AVR_ATmega8__
#define __AVR_ATmega8__
#endif
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include "twi_master.h";
#define MPU6050_ADDR 0x68
/* MPU6050 register address */
#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define PWR_MGMT_1 0x6B
//////////////////////////////////////////////////////////////////////////////////
void print_char(char letter)
{
if (bit_is_set(UCSRA, UDRE))
{
_delay_ms(10);
UDR = letter;
loop_until_bit_is_set(UCSRA, TXC);
}
}
void print_newLine()
{
UDR = 0x0D;
UDR = 0x0A;
}
void print_string(char string[32])
{
for (int i = 0; i < 32; i++)
{
print_char(string[i]);
}
}
void uartInit()
{
UBRRL = 51; // 9600 8mhz
UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
UCSRC = (1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0);
TCCR1A |= (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TCCR1B |= (1 << WGM13) | (1 << WGM12) | (1 << CS11) | (1 << CS10);
ICR1 = 2499; //(F_CPU/64UL/50UL)-1;
DDRB |= (1 << PB1) | (1 << PB2);
OCR1A = 81;
// print_char('k');
// print_newLine();
}
////////////////////////////////////////////////////////////////////////////////////
typedef struct
{
float x;
float y;
float z;
} mpu_data_t;
void ERROR_CHECK(ret_code_t error_code);
void mpu_init(void);
void mpu_get_accel_raw(mpu_data_t *mpu_data);
void mpu_get_accel(mpu_data_t *mpu_data);
void ERROR_CHECK(ret_code_t error_code)
{
if (error_code != SUCCESS)
{
/* Print error code and loop indefinitely until reset */
print_string("ERR");
while (1)
; // loop indefinitely
}
}
void mpu_init(void)
{
ret_code_t error_code;
print_string("0 to PWR_MGMT_1");
uint8_t data[2] = {PWR_MGMT_1, 0};
error_code = tw_master_transmit(MPU6050_ADDR, data, sizeof(data), false);
ERROR_CHECK(error_code);
}
void mpu_get_accel_raw(mpu_data_t *mpu_data)
{
ret_code_t error_code;
/* 2 registers for each of accel x, y and z data */
uint8_t data[6];
data[0] = ACCEL_XOUT_H;
error_code = tw_master_transmit(MPU6050_ADDR, data, 1, true);
ERROR_CHECK(error_code);
error_code = tw_master_receive(MPU6050_ADDR, data, sizeof(data));
ERROR_CHECK(error_code);
/* Default accel config +/- 2g */
mpu_data->x = (int16_t)(data[0] << 8 | data[1]) / 16384.0;
mpu_data->y = (int16_t)(data[2] << 8 | data[3]) / 16384.0;
mpu_data->z = (int16_t)(data[4] << 8 | data[5]) / 16384.0;
}
void mpu_get_accel(mpu_data_t *mpu_data)
{
mpu_get_accel_raw(mpu_data);
mpu_data->x = mpu_data->x * 9.81;
mpu_data->y = mpu_data->y * 9.81;
mpu_data->z = mpu_data->z * 9.81;
}
int main(void)
{
uartInit();
cli();
print_string("INIT");
/* Initialize project configuration */
tw_init(TW_FREQ_400K, true); // set I2C Frequency, enable internal pull-up
mpu_init();
mpu_data_t accel;
print_string("START");
while (1)
{
print_string("Read data:");
mpu_get_accel(&accel);
char str[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
sprintf(str, "%d", accel.x);
print_string(str);
sprintf(str, "%d", accel.y);
print_string(str);
sprintf(str, "%d", accel.z);
print_string(str);
_delay_ms(200);
}
}
Makefile:
all: main.hex program clean
main.o: main.cpp twi_master.h twi_master.c
avr-gcc -Wall -Os -mmcu=atmega8 -c $< -o $@
main.elf: main.o
avr-gcc -Wall -Os -mmcu=atmega8 -o main.elf main.o
main.hex: main.elf
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size --format=avr --mcu=atmega8 main.elf
program:
avrdude -p atmega8 -c usbasp -P usb -V -U flash:w:"W:TWI\main.hex":i
.PHONY: clean
clean:
rm main.o main.elf
Pliki twi_master.c i twi_master.h nie zmienione.
Przy uruchomieniu make'a pojawiają się błędy:
PS W:\TWI> make
avr-gcc -Wall -Os -mmcu=atmega8 -o main.elf main.o
main.o: In function `mpu_init()':
main.cpp:(.text+0xa8): undefined reference to `tw_master_transmit(unsigned char, unsigned char*, unsigned char, bool)'
main.o: In function `mpu_get_accel_raw(mpu_data_t*)':
main.cpp:(.text+0xe0): undefined reference to `tw_master_transmit(unsigned char, unsigned char*, unsigned char, bool)'
main.cpp:(.text+0xea): undefined reference to `tw_master_receive(unsigned char, unsigned char*, unsigned char)'
main.o: In function `main':
main.cpp:(.text+0x220): undefined reference to `tw_init(twi_freq_mode_t, bool)'
make: *** [main.elf] Error 1
Nie mam pomysłu co może być nie tak, proszę o pomoc.