From: Philipp Klaus Date: Thu, 8 Feb 2018 16:55:28 +0000 (+0100) Subject: Magn Sens: cleanup in ./uart X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=95fba3bbe78e03516a6f3d9e95e46c47a687d196;p=avr.git Magn Sens: cleanup in ./uart --- diff --git a/sensors/MagneticFieldSensor/uart/MagnetSensor.c b/sensors/MagneticFieldSensor/uart/MagnetSensor.c deleted file mode 100644 index a65b195..0000000 --- a/sensors/MagneticFieldSensor/uart/MagnetSensor.c +++ /dev/null @@ -1,150 +0,0 @@ -#include "MagnetSensor.h" - -uint8_t InitMagSensor(uint8_t Addr, uint16_t* Tref, float* SensX, float* SensY, float* SensZ) -{ - - // 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; - - - txBuffer[0] = 0x60; - txBuffer[1] = 0x00; - txBuffer[2] = 0x5C; - txBuffer[3] = 0x00; - - 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; - txBuffer[1] = 0x90; - twi_writeTo(Addr,txBuffer,2,1,true); - twi_readFrom(Addr, ReadReg, 3, true); - uint16_t T_ref = ReadReg[1]<<8 | ReadReg[2]; - *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); - - return status2; -} - - -void MeasureMagSens(uint8_t Addr, uint16_t Tref, float* SensX, float* SensY, float* SensZ){ - uint8_t MagData[9] = {255,255,255,255,255,255,255,255,255}; - char s[30]; - - 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); - - _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]; - int16_t yMag = MagData[5] * 256 + MagData[6]; - int16_t zMag = MagData[7] * 256 + MagData[8]; - int16_t temp_temp = (uint16_t)(tMag) - (uint16_t)(Tref); - float Temperature = 35.0 + (temp_temp/45.2); - uputs("M");uputs(Num);uputs("_T "); - sprintf(s,"%4.2f",Temperature); - uputsnl(s); - uputs("M");uputs(Num);uputs("_X "); - sprintf(s,"%4.3f",((float)xMag*(*SensX))); //muT - uputsnl(s); - uputs("M");uputs(Num);uputs("_Y "); - sprintf(s,"%4.3f",((float)yMag*(*SensY))); //muT - uputsnl(s); - uputs("M");uputs(Num);uputs("_Z "); - sprintf(s,"%4.3f",((float)zMag*(*SensZ))); //muT - uputsnl(s); - uputs("ERR Mag ");uputs(Num);uputsnl(" NO"); // ERROR INFORMATION FOR EPICS - } else { - uputs("ERR Mag ");uputs(Num);uputsnl(" CONNECTION"); - } -} - -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); - }*/ - - 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; - } - - - return Sens*(float)pow(2,Res); -} - -float SensitivityZ(uint8_t Gain, uint8_t Res){ - float Sens =0.805; - 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; - } - - - return Sens*(float)pow(2,Res); -} diff --git a/sensors/MagneticFieldSensor/uart/MagnetSensor.h b/sensors/MagneticFieldSensor/uart/MagnetSensor.h deleted file mode 100644 index 5a71ec7..0000000 --- a/sensors/MagneticFieldSensor/uart/MagnetSensor.h +++ /dev/null @@ -1,6 +0,0 @@ -#include "main.h" - -uint8_t InitMagSensor(uint8_t Addr, uint16_t* Tref, float *SensX, float* SensY, float *SensZ); -void MeasureMagSens(uint8_t Addr, uint16_t Tref, float* SensX, float* SensY, float* SensZ); -float SensitivityXY(uint8_t Gain, uint8_t Res); -float SensitivityZ(uint8_t Gain, uint8_t Res); diff --git a/sensors/MagneticFieldSensor/uart/Makefile b/sensors/MagneticFieldSensor/uart/Makefile deleted file mode 100644 index 8650df1..0000000 --- a/sensors/MagneticFieldSensor/uart/Makefile +++ /dev/null @@ -1,233 +0,0 @@ -# Hey Emacs, this is a -*- makefile -*- - -# AVR-GCC Makefile template, derived from the WinAVR template (which -# is public domain), believed to be neutral to any flavor of "make" -# (GNU make, BSD make, SysV make) - - -MCU = atmega168p -FORMAT = ihex -TARGET = main -SRC = $(TARGET).c uart/uart.c twi/twi.c MagnetSensor.c -ASRC = -OPT = 2 - -# Name of this Makefile (used for "make depend"). -MAKEFILE = Makefile - -# Debugging format. -# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2. -# AVR (extended) COFF requires stabs, plus an avr-objcopy run. -DEBUG = stabs - -# Compiler flag to set the C Standard level. -# c89 - "ANSI" C -# gnu89 - c89 plus GCC extensions -# c99 - ISO C99 standard (not yet fully implemented) -# gnu99 - c99 plus GCC extensions -CSTANDARD = -std=gnu99 - -# Place -D or -U options here -CDEFS = -DF_CPU=16000000 - -# Place -I options here -CINCS = - - -CDEBUG = -g$(DEBUG) -CWARN = -Wall -Wstrict-prototypes -CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wl,--relax -#CEXTRA = -Wa,-adhlns=$(<:.c=.lst) -CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) $(CTUNING) - - -#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs - - -#Additional libraries. - -# Minimalistic printf version -PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min - -# Floating point printf version (requires MATH_LIB = -lm below) -PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt - -PRINTF_LIB = - -# Minimalistic scanf version -SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min - -# Floating point + %[ scanf version (requires MATH_LIB = -lm below) -SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt - -SCANF_LIB = - -MATH_LIB = -lm - -# External memory options - -# 64 KB of external RAM, starting after internal RAM (ATmega128!), -# used for variables (.data/.bss) and heap (malloc()). -#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff - -# 64 KB of external RAM, starting after internal RAM (ATmega128!), -# only used for heap (malloc()). -#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff - -EXTMEMOPTS = - -#LDMAP = $(LDFLAGS) -Wl,-Map=$(TARGET).map,--cref -LDFLAGS = $(EXTMEMOPTS) $(LDMAP) $(PRINTF_LIB_FLOAT) $(SCANF_LIB) $(MATH_LIB) - - -# Programming support using avrdude. Settings and variables. - -AVRDUDE_PROGRAMMER = dragon_jtag -AVRDUDE_PORT = usb - -AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex -#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep - - -# Uncomment the following if you want avrdude's erase cycle counter. -# Note that this counter needs to be initialized first using -Yn, -# see avrdude manual. -#AVRDUDE_ERASE_COUNTER = -y - -# Uncomment the following if you do /not/ wish a verification to be -# performed after programming the device. -AVRDUDE_NO_VERIFY = -V - -# Increase verbosity level. Please use this when submitting bug -# reports about avrdude. See -# to submit bug reports. -#AVRDUDE_VERBOSE = -v -v - -AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) -AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE) $(AVRDUDE_ERASE_COUNTER) - - -CC = avr-gcc -OBJCOPY = avr-objcopy -OBJDUMP = avr-objdump -SIZE = avr-size -NM = avr-nm -AVRDUDE = avrdude -REMOVE = rm -f -MV = mv -f - -# Define all object files. -OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) - -# Define all listing files. -LST = $(ASRC:.S=.lst) $(SRC:.c=.lst) - -# Combine all necessary flags and optional flags. -# Add target processor to flags. -ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) -ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) - - -# Default target. -all: build - -build: elf hex eep - -elf: $(TARGET).elf -hex: $(TARGET).hex -eep: $(TARGET).eep -lss: $(TARGET).lss -sym: $(TARGET).sym - - -# Program the device. -program: $(TARGET).hex $(TARGET).eep - $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) - - -size: - $(SIZE) -C --mcu=$(MCU) $(TARGET).elf - -# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. -COFFCONVERT=$(OBJCOPY) --debugging \ ---change-section-address .data-0x800000 \ ---change-section-address .bss-0x800000 \ ---change-section-address .noinit-0x800000 \ ---change-section-address .eeprom-0x810000 - - -coff: $(TARGET).elf - $(COFFCONVERT) -O coff-avr $(TARGET).elf $(TARGET).cof - - -extcoff: $(TARGET).elf - $(COFFCONVERT) -O coff-ext-avr $(TARGET).elf $(TARGET).cof - - -.SUFFIXES: .elf .hex .eep .lss .sym - -.elf.hex: - $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ - -.elf.eep: - -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ - --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ - -# Create extended listing file from ELF output file. -.elf.lss: - $(OBJDUMP) -h -S $< > $@ - -# Create a symbol table from ELF output file. -.elf.sym: - $(NM) -n $< > $@ - - - -# Link: create ELF output file from object files. -$(TARGET).elf: $(OBJ) - $(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS) - - -# Compile: create object files from C source files. -.c.o: - $(CC) -c $(ALL_CFLAGS) $< -o $@ - - -# Compile: create assembler files from C source files. -.c.s: - $(CC) -S $(ALL_CFLAGS) $< -o $@ - - -# Assemble: create object files from assembler source files. -.S.o: - $(CC) -c $(ALL_ASFLAGS) $< -o $@ - - - -# Target: clean project. -clean: - $(REMOVE) $(TARGET).hex $(TARGET).eep $(TARGET).cof $(TARGET).elf \ - $(TARGET).map $(TARGET).sym $(TARGET).lss \ - $(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d) - -depend: - if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \ - then \ - sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \ - $(MAKEFILE).$$$$ && \ - $(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \ - fi - echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \ - >> $(MAKEFILE); \ - $(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE) - -.PHONY: all build elf hex eep lss sym program coff extcoff clean depend program_bootloader program_arduino program_leo - -program_bootloader: all - dfu-programmer $(MCU) erase && dfu-programmer $(MCU) flash $(TARGET).hex && dfu-programmer $(MCU) start - -# program_arduino: all -# avrdude -patmega32u4 -cavr109 -P$(PORT) -b 57600 -D -Uflash:w:$(TARGET).hex:i - -program_leo: all - avrdude -patmega32u4 -cavr109 -P/dev/ttyACM0 -b57600 -D -Uflash:w:$(TARGET).hex:i diff --git a/sensors/MagneticFieldSensor/uart/main.c b/sensors/MagneticFieldSensor/uart/main.c deleted file mode 100644 index 91fcae8..0000000 --- a/sensors/MagneticFieldSensor/uart/main.c +++ /dev/null @@ -1,56 +0,0 @@ -/* - * main.c - * - * Created on: 07.03.2017 - * Author: Adrian Weber - */ -#include "main.h" - - -int main(void) -{ char s[30]; - uint8_t Status[4]={0,0,0,0}; - uint8_t Status_Mag[4]={0,0,0,0}; - uint16_t TRef[4]={0,0,0,0}; - float SensX[4]; - float SensY[4]; - float SensZ[4]; - - //For CLK setting - CLKPR = (0< -#include -#include -#include -#include -#include -#include -#include -#include -#include "uart/uart.h" -#include "MagnetSensor.h" -#include "twi/twi.h" -#include -#include - - -#ifndef F_CPU -#define F_CPU 16000000 -#endif - -# define USART_BAUDRATE 19200 -# define BAUD_PRESCALE (unsigned int)(1.0 * F_CPU / USART_BAUDRATE / 16 - 0.5); - -#define ADDR_MAG_0 0x0C -#define ADDR_MAG_1 0x0D -#define ADDR_MAG_2 0x0E -#define ADDR_MAG_3 0x0F - - - - -#ifndef __AVR_ATmega168P__ -#define __AVR_ATmega168P__ -#endif /* MAIN_H_ */ - - -#endif