uint8_t ov7670_get(uint8_t reg) { uint8_t data = 0; I2C_start(I2C2, 0x42, I2C_Direction_Transmitter); I2C_write(I2C2, reg); I2C_stop(I2C2); delayx(1000); I2C_start(I2C2, 0x43, I2C_Direction_Receiver); data = I2C_read_nack(I2C2); I2C_stop(I2C2); delayx(1000); return data; }
uint8_t I2C_Read_Reg(I2C_TypeDef* I2Cx, uint8_t Device, uint8_t Register) { uint8_t data; I2C_start(I2Cx, Device <<1, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode I2C_write(I2Cx, Register); // Select the register you want to read from. I2C_stop(I2Cx); // stop the transmission I2C_start(I2Cx, Device <<1, I2C_Direction_Receiver); // start a transmission in Master receiver mode data = I2C_read_nack(I2Cx); // read one byte from the register of interest. I2C_stop(I2Cx); return data; }
//----------------------------------------------------------------------------- // /brief Write a register on the AIC3106. // // /param uint8_t in_reg_addr: The address of the register to be written to. // // /param uint8_t data: Data to be written to the register // // /return uint32_t ERR_NO_ERROR on sucess // //----------------------------------------------------------------------------- uint32_t AIC3106_writeRegister(uint8_t in_reg_addr, uint8_t in_data) { uint32_t rtn; uint8_t i2c_data[2]; i2c_data[0] = in_reg_addr; i2c_data[1] = in_data; // write the register that we want to read. rtn = I2C_write(I2C_PORT_AIC3106, I2C_ADDR_AIC3106, i2c_data, 2, SET_STOP_BIT_AFTER_WRITE); return (rtn); }
// ds1307 rtc write bit rtwr (void) { unsigned char i; // return 0=ok 1=error bit err; I2C_start(); err = I2C_write(0xd0); // address & r/w bit if (!err) { err = I2C_write(0x00); // start register addr=0 if (!err) { for (i=0;i<=7;i++) { err = I2C_write(TIMBUF[i]); if (err) break; } } } I2C_stop(); return (err); }
void mRGB_SetColor(uint8_t *color) { // Addresses was set previously, only require functional code and // data mRGB_cmd[FUNCODE] = SET_RGB; // Set pullup register // Load the requested color in RGB format into the command buffer for(U8 i=0;i<3;i++) mRGB_cmd[DATA1+i] = *(color+i); // Write data on I2C bus I2C_write(mRGB_cmd, 7); }
void AK8963_ReadMag(IMU_DATA_t *mag) { int i = 0; uint8_t buffer[7]; // x/y/z gyro register data, ST2 register stored here, must read ST2 at end of data acquisition uint16_t rmag[3]; I2C_start(I2C1, AK8963_ADDRESS<<1, I2C_Direction_Transmitter); I2C_write(I2C1, AK8963_ST1); I2C_stop(I2C1); // stop the transmission // start a transmission in Master receiver mode I2C_start(I2C1, AK8963_ADDRESS<<1, I2C_Direction_Receiver); if(I2C_read_nack(I2C1) & 0x01){ I2C_start(I2C1, AK8963_ADDRESS<<1, I2C_Direction_Transmitter); I2C_write(I2C1, AK8963_XOUT_L); I2C_stop(I2C1); // stop the transmission I2C_start(I2C1, AK8963_ADDRESS<<1, I2C_Direction_Receiver); for(i = 0; i < 7-1; i++){ buffer[i] = I2C_read_ack(I2C1); } // read one byte and don't request another byte, stop transmission buffer[7-1] = I2C_read_nack(I2C1); if(!(buffer[6] & 0x08)) { // Check if magnetic sensor overflow set, if not then report data // maybe change to 1, 0, 3, 2, 5, 4 rmag[0] = (((int16_t) buffer[0]) << 8) | buffer[1]; // X axis (internal sensor y axis) rmag[1] = (((int16_t) buffer[2]) << 8) | buffer[3]; // Y axis (internal sensor x axis) rmag[2] = (((int16_t) buffer[4]) << 8) | buffer[5]; // Z axis (internal sensor z axis) // maybe it should look lik this: // (float)buffer[0]*MAG_SCALE*mag_calibration[0] - MAG_X_OFFSET; mag->Roll = (float)(rmag[0] - MAG_X_OFFSET) * MAG_SCALE * mag_calibration[0]; mag->Pitch = (float)(rmag[1] - MAG_Y_OFFSET) * MAG_SCALE * mag_calibration[1]; mag->Yaw = (float)(rmag[2] - MAG_Z_OFFSET) * MAG_SCALE * mag_calibration[2]; } } }
//----------------------------------------------------------------------------- // /brief Read data from a register on the AIC3106. // // /param uint8_t in_reg_addr: The address of the register to be read from. // // /param uint8_t * dest_buffer: Pointer to buffer to store retrieved data. // // /return uint32_t ERR_NO_ERROR on sucess // //----------------------------------------------------------------------------- uint32_t AIC3106_readRegister(uint8_t in_reg_addr, uint8_t *dest_buffer) { uint32_t rtn; // write the register address that we want to read. rtn = I2C_write(I2C_PORT_AIC3106, I2C_ADDR_AIC3106, &in_reg_addr, 1, SKIP_STOP_BIT_AFTER_WRITE); if (rtn != ERR_NO_ERROR) return (rtn); // clock out the register data. rtn = I2C_read(I2C_PORT_AIC3106, I2C_ADDR_AIC3106, dest_buffer, 1, SKIP_BUSY_BIT_CHECK); return (rtn); }
/*---------------------------------------------------------------------------- //Initialises the GPIO device with a set of default paramters *----------------------------------------------------------------------------*/ void I2C_GPIO_Init(void){ //Select config register values uint8_t config_reg_data = 0x00; uint8_t seqop = 0; //Sequential Operation mode bit uint8_t disslw = 0; //Slew Rate control bit for SDA output uint8_t odr = 0; //INT pin as an open-drain output uint8_t intpol = 0; //Sets the polarity of the INT output pin. //Merge config reg values into a single byte config_reg_data = 0x00; config_reg_data |= (seqop << 5); config_reg_data |= (disslw << 4); config_reg_data |= (odr << 2); config_reg_data |= (intpol << 1); //Initialise by writing the config variable to the CONFIG register I2C_start(I2C3, GPIO_ADDRESS, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode I2C_write(I2C3, GPIO_IOCON_REG); //Set device pointer to configuration register I2C_write(I2C3, config_reg_data); //Write the config reg data to the config reg I2C_stop(I2C3); Delay_Millis(5); //Ensure config values are implemented before continuing }
// \return uint32_t // ERR_NO_ERROR - register written to correctly. // //----------------------------------------------------------------------------- uint32_t CDCE913_writeByte(uint8_t in_offset, uint8_t in_data) { uint32_t rtn; uint8_t i2c_data[2]; // set bit to indicate this is a byte write. i2c_data[0] = in_offset | BYTE_READ_WRITE_BIT; i2c_data[1] = in_data; // write the register that we want to read. rtn = I2C_write(I2C_PORT_CDCE913, I2C_ADDR_CDCE913, i2c_data, 2, SET_STOP_BIT_AFTER_WRITE); return (rtn); }
void ADXL345_init(void) { I2C_write(ADXL345_Address,0x31,0x08); //测量范围,正负2g,13位模式 分辨率4mg I2C_write(ADXL345_Address,0x2C,0x08); //速率设定为12.5 参考pdf13页 I2C_write(ADXL345_Address,0x2D,0x08); //选择电源模式 参考pdf24页 //I2C_write(ADXL345_Address,0x2E,0x80); //使能 DATA_READY 中断 I2C_write(ADXL345_Address,0x1E,0x00); //X 偏移量 根据测试传感器的状态写入pdf29页 I2C_write(ADXL345_Address,0x1F,0x00); //Y 偏移量 根据测试传感器的状态写入pdf29页 I2C_write(ADXL345_Address,0x20,0x00); //Z 偏移量 根据测试传感器的状态写入pdf29页 I2C_write(ADXL345_Address,0x2F,0x00); //配置freefall 为int1脚 I2C_write(ADXL345_Address,0x28,0x07); I2C_write(ADXL345_Address,0x29,0x04); I2C_write(ADXL345_Address,0x2E,0x00+BIT2);//开启freefall int P1DIR&=~BIT0; P1IFG = 0; P1IES&=~BIT0;//上升中断 P1IE |= BIT0;//17号 delay_ms(10); }
static PyObject *I2CDev_write(I2CDev *self, PyObject *args, PyObject *kwds) { uint32_t n_bytes, i, addr; long byte; PyObject *data, *byte_obj; uint8_t *txbuf; if(!PyArg_ParseTuple(args, "IO!", &addr, &PyList_Type, &data)) { return NULL; } if (self->slave_addr != addr) { if (setSlaveAddress(self->i2c_fd, addr) < 0) { PyErr_SetString(PyExc_IOError, "could not configure I2C interface"); return NULL; } self->slave_addr = addr; } n_bytes = PyList_Size(data); txbuf = malloc(n_bytes); for (i=0; i<n_bytes; i++) { byte_obj = PyList_GetItem(data, i); if (!PyInt_Check(byte_obj)) { PyErr_SetString(PyExc_ValueError, "data list to transmit can only contain integers"); free(txbuf); return NULL; } byte = PyInt_AsLong(byte_obj); if (byte < 0) { // Check for error from PyInt_AsLong: if (PyErr_Occurred() != NULL) return NULL; // Negative numbers are set to 0: byte = 0; } // Just send the LSB if value longer than 1 byte: byte &= 255; txbuf[i] = (uint8_t) byte; } if (I2C_write(self->i2c_fd, (void *) txbuf, n_bytes) < 0) { PyErr_SetString(PyExc_IOError, "could not write to I2C device"); free(txbuf); return NULL; } free(txbuf); Py_INCREF(Py_None); return Py_None; }
alt_u8 i2c_hdmi_rx_hdmi_map_r(alt_u8 reg) { uint32_t offset = reg; uint32_t data; pcie_i2c_read(0x68>>1, offset, &data, 1); return data; #if 0 I2C_start(I2C_CTRL_BASE,0x68>>1,0); //address the chip in write mode data = I2C_write(I2C_CTRL_BASE,reg,0); // set command to read input register. I2C_start(I2C_CTRL_BASE,0x68>>1,1); //send start again but this time in read mode data = I2C_read(I2C_CTRL_BASE,1); // read the input register and send stop return data; #endif }
/************************************************************************* Writes a string or char to the Midas MCCOG21605B6W-SPTLYI LCD display Input: String to display as "Hello World!" Return: *************************************************************************/ void LCD_Write(const char *format, ...){ va_list aptr; int len; Int16 ret; va_start(aptr, format); len = vsprintf(buffer, format, aptr); Uint16 startStop = ((CSL_I2C_START) | (CSL_I2C_STOP)); CSL_Status status; lcd_Init(); int t; Uint16 ustr[sizeof(buffer)+1]; ustr[0] = 0x40; for(t = 0; t < len; t++) { ustr[t+1] = (Uint16)buffer[t]; } status = I2C_write(ustr, (len <= 16)?(len+1):17, I2C_DISPLAY_ADDRESS, TRUE, startStop, CSL_I2C_MAX_TIMEOUT); lcd_Delay(); if(len > 16) { Uint16 l[] = {0x00, 0xC0}; status = I2C_write(l, 2, I2C_DISPLAY_ADDRESS, TRUE, startStop, CSL_I2C_MAX_TIMEOUT); lcd_Delay(); ustr[16] = 0x40; status = I2C_write(ustr+16, len-15, I2C_DISPLAY_ADDRESS, TRUE, startStop, CSL_I2C_MAX_TIMEOUT); lcd_Delay(); } }
// // Originally, 'endTransmission' was an f(void) function. // It has been modified to take one parameter indicating // whether or not a STOP should be performed on the bus. // Calling endTransmission(false) allows a sketch to // perform a repeated start. // // WARNING: Nothing in the library keeps track of whether // the bus tenure has been properly ended with a STOP. It // is very possible to leave the bus in a hung state if // no call to endTransmission(true) is made. Some I2C // devices will behave oddly if they do not see a STOP. // uint32 I2CBus::endTransmission(bool sendStop) { // int8_t ret = 0; // transmit buffer (blocking) if ( sendStop ) { if ( I2C_write(i2cx, dstaddress, txbuffer, txlength) ) { return txlength; } } else { if ( I2C_request(i2cx, dstaddress, txbuffer, txlength) ) { return txlength; } } return 0; }
// Чтение из регистра кодека uint16 aic3204_get(uint16 regnum, uint16* regval) { // Локальные данные int16 retcode = 0; uint8 cmd[1]; // Сформировать пакет I2C cmd[0] = regnum & 0x007F; // 7 бит адреса регистра // Выдать команду retcode |= I2C_write(AIC3204_ICADDR, cmd, 1); // Получить ответ retcode |= I2C_read(AIC3204_ICADDR, cmd, 1); // Ожидание завершения переходных процессов c5515_wait(10); // Возврат результата *regval = cmd[0]; return retcode; }
/*---------------------------------------------------------------------------- //Reads the GPIO register of the GPIO device //Returns: GPIO register byte ( LSB is GP0, MSB is GP7 ) //IMPORTANT: Data is only valid for pins configured as INPUT pins! *----------------------------------------------------------------------------*/ uint8_t I2C_GPIO_Read(void){ uint8_t rxData = 0x00; //Indicate we'd like to read the GPIO register I2C_start(I2C3, GPIO_ADDRESS, I2C_Direction_Transmitter); I2C_write(I2C3, GPIO_GPIO_REG); I2C_stop(I2C3); //Then open in RX mode and get the data I2C_start(I2C3, GPIO_ADDRESS, I2C_Direction_Receiver); rxData = I2C_read_nack(I2C3); I2C_stop(I2C3); // stop the transmission return rxData; }
// \return uint32_t // ERR_NO_ERROR - register read correctly. // //----------------------------------------------------------------------------- uint32_t CDCE913_readByte(uint8_t in_offset, uint8_t *dest_buffer) { uint32_t rtn; // set bit to indicate this is a byte read. SETBIT(in_offset, BYTE_READ_WRITE_BIT); // write the register address that we want to read. rtn = I2C_write(I2C_PORT_CDCE913, I2C_ADDR_CDCE913, &in_offset, 1, SKIP_STOP_BIT_AFTER_WRITE); if (rtn != ERR_NO_ERROR) return (rtn); // clock out the register data. rtn = I2C_read(I2C_PORT_CDCE913, I2C_ADDR_CDCE913, dest_buffer, 1, SKIP_BUSY_BIT_CHECK); return (rtn); }
/* write_display(string, dot) * Writes to the display the string that is passed (string must be 4 characters long), and sets the dot of the last segment if dot = 1 */ void write_display(unsigned char* string, unsigned char dot) { unsigned char displayBuffer[9]; displayBuffer[0] = 0x00; for(int i = 1, j = 0; i < 9; i++) { if(i%2) displayBuffer[i] = alphafonttable[string[j]] & 0xFF; else{ displayBuffer[i] = alphafonttable[string[j]] >> 8; ++j; } } if(dot) displayBuffer[8] |= 1 << 6; I2C_write(HT16K33_SLA, displayBuffer, 9); }
int ds2482_write_config(int8 config){ int8 read_config; i2c_start(); i2c_write(DS2482_I2C_ADDR | I2C_FLAG_WRITE); i2c_write(DS2482_CMD_WCFG); I2C_write(config | (~config << 4)); //This makes the ones compliment in the top nibble ***Checked Working*** i2c_start(); i2c_write(DS2482_I2C_ADDR | I2C_FLAG_READ); read_config = i2c_read(0); i2c_stop(); // check for failure due to incorrect read back if (config != read_config){ DS2482_reset(); return false; } return true; }
uint8_t I2C_readreg(uint32_t reg) { uint8_t tmp; I2C_start(i2c_dev, SLAVE_ADDRESS, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode I2C_write(i2c_dev, (uint8_t) reg); // write one byte to the slave I2C_stop(i2c_dev); // stop the transmission Delay(100); I2C_start(i2c_dev, SLAVE_ADDRESS, I2C_Direction_Receiver); // start a transmission in Master receiver mode tmp = I2C_read_nack(i2c_dev); I2C_stop(i2c_dev); // stop the transmission Delay(100); return tmp; }
void I2C_Read_Multi_Reg(I2C_TypeDef* I2Cx, uint8_t Device, uint8_t Register, uint8_t* buf, uint8_t count) { uint8_t i = 0; I2C_start(I2Cx, Device <<1, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode I2C_write(I2Cx, Register); // Select the register you want to start your read at. I2C_stop(I2Cx); // stop the transmission I2C_start(I2Cx, Device <<1, I2C_Direction_Receiver); // start a transmission in Master receiver mode for(i =0; i < count; i++) { if (i < (count - 1)) { buf[i] = I2C_read_ack(I2Cx); // Read one byte and send an ack to the slave. } else { buf[i] = I2C_read_nack(I2Cx); // Read the last register and send a Nack. I2C_stop(I2Cx); } } }
/*---------------------------------------------------------------------------- //Reads temperature registers from temp sensor //Returns the temperature, in Celsius *----------------------------------------------------------------------------*/ double I2C_Temp_Read(void){ double temperature; uint16_t recievedData[2] = {0,0}; //Indicate we'd like to read the temperature register I2C_start(I2C3, TEMP_ADDRESS, I2C_Direction_Transmitter); I2C_write(I2C3, TEMP_READ_REG); I2C_stop(I2C3); //Then open in RX mode and get the data I2C_start(I2C3, TEMP_ADDRESS, I2C_Direction_Receiver); recievedData[0] = I2C_read_ack(I2C3); //read first byte recievedData[1] = I2C_read_nack(I2C3); //read second byte I2C_stop(I2C3); // stop the transmission recievedData[1] |= recievedData[0]<<8; temperature = pow(2,-8) * (double)recievedData[1]; return temperature; }
static PyObject *I2CDev_readTransaction(I2CDev *self, PyObject *args, PyObject *kwds) { uint32_t n_bytes, i, addr; uint8_t byte; PyObject *data, *byte_obj; uint8_t *rxbuf; if(!PyArg_ParseTuple(args, "IbI", &addr, &byte, &n_bytes)) { return NULL; } if (self->slave_addr != addr) { if (setSlaveAddress(self->i2c_fd, addr) < 0) { PyErr_SetString(PyExc_IOError, "could not configure I2C interface"); return NULL; } self->slave_addr = addr; } rxbuf = malloc(n_bytes); if (I2C_write(self->i2c_fd, (void *) &byte, 1) < 0) { PyErr_SetString(PyExc_IOError, "could not write to I2C device"); free(rxbuf); return NULL; } if (I2C_read(self->i2c_fd, (void *) rxbuf, n_bytes) < 0) { PyErr_SetString(PyExc_IOError, "could not read from I2C device"); free(rxbuf); return NULL; } data = PyList_New(0); for (i=0; i<n_bytes; i++) { byte_obj = PyInt_FromLong((long) rxbuf[i]); PyList_Append(data, byte_obj); Py_DECREF(byte_obj); } free(rxbuf); return data; }
CSL_Status AIC_Write(Uint16 regAddr, Uint16 regValue) { CSL_Status status; Uint16 startStop; Uint16 write_buffer[2]; volatile Uint16 looper; startStop = ((CSL_I2C_START) | (CSL_I2C_STOP)); write_buffer[0] = regAddr; write_buffer[1] = regValue; /* Write data */ status = I2C_write(write_buffer, 2, CSL_I2C_CODEC_ADDR, TRUE, startStop, CSL_I2C_MAX_TIMEOUT); /* Give some delay */ for(looper = 0; looper < CSL_I2C_MAX_TIMEOUT; looper++){;} return status; }
void init_ColorVision(void) { // initialize ov6620 cmos camera i2cdata[0] = 0x01; i2cdata[1] = 0x8F; // Blue Gain control (default 0x80) i2cdata[2] = 0x8F; // Red Gain Control i2cdata[3] = 0x80; // Saturation i2cdata[4] = 0x00; // Reserved i2cdata[5] = 0x4f; // Contrast i2cdata[6] = 0x9f; // Brightness i2cdata[7] = 0xCF; // Sharpness (default 0xC6) i2cdata[8] = 0x00; // Reserved i2cdata[9] = 0x00; // Reserved i2cdata[10] = 0x00; // Reserved i2cdata[11] = 0x00; // Reserved i2cdata[12] = 0x20; // AWB - Blue i2cdata[13] = 0x20; // AWB - Red i2cdata[14] = 0x0D; // COMR i2cdata[15] = 0x05; // COMS i2cdata[16] = 0x9A; // AEC i2cdata[17] = 0x01; // CLKRC i2cdata[18] = 0x28; // COMA i2cdata[19] = 0x01; // 0x01; // COMB I2C_write(I2C0, 0x60, i2cdata, 20, SET_STOP_BIT_AFTER_WRITE); i2cdata[0] = 0x20; i2cdata[1] = 0x01; // COME I2C_write(I2C0, 0x60, i2cdata, 2, SET_STOP_BIT_AFTER_WRITE); i2cdata[0] = 0x28; i2cdata[1] = 0x81; // COMH I2C_write(I2C0, 0x60, i2cdata, 2, SET_STOP_BIT_AFTER_WRITE); i2cdata[0] = 0x39; // changed to PCLK always on. i2cdata[1] = 0x00; //0x40; // COML I2C_write(I2C0, 0x60, i2cdata, 2, SET_STOP_BIT_AFTER_WRITE); VPIF_initReceive(VIDEO_CONN_CAMERA); // PRU setup EVMOMAPL138_lpscTransition(PSC0, DOMAIN0, LPSC_DMAX, PSC_ENABLE); PRU_stop(PRU0); PRU_reset(PRU0); PRU_load(PRU0_PROG, PRUCode, sizeof(PRUCode)/sizeof(uint32_t)); PRU_reset(PRU0); PRU_run(PRU0); // VPIF (95) interrupt serviced by INT4 ICR = 0x010; // clear pending interrupts IER |= 0x010; // enable interrupt on line // PRU (6) interrupt serviced by INT6 ICR = 0x040; // clear pending interrupts IER |= 0x040; // enable interrupt on line pic_data = (int *)ADDR_VIDEO_DATA_BASE; Image_data = (bgr *)(IMAGE_DATA_MEM); Thres_Image = (unsigned char *)(THRES_IMAGE_MEM); Linux_Image = (bgr *)(ADDR_VIDEO_DATA_BASE+LINUX_IMAGE_OFFSET); LCD_Image = (bgr *)(ADDR_VIDEO_DATA_BASE+LCD_IMAGE_OFFSET); ptrshrdmem = (sharedmemstruct *)SHARED_MEM; while (CHKBIT(VPIF->INTSTAT,INT_FRAME_CH1) == 0) {} SETBIT(VPIF->INTSTATCLR, INT_FRAME_CH1); }
return data; #if 0 I2C_start(I2C_CTRL_BASE,0x68>>1,0); //address the chip in write mode data = I2C_write(I2C_CTRL_BASE,reg,0); // set command to read input register. I2C_start(I2C_CTRL_BASE,0x68>>1,1); //send start again but this time in read mode data = I2C_read(I2C_CTRL_BASE,1); // read the input register and send stop return data; #endif } //HDMI_RX_KSV_MAP int i2c_hdmi_rx_ksv_map_w(alt_u8 reg, alt_u8 data1) { #if 0 I2C_start(I2C_CTRL_BASE,0x64>>1,0); //address the chip in write mode data = I2C_write(I2C_CTRL_BASE,reg,0); // write register. data = I2C_write(I2C_CTRL_BASE,data1,1); // write vial. return data; #endif uint32_t data = data1; return pcie_i2c_write(0x64>>1,reg,&data,1); } alt_u8 i2c_hdmi_rx_ksv_map_r(alt_u8 reg) { uint32_t offset = reg; uint32_t data; pcie_i2c_read(0x64>>1, offset, &data, 1); return data; #if 0
int main() { CyGlobalIntEnable; UART_Start(); printf("Start\r\n"); /* //IR receiver// ---------------------------------------------------- unsigned int IR_val; for(;;) { IR_val = get_IR(); printf("%x\r\n\n",IR_val); } ///---------------------------------------------------------- */ /* //Ambient// ---------------------------------------------------- I2C_Start(); uint16 value =0; I2C_write(0x29,0x80,0x00); value = I2C_read(0x29,0x80); printf("%x ",value); I2C_write(0x29,0x80,0x03); value = I2C_read(0x29,0x80); printf("%x\r\n",value); value = I2C_read(0x29,0x81); printf("%x\r\n",value); for(;;) { uint8 Data0Low,Data0High,Data1Low,Data1High; Data0Low = I2C_read(0x29,CH0_L); Data0High = I2C_read(0x29,CH0_H); Data1Low = I2C_read(0x29,CH1_L); Data1High = I2C_read(0x29,CH1_H); uint8 CH0, CH1; CH0 = convert_raw(Data0Low,Data0High); CH1 = convert_raw(Data1Low,Data1High); // printf("%d %d %d %d\r\n",Data0Low,Data0High, Data1Low,Data1High); // printf("%d %d\r\n",CH0,CH1); // printf("%f\r\n",(float)CH1/CH0); double Ch0 = CH0; double Ch1 = CH1; double data = 0; data = getLux(Ch0,Ch1); printf("%lf\r\n",data); } ///---------------------------------------------------------- */ /* //nunchuk// ---------------------------------------------------- nunchuk_start(); nunchuk_init(); for(;;) { nunchuk_read(); } //----------------------------------------------------*/ //accelerometer// //-------------------------------------------------------------- I2C_Start(); uint8 X_L_A, X_H_A, Y_L_A, Y_H_A, Z_L_A, Z_H_A; int16 X_AXIS, Y_AXIS, Z_AXIS; I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL1_REG, 0x37); // set accelerometer & magnetometer into active mode I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL7_REG, 0x22); for(;;) { //print out accelerometer output X_L_A = I2C_read(ACCEL_MAG_ADDR, OUT_X_L_A); X_H_A = I2C_read(ACCEL_MAG_ADDR, OUT_X_H_A); X_AXIS = convert_raw(X_L_A, X_H_A); Y_L_A = I2C_read(ACCEL_MAG_ADDR, OUT_Y_L_A); Y_H_A = I2C_read(ACCEL_MAG_ADDR, OUT_Y_H_A); Y_AXIS = convert_raw(Y_L_A, Y_H_A); Z_L_A = I2C_read(ACCEL_MAG_ADDR, OUT_Z_L_A); Z_H_A = I2C_read(ACCEL_MAG_ADDR, OUT_Z_H_A); Z_AXIS = convert_raw(Z_L_A, Z_H_A); //printf("ACCEL: %d %d %d %d %d %d \r\n", X_L_A, X_H_A, Y_L_A, Y_H_A, Z_L_A, Z_H_A); value_convert_accel(X_AXIS, Y_AXIS, Z_AXIS); printf("\n"); CyDelay(50); } ///---------------------------------------------------------- /* //ultra// ---------------------------------------------------- ultra_isr_StartEx(ultra_isr_handler); // Ultra Sonic Interrupt Ultra_Start(); // Ultra Sonic Start function for(;;) { CyDelay(100); Trig_Write(1); // Trigger High CyDelayUs(10); // 10 micro seconds for trigger input signals Trig_Write(0); // Trigger Low } //----------------------------------------------------*/ /* //reflectance// ---------------------------------------------------- sensor_isr_StartEx(sensor_isr_handler); Refelctance_Start(); IR_led_Write(1); for(;;) { reflectance_period(); //print out each period of reflectance sensors reflectance_digital(); //print out 0 or 1 according to results of reflectance period CyDelay(500); } ///----------------------------------------------------*/ /* //motor// ---------------------------------------------------- motor_Start(); // motor start motor_forward(50,2000); // moving forward motor_turn(10,50,2000); // turn motor_turn(50,10,2000); // turn motor_backward(50,2000); // movinb backward motor_Stop(); // motor stop for(;;) { } ///----------------------------------------------------*/ /* //gyroscope// //----------------------------------------------------- I2C_Start(); uint8 X_AXIS_L, X_AXIS_H, Y_AXIS_L, Y_AXIS_H, Z_AXIS_L, Z_AXIS_H; int16 X_AXIS, Y_AXIS, Z_AXIS; I2C_write(GYRO_ADDR, GYRO_CTRL1_REG, 0x0F); // set gyroscope into active mode I2C_write(GYRO_ADDR, GYRO_CTRL4_REG, 0x30); // set full scale selection to 2000dps for(;;) { //print out gyroscope output X_AXIS_L = I2C_read(GYRO_ADDR, OUT_X_AXIS_L); X_AXIS_H = I2C_read(GYRO_ADDR, OUT_X_AXIS_H); X_AXIS = convert_raw(X_AXIS_H, X_AXIS_L); Y_AXIS_L = I2C_read(GYRO_ADDR, OUT_Y_AXIS_L); Y_AXIS_H = I2C_read(GYRO_ADDR, OUT_Y_AXIS_H); Y_AXIS = convert_raw(Y_AXIS_H, Y_AXIS_L); Z_AXIS_L = I2C_read(GYRO_ADDR, OUT_Z_AXIS_L); Z_AXIS_H = I2C_read(GYRO_ADDR, OUT_Z_AXIS_H); Z_AXIS = convert_raw(Z_AXIS_H, Z_AXIS_L); //printf("X_AXIS_L: %d, X_AXIS_H: %d, average: %d \r\n", X_AXIS_L, X_AXIS_H, (X_AXIS_H+X_AXIS_L)/2); //printf("Y_AXIS_L: %d, Y_AXIS_H: %d, average: %d \r\n", Y_AXIS_L, Y_AXIS_H, (Y_AXIS_H+Y_AXIS_L)/2); //printf("Z_AXIS_L: %d, Z_AXIS_H: %d, average: %d \r\n", Z_AXIS_L, Z_AXIS_H, (Z_AXIS_H+Z_AXIS_L)/2); //printf("H L : %d %d %d %d %d %d \r\n", X_AXIS_L, X_AXIS_H, Y_AXIS_L, Y_AXIS_H, Z_AXIS_L, Z_AXIS_H); //printf("%d %d %d \r\n", X_AXIS, Y_AXIS, Z_AXIS); printf("%d %d %d \r\n", value_convert_gyro(X_AXIS), value_convert_gyro(Y_AXIS), value_convert_gyro(Z_AXIS)); CyDelay(50); } ///-----------------------------------------------------------------*/ /* //magnetometer// //-------------------------------------------------------------- I2C_Start(); uint8 X_L_M, X_H_M, Y_L_M, Y_H_M, Z_L_M, Z_H_M; int16 X_AXIS, Y_AXIS, Z_AXIS; I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL1_REG, 0x37); // set accelerometer & magnetometer into active mode I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL5_REG, 0x10); // set a data rate of 50Hz I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL6_REG, 0x60); // set the full scale selection to +/- 12 Gauss I2C_write(ACCEL_MAG_ADDR, ACCEL_CTRL7_REG, 0x80); // set to continuous-conversion mode for(;;) { X_L_M = I2C_read(ACCEL_MAG_ADDR, OUT_X_L_M); X_H_M = I2C_read(ACCEL_MAG_ADDR, OUT_X_H_M); X_AXIS = convert_raw(X_L_M, X_H_M); Y_L_M = I2C_read(ACCEL_MAG_ADDR, OUT_Y_L_M); Y_H_M = I2C_read(ACCEL_MAG_ADDR, OUT_Y_H_M); Y_AXIS = convert_raw(Y_L_M, Y_H_M); Z_L_M = I2C_read(ACCEL_MAG_ADDR, OUT_Z_L_M); Z_H_M = I2C_read(ACCEL_MAG_ADDR, OUT_Z_H_M); Z_AXIS = convert_raw(Z_L_M, Z_H_M); heading(X_AXIS, Y_AXIS); // printf("MAGNET: %d %d %d %d %d %d \r\n", X_L_M, X_H_M, Y_L_M, Y_H_M, Z_L_M, Z_H_M); //printf("%d %d %d \r\n", X_AXIS,Y_AXIS, Z_AXIS); CyDelay(50); } ///----------------------------------------------------------*/ }
/** * \brief Tests I2C polled mode operation * * \param none * * \return Test result */ CSL_Status CSL_i2cPolledTest(void) { CSL_Status status; CSL_Status result; Uint16 startStop; volatile Uint16 looper; result = CSL_I2C_TEST_FAILED; /* Assign the EEPROM page address */ gI2cWrBuf[0] = 0x0; gI2cWrBuf[1] = 0x0; for(looper = 0; looper < CSL_I2C_DATA_SIZE; looper++) { gI2cWrBuf[looper + CSL_EEPROM_ADDR_SIZE] = looper; gI2cRdBuf[looper] = 0x0000; } /* Initialize I2C module */ status = I2C_init(CSL_I2C0); if(status != CSL_SOK) { printf("I2C Init Failed!!\n"); return(result); } /* Setup I2C module */ i2cSetup.addrMode = CSL_I2C_ADDR_7BIT; i2cSetup.bitCount = CSL_I2C_BC_8BITS; i2cSetup.loopBack = CSL_I2C_LOOPBACK_DISABLE; i2cSetup.freeMode = CSL_I2C_FREEMODE_DISABLE; i2cSetup.repeatMode = CSL_I2C_REPEATMODE_DISABLE; i2cSetup.ownAddr = CSL_I2C_OWN_ADDR; i2cSetup.sysInputClk = CSL_I2C_SYS_CLK; i2cSetup.i2cBusFreq = CSL_I2C_BUS_FREQ; startStop = ((CSL_I2C_START) | (CSL_I2C_STOP)); status = I2C_setup(&i2cSetup); if(status != CSL_SOK) { printf("I2C Setup Failed!!\n"); return(result); } /* Write data */ status = I2C_write(gI2cWrBuf, (CSL_I2C_DATA_SIZE + CSL_EEPROM_ADDR_SIZE), CSL_I2C_EEPROM_ADDR, TRUE, startStop, CSL_I2C_MAX_TIMEOUT); if(status != CSL_SOK) { printf("I2C Write Failed!!\n"); return(result); } printf("I2C Write Complete\n"); /* Give some delay */ for(looper = 0; looper < CSL_I2C_MAX_TIMEOUT; looper++){;} /* Write data EEPROM page address for read operation */ status = I2C_write(gI2cWrBuf, CSL_EEPROM_ADDR_SIZE, CSL_I2C_EEPROM_ADDR, TRUE, startStop, CSL_I2C_MAX_TIMEOUT); if(status != CSL_SOK) { printf("I2C Write Failed!!\n"); return(result); } /* Give some delay */ for(looper = 0; looper < CSL_I2C_MAX_TIMEOUT; looper++){;} /* Read data */ status = I2C_read(gI2cRdBuf, CSL_I2C_DATA_SIZE, CSL_I2C_EEPROM_ADDR, TRUE, startStop, CSL_I2C_MAX_TIMEOUT, FALSE); if(status != CSL_SOK) { printf("I2C Read Failed!!\n"); return(result); } printf("I2C Read Complete\n"); /* Compare the buffers */ for(looper = 0; looper < CSL_I2C_DATA_SIZE; looper++) { if(gI2cWrBuf[looper + CSL_EEPROM_ADDR_SIZE] != gI2cRdBuf[looper]) { printf("Read Write Buffers Does not Match!!\n"); return(result); } } if(looper == CSL_I2C_DATA_SIZE) { printf("Read Write Buffers Match!!\n"); } result = CSL_I2C_TEST_PASSED; return(result); }
/** * \brief Tests I2C polled mode operation * * \param none * * \return Test result */ CSL_Status CSL_i2cPolledTest(void) { CSL_Status status; CSL_Status result; Uint16 startStop; volatile Uint16 looper; volatile int i; result = CSL_I2C_TEST_FAILED; /* Initialize I2C module */ status = I2C_init(CSL_I2C0); if(status != CSL_SOK) { LOG_printf(&trace, "I2C Init Failed!!"); return(result); } /* Setup I2C module */ i2cSetup.addrMode = CSL_I2C_ADDR_7BIT; // EEPROM I2C device address is 7-bit (101.0xxx) i2cSetup.bitCount = CSL_I2C_BC_8BITS; // I2C bit count is 8-bit i2cSetup.loopBack = CSL_I2C_LOOPBACK_DISABLE; i2cSetup.freeMode = CSL_I2C_FREEMODE_DISABLE; i2cSetup.repeatMode = CSL_I2C_REPEATMODE_DISABLE; i2cSetup.ownAddr = CSL_I2C_OWN_ADDR; i2cSetup.sysInputClk = CSL_I2C_SYS_CLK; i2cSetup.i2cBusFreq = CSL_I2C_BUS_FREQ; startStop = ((CSL_I2C_START) | (CSL_I2C_STOP)); status = I2C_setup(&i2cSetup); if(status != CSL_SOK) { LOG_printf(&trace, "I2C Setup Failed!!"); return(result); } for(i = 0; i < CSL_I2C_PAGES; i++){ LOG_printf(&trace, "Page %d", i); /* Assign the EEPROM page address */ gI2cWrBuf[0] = (CSL_I2C_PAGE_SIZE*i)>>8; gI2cWrBuf[1] = (CSL_I2C_PAGE_SIZE*i)&0xFF; for(looper = 0; looper < CSL_I2C_PAGE_SIZE; looper++) { gI2cWrBuf[looper + CSL_EEPROM_ADDR_SIZE] = i*CSL_I2C_PAGE_SIZE + looper; gI2cRdBuf[looper] = 0x0000; } /* Write data */ status = I2C_write(gI2cWrBuf, (CSL_I2C_PAGE_SIZE + CSL_EEPROM_ADDR_SIZE), CSL_I2C_EEPROM_ADDR, TRUE, startStop, CSL_I2C_MAX_TIMEOUT); if(status != CSL_SOK) { LOG_printf(&trace, "\tI2C Write Failed!!"); return(result); } LOG_printf(&trace, "\tI2C Write Complete"); /* Give some delay */ for(looper = 0; looper < CSL_I2C_MAX_TIMEOUT; looper++){;} /* Read data */ status = I2C_read(gI2cRdBuf, CSL_I2C_PAGE_SIZE, CSL_I2C_EEPROM_ADDR, gI2cWrBuf, CSL_EEPROM_ADDR_SIZE, TRUE, startStop, CSL_I2C_MAX_TIMEOUT, FALSE); if(status != CSL_SOK) { LOG_printf(&trace, "\tI2C Read Failed!!"); return(result); } LOG_printf(&trace, "\tI2C Read Complete"); /* Compare the buffers */ for(looper = 0; looper < CSL_I2C_PAGE_SIZE; looper++) { if(gI2cWrBuf[looper + CSL_EEPROM_ADDR_SIZE] != gI2cRdBuf[looper]) { LOG_printf(&trace, "\tRead Write Buffers Does not Match!!"); return(result); } } if(looper == CSL_I2C_PAGE_SIZE) { LOG_printf(&trace, "\tRead Write Buffers Match!!"); } } result = CSL_I2C_TEST_PASSED; return(result); }
int main(void) { SystemInit(); STM32F4_Discovery_LEDInit(LED3); //Orange STM32F4_Discovery_LEDInit(LED4); //Green STM32F4_Discovery_LEDInit(LED5); //Red STM32F4_Discovery_LEDInit(LED6); //Blue STM32F4_Discovery_PBInit(BUTTON_USER, BUTTON_MODE_GPIO); USBD_Init(&USB_OTG_dev,USB_OTG_FS_CORE_ID,&USR_desc,&USBD_CDC_cb,&USR_cb); SystemCoreClockUpdate(); // inicjalizacja dystrybucji czasu procesora init_I2C1(); // na podstawie: http://eliaselectronics.com/stm32f4-tutorials/stm32f4-i2c-mastertutorial/ //acc I2C_start(I2C1, LSM303DL_A_ADDRESS, I2C_Direction_Transmitter); I2C_write(I2C1,0x20); // LSM303_CTRL_REG1_A 0x20 I2C_write(I2C1,0x27); // Enable Accelerometer // 0x27 = 0b00100111 // Normal power mode, all axes enabled I2C_stop(I2C1); // stop the transmission //acc //mag I2C_start(I2C1, LSM303DL_M_ADDRESS, I2C_Direction_Transmitter); I2C_write(I2C1,0x02); //LSM303_MR_REG_M 0x02 I2C_write(I2C1,0x00); // Enable Magnetometer // 0x00 = 0b00000000 // Continuous conversion mode I2C_stop(I2C1); //mag //gyro I2C_start(I2C1, LSM303DL_G_ADDRESS, I2C_Direction_Transmitter); I2C_write(I2C1, 0x20); //L3G_CTRL_REG1 0x20 I2C_write(I2C1, 0x0F); // 0x0F = 0b00001111 // Normal power mode, all axes enabled I2C_stop(I2C1); //gyro char start='0'; while(1) { Delay(5); read_acc(); read_mag(); read_gyro(); start='0'; while(1) { start = usb_cdc_getc(); if(start=='1') { break; } } } /*while (1){ if(usb_cdc_kbhit()){ char c, buffer_out[15]; c = usb_cdc_getc(); switch(c){ case '3': STM32F4_Discovery_LEDToggle(LED3); sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED3_PIN)); usb_cdc_printf(buffer_out); break; case '4': STM32F4_Discovery_LEDToggle(LED4); sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED4_PIN)); usb_cdc_printf(buffer_out); break; case '5': STM32F4_Discovery_LEDToggle(LED5); sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED5_PIN)); usb_cdc_printf(buffer_out); break; case '6': STM32F4_Discovery_LEDToggle(LED6); sprintf(buffer_out,"LED%c = %u\r\n",c,GPIO_ReadInputDataBit(GPIOD,LED6_PIN)); usb_cdc_printf(buffer_out); break; } } button_sts = STM32F4_Discovery_PBGetState(BUTTON_USER); if(button_sts){ STM32F4_Discovery_LEDOff(LED3); STM32F4_Discovery_LEDOff(LED5); STM32F4_Discovery_LEDOff(LED3); STM32F4_Discovery_LEDOff(LED5); } }*/ }