This code consists of twi, usart and gpio. For AT32UC3B0512.
/** * \file * * \brief Empty user application template * */ /** * \mainpage User Application template doxygen documentation * * \par Empty user application template * * Bare minimum empty user application template * * \par Content * * -# Include the ASF header files (through asf.h) * -# "Insert system clock initialization code here" comment * -# Minimal main function that starts with a call to board_init() * -# "Insert application code here" comment * */ /* * Include header files for all drivers that have been imported from * Atmel Software Framework (ASF). */ /* * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> */ #include <avr32/io.h> #include <asf.h> #define USART_SERIAL &AVR32_USART0 #define USART_SERIAL_BAUDRATE 9600 #define USART_SERIAL_CHAR_LENGTH 8 #define USART_SERIAL_PARITY USART_NO_PARITY #define USART_SERIAL_STOP_BIT USART_1_STOPBIT void twi_init(void) { twi_master_options_t opt = { .speed = 50000, .chip = 0x68 }; static const gpio_map_t twi_gpio = { {9,0}, {10,0} }; gpio_enable_module(twi_gpio, sizeof(twi_gpio) / sizeof(twi_gpio[0])); twi_master_setup(&AVR32_TWI, &opt); } void RS232_init(void) { static usart_serial_options_t usart_options = { .baudrate = USART_SERIAL_BAUDRATE, .charlength = USART_SERIAL_CHAR_LENGTH, .paritytype = USART_SERIAL_PARITY, .stopbits = USART_SERIAL_STOP_BIT }; static const gpio_map_t rs232_gpio = { {18,0}, {19,0} }; gpio_enable_module(rs232_gpio,sizeof(rs232_gpio) / sizeof(rs232_gpio[0])); usart_serial_init(USART_SERIAL, &usart_options); } int main (void) { /* Insert system clock initialization code here (sysclk_init()). */ sysclk_init(); board_init(); twi_init(); RS232_init(); uint8_t data_received[10]; twi_package_t packet_read = { .addr = 0x75, // TWI slave memory address data .addr_length = sizeof (uint16_t), // TWI slave memory address data size .chip = 0x68, // TWI slave bus address .buffer = data_received, // transfer data destination buffer .length = 10 // transfer data size (bytes) }; //Enter loop while(1) { // Perform a multi-byte read access then check the result. if(twi_master_read(&AVR32_TWI, &packet_read) == TWI_SUCCESS){ usart_serial_write_packet(USART_SERIAL, &data_received[0], sizeof(data_received[0])); } } /* Insert application code here, after the board has been initialized. */ }
The I2C slave device I use is MPU6050, according to the register map, 0x75 is 'wh am i' register, so the usart should print '0x68' in PUTTY. However, when I connected the RS232 USB to my computer and open the correct COM port in PUTTY, it seemed to have no output.
Is there any problem with this code? THANKS!