From b9e3261dd8a18eb8ebff97c751b991227ed30449 Mon Sep 17 00:00:00 2001 From: Philipp Klaus Date: Wed, 13 Dec 2017 17:41:34 +0100 Subject: [PATCH] sensor board: latest version as of 2017-10-25 from Adrian Weber --- sensors/MagneticFieldSensor/MagnetSensor.c | 31 ++- sensors/MagneticFieldSensor/main.c | 212 +++++++++++++++++++-- sensors/MagneticFieldSensor/uart/uart.c | 19 +- sensors/MagneticFieldSensor/uart/uart.h | 2 + 4 files changed, 238 insertions(+), 26 deletions(-) diff --git a/sensors/MagneticFieldSensor/MagnetSensor.c b/sensors/MagneticFieldSensor/MagnetSensor.c index a65b195..ec3522c 100644 --- a/sensors/MagneticFieldSensor/MagnetSensor.c +++ b/sensors/MagneticFieldSensor/MagnetSensor.c @@ -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]; diff --git a/sensors/MagneticFieldSensor/main.c b/sensors/MagneticFieldSensor/main.c index 91fcae8..7959d3b 100644 --- a/sensors/MagneticFieldSensor/main.c +++ b/sensors/MagneticFieldSensor/main.c @@ -5,7 +5,11 @@ * Author: Adrian Weber */ #include "main.h" - +#define uart_maxstrlen 40 //Definiere maximale Laenge für Empfang +volatile uint8_t uart_str_complete = 0; +volatile uint8_t uart_str_count = 0; +volatile char uart_string[uart_maxstrlen+1] = ""; +volatile uint8_t mode = 0; int main(void) { char s[30]; @@ -21,7 +25,8 @@ int main(void) CLKPR = 0x00; //prescaler 2 PRR = 0; //Init UART - uinit(); + uinit(); + sei(); //Init I2C/TWI twi_init(); @@ -35,22 +40,199 @@ int main(void) // sprintf(s,"%u", Status[0]); //uputs(s); _delay_ms(300); - + uint8_t Command[5]; + uint8_t I2C_Address = 0; + uint8_t status[7]; + uint8_t status2; + uint8_t ReadReg[3] = {0x1,0x3,0x5}; while (1) - { - if (Status[0] == 0) { MeasureMagSens(ADDR_MAG_0, TRef[0], &SensX[0], &SensY[0], &SensZ[0]);} - else { uputsnl("ERR Mag 0 NO_DEVICE"); } - if (Status[1] == 0) { MeasureMagSens(ADDR_MAG_1, TRef[1], &SensX[1], &SensY[1], &SensZ[1]);} - else { uputsnl("ERR Mag 1 NO_DEVICE"); } - if (Status[2] == 0) { MeasureMagSens(ADDR_MAG_2, TRef[2], &SensX[2], &SensY[2], &SensZ[2]);} - else { uputsnl("ERR Mag 2 NO_DEVICE"); } - if (Status[3] == 0) { MeasureMagSens(ADDR_MAG_3, TRef[3], &SensX[3], &SensY[3], &SensZ[3]);} - else { uputsnl("ERR Mag 3 NO_DEVICE"); } - - _delay_ms(500); + { + if (uart_str_complete == 1) { + if (strcmp(uart_string,"CONFIG")==0) { + mode = 1; + } else if (strcmp(uart_string,"END") == 0 ) { + mode = 0; + InitMagSensor(ADDR_MAG_0, &TRef[0], &SensX[0], &SensY[0], &SensZ[0]); + InitMagSensor(ADDR_MAG_1, &TRef[1], &SensX[1], &SensY[1], &SensZ[1]); + InitMagSensor(ADDR_MAG_2, &TRef[2], &SensX[2], &SensY[2], &SensZ[2]); + InitMagSensor(ADDR_MAG_3, &TRef[3], &SensX[3], &SensY[3], &SensZ[3]); + } else if (strcmp(uart_string,"EXIT") == 0 ) { + status2 = twi_writeTo(0x0C, 0x80, 1,1, true); + sprintf(s,"Status:0x%x",status2); + uputsnl(s); + _delay_ms(200); + twi_readFrom(0x0C, &status, 6, true); + sprintf(s,"Status:0x%x 0x%x",status[0],status[1]); + uputsnl(s); + } else if (strcmp(uart_string,"RESET") == 0 ) { + uint8_t val = 0xD0; + status2 = twi_writeTo(0x0C, val, 1,1, true); + sprintf(s,"Status:0x%x",status2); + uputsnl(s); + _delay_ms(200); + twi_readFrom(0x0C, &status, 6, true); + sprintf(s,"Status:0x%x 0x%x",status[0], status[1]); + uputsnl(s); + + } else if (mode != 0) { + + unsigned position = 0; + char * ptr; + ptr = strtok(uart_string," "); + while (ptr != NULL){ + if (ptr[1] == 'x' || ptr[1] == 'X') { + // HEX + ptr++; + ptr++; + if (position == 0) { + I2C_Address = (uint8_t) strToHexInt(ptr); + } else { + Command[position-1] = (uint8_t) strToHexInt(ptr); + } + } else if (ptr[0] == 'x' || ptr[0] == 'X') { + // HEX + ptr++; + if (position == 0) { + I2C_Address = (uint8_t) strToHexInt(ptr); + } else { + Command[position-1] = (uint8_t) strToHexInt(ptr); + } + } else { + //DEC + if (position == 0) { + I2C_Address = (uint8_t) strToInt(ptr); + } else { + Command[position-1] = (uint8_t) strToInt(ptr); + } + + } + + /*switch(position) { + case 0: // I2C Addr + sprintf(s,"%u", Command[position]); + uputsnl(s); + break; + case 1: // Modus + sprintf(s,"%u", Command[position]); + uputsnl(s); + break; + case 2: // Reg Addr + sprintf(s,"%u", Command[position]); + uputsnl(s); + break; + case 3: // Low Byte + sprintf(s,"%u", Command[position]); + uputsnl(s); + break; + case 4: // High Byte + sprintf(s,"%u", Command[position]); + uputsnl(s); + break; + + default: break; + + }*/ + + + position++; + ptr = strtok(NULL, " "); + }//END while ptr + + + + + if (Command[0] == 1){ + Command[0] = 0x60; // write + Command[3] = (Command[3]<<2) & 0xff; + // + status2 = twi_writeTo(I2C_Address, Command, 4,1, true); + twi_readFrom(I2C_Address, &status, 1, true); + 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]); + uputsnl(s); + } else { + // + Command[0] = 0x50; // read + Command[1] = (Command[1]<<2) & 0xff; + sprintf(s,"Status:0x%x | Send to reg: 0x%x",I2C_Address, Command[1]); + uputsnl(s); + twi_writeTo(I2C_Address, Command,2,1,true); + twi_readFrom(I2C_Address, &ReadReg, 3, true); + uint16_t Read = ReadReg[1]<<8 | ReadReg[2]; + sprintf(s,"Status: 0x%x | Read result: D[15:8] 0x%x D[7:0] 0x%x",ReadReg[0], ReadReg[1],ReadReg[2]); + uputsnl(s); + } + + + }//END if String + + uart_str_complete = 0; uart_str_count = 0; + _delay_ms(200); + }// END if uart_str_complete + + //running mode + if (mode == 0) { + if (Status[0] == 0) { MeasureMagSens(ADDR_MAG_0, TRef[0], &SensX[0], &SensY[0], &SensZ[0]);} + else { uputsnl("ERR Mag 0 NO_DEVICE"); } + if (Status[1] == 0) { MeasureMagSens(ADDR_MAG_1, TRef[1], &SensX[1], &SensY[1], &SensZ[1]);} + else { uputsnl("ERR Mag 1 NO_DEVICE"); } + if (Status[2] == 0) { MeasureMagSens(ADDR_MAG_2, TRef[2], &SensX[2], &SensY[2], &SensZ[2]);} + else { uputsnl("ERR Mag 2 NO_DEVICE"); } + if (Status[3] == 0) { MeasureMagSens(ADDR_MAG_3, TRef[3], &SensX[3], &SensY[3], &SensZ[3]);} + else { uputsnl("ERR Mag 3 NO_DEVICE"); } + + _delay_ms(500); + } else {// CONFIG MODE + //Setting: I2C Address, Mode, RegAddr, Data Byte Low, Data byte high. + + } + + //} } - cli(); + //cli(); return 0; } + +ISR(USART_RX_vect) +{ + unsigned char buffer = UDR0; + if (buffer != 10 && buffer != 13 && uart_str_count<(uart_maxstrlen-1)){ + uart_string[uart_str_count] = buffer; + uart_str_count++; + uart_str_complete = 0; + } else { + uart_string[uart_str_count] = '\0'; + uart_str_complete = 1; + + } +} + + +int strToInt(char *str){ + uint32_t Command = 0; + while( *str ){ + Command*=10; + Command+= (((uint32_t) *str++) - 48); + } + + return (int) Command; +} + +int strToHexInt(char *str){ + uint32_t Command = 0; + while( *str ){ + Command*=16; + if ((uint32_t) *str >47 && (uint32_t) *str < 58 ) { + Command+= (((uint32_t) *str++) - 48); + } else if ((uint32_t) *str >64 && (uint32_t) *str < 71 ) { + Command+= (((uint32_t) *str++) - 55); + } else if ((uint32_t) *str >96 && (uint32_t) *str < 103 ) { + Command+= (((uint32_t) *str++) - 87); + } + } + + return (int) Command; +} + + diff --git a/sensors/MagneticFieldSensor/uart/uart.c b/sensors/MagneticFieldSensor/uart/uart.c index b73721b..bc25990 100644 --- a/sensors/MagneticFieldSensor/uart/uart.c +++ b/sensors/MagneticFieldSensor/uart/uart.c @@ -15,7 +15,7 @@ void uinit() UBRR0L = (unsigned char)baud; UCSR0A = 0; //Disable receiver and Enable transmitter - UCSR0B |= (0 << RXCIE0) | (0 << TXCIE0) | (0 << UDRIE0) | (0 << RXEN0) | ( 1 << TXEN0);//(1<