ATtiny85: protótipo de sensor sem fio

Geralmente, para passar de uma ideia para uma implementação, é necessário um protótipo do dispositivo, conveniente para verificar e depurar no local, o que é especialmente importante para um dispositivo móvel. A seguir, tentarei analisar em detalhes o processo de criação de um protótipo de um sensor sem fio baseado no ATtiny85.





— , , : , , . CR2032, , 2.7V ( TMP36), 200mAh.



ATtiny85 5 / RESET . :



  • 3 — NRF24L01+, , 3- ;
  • 1 — BPW17N;
  • 2 — TMP36, , .


, .





RESET /



RESET /. RESET , (Fuses). , RESET . , «» (12V) , Fuse-.



Arduino ATTiny Fuse Reset with 12 Volt Charge Pump. , 12V (Charge Pump).



, .



(4) 5V, (2) (), C1 5V. (2) 5V 1 10V, D2, (3), C2. (3) (2) 15V C3, 20V ( ~17V) C4. R2/R3 A0, , 12V, ( (2) (3)) . Q1 . ATtiny85 .




:





( )


, USB , , . , , :



, .






KiCad EDA, .



KiCad GitHub, attwlight_sensor.pcb.



, YouTube KiCAD Quick-Start Tutorial.



, KiCad , : , , .



, . .



. Ctrl+E ( ) : , , .



, Preferences → Component Libraries, Add . , .



:





, . , . , . , , , .



TMP36



PB5 . VCC D10 P2. RESET RESET. ADC2.



C1, RESET . , RESET , , , . , , , — . , D1 SCK, , SCK , . C1 , , 5V. 1.8-5.5V 3V . TMP36 10nF , . .



NRF24L01+



KiCad , NRF24L01+. 3 MISO MOSI, , : R3, D1 C2. nRF24L01+ with ATtiny85 3 Pins.



, CE (chip enable) , CSN (chip select) , . , CSN , SCK (clock). SCK C2 CSN , SCK , CSN. MISO MOSI , .



1-10μF , C4.



, ATtiny85 . , avrdude , … . , - - . . , .



ATtiny85



8MHz, . , 30cm, , , , - . , , , . , . ATtiny85 1MHz , . .



BPW17N



, , . PCINT3 (Pin Change Interrupt).



R1 C3 , , . 3 Q1 , . , 0.1μF, 10MΩ , — . , / R2, , .



, , , . , , 7μA 260μA . , , (PCINT3 WDT), ADC3, , PCINT3. , .





. KiCad. :


  1. , .
  2. . :

    .
    , 1/10" (2.54mm). , , , , (Ctrl+E) .
    .
  3. netlist , .


. , netlist (Read Current Netlist), , . , netlist .



netlist . 1/10" (2.54mm), ( m), Visibles → Render → Values , , .



:





, . KiCad , . , : Visibles → Render → Footprints Front, .





:





: R1,C3 .





, . , , .



IDE



Arduino IDE AVR .



, Arduino IDE ATtiny85, . , Programming an ATtiny w/ Arduino 1.6. ATtiny85 Arduino . , , — , .



Arduino ATtiny85, arduino-tiny, Arduino 1.5 . , , , , . , , Arduino IDE v1.6.6 - , c 1.6.5.



Arduino IDE v1.6.5
  1. Arduino hardware;
  2. arduino-tiny-0150-0020.zip ;
  3. «Prospective Boards.txt» boards.txt , , ATtiny85, ;
  4. Arudino IDE Tools → Board .



. , Arduino File → Examples → ArduinoISP, , , 10μF, RST GND Arduino.





. Tools → Board , : ATtiny85 @ 1 MHz (internal oscillator; BOD disabled), Tools → Port USB Tools → Programmer , : Arduino as ISP, Upload.





, , :

  • NRF24L01+;
  • ;
  • ;
  • WDT (WatchDog Timer);
  • Pin Change PCINT;
  • .


:





NRF24L01+, ATtiny85. libraries Arduino.



. GitHub attwlight_sensor, attwlight_rx .





void sleep() void loop() PCINIT3 WDT. .



, . ATtiny85 datasheet «23. Register Summary», .



#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>

#define WDTO_INFINITE 255
#define SLEEP_PERIOD WDTO_8S
#define SKIP_WDT_WAKEUPS 14 // x SLEEP_PERIOD + SLEEP_PERIOD (14 ~= 120 sec)

//  ()  PCINT,  ATtiny85   (B),     PCINT
ISR(PCINT0_vect)
{
  light_pcint = true;
}

//    WDT
ISR(WDT_vect)
{
  wakeup_counter++;
}

void sleep()
{
  GIMSK = _BV(PCIE); //  Pin Change 
  if (SLEEP_PERIOD == WDTO_INFINITE || payload.light != LIGHT_FUZZY)
    PCMSK |= _BV(LIGHT_PIN); // PCINT3;           
  ADCSRA &= ~_BV(ADEN); //  ADC;  

  if (SLEEP_PERIOD != WDTO_INFINITE) {
    wdt_enable(SLEEP_PERIOD); //  
    WDTCR |= _BV(WDIE); //    ;   ATtiny85
  }

  //    Power-down; MCUSR &= ~_BV(SM0); MCUSR |= _BV(SM1);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable(); //   ; MCUSR |= _BV(SE);
  sei(); //  

  sleep_cpu(); // 

  cli(); //  ;    PCINT3
  PCMSK &= ~_BV(LIGHT_PIN); // PCINT3; 
  sleep_disable(); //   ; MCUSR &= ~_BV(SE);
  ADCSRA |= _BV(ADEN); //  ADC
  sei(); //  ;     
}




« ». , , 20μA.



#define FUZZY_VOLTAGE 0.4 //      ,  

const unsigned int fuzzy_start = int(1024.0 * (1.0 - FUZZY_VOLTAGE) / 2.0);
const unsigned int fuzzy_stop = 1023 - fuzzy_start;

light_t readLightStatus(unsigned int ms)
{
  delay(ms); //     
  int input = analogRead(LIGHT_PIN); // ADC3

  //   
  if (input >= fuzzy_start && input <= fuzzy_stop)
    return LIGHT_FUZZY;
  if (input < fuzzy_start)
    return LIGHT_OFF;
  if (input > fuzzy_stop)
    return LIGHT_ON;

  // ;     
  return LIGHT_UNKNOWN;
}




AREF , , . , , . .



, Vbg (Bandgap reference voltage) 1.1V. Vbg 1.0-1.2V, 1125300.



long readVcc()
{
  //    Vcc    Vbg == 1.1V
  ADMUX = _BV(MUX3) | _BV(MUX2);
  delay(2); //   1ms  
  ADCSRA |= _BV(ADSC); // ADC Start conversion -  
  while (bit_is_set(ADCSRA, ADSC)); // ADSC == 1   

  //  ADCL  ADCH,  ADCH    => ADCL  
  uint8_t low  = ADCL; //    ADC
  uint8_t high = ADCH; //    ADC

  long result = (high << 8) | low;

  result = 1125300L / result; // 1125300 = 1.1*1023*1000
  return result; // Vcc  
}




, .



float readTmp36(long vcc)
{
  int input = analogRead(TMP36_PIN); //   refVolts
  float refVolts = float(vcc) / 1000.0;
  float voltage = float(input) * refVolts / 1024.0; //  
  float temperatureC = (voltage - 0.5) * 100.0; //  500mV & 10mV == 1°C
  return temperatureC;
}


, , . , , , , .



, , 8KiB :



Sketch uses 7,120 bytes (86%) of program storage space. Maximum is 8,192 bytes.
Global variables use 227 bytes (44%) of dynamic memory, leaving 285 bytes for local variables. Maximum is 512 bytes.


, , Fuses RESET /. Arduino IDE , :



avrdude -c stk500v1 -p attiny85 -P /dev/ttyUSB0 -v -C /etc/avrdude.conf -b 19200 -U lfuse:w:0x62:m -U hfuse:w:0x5f:m -U efuse:w:0xff:m


  • avrdude — Arduino IDE hardware/tools/avr/bin/;
  • /dev/ttyUSB0 — ;
  • /etc/avrdude.conf — Arduino IDE hardware/tools/avr/etc/avrdude.conf.


Fuses, Engbedded Atmel AVR® Fuse Calculator.



, : 8 ( 1MHz); , ; BOD (Brown-out Detector) — ; ; RESET.







. : 120sec, , ; ; 200mAh.



1MHz8MHz
()15.7mA18.3mA
()1.1mA4.3mA
()7μA7μA
( )51ms16ms
( )15ms4ms
()513739


, . , 8MHz . , .



GitHub

Source: https://habr.com/ru/post/pt388079/


All Articles