Commit 463bbced authored by Erik Strand's avatar Erik Strand

Stuff

parent 8c1d6a3c
......@@ -105,6 +105,21 @@ void put_char(volatile unsigned char *port, unsigned char pin, char txchar) {
interrupts();
}
void put_hex(volatile unsigned char *port, unsigned char pin, uint8_t hex_char) {
uint8_t val = (hex_char & (0b1111u << 4)) >> 4;
if (val < 10) {
put_char(port, pin, '0' + val);
} else {
put_char(port, pin, 'A' + (val - 10));
}
val = hex_char & 0b1111u;
if (val < 10) {
put_char(port, pin, '0' + val);
} else {
put_char(port, pin, 'A' + (val - 10));
}
}
void put_string(volatile unsigned char *port, unsigned char pin, char *str) {
//
// print a null-terminated string
......@@ -117,7 +132,18 @@ void put_string(volatile unsigned char *port, unsigned char pin, char *str) {
} while (str[index] != 0);
}
OneWire ds(11); // on pin 10 (a 4.7K resistor is necessary)
void put_line(volatile unsigned char *port, unsigned char pin, char *str) {
put_string(port, pin, str);
put_char(port, pin, '\n');
}
void put_float(volatile unsigned char *port, unsigned char pin, float x) {
static char buffer[10];
dtostrf(x, 8, 2, buffer);
put_string(port, pin, buffer);
}
OneWire ds(2); // on PA2 (a 4.7K resistor is necessary)
void setup(void) {
// Set clock divider to 1.
......@@ -148,9 +174,6 @@ void loop(void) {
PORTB |= led_pin;
//put_char(&serial_port, serial_pin_out, '1');
}
put_string(&serial_port, serial_pin_out, "hello world");
delay(500);
return;
byte i;
byte present = 0;
......@@ -159,8 +182,8 @@ void loop(void) {
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
put_string(&serial_port, serial_pin_out, "No more addresses.");
if (!ds.search(addr)) {
put_line(&serial_port, serial_pin_out, "No more addresses.");
ds.reset_search();
delay(250);
return;
......@@ -168,35 +191,33 @@ void loop(void) {
//Serial.print("ROM =");
put_string(&serial_port, serial_pin_out, "ROM=");
for( i = 0; i < 8; i++) {
for (i = 0; i < 8; i++) {
put_char(&serial_port, serial_pin_out, ' ');
//Serial.print(addr[i], HEX);
put_string(&serial_port, serial_pin_out, "xxx");
//put_string(&serial_port, serial_pin_out, addr[i])
put_hex(&serial_port, serial_pin_out, addr[i]);
}
put_char(&serial_port, serial_pin_out, '\n');
if (OneWire::crc8(addr, 7) != addr[7]) {
put_string(&serial_port, serial_pin_out, "CRC is not valid!");
put_line(&serial_port, serial_pin_out, "CRC is not valid!");
return;
}
//Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
put_string(&serial_port, serial_pin_out, " Chip = DS18S20"); // or old DS1820
put_line(&serial_port, serial_pin_out, " Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
put_string(&serial_port, serial_pin_out, " Chip = DS18B20");
put_line(&serial_port, serial_pin_out, " Chip = DS18B20");
type_s = 0;
break;
case 0x22:
put_string(&serial_port, serial_pin_out, " Chip = DS1822");
put_line(&serial_port, serial_pin_out, " Chip = DS1822");
type_s = 0;
break;
default:
put_string(&serial_port, serial_pin_out, "Device is not a DS18x20 family device.");
put_line(&serial_port, serial_pin_out, "Device is not a DS18x20 family device.");
return;
}
......@@ -212,16 +233,14 @@ void loop(void) {
ds.write(0xBE); // Read Scratchpad
put_string(&serial_port, serial_pin_out, " Data = ");
put_string(&serial_port, serial_pin_out, "xxx");
//put_string(&serial_port, serial_pin_out, present, HEX);
put_string(&serial_port, serial_pin_out, " ");
put_hex(&serial_port, serial_pin_out, present);
put_char(&serial_port, serial_pin_out, ' ');
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
//Serial.print(data[i], HEX);
put_string(&serial_port, serial_pin_out, " thing");
put_hex(&serial_port, serial_pin_out, data[i]);
}
put_string(&serial_port, serial_pin_out, " CRC=");
//put_string(&serial_port, serial_pin_out, OneWire::crc8(data, 8), HEX);
put_hex(&serial_port, serial_pin_out, OneWire::crc8(data, 8));
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
......@@ -245,8 +264,8 @@ void loop(void) {
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
put_string(&serial_port, serial_pin_out, " Temperature = ");
//put_string(&serial_port, serial_pin_out, celsius);
put_float(&serial_port, serial_pin_out, celsius);
put_string(&serial_port, serial_pin_out, " Celsius, ");
//put_string(&serial_port, serial_pin_out, fahrenheit);
put_string(&serial_port, serial_pin_out, " Fahrenheit");
put_float(&serial_port, serial_pin_out, fahrenheit);
put_line(&serial_port, serial_pin_out, " Fahrenheit");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment