--- /dev/null
+#include "MagnetSensor.h"
+//#define CHIP_OLD 1
+
+uint8_t InitMagSensor(uint8_t Addr, uint16_t* Tref, float* SensX, float* SensY, float* SensZ, uint8_t *TC_Enabled)
+{
+
+ // Write register command, AH = 0x00, AL = 0x5C, Hall plate spinning rate = DEFAULT, GAIN_SEL = 5(0x60, 0x00, 0x5C, 0x00)
+ // Address register, (0x00 << 2)
+ uint8_t txBuffer[4] = {0x60,0x00,0x5C,0x00};
+ uint8_t status;uint8_t status2;
+ uint8_t ReadReg[3];
+ uint8_t Gain,Res_x,Res_y,Res_z;
+// char s[30];
+
+ // Read temperature compensation
+ txBuffer[0] = 0x50; //read in MagSensor
+ txBuffer[1] = 0x04; //Register 0x1 (shiftet to left by 2 Bits)
+ twi_writeTo(Addr, txBuffer, 2,1, true);
+ twi_readFrom(Addr, &ReadReg[0], 3, true);
+ *TC_Enabled = (ReadReg[1] & 0x4 ) >> 2;
+
+ // Write reg addr 0x0 (hall config...)
+ 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);
+
+ // Write register command, AH = 0x02, AL = 0xB4, RES for magnetic measurement = 1(0x60, 0x02, 0xB4, 0x08)
+ // Address register, (0x02 << 2)
+ txBuffer[1] = 0x02;
+ txBuffer[2] = 0xB4;
+ txBuffer[3] = 0x08;
+ twi_writeTo(Addr,txBuffer,4,1,true);
+ twi_readFrom(Addr, &status, 1, true);
+
+ 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;
+ twi_writeTo(Addr,txBuffer,2,1,true);
+ twi_readFrom(Addr, ReadReg, 3, true);
+
+ Gain = (ReadReg[2] & 0x70) >>4;
+
+ //Read Resolution;
+ txBuffer[1] = 0x08; //Read Gain;
+ twi_writeTo(Addr,txBuffer,2,1,true);
+ twi_readFrom(Addr, ReadReg, 3, true);
+
+ Res_x = (ReadReg[1] & 0x6) >>1;
+ Res_y = ((ReadReg[1] & 0x1) << 1)|((ReadReg[2] & 0x80) >>7);
+ Res_z = (ReadReg[2] & 0x60) >>5;
+
+ *SensX = SensitivityXY(Gain,Res_x);
+ *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;
+}
+
+
+void MeasureMagSens(uint8_t Addr, uint16_t Tref, float* SensX, float* SensY, float* SensZ, const uint8_t TC_Enabled, int16_t* Meas){
+ uint8_t MagData[9] = {255,255,255,255,255,255,255,255,255};
+ char s[30];
+// char SerNum[10];
+ char Num[2];
+ sprintf(Num,"%1u",Addr-12);
+
+ 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);
+#ifdef USE_UART
+ sprintf(s,"Status %02u 0x%x: 0x%x",SERIALNUM,Addr,status_1);
+ uputsnl(s);
+#endif
+ _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;
+ int16_t yMag;
+ int16_t zMag;
+ if (TC_Enabled) {
+ /*
+ uint16_t u_xMag = MagData[3] * 256 + MagData[4];
+ uint16_t u_yMag = MagData[5] * 256 + MagData[6];
+ uint16_t u_zMag = MagData[7] * 256 + MagData[8];
+ xMag = u_xMag ^ 0x8000;
+ yMag = u_yMag ^ 0x8000;
+ zMag = u_zMag ^ 0x8000;*/
+ xMag = (MagData[3] ^ 0x80) * 256 + MagData[4];
+ yMag = (MagData[5] ^ 0x80) * 256 + MagData[6];
+ zMag = (MagData[7] ^ 0x80) * 256 + MagData[8];
+ } else {
+ xMag = MagData[3] * 256 + MagData[4];
+ yMag = MagData[5] * 256 + MagData[6];
+ zMag = MagData[7] * 256 + MagData[8];
+ }
+ int16_t temp_temp = (uint16_t)(tMag) - (uint16_t)(Tref);
+ float Temperature = 35.0 + (temp_temp/45.2);
+
+ Meas[0] = xMag;
+ Meas[1] = yMag;
+ Meas[2] = zMag;
+ Meas[3] = temp_temp*22.12 + 35; //Millikelvin
+#ifdef USE_UART
+ uputs("M");sprintf(SerNum,"_%02u_",SERIALNUM);uputs(SerNum);uputs(Num);uputs("_T ");
+ sprintf(s,"%4.2f",Temperature);
+ uputsnl(s);
+ uputs("M");sprintf(SerNum,"_%02u_",SERIALNUM);uputs(SerNum);uputs(Num);uputs("_X ");
+ sprintf(s,"%4.3f",((float)xMag*(*SensX))); //muT
+ uputsnl(s);
+ uputs("M");sprintf(SerNum,"_%02u_",SERIALNUM);uputs(SerNum);uputs(Num);uputs("_Y ");
+ sprintf(s,"%4.3f",((float)yMag*(*SensY))); //muT
+ uputsnl(s);
+ uputs("M");sprintf(SerNum,"_%02u_",SERIALNUM);uputs(SerNum);uputs(Num);uputs("_Z ");
+ sprintf(s,"%4.3f",((float)zMag*(*SensZ))); //muT
+ uputsnl(s);
+ uputs("ERR Mag ");sprintf(SerNum,"%2u ",SERIALNUM);uputs(SerNum);uputs(Num);uputsnl(" NO"); // ERROR INFORMATION FOR EPICS
+#endif
+ } else {
+#ifdef USE_UART
+ uputs("ERR Mag ");sprintf(SerNum,"%2u ",SERIALNUM);uputs(SerNum);uputs(Num);uputsnl(" CONNECTION");
+#endif
+ }
+}
+
+float SensitivityXY(uint8_t Gain, uint8_t Res){
+ float Sens =0.805;
+ /*if (Gain != 0) {
+ for (int i=1; i<=Gain;i++){
+ if (i<3) {
+ logVal = 1;
+ } else if (i<5) {
+ logVal = 2;
+ } else {
+ logVal = 3;
+ }
+ Sens = roundf((Sens - 0.161/((float)logVal)) * 1000) / 1000;
+ }
+ sprintf(s,"LOG: %f",Sens);
+ uputsnl(s);
+ }*/
+
+
+#ifdef CHIP_OLD
+ switch(Gain) {
+ case 0: Sens=0.805; break;
+ case 1: Sens=0.644; break;
+ case 2: Sens=0.483; break;
+ case 3: Sens=0.403; break;
+ case 4: Sens=0.322; break;
+ case 5: Sens=0.268; break;
+ case 6: Sens=0.215; break;
+ default: Sens=0.161; break;
+ }
+#else
+ switch(Gain) {
+ case 0: Sens=0.751; break;
+ case 1: Sens=0.601; break;
+ case 2: Sens=0.451; break;
+ case 3: Sens=0.376; break;
+ case 4: Sens=0.3002; break;
+ case 5: Sens=0.250; break;
+ case 6: Sens=0.200; break;
+ default: Sens=0.150; break;
+ }
+
+#endif
+
+
+ return Sens*(float)pow(2,Res);
+}
+
+float SensitivityZ(uint8_t Gain, uint8_t Res){
+ float Sens =0.805;
+#ifdef CHIP_OLD
+ switch(Gain) {
+ case 0: Sens=1.468; break;
+ case 1: Sens=1.174; break;
+ case 2: Sens=0.881; break;
+ case 3: Sens=0.734; break;
+ case 4: Sens=0.587; break;
+ case 5: Sens=0.489; break;
+ case 6: Sens=0.391; break;
+ default: Sens=0.294; break;
+ }
+#else
+ switch(Gain) {
+ case 0: Sens=1.210; break;
+ case 1: Sens=0.968; break;
+ case 2: Sens=0.726; break;
+ case 3: Sens=0.605; break;
+ case 4: Sens=0.484; break;
+ case 5: Sens=0.403; break;
+ case 6: Sens=0.323; break;
+ default: Sens=0.242; break;
+ }
+#endif
+
+
+ return Sens*(float)pow(2,Res);
+}
--- /dev/null
+#include "main.h"
+
+uint8_t InitMagSensor(uint8_t Addr, uint16_t* Tref, float *SensX, float* SensY, float *SensZ, uint8_t *TC_Enabled);
+void MeasureMagSens(uint8_t Addr, uint16_t Tref, float* SensX, float* SensY, float* SensZ, const uint8_t TC_Enabled,int16_t* Meas);
+float SensitivityXY(uint8_t Gain, uint8_t Res);
+float SensitivityZ(uint8_t Gain, uint8_t Res);
--- /dev/null
+# Hey Emacs, this is a -*- makefile -*-\r
+\r
+# AVR-GCC Makefile template, derived from the WinAVR template (which\r
+# is public domain), believed to be neutral to any flavor of "make"\r
+# (GNU make, BSD make, SysV make)\r
+\r
+\r
+#MCU = atmega168p\r
+MCU = atmega328p\r
+FORMAT = ihex\r
+TARGET = main\r
+SRC = $(TARGET).c ../MagneticFieldSensor/uart/uart.c ../MagneticFieldSensor/twi/twi.c MagnetSensor.c ../MagneticFieldSensor/BME280.c\r
+ASRC = \r
+OPT = 2\r
+\r
+# Name of this Makefile (used for "make depend").\r
+MAKEFILE = Makefile\r
+\r
+# Debugging format.\r
+# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.\r
+# AVR (extended) COFF requires stabs, plus an avr-objcopy run.\r
+DEBUG = stabs\r
+\r
+# Compiler flag to set the C Standard level.\r
+# c89 - "ANSI" C\r
+# gnu89 - c89 plus GCC extensions\r
+# c99 - ISO C99 standard (not yet fully implemented)\r
+# gnu99 - c99 plus GCC extensions\r
+CSTANDARD = -std=gnu99\r
+\r
+# Place -D or -U options here\r
+CDEFS = -DF_CPU=1000000\r
+\r
+# Place -I options here\r
+CINCS =\r
+\r
+\r
+CDEBUG = -g$(DEBUG)\r
+CWARN = -Wall -Wstrict-prototypes \r
+CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wl,--relax\r
+#CEXTRA = -Wa,-adhlns=$(<:.c=.lst)\r
+CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) $(CTUNING) -DSERIALNUM=$(SNUM)\r
+\r
+\r
+#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs \r
+\r
+\r
+#Additional libraries.\r
+\r
+# Minimalistic printf version\r
+PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min\r
+\r
+# Floating point printf version (requires MATH_LIB = -lm below)\r
+PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt\r
+\r
+PRINTF_LIB = \r
+\r
+# Minimalistic scanf version\r
+SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min\r
+\r
+# Floating point + %[ scanf version (requires MATH_LIB = -lm below)\r
+SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt\r
+\r
+SCANF_LIB = \r
+\r
+MATH_LIB = -lm\r
+\r
+# External memory options\r
+\r
+# 64 KB of external RAM, starting after internal RAM (ATmega128!),\r
+# used for variables (.data/.bss) and heap (malloc()).\r
+#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff\r
+\r
+# 64 KB of external RAM, starting after internal RAM (ATmega128!),\r
+# only used for heap (malloc()).\r
+#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff\r
+\r
+EXTMEMOPTS =\r
+\r
+#LDMAP = $(LDFLAGS) -Wl,-Map=$(TARGET).map,--cref\r
+LDFLAGS = $(EXTMEMOPTS) $(LDMAP) $(PRINTF_LIB_FLOAT) $(SCANF_LIB) $(MATH_LIB)\r
+\r
+\r
+# Programming support using avrdude. Settings and variables.\r
+\r
+#AVRDUDE_PROGRAMMER = dragon_isp\r
+AVRDUDE_PROGRAMMER = avrisp2\r
+AVRDUDE_PORT = usb\r
+\r
+AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex\r
+#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep\r
+\r
+\r
+# Uncomment the following if you want avrdude's erase cycle counter.\r
+# Note that this counter needs to be initialized first using -Yn,\r
+# see avrdude manual.\r
+#AVRDUDE_ERASE_COUNTER = -y\r
+\r
+# Uncomment the following if you do /not/ wish a verification to be\r
+# performed after programming the device.\r
+AVRDUDE_NO_VERIFY = -V\r
+\r
+# Increase verbosity level. Please use this when submitting bug\r
+# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude> \r
+# to submit bug reports.\r
+#AVRDUDE_VERBOSE = -v -v\r
+\r
+AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)\r
+AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE) $(AVRDUDE_ERASE_COUNTER)\r
+\r
+# Philipp Klaus: In this project, we need to use a slower bit\r
+# 2017-12-13 clock in order to flash the firmware properly:\r
+AVRDUDE_FLAGS += -B 5\r
+\r
+\r
+CC = avr-gcc\r
+OBJCOPY = avr-objcopy\r
+OBJDUMP = avr-objdump\r
+SIZE = avr-size\r
+NM = avr-nm\r
+AVRDUDE = avrdude\r
+REMOVE = rm -f\r
+MV = mv -f\r
+\r
+# Define all object files.\r
+OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) \r
+\r
+# Define all listing files.\r
+LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)\r
+\r
+# Combine all necessary flags and optional flags.\r
+# Add target processor to flags.\r
+ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)\r
+ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)\r
+\r
+\r
+# Default target.\r
+all: build\r
+\r
+build: elf hex eep\r
+\r
+elf: $(TARGET).elf\r
+hex: $(TARGET).hex\r
+eep: $(TARGET).eep\r
+lss: $(TARGET).lss \r
+sym: $(TARGET).sym\r
+\r
+\r
+# Program the device. \r
+program: $(TARGET).hex $(TARGET).eep\r
+ $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)\r
+\r
+\r
+size: \r
+ $(SIZE) -C --mcu=$(MCU) $(TARGET).elf \r
+\r
+# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.\r
+COFFCONVERT=$(OBJCOPY) --debugging \\r
+--change-section-address .data-0x800000 \\r
+--change-section-address .bss-0x800000 \\r
+--change-section-address .noinit-0x800000 \\r
+--change-section-address .eeprom-0x810000 \r
+\r
+\r
+coff: $(TARGET).elf\r
+ $(COFFCONVERT) -O coff-avr $(TARGET).elf $(TARGET).cof\r
+\r
+\r
+extcoff: $(TARGET).elf\r
+ $(COFFCONVERT) -O coff-ext-avr $(TARGET).elf $(TARGET).cof\r
+\r
+\r
+.SUFFIXES: .elf .hex .eep .lss .sym\r
+\r
+.elf.hex:\r
+ $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@\r
+\r
+.elf.eep:\r
+ -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \\r
+ --change-section-lma .eeprom=0 -O $(FORMAT) $< $@\r
+\r
+# Create extended listing file from ELF output file.\r
+.elf.lss:\r
+ $(OBJDUMP) -h -S $< > $@\r
+\r
+# Create a symbol table from ELF output file.\r
+.elf.sym:\r
+ $(NM) -n $< > $@\r
+\r
+\r
+\r
+# Link: create ELF output file from object files.\r
+$(TARGET).elf: $(OBJ)\r
+ $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)\r
+\r
+\r
+# Compile: create object files from C source files.\r
+.c.o:\r
+ $(CC) -c $(ALL_CFLAGS) $< -o $@ \r
+\r
+\r
+# Compile: create assembler files from C source files.\r
+.c.s:\r
+ $(CC) -S $(ALL_CFLAGS) $< -o $@\r
+\r
+\r
+# Assemble: create object files from assembler source files.\r
+.S.o:\r
+ $(CC) -c $(ALL_ASFLAGS) $< -o $@\r
+\r
+\r
+\r
+# Target: clean project.\r
+clean:\r
+ $(REMOVE) $(TARGET).hex $(TARGET).eep $(TARGET).cof $(TARGET).elf \\r
+ $(TARGET).map $(TARGET).sym $(TARGET).lss \\r
+ $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d)\r
+\r
+depend:\r
+ if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \\r
+ then \\r
+ sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \\r
+ $(MAKEFILE).$$$$ && \\r
+ $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \\r
+ fi\r
+ echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \\r
+ >> $(MAKEFILE); \\r
+ $(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE)\r
+\r
+.PHONY: all build elf hex eep lss sym program coff extcoff clean depend program_bootloader program_arduino program_leo\r
+\r
+program_bootloader: all\r
+ dfu-programmer $(MCU) erase && dfu-programmer $(MCU) flash $(TARGET).hex && dfu-programmer $(MCU) start\r
+\r
+program_dragon: all\r
+ avrdude -p$(MCU) -cdragon_isp -Pusb -B 5 -Uflash:w:$(TARGET).hex:i\r
+\r
+program_avrisp2: all\r
+ avrdude -p$(MCU) -cavrisp2 -Pusb -B 5 -Uflash:w:$(TARGET).hex:i\r
+\r
+#program: all -> See line 145!\r
+\r
+program_leo: all\r
+ avrdude -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D -Uflash:w:$(TARGET).hex:i\r
+\r
+fuses:\r
+ $(AVRDUDE) $(AVRDUDE_FLAGS) -U lfuse:w:0x46:m -U hfuse:w:0xd9:m -U efuse:w:0xff:m\r
--- /dev/null
+/*\r
+ * main.c\r
+ *\r
+ * Created on: 07.03.2017\r
+ * Author: Adrian Weber\r
+ */\r
+#include "main.h"\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
+#define USE_UART\r
+#define USE_BME\r
+\r
+\r
+#define LED_GREEN_ON() PORTC |= (1<<PC1)\r
+#define LED_GREEN_OFF() PORTC &= ~(1<<PC1)\r
+#define LED_RED_ON() PORTC |= (1<<PC2)\r
+#define LED_RED_OFF() PORTC &= ~(1<<PC2)\r
+\r
+\r
+ISR(TIMER2_OVF_vect) {\r
+} \r
+\r
+\r
+int main(void) { \r
+ char s[30];\r
+ uint8_t Status[2]={0,0};\r
+ uint8_t TC_Enabled;\r
+ uint16_t TRef= 0;\r
+ float SensX;\r
+ float SensY;\r
+ float SensZ;\r
+ int16_t Meas[4];\r
+\r
+ \r
+ //For CLK setting\r
+ CLKPR = (1<<CLKPCE); // No prescale\r
+ CLKPR = 0x03; //prescaler 8 = 1 MHz\r
+ \r
+ //Just LED\r
+ DDRC = 0b00000110;\r
+ PORTC = 0b00000000;\r
+ \r
+ SMCR = (3 << SM0) | (1 << SE); //select power-save and enable sleep\r
+ MCUCR = (1 << BODS) | (1 << BODSE); //BOD off in sleep\r
+ MCUCR = (1 << BODS) | (0 << BODSE); //BOD off in sleep\r
+ PRR = (1 << PRTIM0) | (1 << PRTIM1) | (1 << PRSPI) | (1 << PRADC) | (0 << PRUSART0) | (0 << PRTIM2); //disable what is not needed\r
+ \r
+ TCCR2A = 0; //normal, no pin connection\r
+ TCCR2B = (6 << CS20); //prescaler 256 -> 32kHz, 256 cnts = 0.5 Hz\r
+ TIMSK2 = (1 << TOIE2); //overflow interrupt\r
+ ASSR = (1 << AS2); //asynchronous mode\r
+ \r
+ //Init UART\r
+#ifdef USE_UART \r
+ uinit(); \r
+#endif\r
+ sei();\r
+\r
+ //Init I2C/TWI\r
+ twi_init();\r
+\r
+ //Init Magnetic field Sensor\r
+ Status[0] = InitMagSensor(ADDR_MAG_0, &TRef, &SensX, &SensY, &SensZ, &TC_Enabled);\r
+\r
+#ifdef USE_BME \r
+ BME280 bme280;\r
+ bme280.settings.commInterface = I2C_MODE;\r
+ bme280.settings.I2CAddress = ADDR_ENV;\r
+ bme280.settings.runMode = 3; //Normal mode\r
+ bme280.settings.tStandby = 0;\r
+ bme280.settings.filter = 1;\r
+ bme280.settings.tempOverSample = 1;\r
+ bme280.settings.pressOverSample = 4;\r
+ bme280.settings.humidOverSample = 4;\r
+\r
+ Status[1] = InitBME(&bme280); \r
+#endif\r
+#ifdef USE_UART \r
+ sprintf(s,"%u", Status[0]);\r
+ uputs(s);\r
+#endif \r
+ \r
+ while(1) {\r
+ LED_GREEN_ON();\r
+// _delay_ms(1);\r
+ MeasureMagSens(ADDR_MAG_0, TRef, &SensX, &SensY, &SensZ, TC_Enabled, &Meas[0]);\r
+ LED_GREEN_OFF();\r
+ sleep_mode();\r
+ };\r
+ \r
+// _delay_ms(300);\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
+// \r
+// if (mode == 0) {\r
+// // normal running mode\r
+// if (Status[0] == 0) { MeasureMagSens(ADDR_MAG_0, TRef[0], &SensX[0], &SensY[0], &SensZ[0], TC_Enabled[0]);}\r
+// else { uputs("ERR Mag ");sprintf(SerNum,"%2u ",SERIALNUM);uputs(SerNum); uputsnl("0 NO_DEVICE"); }\r
+// #if MULTISENSE == 0\r
+// if (Status[1] == 0) { MeasureMagSens(ADDR_MAG_1, TRef[1], &SensX[1], &SensY[1], &SensZ[1], TC_Enabled[1]);}\r
+// else { uputs("ERR Mag ");sprintf(SerNum,"%2u ",SERIALNUM);uputs(SerNum); uputsnl("1 NO_DEVICE"); }\r
+// if (Status[2] == 0) { MeasureMagSens(ADDR_MAG_2, TRef[2], &SensX[2], &SensY[2], &SensZ[2], TC_Enabled[2]);}\r
+// else { uputs("ERR Mag ");sprintf(SerNum,"%2u ",SERIALNUM);uputs(SerNum); uputsnl("2 NO_DEVICE"); }\r
+// if (Status[3] == 0) { MeasureMagSens(ADDR_MAG_3, TRef[3], &SensX[3], &SensY[3], &SensZ[3], TC_Enabled[3]);}\r
+// else { uputs("ERR Mag ");sprintf(SerNum,"%2u ",SERIALNUM);uputs(SerNum); uputsnl("3 NO_DEVICE"); }\r
+// #endif\r
+// #if MULTISENSE == 1\r
+// char Temp[30]; char Pres[30]; char Hum[30];\r
+// sprintf(Temp,"%3.2f",readTempC(&bme280));\r
+// sprintf(Pres,"%6.3f",readFloatPressure(&bme280));\r
+// sprintf(Hum,"%4.3f",readFloatHumidity(&bme280));\r
+// uputs("Temp: ");uputs(Temp);uputs(" Pres: ");uputs(Pres);uputs(" Humid: ");uputsnl(Hum);\r
+// \r
+// uint32_t lum = TSL2591_getFullLuminosity(&tsl);\r
+// uint16_t ir = lum >> 16;\r
+// uint16_t full = lum & 0xFFFF;\r
+// \r
+// uputs("Bright: ");\r
+// sprintf(s,"%u",full); uputs(s);\r
+// uputs(" / ");\r
+// sprintf(s,"%u",ir); uputsnl(s);\r
+// #endif\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
+ 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
--- /dev/null
+/*\r
+ * main.h\r
+ *\r
+ * Created on: 07.03.2017\r
+ * Author: Adrian Weber\r
+ */\r
+\r
+#ifndef MAIN_H_\r
+#define MAIN_H_\r
+\r
+//#define SERIALNUM 0\r
+\r
+#include <avr/io.h>\r
+#include <util/delay.h>\r
+#include <string.h>\r
+#include <stdlib.h>\r
+#include <stdio.h>\r
+#include <stdint.h>\r
+#include <stdbool.h>\r
+#include <inttypes.h>\r
+#include <math.h>\r
+#include "../MagneticFieldSensor/uart/uart.h"\r
+#include "MagnetSensor.h"\r
+#include "../MagneticFieldSensor/BME280.h"\r
+#include "../MagneticFieldSensor/twi/twi.h"\r
+#include <avr/interrupt.h>\r
+#include <avr/pgmspace.h>\r
+#include <avr/sleep.h>\r
+\r
+\r
+#ifndef F_CPU\r
+#define F_CPU 8000000\r
+#endif\r
+\r
+# define USART_BAUDRATE 19200\r
+# define BAUD_PRESCALE (unsigned int)(1.0 * F_CPU / USART_BAUDRATE / 16 - 0.5);\r
+\r
+#define ADDR_MAG_0 0x0C\r
+#define ADDR_MAG_1 0x0D\r
+#define ADDR_MAG_2 0x0E\r
+#define ADDR_MAG_3 0x0F\r
+\r
+\r
+#define ADDR_ENV 0x76\r
+\r
+#define MULTISENSE 1 //0: 4 magnetic sensors, 1: 1x magnetic, environment\r
+\r
+#ifndef __AVR_ATmega168P__\r
+#define __AVR_ATmega168P__\r
+#endif /* MAIN_H_ */\r
+\r
+\r
+#endif\r