#include "lpc17xx_pinsel.h" #include "lpc17xx_i2c.h" #include "lpc17xx_timer.h" #include "stdio.h" #define I2CDEV LPC_I2C2 static const int READ_WRITE_OPERATION = 0x42; static const int COMMAND_GET_DATA = 0x41; void readFromCompassHMC6352(void); static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len) { I2C_M_SETUP_Type rxsetup; rxsetup.sl_addr7bit = addr; rxsetup.tx_data = NULL; // Get address to read at writing address rxsetup.tx_length = 0; rxsetup.rx_data = buf; rxsetup.rx_length = len; rxsetup.retransmissions_max = 3; if (I2C_MasterTransferData(I2CDEV, &rxsetup, I2C_TRANSFER_POLLING) == SUCCESS){ return (0); } else { return (-1); } } static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len) { I2C_M_SETUP_Type txsetup; txsetup.sl_addr7bit = addr; txsetup.tx_data = buf; txsetup.tx_length = len; txsetup.rx_data = NULL; txsetup.rx_length = 0; txsetup.retransmissions_max = 3; if (I2C_MasterTransferData(I2CDEV, &txsetup, I2C_TRANSFER_POLLING) == SUCCESS){ return (0); } else { return (-1); } } void readFromCompassHMC6352(void) { int value = -1; uint8_t data[2]={0,0}; while(1) { // Send a "Get Data" command to the compass data[0]= COMMAND_GET_DATA; I2CWrite(READ_WRITE_OPERATION, data, 2); // datasheet suggests to wait at least 6000 microseconds Timer0_Wait(6); // Read two bytes from the compass (compass heading) data[0] = 1; I2CRead(READ_WRITE_OPERATION, data, 2); value = ((data[0] << 8)+data[1]) / 10; // read compass twice to clear last reading Timer0_Wait(6); data[0] = 0; I2CRead(READ_WRITE_OPERATION,data, 2); //Display the value on the display if(value >= 0 && value <= 359) { //Display degree value [COLOR=red]printf("The angle is %d\n",value); [/COLOR] } Timer0_Wait(60); } } static void init_i2c(void) { PINSEL_CFG_Type PinCfg; /* Initialize I2C2 pin connect * P0.10 - I2C_SDA(Serial Data) * P0.11 - I2C_SCL(Serial Clock line) * */ PinCfg.Funcnum = 2; PinCfg.Pinnum = 10; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 11; PINSEL_ConfigPin(&PinCfg); // Initialize I2C peripheral I2C_Init(LPC_I2C2, 100000); /* Enable I2C2 operation */ I2C_Cmd(LPC_I2C2, ENABLE); } int main(void) { init_i2c(); Timer0_Wait(1);} readFromCompassHMC6352(); return 1; } |
#include "lpc17xx_pinsel.h" #include "lpc17xx_i2c.h" #include "lpc17xx_timer.h" #include "stdio.h" #define I2CDEV LPC_I2C2 static const int READ_WRITE_OPERATION = 0x21; static const int COMMAND_GET_DATA = 0x41; void readFromCompassHMC6352(void); static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len); static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len); static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len) { I2C_M_SETUP_Type rxsetup; rxsetup.sl_addr7bit = addr; rxsetup.tx_data = NULL; // Get address to read at writing address rxsetup.tx_length = 0; rxsetup.rx_data = buf; rxsetup.rx_length = len; rxsetup.retransmissions_max = 3; if (I2C_MasterTransferData(I2CDEV, &rxsetup, I2C_TRANSFER_POLLING) == SUCCESS){ printf("read success\n\r"); return (0); } else { printf("read fail\n\r"); return (-1); } } static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len) { I2C_M_SETUP_Type txsetup; txsetup.sl_addr7bit = addr; txsetup.tx_data = buf; txsetup.tx_length = len; txsetup.rx_data = NULL; txsetup.rx_length = 0; txsetup.retransmissions_max = 3; if (I2C_MasterTransferData(I2CDEV, &txsetup, I2C_TRANSFER_POLLING) == SUCCESS){ printf("write success\n\r"); return (0); } else { printf("write fail\n\r"); return (-1); } } void readFromCompassHMC6352(void) { int value = -1; uint8_t data[2]={0,0}; while(1) { // Send a "Get Data" command to the compass data[0]= COMMAND_GET_DATA; I2CWrite(READ_WRITE_OPERATION, data, 2); // datasheet suggests to wait at least 6000 microseconds Timer0_Wait(6); // Read two bytes from the compass (compass heading) data[0] = 1; I2CRead(READ_WRITE_OPERATION, data, 2); value = ((data[0] << 8)+data[1]) / 10; // read compass twice to clear last reading Timer0_Wait(6); data[0] = 0; I2CRead(READ_WRITE_OPERATION,data, 2); //Display the value on the display if(value >= 0 && value <= 359) { //Display degree value printf("The angle is %d\n",value); } Timer0_Wait(60); } } static void init_i2c(void) { PINSEL_CFG_Type PinCfg; /* Initialize I2C2 pin connect * P0.10 - I2C_SDA(Serial Data) * P0.11 - I2C_SCL(Serial Clock line) * */ PinCfg.Funcnum = 2; //PinCfg.OpenDrain = 1; PinCfg.Pinnum = 10; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 11; PINSEL_ConfigPin(&PinCfg); // Initialize I2C peripheral I2C_Init(LPC_I2C2, 10000); /* Enable I2C2 operation */ I2C_Cmd(LPC_I2C2, ENABLE); } int main(void) { init_i2c(); Timer0_Wait(1); readFromCompassHMC6352(); return 1; } |
#include "lpc17xx_pinsel.h" #include "lpc17xx_i2c.h" #include "lpc17xx_timer.h" #include "stdio.h" #define I2CDEV LPC_I2C2 static const int READ_WRITE_OPERATION = 0x21; static const int COMMAND_GET_DATA = 0x41; void readFromCompassHMC6352(void); static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len); static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len); static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len) { I2C_M_SETUP_Type rxsetup; rxsetup.sl_addr7bit = addr; rxsetup.tx_data = NULL; // Get address to read at writing address rxsetup.tx_length = 0; rxsetup.rx_data = buf; rxsetup.rx_length = len; rxsetup.retransmissions_max = 3; if (I2C_MasterTransferData(I2CDEV, &rxsetup, I2C_TRANSFER_POLLING) == SUCCESS){ return (0); } else { return (-1); } } static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len) { I2C_M_SETUP_Type txsetup; txsetup.sl_addr7bit = addr; txsetup.tx_data = buf; txsetup.tx_length = len; txsetup.rx_data = NULL; txsetup.rx_length = 0; txsetup.retransmissions_max = 3; if (I2C_MasterTransferData(I2CDEV, &txsetup, I2C_TRANSFER_POLLING) == SUCCESS){ return (0); } else { return (-1); } } void readFromCompassHMC6352(void) { int value = -1; uint8_t data[2]={0,0}; while(1) { // Send a "Get Data" command to the compass data[0]= COMMAND_GET_DATA; I2CWrite(READ_WRITE_OPERATION, data, 2); // datasheet suggests to wait at least 6000 microseconds Timer0_Wait(6); // Read two bytes from the compass (compass heading) data[0] = 1; I2CRead(READ_WRITE_OPERATION, data, 2); value = ((data[0] << 8)+data[1]) / 10; // read compass twice to clear last reading Timer0_Wait(6); data[0] = 0; I2CRead(READ_WRITE_OPERATION,data, 2); //Display the value on the display if(value >= 0 && value <= 359) { //Display degree value printf("The angle is %d\n",value); } Timer0_Wait(60); } } static void init_i2c(void) { PINSEL_CFG_Type PinCfg; /* Initialize I2C2 pin connect * P0.10 - I2C_SDA(Serial Data) * P0.11 - I2C_SCL(Serial Clock line) * */ PinCfg.Funcnum = 2; PinCfg.Pinnum = 10; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 11; PINSEL_ConfigPin(&PinCfg); // Initialize I2C peripheral I2C_Init(LPC_I2C2, 100000); /* Enable I2C2 operation */ I2C_Cmd(LPC_I2C2, ENABLE); } int main(void) { init_i2c(); Timer0_Wait(1); readFromCompassHMC6352(); return 1; } |
#include "lpc17xx_pinsel.h" #include "lpc17xx_i2c.h" #include "lpc17xx_timer.h" #include "stdio.h" #define I2CDEV LPC_I2C2 static const int READ_WRITE_OPERATION = 0x21; static const int COMMAND_GET_DATA = 0x41; void readFromCompassHMC6352(void); static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len); static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len); static int I2CRead(uint8_t addr, uint8_t* buf, uint32_t len) { I2C_M_SETUP_Type rxsetup; rxsetup.sl_addr7bit = addr; rxsetup.tx_data = NULL; // Get address to read at writing address rxsetup.tx_length = 0; rxsetup.rx_data = buf; rxsetup.rx_length = len; rxsetup.retransmissions_max = 3; if (I2C_MasterTransferData(I2CDEV, &rxsetup, I2C_TRANSFER_POLLING) == SUCCESS){ return (0); } else { return (-1); } } static int I2CWrite(uint8_t addr, uint8_t* buf, uint32_t len) { I2C_M_SETUP_Type txsetup; txsetup.sl_addr7bit = addr; txsetup.tx_data = buf; txsetup.tx_length = len; txsetup.rx_data = NULL; txsetup.rx_length = 0; txsetup.retransmissions_max = 3; if (I2C_MasterTransferData(I2CDEV, &txsetup, I2C_TRANSFER_POLLING) == SUCCESS){ return (0); } else { return (-1); } } void readFromCompassHMC6352(void) { int value = -1; uint8_t data[2]={0,0}; while(1) { // Send a "Get Data" command to the compass data[0]= COMMAND_GET_DATA; I2CWrite(READ_WRITE_OPERATION, data, 2); // datasheet suggests to wait at least 6000 microseconds Timer0_Wait(6); // Read two bytes from the compass (compass heading) data[0] = 1; I2CRead(READ_WRITE_OPERATION, data, 2); value = ((data[0] << 8)+data[1]) / 10; // read compass twice to clear last reading Timer0_Wait(6); data[0] = 0; I2CRead(READ_WRITE_OPERATION,data, 2); //Display the value on the display if(value >= 0 && value <= 359) { //Display degree value printf("The angle is %d\n",value); } Timer0_Wait(60); } } static void init_i2c(void) { PINSEL_CFG_Type PinCfg; /* Initialize I2C2 pin connect * P0.10 - I2C_SDA(Serial Data) * P0.11 - I2C_SCL(Serial Clock line) * */ PinCfg.Funcnum = 2; PinCfg.Pinnum = 10; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 11; PINSEL_ConfigPin(&PinCfg); // Initialize I2C peripheral I2C_Init(LPC_I2C2, 100000); /* Enable I2C2 operation */ I2C_Cmd(LPC_I2C2, ENABLE); } int main(void) { init_i2c(); Timer0_Wait(1); readFromCompassHMC6352(); return 1; } |