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(); }
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 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); }