...
 
Commits (7)
// Compile with
// avr-g++ -x c++ -mmcu=attiny44 -Wall -Os -c -DF_CPU=20000000 -I/usr/share/arduino/hardware/arduino/cores/arduino -I/usr/share/arduino/hardware/arduino/variants/standard blink.cpp
// avr-g++ -x c++ -mmcu=attiny44 -Wall -Os -c -DF_CPU=20000000 -I/usr/share/arduino/hardware/arduino/cores/arduino -I/usr/share/arduino/hardware/arduino/variants/standard /usr/share/arduino/hardware/arduino/cores/arduino/wiring.c
// avr-g++ -x c++ -mmcu=attiny44 -Wall -Os -c -DF_CPU=20000000 -I/usr/share/arduino/hardware/arduino/cores/arduino -I/usr/share/arduino/hardware/arduino/variants/standard /usr/share/arduino/hardware/arduino/cores/arduino/wiring_digital.c
// avr-ar rcs libcore.a hooks.o wiring.o wiring_digital.o
#include <Arduino.h>
void setup() {
pinMode(5, OUTPUT);
}
void loop() {
digitalWrite(5, LOW);
delay(1000);
digitalWrite(5, HIGH);
delay(400);
}
//
//
// serial_button.c
//
// 115200 baud FTDI connection that outputs '0' or '1' depending
// on the state of a physical button
//
// set lfuse to 0x5E for 20 MHz xtal
//
// Neil Gershenfeld
// 12/8/10
// Erik Strand
// 11/26/2018
//
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include "OneWire.h"
#define output(directions,pin) (directions |= pin) // set port direction for output
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define bit_delay_time 8.5 // bit delay for 115200 with overhead
#define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
#define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
#define char_delay() _delay_ms(10) // char delay
#define delay(duration) _delay_ms(duration); // drop-in replacement for Arduino delay
#define serial_port PORTA
#define serial_direction DDRA
#define serial_pins PINA
#define serial_pin_in (1 << PA0)
#define serial_pin_out (1 << PA1)
#define led_pin (1 << PB2)
#define button_pin (1 << PA7)
#define max_buffer 25
void put_char(volatile unsigned char *port, unsigned char pin, char txchar) {
//
// send character in txchar on port pin
// assumes line driver (inverts bits)
//
// start bit
//
clear(*port,pin);
bit_delay();
//
// unrolled loop to write data bits
//
if bit_test(txchar,0)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,1)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,2)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,3)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,4)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,5)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,6)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,7)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
//
// stop bit
//
set(*port,pin);
bit_delay();
//
// char delay
//
bit_delay();
}
void put_string(volatile unsigned char *port, unsigned char pin, char *str) {
//
// print a null-terminated string
//
static int index;
index = 0;
do {
put_char(port, pin, str[index]);
++index;
} while (str[index] != 0);
}
OneWire ds(11); // on pin 10 (a 4.7K resistor is necessary)
//void setup(void) {
// Serial.begin(9600);
//}
void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
put_string(&serial_port, serial_pin_out, "No more addresses.");
ds.reset_search();
delay(250);
return;
}
//Serial.print("ROM =");
put_string(&serial_port, serial_pin_out, "ROM=");
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])
}
if (OneWire::crc8(addr, 7) != addr[7]) {
put_string(&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
type_s = 1;
break;
case 0x28:
put_string(&serial_port, serial_pin_out, " Chip = DS18B20");
type_s = 0;
break;
case 0x22:
put_string(&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.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
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, " ");
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_string(&serial_port, serial_pin_out, " CRC=");
//put_string(&serial_port, serial_pin_out, OneWire::crc8(data, 8), HEX);
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
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_string(&serial_port, serial_pin_out, " Celsius, ");
//put_string(&serial_port, serial_pin_out, fahrenheit);
put_string(&serial_port, serial_pin_out, " Fahrenheit");
}
int main(void) {
// Set clock divider to 1.
CLKPR = (1 << CLKPCE);
CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
// Initialize output pins.
set(serial_port, serial_pin_out);
output(serial_direction, serial_pin_out);
// Configure led pin as an output.
DDRB |= led_pin;
// Configure button_pin as an input.
DDRA &= ~button_pin;
// Activate button_pin's pullup resistor.
PORTA |= button_pin;
while (1) {
// Turn on the LED when the button is pressed.
if (PINA & button_pin) {
// Turn off the LED.
PORTB &= ~led_pin;
//put_char(&serial_port, serial_pin_out, '0');
} else {
PORTB |= led_pin;
//put_char(&serial_port, serial_pin_out, '1');
}
//_delay_us(10000);
loop();
}
}
This diff is collapsed.
#ifndef OneWire_h
#define OneWire_h
#ifdef __cplusplus
#include <stdint.h>
#if defined(__AVR__)
#include <util/crc16.h>
#endif
#if ARDUINO >= 100
#include <Arduino.h> // for delayMicroseconds, digitalPinToBitMask, etc
#else
#include "WProgram.h" // for delayMicroseconds
#include "pins_arduino.h" // for digitalPinToBitMask, etc
#endif
// You can exclude certain features from OneWire. In theory, this
// might save some space. In practice, the compiler automatically
// removes unused code (technically, the linker, using -fdata-sections
// and -ffunction-sections when compiling, and Wl,--gc-sections
// when linking), so most of these will not result in any code size
// reduction. Well, unless you try to use the missing features
// and redesign your program to not need them! ONEWIRE_CRC8_TABLE
// is the exception, because it selects a fast but large algorithm
// or a small but slow algorithm.
// you can exclude onewire_search by defining that to 0
#ifndef ONEWIRE_SEARCH
#define ONEWIRE_SEARCH 1
#endif
// You can exclude CRC checks altogether by defining this to 0
#ifndef ONEWIRE_CRC
#define ONEWIRE_CRC 1
#endif
// Select the table-lookup method of computing the 8-bit CRC
// by setting this to 1. The lookup table enlarges code size by
// about 250 bytes. It does NOT consume RAM (but did in very
// old versions of OneWire). If you disable this, a slower
// but very compact algorithm is used.
//#ifndef ONEWIRE_CRC8_TABLE
//#define ONEWIRE_CRC8_TABLE 1
//#endif
// You can allow 16-bit CRC checks by defining this to 1
// (Note that ONEWIRE_CRC must also be 1.)
#ifndef ONEWIRE_CRC16
#define ONEWIRE_CRC16 1
#endif
// Board-specific macros for direct GPIO
#include "OneWire_direct_regtype.h"
class OneWire
{
private:
IO_REG_TYPE bitmask;
volatile IO_REG_TYPE *baseReg;
#if ONEWIRE_SEARCH
// global search state
unsigned char ROM_NO[8];
uint8_t LastDiscrepancy;
uint8_t LastFamilyDiscrepancy;
bool LastDeviceFlag;
#endif
public:
OneWire(uint8_t pin) { begin(pin); }
void begin(uint8_t pin);
// Perform a 1-Wire reset cycle. Returns 1 if a device responds
// with a presence pulse. Returns 0 if there is no device or the
// bus is shorted or otherwise held low for more than 250uS
uint8_t reset(void);
// Issue a 1-Wire rom select command, you do the reset first.
void select(const uint8_t rom[8]);
// Issue a 1-Wire rom skip command, to address all on bus.
void skip(void);
// Write a byte. If 'power' is one then the wire is held high at
// the end for parasitically powered devices. You are responsible
// for eventually depowering it by calling depower() or doing
// another read or write.
void write(uint8_t v, uint8_t power = 0);
void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0);
// Read a byte.
uint8_t read(void);
void read_bytes(uint8_t *buf, uint16_t count);
// Write a bit. The bus is always left powered at the end, see
// note in write() about that.
void write_bit(uint8_t v);
// Read a bit.
uint8_t read_bit(void);
// Stop forcing power onto the bus. You only need to do this if
// you used the 'power' flag to write() or used a write_bit() call
// and aren't about to do another read or write. You would rather
// not leave this powered if you don't have to, just in case
// someone shorts your bus.
void depower(void);
#if ONEWIRE_SEARCH
// Clear the search state so that if will start from the beginning again.
void reset_search();
// Setup the search to find the device type 'family_code' on the next call
// to search(*newAddr) if it is present.
void target_search(uint8_t family_code);
// Look for the next device. Returns 1 if a new address has been
// returned. A zero might mean that the bus is shorted, there are
// no devices, or you have already retrieved all of them. It
// might be a good idea to check the CRC to make sure you didn't
// get garbage. The order is deterministic. You will always get
// the same devices in the same order.
bool search(uint8_t *newAddr, bool search_mode = true);
#endif
#if ONEWIRE_CRC
// Compute a Dallas Semiconductor 8 bit CRC, these are used in the
// ROM and scratchpad registers.
static uint8_t crc8(const uint8_t *addr, uint8_t len);
#if ONEWIRE_CRC16
// Compute the 1-Wire CRC16 and compare it against the received CRC.
// Example usage (reading a DS2408):
// // Put everything in a buffer so we can compute the CRC easily.
// uint8_t buf[13];
// buf[0] = 0xF0; // Read PIO Registers
// buf[1] = 0x88; // LSB address
// buf[2] = 0x00; // MSB address
// WriteBytes(net, buf, 3); // Write 3 cmd bytes
// ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
// if (!CheckCRC16(buf, 11, &buf[11])) {
// // Handle error.
// }
//
// @param input - Array of bytes to checksum.
// @param len - How many bytes to use.
// @param inverted_crc - The two CRC16 bytes in the received data.
// This should just point into the received data,
// *not* at a 16-bit integer.
// @param crc - The crc starting value (optional)
// @return True, iff the CRC matches.
static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0);
// Compute a Dallas Semiconductor 16 bit CRC. This is required to check
// the integrity of data received from many 1-Wire devices. Note that the
// CRC computed here is *not* what you'll get from the 1-Wire network,
// for two reasons:
// 1) The CRC is transmitted bitwise inverted.
// 2) Depending on the endian-ness of your processor, the binary
// representation of the two-byte return value may have a different
// byte order than the two bytes you get from 1-Wire.
// @param input - Array of bytes to checksum.
// @param len - How many bytes to use.
// @param crc - The crc starting value (optional)
// @return The CRC16, as defined by Dallas Semiconductor.
static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0);
#endif
#endif
};
// Prevent this name from leaking into Arduino sketches
#ifdef IO_REG_TYPE
#undef IO_REG_TYPE
#endif
#endif // __cplusplus
#endif // OneWire_h
This diff is collapsed.
#ifndef OneWire_Direct_RegType_h
#define OneWire_Direct_RegType_h
#include <stdint.h>
// Platform specific I/O register type
#if defined(__AVR__)
#define IO_REG_TYPE uint8_t
#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK66FX1M0__) || defined(__MK64FX512__)
#define IO_REG_TYPE uint8_t
#elif defined(__IMXRT1052__) || defined(__IMXRT1062__)
#define IO_REG_TYPE uint32_t
#elif defined(__MKL26Z64__)
#define IO_REG_TYPE uint8_t
#elif defined(__SAM3X8E__) || defined(__SAM3A8C__) || defined(__SAM3A4C__)
#define IO_REG_TYPE uint32_t
#elif defined(__PIC32MX__)
#define IO_REG_TYPE uint32_t
#elif defined(ARDUINO_ARCH_ESP8266)
#define IO_REG_TYPE uint32_t
#elif defined(ARDUINO_ARCH_ESP32)
#define IO_REG_TYPE uint32_t
#define IO_REG_MASK_ATTR
#elif defined(ARDUINO_ARCH_STM32)
#define IO_REG_TYPE uint32_t
#elif defined(__SAMD21G18A__)
#define IO_REG_TYPE uint32_t
#elif defined(RBL_NRF51822)
#define IO_REG_TYPE uint32_t
#elif defined(__arc__) /* Arduino101/Genuino101 specifics */
#define IO_REG_TYPE uint32_t
#elif defined(__riscv)
#define IO_REG_TYPE uint32_t
#else
#define IO_REG_TYPE unsigned int
#endif
#endif
Sensor IDs
brain2: 28 06 71 CD 0A 00 00 C2
node 1: 28 18 4C CD 0A 00 00 E2
node 2: 28 78 61 CD 0A 00 00 35
node 3: 28 71 47 CD 0A 00 00 C7
node 4: 28 70 47 CD 0A 00 00 F0
//
//
// serial_button.c
//
// 115200 baud FTDI connection that outputs '0' or '1' depending
// on the state of a physical button
//
// 115200 baud FTDI connection
// set lfuse to 0x5E for 20 MHz xtal
//
// Neil Gershenfeld
// 12/8/10
// Erik Strand
// 11/26/2018
// 11/26/2018 and beyond
//
#include <avr/io.h>
#include <util/delay.h>
......@@ -36,7 +32,7 @@
#define serial_pin_out (1 << PA1)
#define led_pin (1 << PB2)
#define button_pin (1 << PA7)
//#define button_pin (1 << PA7) // this is no longer a button
#define max_buffer 25
......@@ -158,44 +154,52 @@ void setup(void) {
DDRB |= led_pin;
// Configure button_pin as an input.
DDRA &= ~button_pin;
//DDRA &= ~button_pin;
// Activate button_pin's pullup resistor.
PORTA |= button_pin;
//PORTA |= button_pin;
}
void loop(void) {
if (PINA & button_pin) {
// Turn off the LED.
PORTB &= ~led_pin;
//put_char(&serial_port, serial_pin_out, '0');
} else {
PORTB |= led_pin;
//put_char(&serial_port, serial_pin_out, '1');
}
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
float celsius;
const unsigned n_nodes = 5;
static float measurements[n_nodes]; // so we can store each temp reading before printing
if (!ds.search(addr)) {
put_line(&serial_port, serial_pin_out, "No more addresses.");
PORTB |= led_pin;
//put_line(&serial_port, serial_pin_out, "No more addresses.");
put_float(&serial_port, serial_pin_out, measurements[i]);
for (unsigned i = 1; i < n_nodes; ++i) {
put_string(&serial_port, serial_pin_out, ", ");
put_float(&serial_port, serial_pin_out, measurements[i]);
}
put_char(&serial_port, serial_pin_out, '\n');
ds.reset_search();
delay(250);
for (unsigned i = 0; i < n_nodes; ++i) {
measurements[i] = 0;
}
// Wait five seconds
delay(200);
PORTB &= ~led_pin;
delay(4800);
return;
}
//Serial.print("ROM =");
/*
put_string(&serial_port, serial_pin_out, "ROM=");
for (i = 0; i < 8; i++) {
put_char(&serial_port, serial_pin_out, ' ');
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_line(&serial_port, serial_pin_out, "CRC is not valid!");
......@@ -205,20 +209,20 @@ void loop(void) {
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
put_line(&serial_port, serial_pin_out, " Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
//put_line(&serial_port, serial_pin_out, " Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
put_line(&serial_port, serial_pin_out, " Chip = DS18B20");
type_s = 0;
break;
//put_line(&serial_port, serial_pin_out, " Chip = DS18B20");
type_s = 0;
break;
case 0x22:
put_line(&serial_port, serial_pin_out, " Chip = DS1822");
type_s = 0;
break;
//put_line(&serial_port, serial_pin_out, " Chip = DS1822");
type_s = 0;
break;
default:
put_line(&serial_port, serial_pin_out, "Device is not a DS18x20 family device.");
return;
put_line(&serial_port, serial_pin_out, "Device is not a DS18x20 family device.");
return;
}
ds.reset();
......@@ -232,15 +236,15 @@ void loop(void) {
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
put_string(&serial_port, serial_pin_out, " Data = ");
put_hex(&serial_port, serial_pin_out, present);
put_char(&serial_port, serial_pin_out, ' ');
//put_string(&serial_port, serial_pin_out, " Data = ");
//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();
put_hex(&serial_port, serial_pin_out, data[i]);
//put_hex(&serial_port, serial_pin_out, data[i]);
}
put_string(&serial_port, serial_pin_out, " CRC=");
put_hex(&serial_port, serial_pin_out, OneWire::crc8(data, 8));
//put_string(&serial_port, serial_pin_out, " CRC=");
//put_hex(&serial_port, serial_pin_out, OneWire::crc9(data, 8));
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
......@@ -262,10 +266,32 @@ void loop(void) {
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
//fahrenheit = celsius * 1.8 + 32.0;
switch (addr[1]) {
case 0x06:
measurements[0] = celsius;
break;
case 0x18:
measurements[1] = celsius;
break;
case 0x78:
measurements[2] = celsius;
break;
case 0x71:
measurements[3] = celsius;
break;
case 0x70:
measurements[4] = celsius;
break;
default:
put_line(&serial_port, serial_pin_out, "Error: unrecognized temperature node");
}
/*
put_string(&serial_port, serial_pin_out, " Temperature = ");
put_float(&serial_port, serial_pin_out, celsius);
put_string(&serial_port, serial_pin_out, " Celsius, ");
put_float(&serial_port, serial_pin_out, fahrenheit);
put_line(&serial_port, serial_pin_out, " Fahrenheit");
*/
}