#include "MagnetSensor.h"
//#define CHIP_OLD 1
-
+#define LED_RED_ON() PORTC |= (1<<PC2)
+#define LED_RED_OFF() PORTC &= ~(1<<PC2)
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){
+void MeasureMagSens(uint8_t Addr, uint16_t Tref, float* SensX, float* SensY, float* SensZ, const uint8_t TC_Enabled, float* Meas){
uint8_t MagData[9] = {255,255,255,255,255,255,255,255,255};
char s[30];
-// char SerNum[10];
+ char SerNum[10];
char Num[2];
sprintf(Num,"%1u",Addr-12);
sprintf(s,"Status %02u 0x%x: 0x%x",SERIALNUM,Addr,status_1);
uputsnl(s);
#endif
- _delay_ms(100);
-
+// _delay_ms(100);
+sleep_mode();
uint8_t testdata2 = 0x4F;// Read Measured Values
twi_writeTo(Addr, &testdata2, 1, 1, true);
twi_readFrom(Addr,MagData, 9, true);
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[0] = ((float)xMag*(*SensX));
+ Meas[1] = ((float)yMag*(*SensY));
+ Meas[2] = ((float)zMag*(*SensZ));
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);
+#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("_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
+// uputs("ERR Mag ");sprintf(SerNum,"%2u ",SERIALNUM);uputs(SerNum);uputs(Num);uputsnl(" NO"); // ERROR INFORMATION FOR EPICS
#endif
} else {
#ifdef USE_UART
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
\r
#define LED_GREEN_ON() PORTC |= (1<<PC1)\r
\r
\r
ISR(TIMER2_OVF_vect) {\r
+// LED_RED_ON();\r
+// _delay_us(10);\r
+// LED_RED_OFF();\r
} \r
\r
\r
float SensX;\r
float SensY;\r
float SensZ;\r
- int16_t Meas[4];\r
-\r
+ float Meas[4];\r
+ float sum, diffsum, basesum;\r
+ uint8_t first = 1;\r
\r
//For CLK setting\r
CLKPR = (1<<CLKPCE); // No prescale\r
- CLKPR = 0x03; //prescaler 8 = 1 MHz\r
- \r
+ CLKPR = 0x02; //prescaler 4 = 2 MHz\r
+ OSCCAL = 0x58;\r
//Just LED\r
DDRC = 0b00000110;\r
PORTC = 0b00000000;\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
+ PRR = (1 << PRTIM1) | (1 << PRTIM0) | (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
+ TCCR2B = (5 << CS20); //prescaler 1024 -> 1kHz, 256 cnts = 8 Hz\r
TIMSK2 = (1 << TOIE2); //overflow interrupt\r
ASSR = (1 << AS2); //asynchronous mode\r
\r
#endif \r
\r
while(1) {\r
- LED_GREEN_ON();\r
-// _delay_ms(1);\r
+\r
MeasureMagSens(ADDR_MAG_0, TRef, &SensX, &SensY, &SensZ, TC_Enabled, &Meas[0]);\r
- LED_GREEN_OFF();\r
- sleep_mode();\r
- };\r
+ sum = abs(Meas[0]) + abs(Meas[1]) + abs(Meas[2]);\r
+ \r
+ if(first) {basesum = sum; first = 0;}\r
+ else {\r
+ diffsum = abs(sum - basesum);\r
+ if(diffsum > 500) {\r
+ LED_RED_ON();\r
+ _delay_us(200);\r
+ LED_RED_OFF();\r
+ }\r
+ else {\r
+ LED_GREEN_ON();\r
+ _delay_us(200);\r
+ LED_GREEN_OFF();\r
+ }\r
+ sleep_mode();\r
+ sleep_mode();\r
+ sleep_mode();\r
+ }\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