Forum: Mikrocontroller und Digitale Elektronik Seltsamer Fehler mit IRtempsensor nach Neustart?


von Stefan S. (kami)


Angehängte Dateien:

Lesenswert?

Hallo zusammen,

ich habe von Zytemp einen TN168 
(http://www.zytemp.com/products/tn168.asp). Wie auf dem Bild zusehen, 
habe ich über die Debug-Pins den Sensor mit einem Mega 2560 Arduino 
Board verbunden. Ich habe dann diese Library verwendet:

https://github.com/freetronics/IRTemp

um den Sensor über die 3 Daten-Pins auszulesen.

Im ersten Anlauf klappt das alles super. Ich konfiguriere diesen Code:
1
/*
2
 * File:    readTemperature.ino
3
 * Version: 1.0
4
 * Author:  Andy Gelme (@geekscape)
5
 * License: GPLv3
6
 *
7
 * For more information see www.freetronics.com/irtemp
8
 *
9
 * IRTemp library uses an Arduino interrupt:
10
 *   If PIN_CLOCK = 2, then Arduino interrupt 0 is used
11
 *   If PIN_CLOCK = 3, then Arduino interrupt 1 is used
12
 */
13
14
#include "IRTemp.h"
15
16
static const byte PIN_DATA    = 2;
17
static const byte PIN_CLOCK   = 3;  // Must be either pin 2 or pin 3
18
static const byte PIN_ACQUIRE = 4;
19
20
static const TempUnit SCALE=CELSIUS;  // Options are CELSIUS, FAHRENHEIT
21
22
IRTemp irTemp(PIN_ACQUIRE, PIN_CLOCK, PIN_DATA);
23
24
void setup(void) {
25
  Serial.begin(9600);
26
  Serial.println("IRTemp example");
27
  Serial.println("~~~~~~~~~~~~~~");
28
}
29
30
void loop(void) {
31
  float irTemperature = irTemp.getIRTemperature(SCALE);
32
  printTemperature("IR", irTemperature);
33
34
  float ambientTemperature = irTemp.getAmbientTemperature(SCALE);
35
  printTemperature("Ambient", ambientTemperature);
36
37
  delay(1000);
38
}
39
40
void printTemperature(
41
  char  *type,
42
  float  temperature) {
43
44
  Serial.print(type);
45
  Serial.print(" temperature: ");
46
47
  if (isnan(temperature)) {
48
    Serial.println("Failed");
49
  }
50
  else {
51
    Serial.print(temperature);
52
    Serial.println(SCALE == FAHRENHEIT  ?  " F"  :  " C");
53
  }
54
}

auf die Pins 2,3,4 und lade alles auf den 2560. Danach erhalte ich 
richtige Werte vom Sensor.

Alles gut soweit.

Aber wenn ich nun das Board vom USB abtrenne und wieder anstecke bekomme 
ich nur noch Fehler. Der einzige Weg ist nun nur noch alle 3 Daten-Pins 
auf z.B. 7,8,9 zu ändern und dann den Code wieder auf das Board zu 
laden. Dann funktioniert alles wieder super bis zum Power UP. Ein Reset 
verändert in diesem Fall nix danach geht es immer noch. Nur beim POWER 
UP klappt nix mehr.

Bitte um Hilfe.

Gruß kami

von Stefan S. (kami)


Lesenswert?

Okay,

ich bin einen schritt weiter. Der IR-Sensor hängt sich dadurch auf, das 
er zeitgleich mit dem Board neugestartet wird. Trennt man die 3,3V 
Versorgung und lässt dann das Board wieder anlaufen, geht es wieder.

Aber wie repariere ich das jetzt?

von Stefan S. (kami)


Lesenswert?

Hat den keiner hier ein paar Tipps, wie man vorgehen kann. Es kann doch 
nicht sein, das der IR Sensor mal geht und mal nicht. Wie kann ich beide 
Controller und Sensor gleich starten lassen?

Gruß kami

von Stefan S. (kami)


Lesenswert?

Hier nochmal ein neuer Code,

mit etwas mehr Debug infos:
1
byte n = 0;                            // Interrupt Bit Count        
2
volatile byte pos = 0;            // Values Position Count
3
volatile byte values[5] = {
4
  0,0,0,0,0};                                                    // Values to be stored by sensor
5
byte cbit = 0;                    // Current bit read in
6
7
boolean irFlag = false;            // Flag to indicate IR reading has been made
8
boolean ambFlag = false;          // Flag to indicate ambient temp reading has been made
9
10
11
byte irValues[5] = {
12
  0,0,0,0,0};                                       // Variable to store IR readings
13
byte ambValues[5] = {
14
  0,0,0,0,0};                                            // Variable to store Ambient readings
15
16
const int len = 5;                        // Length of values array
17
const int clkPin = 3;                            // Pins
18
const int dataPin = 7;
19
const int actionPin = 9;
20
21
void setup(){
22
  Serial.begin(9600);            
23
24
  pinMode(clkPin, INPUT);          // Initialize pins
25
  pinMode(dataPin, INPUT);
26
  pinMode(actionPin, OUTPUT);
27
  digitalWrite(clkPin, HIGH);
28
  digitalWrite(dataPin, HIGH);
29
  digitalWrite(actionPin, HIGH);
30
  delay(200);
31
  Serial.println("Type to Start...");                    // Wait for input to start
32
  //while(!Serial.available());
33
  Serial.println("Starting...");
34
  Serial.println("IR (C), Ambient (C), Time Since Start (ms)");
35
36
  attachInterrupt(1,tn9Data,FALLING);              // Interrupt
37
  digitalWrite(actionPin,LOW);
38
  delay(2000);          // Make sensor start sending data
39
}
40
41
void loop(){
42
43
44
45
  if(pos == len && values[0] == 0x4C){            // If sensor has sent IR packet...
46
    for(int i = 0; i < len; i++){                      // Store values to irValues
47
      irValues[i] = values[i];
48
    }
49
    irFlag = true;                               // Indicate IR reading
50
    pos = 0;
51
    digitalWrite(actionPin,LOW);             // Make sensor start sending data
52
  }
53
54
  if(pos == len && values[0] == 0x66){          // If sensor has sent ambient packet...
55
    for(int i = 0; i < len; i++){                    // Store values to ambValues
56
      ambValues[i] = values[i];
57
    }
58
    ambFlag = true;                      // Indicate Ambient reading
59
    pos = 0;
60
    digitalWrite(actionPin,LOW);           // Make sensor start sending data    
61
  }
62
63
  if(pos == len && values[0] == 0x53){         // If sensor has sent junk packet
64
    pos = 0;
65
    digitalWrite(actionPin,LOW);          // Make sensor start sending data   
66
  }
67
68
  if(irFlag && ambFlag){        // If successful IR and Ambient reading...
69
    digitalWrite(actionPin,HIGH);    // Make sensor stop sending data.  Because Timing is weird, I want to ensure the interrupts do not happen during this section.   
70
    word tempword = 0;        // Next 4 lines isolate temperature component of values
71
    tempword = tempword | irValues[1];
72
    tempword = tempword << 8;
73
    tempword = tempword | irValues[2];
74
    if(tn9Check(irValues)){        // If checksum is valid, print IR temperature
75
      //Serial.print("IR = ");
76
      Serial.print(int(tempword)/16.0 - 273.15);
77
      Serial.print(", ");       
78
    }
79
    else{                  // If checksum isn't valid, print impossible temp
80
      //Serial.print("IR = ");
81
      Serial.print("-273.15, "); 
82
    }
83
84
    tempword = 0;          // Isolate temperature component again for ambient
85
    tempword = tempword | ambValues[1];
86
    tempword = tempword << 8;
87
    tempword = tempword | ambValues[2];
88
    if(tn9Check(ambValues)){        // If checksum is valid, print ambient temperature
89
      //Serial.print("Amb = ");
90
      Serial.print(int(tempword)/16.0 - 273.15);        
91
    }
92
    else{            // If checksum isn't valid, print impossible temp
93
      //Serial.print("Amb = ");
94
      Serial.print("-273.15"); 
95
    }
96
    irFlag = false;          // Reset flags
97
    ambFlag = false;
98
    
99
    Serial.print(", ");
100
    Serial.println(millis()); 
101
    
102
    Serial.println(digitalRead(actionPin));         // Print time for logging purposes
103
    delay(2000);               // Simulate other sensors or code
104
    digitalWrite(actionPin,LOW);               // Make sensor start sending data  
105
}                      
106
107
108
}
109
110
111
void tn9Data(){            // Interrupt Function
112
  cbit =  digitalRead(dataPin);      // Read bit
113
  if(pos >= len) pos = 0;                // Keep index below 5
114
  values[pos] = (values[pos] << 1) | cbit;          // Store to values
115
  n++;              // Increment bit count
116
  if(n == 8){            // Increment position count based on bits read in
117
    pos++;
118
    n = 0; 
119
  }
120
  if(pos == len){ 
121
     for(int i = 0; i < len; i++){                    // Store values to ambValues
122
      Serial.print(values[i]);
123
      Serial.print(" ");
124
    }  
125
    
126
    Serial.print("\n");      // If complete "packet" sent, stop sensor from sending 
127
    Serial.println(digitalRead(actionPin));
128
    digitalWrite(actionPin,HIGH);      // again until main loop allows it.
129
  }
130
}
131
132
133
boolean tn9Check(byte tn9Values[]){      // Checksum calculating function
134
  int mcheck = (int)tn9Values[0] + (int)tn9Values[1] + (int)tn9Values[2];  // Checksum calculation
135
  int scheck = (int)tn9Values[3];             // Checksum sent by sensor
136
  boolean crc = false;            // Initialize return value
137
  if(mcheck > 510) mcheck = mcheck - 512;          // Handle sensor byte rollover
138
  if(mcheck > 255) mcheck = mcheck - 256;          // Handle sensor byte rollover
139
  if(mcheck == scheck) crc = true;      // Check checksum
140
  return(crc);            // Return
141
}

wenn der Sensor läuft dann sieht der Output so aus:

102 18 107 227 13
83 0 0 83 13
102 18 107 227 13
76 18 75 169 13
IR= 19.54, 21.54, 7173

102 18 107 227 13
83 0 0 83 13
102 18 107 227 13
76 18 75 169 13
IR= 19.54, 21.54, 9561

102 18 107 227 13
83 0 0 83 13
102 18 107 227 13
76 18 75 169 13
IR= 19.54, 21.54, 11948

und so wenn Fehler ist:

83 0 0 83 13
83 0 0 83 13
83 0 0 83 13
83 0 0 83 13
83 0 0 83 13
83 0 0 83 13

Gruß kami

von Stefan S. (kami)


Lesenswert?

Hi,

so für die Allgemeinheit. Problem gelöst. Man muss den Button am 
IR-Sensor für MODE 2 mal betätigen beim SETUP oder bei einem Fehler dann 
geht der Sensor wieder super :)

Habe ich jetzt mit einem DIGITAL PIN und HIGH LOW -Wechsel gelöst.

Gruß kami

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.