void TLC59116_WriteReg(byte addr, byte reg, byte val) { I2C1_Start(); I2C1_Wr(0xC0 | (addr << 1) | WRITE); I2C1_Wr(AUTOINC_ALL | reg); I2C1_Wr(val); I2C1_Stop(); }
void TLC59116_ResetAll() { I2C1_Start(); I2C1_Wr(0xD6); I2C1_Wr(0xA5); I2C1_Wr(0x5A); I2C1_Stop(); }
//https://electrosome.com/wp-content/uploads/2012/05/data-write.png void write_ds1307(unsigned short address, unsigned short w_data) { I2C1_Start(); I2C1_Wr(0xD0); I2C1_Wr(address); I2C1_Wr(w_data); I2C1_Stop(); }
void escreve(unsigned short address, unsigned short dado) { I2C1_Start(); I2C1_Wr(0xA0); //endereco disp + escrever I2C1_Wr(address); I2C1_Wr(dado); I2C1_Stop(); Delay_ms(10); }
void TLC59116_WriteLEDs(byte addr, byte * val) { I2C1_Start(); I2C1_Wr(0xC0 | (addr << 1) | WRITE); I2C1_Wr(AUTOINC_ALL | LEDOUT0); I2C1_Wr(val[0]); I2C1_Wr(val[1]); I2C1_Wr(val[2]); I2C1_Wr(val[3]); I2C1_Stop(); }
void setup_ds1621() { I2C1_Start(); I2C1_Wr(0x90); // connect to DS1621 (#0) I2C1_Wr(0xAC); // Access Config I2C1_Wr(0x02); // set for continuous conversion I2C1_Repeated_Start(); I2C1_Wr(0x90); // restart I2C1_Wr(0xEE); // start conversions I2C1_Stop(); }
void Write_Time(unsigned char sec, unsigned char min, unsigned char hours, unsigned char day, unsigned char dayofweek, unsigned char month, unsigned char year) { I2C1_Start(); // issue start signal I2C1_Wr(DEVICEID_DS1307); // address DS1307 which is 0xD0 I2C1_Wr(0); // start from word at address (REG0) I2C1_Wr(0x80 + sec); // write $80 to REG0. (pause counter + 0 sec) I2C1_Wr(min); // write 0 to minutes word to (REG1) I2C1_Wr(hours); // write 17 to hours word (24-hours mode)(REG2) I2C1_Wr(dayofweek); // write 5 - Tuesday (REG3) I2C1_Wr(day); // write 18 to date word (REG4) I2C1_Wr(month); // write 10 (Oct) to month word (REG5) I2C1_Wr(year); // write 12 to year word (REG6) I2C1_Stop(); // issue stop signal I2C1_Start(); // issue start signal I2C1_Wr(DEVICEID_DS1307); // address DS1307 which is 0xD0 I2C1_Wr(0); // start from word at address 0 I2C1_Wr(0); // write 0 to REG0 (enable counting + 0 sec) I2C1_Stop(); // issue stop signal }
unsigned short le(unsigned short address) { unsigned short dado; I2C1_Start(); I2C1_Wr(0xA0); //endereco disp + escrever I2C1_Wr(address); //aponta p/ endereco dado I2C1_Repeated_Start(); I2C1_Wr(0xA1); //endereco disp + ler dado = I2C1_Rd(0u);//no acknowledge ? I2C1_Stop(); return (dado); }
//Address reference //https://electrosome.com/wp-content/uploads/2012/05/Time-Keeper-Registers.png //https://electrosome.com/wp-content/uploads/2012/05/Reading-Data-from-DS1307.jpg unsigned short read_ds1307(unsigned short address) { unsigned short temp; I2C1_Start(); I2C1_Wr(0xD0); I2C1_Wr(address); I2C1_Repeated_Start(); I2C1_Wr(0xD1); temp = I2C1_Rd(0); I2C1_Stop(); return(temp); }
void MPU6050_get(int cmd, uint8_t read[6]) { I2Cerror = 0; I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); if (I2Cerror == 0) { I2C1_SendByte(cmd); I2C1_WaitAck(); if (I2Cerror == 0) { I2C1_Stop(); I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFF));//ff-1(Read) I2C1_WaitAck(); if (I2Cerror == 0) { read[0] = I2C1_ReceiveByte(); //receive I2C1_Ack(); read[1] = I2C1_ReceiveByte(); //receive I2C1_Ack(); read[2] = I2C1_ReceiveByte(); //receive I2C1_Ack(); read[3] = I2C1_ReceiveByte(); //receive I2C1_Ack(); read[4] = I2C1_ReceiveByte(); //receive I2C1_Ack(); read[5] = I2C1_ReceiveByte(); //receive I2C1_NoAck(); I2C1_Stop(); } } } }
void Main() { Setup(); I2C_Master_Init(); I2C1_Init(100000); while(1){ I2C1_Start(); I2C1_Wr(8 << 1); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(100); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Stop(); Delay_ms(500); I2C1_Start(); I2C1_Wr(8 << 1); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Stop(); Delay_ms(500); } }
//*********************************************************** void MemWrt(unsigned int address,unsigned char data_m) { unsigned char Add_H,Add_L; Add_H=((address&0xFF00)>>8); Add_L=(address&0x00FF); I2C1_Init(100000); I2C1_Start(); I2C1_Wr(0xA0); I2C1_Wr(Add_H); I2C1_Wr(Add_L); I2C1_Wr(data_m); I2C1_Stop(); Delay_ml(20); }//MemWrt
void Read_Time(unsigned char *sec, unsigned char *min, unsigned char *hr, unsigned char *week_day, unsigned char *day, unsigned char *mn, unsigned char *year) { I2C1_Start(); I2C1_Wr(DEVICEID_DS1307); I2C1_Wr(0); I2C1_Repeated_Start(); I2C1_Wr(0xD1); *sec =I2C1_Rd(1); *min =I2C1_Rd(1); *hr =I2C1_Rd(1); *week_day =I2C1_Rd(1); *day =I2C1_Rd(1); *mn =I2C1_Rd(1); *year =I2C1_Rd(0); I2C1_Stop(); }
//*********************************************************** unsigned char MemRd(unsigned int address) { unsigned char data_m,Add_H,Add_L; Add_H=((address&0xff00)>>8); Add_L=(address&0x00FF); I2C1_Init(100000); I2C1_Start(); I2C1_Wr(0xA0); I2C1_Wr(Add_H); I2C1_Wr(Add_L); I2C1_Repeated_Start(); I2C1_Wr(0xA1); data_m = I2C1_Rd(0u); I2C1_Stop(); return data_m; }//MemRd
void Read_Compass(){ //Lcd_Out(1,1,"Read_Compass"); //delay_ms(1000); I2C1_Start(); I2C1_Wr(0x3C); //sellecting magnatometer I2C1_Wr(0x02); I2C1_Wr(0x00); //single mode readinh I2C1_Stop(); I2C1_Start(); Delay_ms(15); if(I2c1_Is_Idle()) { I2C1_Wr(0x3C); I2C1_Wr(0x03); I2C1_Repeated_Start(); I2C1_Wr(0x3D); x_h=I2c1_Rd(0) ; } I2C1_Stop(); I2C1_Start(); Delay_ms(15); if(I2c1_Is_Idle()) { I2C1_Wr(0x3C); I2C1_Wr(0x04); I2C1_Repeated_Start(); I2C1_Wr(0x3D); x_l=I2c1_Rd(0); } I2C1_Stop(); I2C1_Start(); Delay_ms(15); if(I2c1_Is_Idle()) { I2C1_Wr(0x3C); I2C1_Wr(0x05); I2C1_Repeated_Start(); I2C1_Wr(0x3D); z_h=I2c1_Rd(0); } I2C1_Stop(); I2C1_Start(); Delay_ms(15); if(I2c1_Is_Idle()) { I2C1_Wr(0x3C); I2C1_Wr(0x06); I2C1_Repeated_Start(); I2C1_Wr(0x3D); z_l=I2c1_Rd(0) ; } I2C1_Stop(); I2C1_Start(); Delay_ms(15); if(I2c1_Is_Idle()) { I2C1_Wr(0x3C); I2C1_Wr(0x07); I2C1_Repeated_Start(); I2C1_Wr(0x3D); y_h=I2c1_Rd(0) ; } I2C1_Stop(); I2C1_Start(); Delay_ms(15); if(I2c1_Is_Idle()) { I2C1_Wr(0x3C); I2C1_Wr(0x08); I2C1_Repeated_Start(); I2C1_Wr(0x3D); y_l=I2c1_Rd(0) ; } I2C1_Stop(); X_Value=convert(x_h,x_l); Y_Value=convert(y_h,y_l); // WordToStr(X_Value,to_LCD); // Lcd_Out(1,1,to_LCD); if(X_Value>=0 && Y_Value>=0){ //1 angle=atan((((double)Y_Value/(double)X_Value))); angle=angle*((180)/(3.14159265)); }else if(X_Value<0 && Y_Value>=0){ //2 angle=atan((((double)Y_Value/((double)(-1)*X_Value)))); angle=angle*((180)/(3.14159265)); angle=180-angle; }else if(X_Value<0 && Y_Value<0){ //3 angle=atan((((double)Y_Value/(double)X_Value))); angle=angle*((180)/(3.14159265)); angle=angle+180; }else if(X_Value>=0 && Y_Value<0){ //4 angle=atan(((((double)(-1)*Y_Value)/(double)X_Value))); angle=angle*((180)/(3.14159265)); angle=360-angle; } if(X_value>=0){ }else{ X_value=(-1)*X_value; } if(Y_value>=0){ }else{ Y_value=(-1)*Y_value; } Delay_ms(100); }
int MPU6050_Init(void) { uint8_t mpu_adr; // Check to make sure there is a device out there and its on the // correct address I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); I2C1_SendByte(0x75); // Who Am I I2C1_WaitAck(); I2C1_Stop(); Delay_ms(1); I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFF));//ff-1(Read) I2C1_WaitAck(); mpu_adr = I2C1_ReceiveByte();//receive I2C1_NoAck(); I2C1_Stop(); // if wrong address or no device then bail out with an error if (mpu_adr != 0x68) { return -1; } Delay_ms(5); // force a device reset I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); I2C1_SendByte(0x6B); // Force a reset I2C1_WaitAck(); I2C1_SendByte(0x80); I2C1_WaitAck(); I2C1_Stop(); Delay_ms(150); // set the internal clock to be the Z AXIS gyro I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); I2C1_SendByte(0x6B); I2C1_WaitAck(); I2C1_SendByte(0x03); // clock source AKA - changed from 0x00 (internal clock) I2C1_WaitAck(); I2C1_Stop(); Delay_ms(5); // turn off all sleep modes I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); I2C1_SendByte(0x6C); I2C1_WaitAck(); I2C1_SendByte(0x00); // wake up ctrl I2C1_WaitAck(); I2C1_Stop(); Delay_ms(5); // Set the sample rate on the accel and refresh rate on the gyro I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); I2C1_SendByte(0x19); // Sample output rate I2C1_WaitAck(); I2C1_SendByte(0x00); I2C1_WaitAck(); I2C1_Stop(); Delay_ms(5); // turn on the built in LPF I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); I2C1_SendByte(0x1A); I2C1_WaitAck(); I2C1_SendByte(0x00); //low pass disable AKA - was 0x02 for 98hz I2C1_WaitAck(); I2C1_Stop(); Delay_ms(5); // set the gyro scale I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); I2C1_SendByte(0x1B); I2C1_WaitAck(); I2C1_SendByte(0x00); //set to 250LSB/Deg/s I2C1_WaitAck(); I2C1_Stop(); Delay_ms(5); // set the accel scale I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); I2C1_SendByte(0x1C); I2C1_WaitAck(); I2C1_SendByte(0x00); //set to accel to +/-2g scale AKA - was 0x08 for +/-4g I2C1_WaitAck(); I2C1_Stop(); Delay_ms(5); // configure the interrupt(s) pin because we don't use it I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); I2C1_SendByte(0x37); // init pin config I2C1_WaitAck(); I2C1_SendByte(0x00); I2C1_WaitAck(); I2C1_Stop(); Delay_ms(5); // disable the interrupt pin(s) I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); I2C1_SendByte(0x38); // init enable I2C1_WaitAck(); I2C1_SendByte(0x00); I2C1_WaitAck(); I2C1_Stop(); Delay_ms(5); /* // this was bad code and was removed I2C1_Start(); I2C1_SendByte((MPU6050_ADDR & 0xFE));//fe-0(Write) I2C1_WaitAck(); I2C1_SendByte(0x6A); I2C1_WaitAck(); I2C1_SendByte(0x01); // reset signal paths I2C1_WaitAck(); I2C1_Stop(); Delay_ms(5); */ return 0; }