连续签到天数:1天 | 签到总天数:75天 | 签到总奖励:2866金币 |
|
马上注册,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册 
×
I2C
- #include <message.h>
- #include <stdlib.h>
- #include <i2c.h>
- #include <panic.h>
- #define MESSAGE_TX_DATA 0x4000
- #define MESSAGE_RX_DATA 0x4001
- #define I2C_DEVICE_ADDRESS 0x22
- #define I2C_A_DEVICE_REGISTER 0x10
- typedef struct
- {
- TaskData task;
- } I2CTaskData;
- I2CTaskData theI2CTaskData;
- uint16 i2c_register_data;
- static void I2CMessageHandler(Task pTask, MessageId pId, Message pMessage);
- static void i2c_example_init(void);
- static void i2c_write_16bit_device_register(uint8 device_address, uint8 device_register, uint16 data);
- static void i2c_read_16bit_device_register(uint8 device_address, uint8 device_register, uint16 *data);
- int main(void)
- {
- i2c_example_init();
- /* Start the message scheduler loop */
- MessageLoop();
- /* Never get here...*/
- return 0;
- }
- void i2c_example_init(void)
- {
- /* Assign task message handler */
- theI2CTaskData.task.handler = I2CMessageHandler;
- /* Send MESSAGE_TX_DATA to the task */
- MessageSendLater(&theI2CTaskData.task, MESSAGE_TX_DATA, 0, 1000);
- /* Send MESSAGE_RX_DATA to the task */
- MessageSendLater(&theI2CTaskData.task, MESSAGE_RX_DATA, 0, 2000);
- }
- void i2c_write_16bit_device_register(uint8 device_address, uint8 device_register, uint16 data)
- {
- uint8 i2c_data[3];
- i2c_data[0] = device_register;
- i2c_data[1] = (data >> 8) & 0xFF;
- i2c_data[2] = data & 0xFF;
- /*
- I2cTransfer(uint16 address, const uint8 *tx, uint16 tx_len, uint8 *rx, uint16 rx_len)
- If tx_len is non-zero and rx_len is zero then the sequence reduces to:
- - Start condition (S).
- - Write slave address and direction byte (address | 0).
- - Write tx_len data bytes from the buffer at tx.
- - Stop condition (P).
- */
- PanicZero(I2cTransfer(device_address, i2c_data, 3, NULL, 0));
- }
- void i2c_read_16bit_device_register(uint8 device_address, uint8 device_register, uint16 *data)
- {
- uint8 i2c_data[1];
- uint8 i2c_rx_data[2];
- i2c_data[0] = device_register;
- /*
- I2cTransfer(uint16 address, const uint8 *tx, uint16 tx_len, uint8 *rx, uint16 rx_len)
- If tx_len is non-zero and rx_len is non-zero then the sequence is:
- - Start condition (S).
- - Write slave address and direction byte (address | 0).
- - Write tx_len data bytes from the buffer at tx.
- - Repeated start condition (Sr).
- - Write slave address and direction byte (address | 1).
- - Read rx_len bytes into the buffer at rx, acknowledging all but the final byte.
- - Stop condition (P).
- */
- PanicZero(I2cTransfer(device_address, i2c_data, 1, i2c_rx_data, 2));
- *data = (i2c_rx_data[0] << 8) + i2c_rx_data[1];
- }
- void I2CMessageHandler(Task pTask, MessageId pId, Message pMessage)
- {
- switch (pId)
- {
- case MESSAGE_TX_DATA:
- i2c_write_16bit_device_register(I2C_DEVICE_ADDRESS, I2C_A_DEVICE_REGISTER,
- 0x1234);
- /* Send MESSAGE_TX_DATA to the task */
- MessageSendLater(&theI2CTaskData.task, MESSAGE_TX_DATA, 0, 1000);
- break;
- case MESSAGE_RX_DATA:
- i2c_read_16bit_device_register(I2C_DEVICE_ADDRESS, I2C_A_DEVICE_REGISTER,
- &i2c_register_data);
- /* Send MESSAGE_RX_DATA to the task */
- MessageSendLater(&theI2CTaskData.task, MESSAGE_RX_DATA, 0, 2000);
- break;
- default:
- break;
- }
- }
复制代码
|
|