diff --git a/841-tinycore-micronucleus-vcc-header-test/841vcc.h b/841-tinycore-micronucleus-vcc-header-test/841vcc.h new file mode 120000 index 0000000..5c7efd5 --- /dev/null +++ b/841-tinycore-micronucleus-vcc-header-test/841vcc.h @@ -0,0 +1 @@ +../mylibs/841vcc.h \ No newline at end of file diff --git a/841-tinycore-micronucleus-vcc-header-test/Makefile b/841-tinycore-micronucleus-vcc-header-test/Makefile new file mode 100644 index 0000000..dac0e6b --- /dev/null +++ b/841-tinycore-micronucleus-vcc-header-test/Makefile @@ -0,0 +1,53 @@ +# Arduino Make file. Refer to https://github.com/sudar/Arduino-Makefile + +# attiny841: +# BOARD_TAG = attinyx41 +# BOARD_SUB = 841 +# attiny861: +# BOARD_TAG = attinyx61 +# BOARD_SUB = 861 +# attiny85: +# BOARD_TAG = attinyx5 +# BOARD_SUB = 85 +# attiny84: +# BOARD_TAG = attinyx4 +# BOARD_SUB = 84 + +ARDUINO_VERSION = 10810 + +PROJECT_DIR = $(shell pwd) +BOARD_TAG = attinyx41 +BOARD_SUB = 841 +ARDUINO_DIR = /usr/local/arduino +ARDMK_DIR = /usr/local/arduino/Arduino-Makefile +MONITOR_PORT = /dev/ttyACM0 +ISP_PORT = /dev/ttyACM0 +AVRDUDE = /usr/local/bin/avrdude +ARDUINO_LIBS = +ARDUINO_SKETCHBOOK = . + +# mk stuff +ALTERNATE_CORE = ATTinyCore +F_CPU = 8000000L +MONITOR_BAUDRATE = 115200 +AVRDUDE_ARD_PROGRAMMER = stk500v2 +AVRDUDE_ARD_BAUDRATE = 9600 +AVR_TOOLS_DIR = /usr/local/avr +AVRDUDE_CONF = /usr/local/etc/avrdude.conf + +# Micronucleus +MNINST = sudo micronucleus + +# compiler stuff +CFLAGS_STD = -std=gnu11 +CXXFLAGS_STD = -std=gnu++11 +CXXFLAGS + = -pedantic -Wall -Wextra +CURRENT_DIR = $(shell pwd) + +# keep this! +include $(ARDMK_DIR)/Arduino.mk + +# install using micronucleus +install: $(TARGET_HEX) verify_size + @echo "########### Press RESET on the Nanite! ##############" + $(MNINST) $(TARGET_HEX) diff --git a/841-tinycore-micronucleus-vcc-header-test/README.md b/841-tinycore-micronucleus-vcc-header-test/README.md new file mode 100644 index 0000000..fe21e57 --- /dev/null +++ b/841-tinycore-micronucleus-vcc-header-test/README.md @@ -0,0 +1,17 @@ +# Usable Pins + +``` +PIN_A0 (10) +PIN_A1 ( 9) +PIN_A2 ( 8) +PIN_A3 ( 7) +PIN_A4 ( 6) +PIN_A5 ( 5) +PIN_A6 ( 4) +PIN_A7 ( 3) +PIN_B0 ( 0) +PIN_B1 ( 1) +PIN_B2 ( 2) +PIN_B3 (11) /* RESET */ +LED_BUILTIN (2) +``` diff --git a/841-tinycore-micronucleus-vcc-header-test/SensorSerial.ino b/841-tinycore-micronucleus-vcc-header-test/SensorSerial.ino new file mode 100644 index 0000000..4c0a576 --- /dev/null +++ b/841-tinycore-micronucleus-vcc-header-test/SensorSerial.ino @@ -0,0 +1,93 @@ +/* -*-c++-*- + Blink + Turns on an LED on for one second, then off for one second, repeatedly. + This example code is in the public domain. + */ + +/* + ./hardware/ATTinyCore/avr/cores/tinymodern/core_pins.h + Caution: reverse Pinout: PA3 => Pin 3 + + Nanite841 Pinout from above: + + button +SDO/MISO PA5 PA6 SDI/MOSI + SCK PA4 PA7 + CS PA3 PB2 + RX0 PA2 PB2 + TX0 PA1 PB1 + PA0 PB0 + GND VCC + usb + + Bosch BME280 Breakout from above pin header left: + + VCC + GND + SCL => SCK + SDI => MOSI + CSB => PA3/3 + SDO => MISO + + */ + +#include +#include +#include "841vcc.h" + +#define NODE_ID 1 +#define OUTPUT_PIN 7 +#define LED 2 //LED_BUILTIN // PORTB2 +#define CS 3 +#define DELAY 2000 + +#define SET_OUTPUT(pin) DDRB |= (1 << pin) +#define SET_HIGH(pin) PORTB |= (1 << pin) +#define SET_LOW(pin) PORTB &= ~(1 << pin) + +void print_asufloat(uint32_t val, uint16_t factor) { + Serial.print(val / factor); + Serial.print("."); + Serial.print(val % factor); +} + +void print_asifloat(int32_t val, uint16_t factor) { + Serial.print(val / factor); + Serial.print("."); + Serial.print(val % factor); +} + +void print_measurements() { + float vcc; + + SET_LOW(LED); + + adc_enable(); + adc_start(); + vcc = adc_get_vcc(); + adc_disable(); + + Serial.print("Voltage: "); + Serial.println(vcc); + + SET_HIGH(LED); +} + +void halt() { + while(1); +} + + +void setup() { + SET_OUTPUT(LED); + Serial.begin(115200); + Serial.println("init"); + + adc_setup_vcc_measurement(); +} + +void loop() { + print_measurements(); + delay(DELAY); +} + diff --git a/841-tinycore-micronucleus-vcc-header-test/hardware b/841-tinycore-micronucleus-vcc-header-test/hardware new file mode 120000 index 0000000..d34e522 --- /dev/null +++ b/841-tinycore-micronucleus-vcc-header-test/hardware @@ -0,0 +1 @@ +/usr/local/arduino/hardware \ No newline at end of file diff --git a/mylibs/841vcc.h b/mylibs/841vcc.h new file mode 100644 index 0000000..fd6654c --- /dev/null +++ b/mylibs/841vcc.h @@ -0,0 +1,47 @@ +/* + Macros for easier usage of vcc measurements. + For attiny8441 only. + + We use REFS=0 to select VCC as Reference. i.e. ADMUXB = 0 << REFS0 + We then use channel #13 to read the BandGap. i.e. ADMUXA = 13 << MUX0 + + And calculate the value of VCC from the reading. i.e. ADCW / 1024 = 1.1 / VCC + Rearrange i.e. VCC = 1.1 * 1024 / ADCW + + Thanks for the help of the guys at AVR-Freaks: + https://www.avrfreaks.net/forum/measuring-vcc-attiny841-using-adc-and-internal-bandgap +*/ + + +#include + +#ifndef VCC_8441_H +#define VCC_8441_H + +// disable ADC globally +#define adc_disable() ADCSRA &= ~(1<> 1.1 * 1024 / 225 #=> 5.006222222222223 +// >> 1.1 * 1024 / 341 #=> 3.3032258064516133 +#define adc_get_vcc() 1.1 * 1024 / ADCW; + +#endif