Ejemplo n.º 1
0
Archivo: imu.cpp Proyecto: pbreed/SBCAR
void ImuRead(ImuRegisters & r, bool bDoMag)
{
I2CStart( 0x68,false,2);
I2CSend(0x3B);//59
I2CStop(20);

rv=I2CReadBuf(0x68, (PBYTE)&r, 14);

if(bDoMag)
{

I2CStart(MAGADDR,false,2);
I2CSend(0x03);
I2CStop(20);
rv2=I2CReadBuf(MAGADDR, (PBYTE)&r.mx, 6);

I2CStart(MAGADDR,false,2);
I2CSend(0x0A);
I2CSend(0x01);
I2CStop(20);
}
else
{
 r.mx=0;
 r.my=0;
 r.mz=0;
}

}
Ejemplo n.º 2
0
char i2c_recv(char addr, char count)
{
  char byteptr, byte_in;

  if (I2CStart()) return 1;
  i2cError = 0;
  byteptr = 0;

  byte_in = addr | 0x01;

  if (ByteOutI2C(byte_in))
    {
      if (i2cError == I2CERR_NAK) I2CStop();
      return i2cError;
    }

  while(count)
    {
      count-=1;
      if (count) {
	byte_in = I2CByteIn(0);
      } else {
	byte_in = I2CByteIn(1);   /* No ACK during last byte */
      }
      i2cReceiveBuffer[byteptr] = byte_in;
      byteptr++;
    }

  I2CStop();

  return (i2cError ? 1 : 0);
}
Ejemplo n.º 3
0
char I2CSendStop(char addr, char count, char send_stop)
{
  char byteptr, byte_out;

  if (I2CStart()) return 1;
  i2cError = 0;

  byte_out = addr & 0xfe;     /* Ensure that it's a write address */
  count++;                    /* Include slave address to byte count */
  byteptr = 0;
  while(count)
    {
      if (ByteOutI2C(byte_out))
        {
	  if (i2cError == I2CERR_NAK && send_stop) I2CStop();
	  return i2cError;
        }
      byte_out = i2cTransmitBuffer[byteptr];
      byteptr++;
      count--;
    }

  if (send_stop) I2CStop();
  return 0;
}
Ejemplo n.º 4
0
/******读SD2200实时数据寄存器******/
void I2CReadDate(void)
{
	uchar m,tmp;
	if(!I2CStart())return;
	I2CSendByte(0x65,1);//从年开始读取数据
	if(!I2CWaitAck()){I2CStop();return;}
	for(m=0;m<7;m++)
	{
		timeBuf[m]=I2CReceiveByte();
		if (m!=6)         //最后一个数据不应答
		{
			I2CAck();
		}
	}
	I2CNoAck();
	I2CStop();
	/*
	for(m=0;m<SEND_TIME_LEN;m++)
   {           //BCD处理
		tmp=timeBuf[m+4]/16;
		sendTimeBuf[m]=timeBuf[m+4]%16;
		sendTimeBuf[m]=sendTimeBuf[m]+tmp*10;
		showTimeBuf[2*m]=timeBuf[m+4]/16;
	    showTimeBuf[2*m+1]=timeBuf[m+4]%16;
   }*/
   //展开处理 因小时需单独处理 12点以上的需减去40
   	tmp=timeBuf[4]/16;
	sendTimeBuf[0]=timeBuf[4]%16;
	sendTimeBuf[0]=sendTimeBuf[0]+tmp*10;
	if(sendTimeBuf[0]>=40){
		sendTimeBuf[0]-=40;
	}
	showTimeBuf[0]=sendTimeBuf[0]/10;
    showTimeBuf[1]=sendTimeBuf[0]%10;

	tmp=timeBuf[5]/16;
	sendTimeBuf[1]=timeBuf[5]%16;
	sendTimeBuf[1]=sendTimeBuf[1]+tmp*10;
	showTimeBuf[2]=sendTimeBuf[1]/10;
    showTimeBuf[3]=sendTimeBuf[1]%10;

	tmp=timeBuf[6]/16;
	sendTimeBuf[2]=timeBuf[6]%16;
	sendTimeBuf[2]=sendTimeBuf[2]+tmp*10;
	showTimeBuf[4]=sendTimeBuf[2]/10;
    showTimeBuf[5]=sendTimeBuf[2]%10;
  	//年月日
	tmp=timeBuf[0]/16;
	sendTimeBuf[3]=timeBuf[0]%16;
	sendTimeBuf[3]=sendTimeBuf[3]+tmp*10;
	tmp=timeBuf[1]/16;
	sendTimeBuf[4]=timeBuf[1]%16;
	sendTimeBuf[4]=sendTimeBuf[4]+tmp*10;
	tmp=timeBuf[2]/16;
	sendTimeBuf[5]=timeBuf[2]%16;
	sendTimeBuf[5]=sendTimeBuf[5]+tmp*10;


}
Ejemplo n.º 5
0
Archivo: i2c.c Proyecto: adinovus/pic
void main()
{
/* Buffer where we will read/write our data */
	unsigned char I2CData[] = {0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x09, 0x00};
	unsigned char i;
	/* Initialize I2C Port */
	I2CInit();
	/* Send Start condition */
	I2CStart();
	/* Send DS1307 slave address with write operation */
	I2CSend(0xD0);
	/* Send subaddress 0x00, we are writing to this location */
	I2CSend(0x00);

	/* Loop to write 8 bytes */
	for (i = 0; i < 8; i++)
 {
		/* send I2C data one by one */
		//I2CSend(I2CInitval[i]);
I2CSend(I2CData[i]);
	}

	/* Send a stop condition - as transfer finishes */
	I2CStop();

	/* We will now read data from DS1307 */
	/* Reading for a memory based device always starts with a dummy write */
	/* Send a start condition */
	I2CStart();
	/* Send slave address with write */
	I2CSend(0xD0);
	/* Send address for dummy write operation */
	/* this address is actually where we are going to read from */
	I2CSend(0x00);

	/* Send a repeated start, after a dummy write to start reading */
	I2CRestart();
	/* send slave address with read bit set */
	I2CSend(0xD1);
	/* Loop to read 8 bytes from I2C slave */
	for (i = 8; i > 0; i--) {
		/* read a byte */
		I2CData[i] = I2CRead();
		/* ACK if its not the last byte to read */
		/* if its the last byte then send a NAK */
		if (i - 1)
			I2CAck();
		else
			I2CNak();
	}
	/* Send stop */
	I2CStop();
	/* end of program */
	while(1);
}
Ejemplo n.º 6
0
BYTE read_register(BYTE address,BYTE regist)
{
    I2CStart();
	I2CWrite(address  | 0);
	I2CWrite(regist);
	I2CRestart();
	I2CWrite(address  | 1);
	BYTE data = I2CRead(1);
	I2CStop();
	I2CStop();
	I2CStop();
	return data;
}
Ejemplo n.º 7
0
void I2CDataCom(unsigned char read_write,unsigned char chipAdr,unsigned char* regAdr,unsigned char* data,unsigned char nbData,unsigned char nbReg) {
/*Standard  polling I2C function for read and write*/ 
    unsigned char i;
 
    I2CStart();
    //Send adress of MPU shifted to 1 to the left in order to send Write command
    if(I2CTransfer(chipAdr)) {
        I2CStop();
        return;
    }
    //send address register
    for(i=0;i<nbReg;i++) {
        if(I2CTransfer(regAdr[i])) {
            I2CStop();
            return;
        }
    }
    if(read_write) {    //If a read request is needed
        //send restart command
        I2CRestart();   
        //resend the address of MPU and add READ command
        if(I2CTransfer(chipAdr|I2C_READ)) {
            I2CStop();
            return;        
        }

        for(i=0;i<nbData;i++) {
            data[i] = I2CReceive();  
            if(i == nbData-1) {
                //Generate ACK for all datas exept the last
                I2CACKDis();    //send a nack
                I2CAckGen();    //generate ACK procedure
            }
            else {
                //Generate NACK for the last value  
                I2CACKEn();
                I2CAckGen();
            }
        }
    } else {
        for(i=0;i<nbData;i++)
        //send datas
            if(I2CTransfer(data[i]))
                break;          //testing if acknowledge has been received
   
    }
    //send STOP
    I2CStop();
}
Ejemplo n.º 8
0
Archivo: imu.cpp Proyecto: pbreed/SBCAR
void WriteReg(BYTE rg, BYTE val)
{
I2CStart( 0x68,false,2);
I2CSend(rg);
I2CSend(val);
I2CStop(20);
}
Ejemplo n.º 9
0
unsigned char DS2482WriteConfig(unsigned char config)
{

    unsigned char read_config;

    I2CStart();
    I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Transmitter);
    I2CWrite(DS2482_CMD_WCFG);
    I2CWrite(config | (~config << 4));
    I2CRestart();
    I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Receiver);
    I2CNotAck();
    I2CStop();
    read_config = I2CRead();

    // check for failure due to incorrect read back
    if (config != read_config)
    {
        DS2482Reset();
        return false;
    }

    return true;

}
Ejemplo n.º 10
0
void OneWireWriteByte(unsigned char sendbyte)
{
    unsigned char status;
    int poll_count = 0;

    I2CStart();
    I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Transmitter);
    I2CWrite(DS2482_CMD_1WWB);
    I2CWrite(sendbyte);
    I2CRestart();
    I2CSendAddress(DS2482_I2C_ADDR, I2C_Direction_Receiver);
    I2CAck();

    do
    {
        status = I2CRead();
    }
    while ((status & DS2482_STATUS_1WB) && (poll_count++ < POLL_LIMIT));

    I2CNotAck();
    I2CStop();
    status = I2CRead();

    if (poll_count >= POLL_LIMIT)
        DS2482Reset();

}
Ejemplo n.º 11
0
uint8_t DS1307Write(uint8_t address,uint8_t data)
{
	uint8_t res;	//result
	
	//Start
	I2CStart();
	
	//SLA+W
	res=I2CWriteByte(0b11010000);	//DS1307 address + W
	
	//Error
	if(!res)	return FALSE;
	
	//Now send the address of required register
	res=I2CWriteByte(address);
	
	//Error
	if(!res)	return FALSE;
	
	//Now write the value
	res=I2CWriteByte(data);
	
	//Error
	if(!res)	return FALSE;
	
	//STOP
	I2CStop();
	
	return TRUE;
}
Ejemplo n.º 12
0
void TM1651_Init(uint8_t backlight)
{
   I2CStart();
 I2CWritebyte(0x88|  backlight);			 //显示控制命令,开显示,脉冲宽度为11/16 0x08表示显示0|0x08|0x00
	//脉冲宽度 1/16-0b000 2/16-0b001 4/16-0b010 10/16-0b011 12/16-0b101 13/16-0b110 14/16-0b111
 I2CStop();
} 
Ejemplo n.º 13
0
/*写SD2200状态寄存器命令*/
void I2CWriteStatus(void)
{		
	if(!I2CStart())return;
	I2CSendByte(0x60,1);      //发送SD2200状态寄存器_1命令
	if(!I2CWaitAck()){I2CStop();return;}   
//	I2CSendByte(0x03,0);      //IC进行复位初始化,24小时制
	I2CSendByte(0x02,0);      //IC不进行复位初始化,24小时制
	I2CWaitAck();
	I2CStop();        
	I2CStart();
	I2CSendByte(0x62,1);      //发送SD2200状态寄存器_2命令
	I2CWaitAck();   
	I2CSendByte(0x00,0);      //清TEST位,禁止中断输出
	I2CWaitAck();
	I2CStop();        
}
Ejemplo n.º 14
0
Archivo: 24c32.c Proyecto: gerbert/ws2
uint8_t AT24_read(struct eeprom_data *data) {
	uint8_t ret = ESUCCESS;
	uint8_t counter = 0;
	uint8_t *p;
	struct eeprom_data *tmp = (struct eeprom_data *)malloc(sizeof(tmp));

	memset(tmp, 0, EEPROM_MU_WR_SIZE);

	// Send EEPROM's i2c address + raise read flag
	ret = I2CStart(at24_addr | TW_READ);
	if (ret > 0)
		return ret;

	// Read one page
	do {
		if (counter + 1 == EEPROM_MU_WR_SIZE)
			ret = I2CReadByte(p, I2C_NOACK);
		else
			ret = I2CReadByte(p, I2C_ACK);

		if (ret > 0)
			return EEEPDATAREAD;
		tmp += *p++;
		counter++;
	} while (counter != EEPROM_MU_WR_SIZE);
	I2CStop();

	data = tmp;
	return ESUCCESS;
}
Ejemplo n.º 15
0
Archivo: pu-test.c Proyecto: gke/UAVP
void BaroTest(void)
{
	uint8 r;

	TxString("\r\nBarometer test\r\n");
	if ( !_UseBaro ) goto BAerror;

	if ( BaroType == BARO_ID_BMP085 )
		TxString("Type:\tBMP085\r\n");
	else
		TxString("Type:\tSMD500\r\n");
	
	if( !StartBaroADC(BARO_PRESS) ) goto BAerror;
	Delay1mS(BARO_PRESS_TIME);
	r = ReadValueFromBaro();
	TxString("Press: \t");	
	TxVal32((int32)BaroVal, 0, 0);
		
	if( !StartBaroADC(BaroTemp) ) goto BAerror;
	Delay1mS(BARO_TEMP_TIME);
	r = ReadValueFromBaro();
	TxString("\tTemp: ");
	TxVal32((int32)BaroVal, 0, 0);	
	TxNextLine();

	TxNextLine();

	return;
BAerror:
	I2CStop();
	TxString("FAIL\r\n");
} // BaroTest
Ejemplo n.º 16
0
void TM1650_CMD(uchar data)
{
	I2CStart();
	I2CWrByte(data);
	I2Cask();
	I2CStop();
}
Ejemplo n.º 17
0
void tda731xSetSpeakers(void)
{
	int8_t spFrontLeft = 0;
	int8_t spFrontRight = 0;

	int8_t spRearLeft = 0;
	int8_t spRearRight = 0;

	if (sndPar[MODE_SND_BALANCE].value > 0) {
		spFrontLeft -= sndPar[MODE_SND_BALANCE].value;
		spRearLeft -= sndPar[MODE_SND_BALANCE].value;
	} else {
		spFrontRight += sndPar[MODE_SND_BALANCE].value;
		spRearRight += sndPar[MODE_SND_BALANCE].value;
	}
	if (sndPar[MODE_SND_FRONTREAR].value > 0) {
		spRearLeft -= sndPar[MODE_SND_FRONTREAR].value;
		spRearRight -= sndPar[MODE_SND_FRONTREAR].value;
	} else {
		spFrontLeft += sndPar[MODE_SND_FRONTREAR].value;
		spFrontRight += sndPar[MODE_SND_FRONTREAR].value;
	}

	I2CStart(TDA731X_I2C_ADDR);
	I2CWriteByte(TDA731X_SP_REAR_LEFT | -spRearLeft);
	I2CWriteByte(TDA731X_SP_REAR_RIGHT | -spRearRight);
	I2CWriteByte(TDA731X_SP_FRONT_LEFT | -spFrontLeft);
	I2CWriteByte(TDA731X_SP_FRONT_RIGHT | -spFrontRight);
	I2CStop();

	return;
}
Ejemplo n.º 18
0
void ks0066Init(void)
{
	I2CStart(PCF8574_ADDR, I2C_WRITE);

	i2cData &= ~PCF8574_BL_LINE;
	i2cData &= ~PCF8574_E_LINE;
	i2cData &= ~PCF8574_RW_LINE;
	i2cData &= ~PCF8574_RS_LINE;

	ks0066SetData(KS0066_FUNCTION | KS0066_8BIT);	/* Init data */
	ks0066WriteStrob();
	delay_ms(20);
	ks0066WriteStrob();
	delay_ms(5);
	ks0066WriteStrob();
	delay_us(120);
	ks0066WriteStrob();

	i2cData &= ~PCF8574_RW_LINE;
	i2cData &= ~PCF8574_RS_LINE;

	ks0066SetData(KS0066_FUNCTION);
	ks0066WriteStrob();

	I2CStop();

	ks0066WriteCommand(KS0066_FUNCTION | KS0066_2LINES);
	ks0066WriteCommand(KS0066_DISPLAY | KS0066_DISPAY_ON);
	ks0066WriteCommand(KS0066_CLEAR);
	delay_ms(2);
	ks0066WriteCommand(KS0066_SET_MODE | KS0066_INC_ADDR);

	return;
}
Ejemplo n.º 19
0
/*
 Write a value to a register on ADXL345
 */
void accelWriteReg(const Accel *a, int reg, BYTE val){
    int I2C = a->I2C;
    sendStart(I2C);                 // start transaction
    sendByte(I2C, a->write);        // write accel device
    sendByte(I2C, reg);             // accel device register
    sendByte(I2C, val);             // value
    I2CStop(I2C);
}
Ejemplo n.º 20
0
/**
 * Invokes an I2C stop condition
 * 
 */
void I2C_StopTransfer()
{
    // Send the Stop signal
    I2CStop(I2C1);

    // Wait for the signal to complete
    while (!(I2CGetStatus(I2C1) & I2C_STOP));
}
Ejemplo n.º 21
0
void tda731xSetInput(void)
{
	I2CStart(TDA731X_I2C_ADDR);
	I2CWriteByte(TDA731X_SW | (3 - sndPar[MODE_SND_GAIN0 + aproc.input].value) << 3 | !(aproc.extra & APROC_EXTRA_LOUDNESS) << 2 | aproc.input);
	I2CStop();

	return;
}
Ejemplo n.º 22
0
void tda731xSetVolume(void)
{
	I2CStart(TDA731X_I2C_ADDR);
	I2CWriteByte(TDA731X_VOLUME | -sndPar[MODE_SND_VOLUME].value);
	I2CStop();

	return;
}
Ejemplo n.º 23
0
/************显示函数 固定地址写数据 ************/
void disp(uint8_t add, uint8_t  value)
{
 I2CStart();
 I2CWritebyte(0x44);			//数据命令设置,固定地址,写数据到显示寄存器	
 I2CStop();

 I2CStart();
  I2CWritebyte(add);				//地址命令设置,写入add对应地址

 I2CWritebyte(CODE00[value]);			//给add地址写数据
 I2CStop();

// I2CStart();
//  I2CWritebyte(0x80);				//显示控制命令,开显示,脉冲宽度为11/16 0x08表示显示0|0x08|0x00
//	//脉冲宽度 1/16-0b000 2/16-0b001 4/16-0b010 10/16-0b011 12/16-0b101 13/16-0b110 14/16-0b111
// I2CStop();

}
Ejemplo n.º 24
0
Archivo: gps.c Proyecto: petejbell/pits
// *****************************************************************************
// read one byte
// *****************************************************************************
uint8_t I2CGetc(struct i2c_info *bb)
{
    uint8_t rv;
    I2CStart(bb);
    I2CSend(bb, (bb->address * 2)+1); // address
    rv = I2CRead(bb, 1);
    I2CStop(bb); // stop
    return rv;    
}
Ejemplo n.º 25
0
void communications() {
    I2CStart();
    I2CPut(0xb0);
    I2CPut('U');
    I2CStart();
    I2CPut(0xb1);
    I2CPut('Y');
    I2CStop();
}
Ejemplo n.º 26
0
void tea63x0SetSpeakers()
{
    int8_t spFR = sndPar[MODE_SND_FRONTREAR].value;

    // Front channels
    I2CStart(TEA63X0_I2C_ADDR);
    I2CWriteByte(TEA63X0_FADER);
    I2CWriteByte(TEA63X0_MFN | TEA63X0_FCH | (spFR < 0 ? 15 + spFR : 15));
    I2CStop();

    // Rear channels
    I2CStart(TEA63X0_I2C_ADDR);
    I2CWriteByte(TEA63X0_FADER);
    I2CWriteByte(TEA63X0_MFN | (spFR < 0 ? 15 : 15 - spFR));
    I2CStop();

    return;
}
Ejemplo n.º 27
0
void Tm1651_BackLight(uint16_t jibie)
{
  
  I2CStart();
  I2CWritebyte(0x88|jibie);			 //显示控制命令,开显示,脉冲宽度为11/16 0x08表示显示0|0x08|0x00
	//脉冲宽度 1/16-0b000 2/16-0b001 4/16-0b010 10/16-0b011 12/16-0b101 13/16-0b110 14/16-0b111
  //F 4bit 1表示开显示 0关显示 3bit 2bit 1bit表示脉冲宽度
  I2CStop();
}
Ejemplo n.º 28
0
void tea63x0SetInputMute(void)
{
    I2CStart(TEA63X0_I2C_ADDR);
    I2CWriteByte(TEA63X0_AUDIO_SW);
    I2CWriteByte((aproc.mute ? TEA63X0_GMU : 0) | (1 << aproc.input));
    I2CStop();

    return;
}
Ejemplo n.º 29
0
Archivo: pu-test.c Proyecto: gke/UAVP
void DoCompassTest()
{
	uint16 v;

	TxString("\r\nCompass test\r\n");

	I2CStart();
	if( SendI2CByte(COMPASS_I2C_ID+1) != I2C_ACK ) goto CTerror;
	v = ((uint16)RecvI2CByte(I2C_ACK)*256) | RecvI2CByte(I2C_NACK);
	I2CStop();

	TxVal32((int32)v, 1, 0);
	TxString("deg\r\n");		
	return;
CTerror:
	I2CStop();
	TxString("FAIL\r\n");
} // DoCompassTest
Ejemplo n.º 30
0
BOOLEAN NTAPI
VideoPortDDCMonitorHelper(
   PVOID HwDeviceExtension,
   PVOID I2CFunctions,
   PUCHAR pEdidBuffer,
   ULONG EdidBufferSize
   )
{
   PDDC_CONTROL ddc = (PDDC_CONTROL)I2CFunctions;
   PI2C_CALLBACKS i2c = &ddc->I2CCallbacks;
   INT Count, i;
   PUCHAR pBuffer = (PUCHAR)pEdidBuffer;
   BOOL Ack;

   TRACE_(VIDEOPRT, "VideoPortDDCMonitorHelper()\n");

   ASSERT_IRQL_LESS_OR_EQUAL(PASSIVE_LEVEL);
   if (ddc->Size != sizeof (ddc))
     {
        WARN_(VIDEOPRT, "ddc->Size != %d (%d)\n", sizeof (ddc), ddc->Size);
        return FALSE;
     }

   /* select eeprom */
   if (!I2CStart(HwDeviceExtension, i2c, DDC_EEPROM_ADDRESS | WRITE))
     return FALSE;
   /* set address */
   if (!I2CWrite(HwDeviceExtension, i2c, 0x00))
     return FALSE;
   /* change into read mode */
   if (!I2CRepStart(HwDeviceExtension, i2c, DDC_EEPROM_ADDRESS | READ))
     return FALSE;
   /* read eeprom */
   RtlZeroMemory(pEdidBuffer, EdidBufferSize);
   Count = min(128, EdidBufferSize);
   for (i = 0; i < Count; i++)
     {
        Ack = ((i + 1) < Count);
        pBuffer[i] = I2CRead(HwDeviceExtension, i2c, Ack);
     }
   I2CStop(HwDeviceExtension, i2c);

   /* check EDID header */
   if (pBuffer[0] != 0x00 || pBuffer[1] != 0xff ||
       pBuffer[2] != 0xff || pBuffer[3] != 0xff ||
       pBuffer[4] != 0xff || pBuffer[5] != 0xff ||
       pBuffer[6] != 0xff || pBuffer[7] != 0x00)
     {
        WARN_(VIDEOPRT, "VideoPortDDCMonitorHelper(): Invalid EDID header!\n");
        return FALSE;
     }

   INFO_(VIDEOPRT, "VideoPortDDCMonitorHelper(): EDID version %d rev. %d\n", pBuffer[18], pBuffer[19]);
   INFO_(VIDEOPRT, "VideoPortDDCMonitorHelper() - SUCCESS!\n");
   return TRUE;
}