]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
sensor board: latest version as of 2017-10-25 from Adrian Weber
authorPhilipp Klaus <philipp.klaus@gmail.com>
Wed, 13 Dec 2017 16:41:34 +0000 (17:41 +0100)
committerPhilipp Klaus <philipp.klaus@gmail.com>
Wed, 13 Dec 2017 16:41:34 +0000 (17:41 +0100)
sensors/MagneticFieldSensor/MagnetSensor.c
sensors/MagneticFieldSensor/main.c
sensors/MagneticFieldSensor/uart/uart.c
sensors/MagneticFieldSensor/uart/uart.h

index a65b195b21dee910bbd602f17cfa7837c40746ba..ec3522c39e007f9d5755200f9c193ade7de0a204 100644 (file)
@@ -9,12 +9,12 @@ uint8_t InitMagSensor(uint8_t Addr, uint16_t* Tref, float* SensX, float* SensY,
     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);
@@ -27,11 +27,13 @@ uint8_t InitMagSensor(uint8_t Addr, uint16_t* Tref, float* SensX, float* SensY,
     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; 
@@ -53,6 +55,14 @@ uint8_t InitMagSensor(uint8_t Addr, uint16_t* Tref, float* SensX, float* SensY,
     *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;
 }
 
@@ -66,15 +76,16 @@ void MeasureMagSens(uint8_t Addr, uint16_t Tref, float* SensX, float* SensY, flo
 
     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];
index 91fcae886064dd8bf99f0e052e6b521ac7cb5736..7959d3bb098d645daccf7a14e8071229b8314e9f 100644 (file)
@@ -5,7 +5,11 @@
  *      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
@@ -21,7 +25,8 @@ int main(void)
     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
@@ -35,22 +40,199 @@ int main(void)
      // 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
index b73721b8de4bdd7817029d9abe5ab5a37337730e..bc25990975e80b870359604cac67e39f598ececb 100644 (file)
@@ -15,7 +15,7 @@ void uinit()
        UBRR0L = (unsigned char)baud;\r
        UCSR0A = 0;\r
        //Disable receiver and Enable transmitter\r
-       UCSR0B |= (0 << RXCIE0) | (0 << TXCIE0) | (0 << UDRIE0) | (0 << RXEN0) | ( 1 << TXEN0);//(1<<TXEN0);\r
+       UCSR0B |= (1 << RXCIE0) | (0 << TXCIE0) | (0 << UDRIE0) | (1 << RXEN0) | ( 1 << TXEN0);//(1<<TXEN0);\r
        //Set frame format: 8data, 1stop bit\r
        UCSR0C |= (0<<USBS0)|(3<<UCSZ00);\r
 }\r
@@ -40,3 +40,20 @@ void uputsnl( char *s )
   uputchar('\r');\r
   uputchar('\n');\r
  }\r
\r
+bool ugetavail(void){\r
+  return (UCSR0A & (1<<RXC0));\r
+  \r
+}\r
+/*\r
+bool ugetchar(char &c){\r
+  \r
+ if (ugetavail() == 1){\r
+   *s = UDR0; \r
+   \r
+   return true;\r
+ } else {\r
+   return false;\r
+ }\r
+  \r
+}*/
\ No newline at end of file
index 3eed1f2866beb5d2b6fdd07fb624b4c596747acd..f34425b211e718c44517d688c5e37fc4effaf3bc 100644 (file)
@@ -12,5 +12,7 @@ void uinit( void );
 void uputchar( char c );\r
 void uputs( char *s );\r
 void uputsnl( char *s );\r
+bool ugetavail( void );\r
+//bool ugetchar(char c)\r
 \r
 #endif /* UART_H_ */\r