We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I want to write/read a integer/ long integer on a I2C device, but all the routines I have supports only character(single byte) writing/ reading from I2C device. Can anyone help me?
That's how the protocol of I2c is defined. You can send only one byte at a time, if it is now the device address, the subaddress or just data. The 9th bit in the protocol is reserved for the acknowledge, so only 8 bits can be send. However, you can send as many bytes as you want (there's in theory no limit in the amount of bytes). So, your integer could be divided into two bytes to send on the bus, as Stephan suggests. --Geert
use a union union LCD_noname_104 { unsigned char y8[2]; unsigned int y16; } LCD_select_font_y; for sending bytes: I2C_send = LCD_select_font_y.y8[0]; I2C_send = LCD_select_font_y.y8[1]; for using the int: LCD_select_font_y.y16 = 1234; breaking up an int costs more bytes code geert starre
"use a union" This non-portable but usually fast. See: http://www.keil.com/forum/docs/thread3210.asp
The Keil compiled does not execute shift 8, it is 'smart'enough to realize that shift 8 means 'grab the other byte'. Thus shift 8 is fast and simple. Erik
"The Keil compiled does not execute shift 8, it is 'smart'enough to realize that shift 8 means 'grab the other byte'. Thus shift 8 is fast and simple." Yes; that's why I said, "Shifting is portable but may be slow; The others are non-portable, but likely to be fast." (now with added emphasis). Portability is definite; performance depends on the implementation - and which is the most important depends upon the requirements of the application!