uint8_t status;uint8_t status2;
uint8_t ReadReg[3];
uint8_t Gain,Res_x,Res_y,Res_z;
-
-
- txBuffer[0] = 0x60;
- txBuffer[1] = 0x00;
- txBuffer[2] = 0x5C;
- txBuffer[3] = 0x00;
+ char s[30];
+
+ txBuffer[0] = 0x60; //write in MagSensor
+ txBuffer[1] = 0x00; //Upper Byte
+ txBuffer[2] = 0x5C; //Lower Byte
+ txBuffer[3] = 0x00; //Register Address (shiftet to left by 2 Bits)
status2 = twi_writeTo(Addr, txBuffer, 4,1, true);
twi_readFrom(Addr, &status, 1, true);
twi_writeTo(Addr,txBuffer,4,1,true);
twi_readFrom(Addr, &status, 1, true);
- txBuffer[0] = 0x50;
- txBuffer[1] = 0x90;
+ txBuffer[0] = 0x50;//Read in MagSensor
+ txBuffer[1] = 0x90;//Address of register (shiftet to left by 2 Bits)
twi_writeTo(Addr,txBuffer,2,1,true);
twi_readFrom(Addr, ReadReg, 3, true);
uint16_t T_ref = ReadReg[1]<<8 | ReadReg[2];
+ //sprintf(s,"0x%x 0x%x",ReadReg[0], T_ref);
+ //uputsnl(s);
*Tref = T_ref;
//Read Gain;
txBuffer[1] = 0x00;
*SensY = SensitivityXY(Gain,Res_y);
*SensZ = SensitivityZ(Gain,Res_z);
+ ////////////////////OFFSET_X
+ /* txBuffer[0] = 0x60;
+ txBuffer[1] = 0xee;
+ txBuffer[2] = 0xee;
+ txBuffer[3] = 0x10;
+ twi_writeTo(Addr,txBuffer,4,1,true);
+ twi_readFrom(Addr, &status, 1, true);*/
+ ///////////////////////////////////////
return status2;
}
uint8_t dataTest = 0x3F; // Start Single Measurement
twi_writeTo(Addr,&dataTest,1,1, true);
-
uint8_t status_1;
twi_readFrom(Addr, &status_1, 1, true);
-
+ sprintf(s,"Status: 0x%x",status_1);
+ uputsnl(s);
_delay_ms(100);
uint8_t testdata2 = 0x4F;// Read Measured Values
twi_writeTo(Addr, &testdata2, 1, 1, true);
twi_readFrom(Addr,MagData, 9, true);
+
if (MagData[0] == 3) {
uint16_t tMag = (uint16_t)( MagData[1] * 256 + MagData[2]);
int16_t xMag = MagData[3] * 256 + MagData[4];
* Author: Adrian Weber\r
*/\r
#include "main.h"\r
-\r
+#define uart_maxstrlen 40 //Definiere maximale Laenge für Empfang\r
+volatile uint8_t uart_str_complete = 0; \r
+volatile uint8_t uart_str_count = 0; \r
+volatile char uart_string[uart_maxstrlen+1] = ""; \r
+volatile uint8_t mode = 0;\r
\r
int main(void)\r
{ char s[30];\r
CLKPR = 0x00; //prescaler 2\r
PRR = 0;\r
//Init UART\r
- uinit();\r
+ uinit(); \r
+ sei();\r
\r
//Init I2C/TWI\r
twi_init();\r
// sprintf(s,"%u", Status[0]);\r
//uputs(s);\r
_delay_ms(300);\r
-\r
+ uint8_t Command[5];\r
+ uint8_t I2C_Address = 0;\r
+ uint8_t status[7];\r
+ uint8_t status2;\r
+ uint8_t ReadReg[3] = {0x1,0x3,0x5};\r
while (1)\r
- { \r
- if (Status[0] == 0) { MeasureMagSens(ADDR_MAG_0, TRef[0], &SensX[0], &SensY[0], &SensZ[0]);}\r
- else { uputsnl("ERR Mag 0 NO_DEVICE"); }\r
- if (Status[1] == 0) { MeasureMagSens(ADDR_MAG_1, TRef[1], &SensX[1], &SensY[1], &SensZ[1]);}\r
- else { uputsnl("ERR Mag 1 NO_DEVICE"); }\r
- if (Status[2] == 0) { MeasureMagSens(ADDR_MAG_2, TRef[2], &SensX[2], &SensY[2], &SensZ[2]);}\r
- else { uputsnl("ERR Mag 2 NO_DEVICE"); }\r
- if (Status[3] == 0) { MeasureMagSens(ADDR_MAG_3, TRef[3], &SensX[3], &SensY[3], &SensZ[3]);}\r
- else { uputsnl("ERR Mag 3 NO_DEVICE"); }\r
-\r
- _delay_ms(500);\r
+ { \r
+ if (uart_str_complete == 1) {\r
+ if (strcmp(uart_string,"CONFIG")==0) {\r
+ mode = 1;\r
+ } else if (strcmp(uart_string,"END") == 0 ) {\r
+ mode = 0;\r
+ InitMagSensor(ADDR_MAG_0, &TRef[0], &SensX[0], &SensY[0], &SensZ[0]);\r
+ InitMagSensor(ADDR_MAG_1, &TRef[1], &SensX[1], &SensY[1], &SensZ[1]);\r
+ InitMagSensor(ADDR_MAG_2, &TRef[2], &SensX[2], &SensY[2], &SensZ[2]);\r
+ InitMagSensor(ADDR_MAG_3, &TRef[3], &SensX[3], &SensY[3], &SensZ[3]);\r
+ } else if (strcmp(uart_string,"EXIT") == 0 ) {\r
+ status2 = twi_writeTo(0x0C, 0x80, 1,1, true);\r
+ sprintf(s,"Status:0x%x",status2);\r
+ uputsnl(s);\r
+ _delay_ms(200);\r
+ twi_readFrom(0x0C, &status, 6, true);\r
+ sprintf(s,"Status:0x%x 0x%x",status[0],status[1]);\r
+ uputsnl(s);\r
+ } else if (strcmp(uart_string,"RESET") == 0 ) {\r
+ uint8_t val = 0xD0;\r
+ status2 = twi_writeTo(0x0C, val, 1,1, true);\r
+ sprintf(s,"Status:0x%x",status2);\r
+ uputsnl(s);\r
+ _delay_ms(200);\r
+ twi_readFrom(0x0C, &status, 6, true);\r
+ sprintf(s,"Status:0x%x 0x%x",status[0], status[1]);\r
+ uputsnl(s);\r
+ \r
+ } else if (mode != 0) {\r
+ \r
+ unsigned position = 0;\r
+ char * ptr;\r
+ ptr = strtok(uart_string," ");\r
+ while (ptr != NULL){\r
+ if (ptr[1] == 'x' || ptr[1] == 'X') {\r
+ // HEX\r
+ ptr++;\r
+ ptr++;\r
+ if (position == 0) {\r
+ I2C_Address = (uint8_t) strToHexInt(ptr);\r
+ } else {\r
+ Command[position-1] = (uint8_t) strToHexInt(ptr);\r
+ }\r
+ } else if (ptr[0] == 'x' || ptr[0] == 'X') {\r
+ // HEX\r
+ ptr++;\r
+ if (position == 0) {\r
+ I2C_Address = (uint8_t) strToHexInt(ptr);\r
+ } else {\r
+ Command[position-1] = (uint8_t) strToHexInt(ptr);\r
+ }\r
+ } else {\r
+ //DEC \r
+ if (position == 0) {\r
+ I2C_Address = (uint8_t) strToInt(ptr);\r
+ } else {\r
+ Command[position-1] = (uint8_t) strToInt(ptr);\r
+ }\r
+ \r
+ }\r
+ \r
+ /*switch(position) {\r
+ case 0: // I2C Addr\r
+ sprintf(s,"%u", Command[position]);\r
+ uputsnl(s); \r
+ break;\r
+ case 1: // Modus\r
+ sprintf(s,"%u", Command[position]);\r
+ uputsnl(s); \r
+ break;\r
+ case 2: // Reg Addr\r
+ sprintf(s,"%u", Command[position]);\r
+ uputsnl(s); \r
+ break;\r
+ case 3: // Low Byte\r
+ sprintf(s,"%u", Command[position]);\r
+ uputsnl(s); \r
+ break;\r
+ case 4: // High Byte\r
+ sprintf(s,"%u", Command[position]);\r
+ uputsnl(s); \r
+ break;\r
+ \r
+ default: break;\r
+ \r
+ }*/\r
+ \r
+ \r
+ position++; \r
+ ptr = strtok(NULL, " ");\r
+ }//END while ptr\r
+ \r
+ \r
+ \r
+ \r
+ if (Command[0] == 1){\r
+ Command[0] = 0x60; // write\r
+ Command[3] = (Command[3]<<2) & 0xff;\r
+ // <I2c Address> <Write:1> <Data15:0> <Data7:0> <RegAddress>\r
+ status2 = twi_writeTo(I2C_Address, Command, 4,1, true);\r
+ twi_readFrom(I2C_Address, &status, 1, true);\r
+ sprintf(s,"Status: 0x%x | Write Command: Data[15:8] 0x%x Data[7:0] 0x%x RegAddress: 0x%x",status[0], Command[1],Command[2],Command[3]);\r
+ uputsnl(s);\r
+ } else {\r
+ // <I2c Address> <Read:0> <RegAddress>\r
+ Command[0] = 0x50; // read\r
+ Command[1] = (Command[1]<<2) & 0xff;\r
+ sprintf(s,"Status:0x%x | Send to reg: 0x%x",I2C_Address, Command[1]);\r
+ uputsnl(s);\r
\r
+ twi_writeTo(I2C_Address, Command,2,1,true);\r
+ twi_readFrom(I2C_Address, &ReadReg, 3, true);\r
+ uint16_t Read = ReadReg[1]<<8 | ReadReg[2];\r
+ sprintf(s,"Status: 0x%x | Read result: D[15:8] 0x%x D[7:0] 0x%x",ReadReg[0], ReadReg[1],ReadReg[2]);\r
+ uputsnl(s);\r
+ }\r
+ \r
+ \r
+ }//END if String\r
+ \r
+ uart_str_complete = 0; uart_str_count = 0;\r
+ _delay_ms(200);\r
+ }// END if uart_str_complete\r
+ \r
+ //running mode\r
+ if (mode == 0) {\r
+ if (Status[0] == 0) { MeasureMagSens(ADDR_MAG_0, TRef[0], &SensX[0], &SensY[0], &SensZ[0]);}\r
+ else { uputsnl("ERR Mag 0 NO_DEVICE"); }\r
+ if (Status[1] == 0) { MeasureMagSens(ADDR_MAG_1, TRef[1], &SensX[1], &SensY[1], &SensZ[1]);}\r
+ else { uputsnl("ERR Mag 1 NO_DEVICE"); }\r
+ if (Status[2] == 0) { MeasureMagSens(ADDR_MAG_2, TRef[2], &SensX[2], &SensY[2], &SensZ[2]);}\r
+ else { uputsnl("ERR Mag 2 NO_DEVICE"); }\r
+ if (Status[3] == 0) { MeasureMagSens(ADDR_MAG_3, TRef[3], &SensX[3], &SensY[3], &SensZ[3]);}\r
+ else { uputsnl("ERR Mag 3 NO_DEVICE"); }\r
+ \r
+ _delay_ms(500);\r
+ } else {// CONFIG MODE\r
+ //Setting: I2C Address, Mode, RegAddr, Data Byte Low, Data byte high.\r
+ \r
+ }\r
+\r
+ //}\r
}\r
- cli();\r
+ //cli();\r
return 0;\r
}\r
\r
+\r
+ISR(USART_RX_vect) \r
+{ \r
+ unsigned char buffer = UDR0;\r
+ if (buffer != 10 && buffer != 13 && uart_str_count<(uart_maxstrlen-1)){\r
+ uart_string[uart_str_count] = buffer;\r
+ uart_str_count++;\r
+ uart_str_complete = 0;\r
+ } else {\r
+ uart_string[uart_str_count] = '\0'; \r
+ uart_str_complete = 1;\r
+ \r
+ }\r
+} \r
+\r
+\r
+int strToInt(char *str){\r
+ uint32_t Command = 0;\r
+ while( *str ){\r
+ Command*=10;\r
+ Command+= (((uint32_t) *str++) - 48);\r
+ }\r
+\r
+ return (int) Command;\r
+}\r
+\r
+int strToHexInt(char *str){\r
+ uint32_t Command = 0;\r
+ while( *str ){\r
+ Command*=16;\r
+ if ((uint32_t) *str >47 && (uint32_t) *str < 58 ) {\r
+ Command+= (((uint32_t) *str++) - 48);\r
+ } else if ((uint32_t) *str >64 && (uint32_t) *str < 71 ) {\r
+ Command+= (((uint32_t) *str++) - 55);\r
+ } else if ((uint32_t) *str >96 && (uint32_t) *str < 103 ) { \r
+ Command+= (((uint32_t) *str++) - 87);\r
+ }\r
+ }\r
+\r
+ return (int) Command;\r
+}\r
+\r
+\r