Microchip ATTINY824 Handleiding


Lees hieronder de đź“– handleiding in het Nederlandse voor Microchip ATTINY824 (33 pagina's) in de categorie Niet gecategoriseerd. Deze handleiding was nuttig voor 32 personen en werd door 2 gebruikers gemiddeld met 4.5 sterren beoordeeld

Pagina 1/33
AN3641
Low-Power, Cost-Efficient PIR Motion Detection Using the
tinyAVR® 2 Family
Features
• 12-bit ADC, Differential Measurement
• ADC Sample Accumulation Up To 1024 Samples
• Programmable Gain Amplifier (PGA) Up To 16x Gain
• Signal Amplifying, Oversampling And Analyzing Using The Built-in ADC With PGA
• Hardware Averaging Using Burst Mode
• ADC Triggered Through The Event System Using The Periodic Interrupt Timer (PIT)
• Reduced Bill Of Material (BoM) Cost Compared To Typical Passive InfraRed (PIR) Solutions
• Visualize Data Using The Microchip Data Visualizer And Data Streamer Protocol
• Visualize Power Consumption Using The Microchip Data Visualizer And a Power Debugger
Introduction
Author: Kjetil Kirkholt, Microchip Technology Inc.
This application note describes how to use the 12-bit differential ADC with PGA in the tinyAVR
® 2 Family of
microcontrollers and how to collect measurements using a sensor while keeping the currentPassive InfraRed (PIR)
consumption at a minimum.
In this application example, the signal from the PIR sensor is amplified, oversampled, digitally filtered, and analyzed
to determine if movement has occurred. LED0 on the ATtiny1627 Curiosity Nano (DM080104) board is used to
indicate when movement is detected. The measured data can be sent to the serial terminal and illustrated in the Data
Visualizer application or plugin for Atmel Studio.
The output signal levels from a PIR sensor are typically very low and less than 1 mV. To detect the movement and
avoid false detections, the signal needs to be amplified before being sampled by the ADC. In typical PIR solutions,
this is achieved by using several Operational Amplifier (op amp) stages with high gain. In this application example, no
external op amps are used, and the amplification of the signal is purely done inside the microcontroller (MCU) by
using the internal PGA. The ADC supports sampling in bursts, where a configurable number of conversion results are
accumulated automatically into a single ADC result (sample accumulation). The application example accumulates 16
samples, which increases the resolution of the 12-bit ADC by two bits. It is possible to oversample the signal up to
1024 times and gain five additional bits, resulting in a 17-bit ADC resolution, if needed.
More information on oversampling can be found in the ADC Oversampling with tinyAVR 0- and 1-series, and
megaAVR 0-series product brief.
For demonstration purposes, the ATtiny1627 Curiosity Nano board, Curiosity Nano Base for Click boards
™, and a
modified MIKROE-3339 PIR Click boards™ are used.
The example code for replicating the results described in this application note is available from Atmel START:
• PIR sensor with tinyAVR 2-series ADC
–start.atmel.com/#example/Atmel%3AApplication_AVR_Examples%3A1.0.0%3A%3AApplication
%3APIR_Motion_Detection%3A
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 1
The bare metal code example (without using Atmel START drivers) is available on GitHub:
View Code Examples on GitHub
Click to browse repositories
More information about the application is described in Sections and .Demo Operation Code Implementation
Additional details on the ADC performance and general configuration are available in the device data sheet.
AN3641
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 2
Table of Contents
Features......................................................................................................................................................... 1
Introduction.....................................................................................................................................................1
1. Block Diagram.........................................................................................................................................5
2. PIR Sensor Theory..................................................................................................................................6
3. Demo Operation......................................................................................................................................8
3.1. Configuring, Customizing, Tuning the PIR Application Sampling, Filtering and Detection
Parameters...................................................................................................................................8
3.1.1. Sample Acquisition Parameters.....................................................................................8
3.1.2. Filtering and Detection Algorithm Parameters...............................................................8
3.1.3. Debug Interface Parameters..........................................................................................9
3.2. Hardware Prerequisite..................................................................................................................9
3.2.1. Click board™ Modifications............................................................................................9
3.2.1.1. Filter Simulation Using MPLAB® Mindi™ Analog Simulator ......................11
3.3. Hardware Setup......................................................................................................................... 12
3.4. Software Prerequisite.................................................................................................................13
3.5. Running the Example.................................................................................................................13
4. Source Code Overview......................................................................................................................... 15
4.1. MCU and Peripheral Configuration............................................................................................ 15
4.2. Flow Chart..................................................................................................................................16
4.3. Timing Diagram.......................................................................................................................... 17
4.4. Code Implementation................................................................................................................. 17
4.4.1. Peripheral Initialization.................................................................................................17
4.4.2. Main Code................................................................................................................... 18
4.4.3. Warm-up and Filter Creation........................................................................................19
4.4.4. Update Average Filters................................................................................................ 20
4.4.5. Check for Movement....................................................................................................20
4.4.6. ADC Result Ready Interrupt Routine...........................................................................21
5. Summary...............................................................................................................................................22
6. Plotting Graph in .......................................................................................................... 23Data Visualizer
7. Plotting Power Consumption.................................................................................................................24
7.1. Power Consumption...................................................................................................................25
8. Get Code Examples from Atmel START............................................................................................... 27
9. Get Code Examples from GitHub..........................................................................................................28
10. Revision History.................................................................................................................................... 29
The Microchip Website.................................................................................................................................30
Product Change Notification Service............................................................................................................30
Customer Support........................................................................................................................................ 30
AN3641
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 3
Product Identification System.......................................................................................................................31
Microchip Devices Code Protection Feature................................................................................................ 31
Legal Notice................................................................................................................................................. 31
Trademarks.................................................................................................................................................. 31
Quality Management System....................................................................................................................... 32
Worldwide Sales and Service.......................................................................................................................33
AN3641
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 4
1. Block Diagram
Figure 1-1. Block Diagram of the System
AN3641
Block Diagram
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 5
2. PIR Sensor Theory
A PIR sensor detects changes in the amount of infrared radiation “seen” by the sensor elements, which varies
depending on the temperature and surface characteristics of the object in front of the sensor.
When a person passes between the sensor and the background, the sensor detects the change from ambient
temperature to body temperature, and back again. The sensor converts the resulting change in the incoming infrared
radiation into a change in the output voltage (VPIR(t)), as shown in the . OtherPIR sensor motion detection principle
objects with the same temperature as the background, but with different surface characteristics, will also cause the
sensor to detect a different emission pattern.
Figure 2-1. PIR Sensor Motion Detection Principle
The figure below shows a plot of the raw data captured from the sensor using Data Visualizer. A hand is placed over
the sensor and held there for a short time and then removed.
AN3641
PIR Sensor Theory
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 6
Figure 2-2. PIR Sensor Raw Data Motion Detection Using Data Visualizer
No Movement Movement in
front of sensor Movement No MovementMovement away
from sensorstopped
AN3641
PIR Sensor Theory
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 7
3. Demo Operation
When using a PIR sensor in an application, it is necessary to allow the sensor some time to warm up. During warm-
up the PIR sensor will reach equilibrium to the IR radiation in its current field of view and thus “learn” its environment
to stabilize the output signal. During this time, it is important to keep the environment in the sensor's field of view
undisturbed. The warm-up time can be adjusted in the application firmware by changing the define
PIR_WARMUP_TIME_MS. While in warm-up, LED0 will flash at 1 Hz. When LED0 stops flashing, the warm-up is
complete, and the system is now operating normally.
Waving a hand or walking in front of the sensor will now be detected and cause LED0 to flash at 4 Hz and will
continue flashing as long as there is movement in the senor's field of view. When the movement stops, LED0 turns
off.
3.1 Configuring, Customizing, Tuning the PIR Application Sampling, Filtering and
Detection Parameters
The demo application has been created in a way that makes it easy to modify the sampling acquisition parameters,
the filtering parameters, and detection threshold parameters. These parameters are all conveniently located in the
main.c file of the project. Tuning these parameters will allow you to customize your design to optimize the signal
integrity, range, and power consumption for your design.
3.1.1 Sample Acquisition Parameters
• PIR_OVERSAMPLE_RATE
– This parameter sets the numbers of times the signal is oversampled. Increasing this parameter will result in
an increased resolution, thus increasing the detection range, but it will also increase power consumption.
Decreasing this parameter will have the opposite effect on the detection range and power consumption.
• PIR_SAMPLE_RATE_PER_SECOND
– This parameter sets the number of samples done each second. Increasing this parameter will make the
application respond faster to movement, increase the detection range, but it will also increase power
consumption. Decreasing this parameter will have the opposite effect on responsiveness, range, and power
consumption.
• PIR_PGA_GAIN
– This parameter sets the PGA gain. Increasing or decreasing this parameter will affect the PGA gain
resulting in a higher or lower signal to the ADC, which will affect the effective detection range that the
system. Changing the gain will not affect power consumption.
3.1.2 Filtering and Detection Algorithm Parameters
• PIR_WARMUP_TIME
– This parameter sets the PIR warm-up time. This parameter should be adjusted to match the warm-up time
needed for the selected PIR sensor in the application.
• PIR_DETECTION_THRESHOLD
– This parameter sets the detection threshold of the application. Decreasing this parameter can increase the
effective range that the system can detect movement on, but at the same time increase the chance of false
detections due to sudden changes in ambient temperatures or other disturbances. Increasing this
parameter decreases the range, but the likelihood of false detections will be lower. Setting this parameter
too high can result in no detections.
• PIR_LONG_TERM_FILTER_RANGE
– This parameter sets the length of the long term filter. Changes to this parameter need to be done carefully
while also considering the PIR_SHORT_TERM_FILTER_RANGE and PIR_DETECTION_THRESHOLD
parameters. Changes can affect the effective range of the system and increase power consumption.
• PIR_SHORT_TERM_FILTER_RANGE
– This parameter sets the length of the short term filter. Changes to this parameter need to be done carefully
while also considering the PIR_LONG_TERM_FILTER_RANGE and PIR_DETECTION_THRESHOLD
parameters. Changes can affect the effective range of the system and increase power consumption.
AN3641
Demo Operation
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 8
3.1.3 Debug Interface Parameters
• PIR_DEBUG_MESSAGES
– This parameter, if defined, will unlock the possibility to send debug data over UART to Data Visualizer.
Adding this parameter will increase the power consumption of the system and should be removed if low
power consumption is important.
Figure 3-1. Data Visualizer with Movement
ADC raw data
Filter delta
Fast filter
Slow filter
No movement in front of sensor
Hand swiped 50cm above the sensor
No movement in front of sensor
PIR_DETECTION_THRESHOLD
3.2 Hardware Prerequisite
• ATtiny1627 Curiosity Nano (DM080104): www.microchip.com/developmenttools/ProductDetails/DM080104
• Curiosity Nano Base for Click boards™ (AC164162): www.microchip.com/DevelopmentTools/ProductDetails/
AC164162
• MIKROE-3339 Click board: www.mikroe.com/pir-click
3.2.1 Click board™ Modifications
Before the Click board can be used in this demo, it needs to be modified by removing the op amp gain stage and
some other components.
The PIR Click board is used for demonstration purposes. The original Click board has an op amp solution for
amplifying the signal, which is shown in the figure below. This needs to be removed to demonstrate that the tinyAVR
®
2 family microcontroller can amplify the raw signal from the PIR sensor using the integrated PGA and differential
ADC.
AN3641
Demo Operation
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 9
Figure 3-2. Original Click board™ and Schematic
Unnecessary components are removed from the Click board showed by the red Xs in the figure below. New
connections and components are in blue. U2, R9, R10, R1, and PWR LED are not part of the original signal chain but
are removed to minimize the power consumption.
Figure 3-3. Planned Modifications to the Click board™ and Schematics
Resistor R5 is changed from 30 kΩ to 470 kΩ. R5 and C8 (100 nF) form a low-pass filter with a cutoff frequency of
3.38 Hz. This filter will transfer the full AC signal from the PIR sensor in addition to the DC bias and filters out high-
frequency noise.
Resistor R11 is changed from 100 kΩ to 470 kΩ, and C1 is changed from 10 µF to 2.2 µF. When R11 is combined
with capacitor C1, it forms a low-pass filter with a cutoff frequency of 0.154 Hz. This filter will block most of the AC
signal from the PIR and only transfer the sensors DC bias to the negative input of the ADC.
AN3641
Demo Operation
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 10
Figure 3-4. Modified Click board™ and Schematics
The cut-off frequency for both filters should be selected based on the application needs and what type of movement
the system is designed to detect.
3.2.1.1 Filter Simulation Using MPLAB
® Mindi™ Analog Simulator
When connecting the PIR sensor, as shown in the figure above, the DC bias will be applied to both the positive and
the negative input of the ADC. Since the ADC is configured in differential mode, the DC bias is eliminated, and the full
ADC range will be available for the AC signal.
By using the MPLAB® Mindi™ Analog Simulator, it is possible to simulate the behavior of the two low-pass filters
when a simulated PIR signal is applied. The simulation also shows how the differential signal to the ADC will look like
after the DC bias voltage is removed by the ADC differential inputs.
The PIR signal is simulated using a 1 Hz sine wave with 1 mV amplitude placed at an 800 mV bias voltage.
Figure 3-5. MPLAB® Mindi™ Filter Simulation
The two low-pass filters, in combination with the ADC differential input, effectively form a bandpass filter. The figure
below shows an MPLAB® Mindi™ simulation of the frequency response of the two filters in combination with the
differential ADC.
AN3641
Demo Operation
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 11
Figure 3-6. MPLAB® Mindi™ Filter Response
More information about MPLAB® Mindi™ Analog Simulator, how to download and to use it can be found here:
www.microchip.com/mplab/mplab-mindi.
3.3 Hardware Setup
This section provides information on the hardware setup and pin configuration, as shown below
AN3641
Demo Operation
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 12
Figure 3-7. Visual Representation of HW Setup
AN1
AN1
AN1
AN1 AN1
RST1
RST1
RST1
RST1 RST1
CS1
CS1
CS1
CS1 CS1
SCK
SCK
SCK
SCK SCK
MISO
MISO
MISO
MISO MISO
MOSI
MOSI
MOSI
MOSI MOSI
+3.3V
+3.3V
+3.3V
+3.3V +3.3V
GND
GND
GND
GND GND
+5V
+5V
+5V
+5V +5V
GND
GND
GND
GND GND
PWM1
PWM1
PWM1
PWM1 PWM1
INT1
INT1
INT1
INT1 INT1
RX1
RX1
RX1
RX1 RX1
TX1
TX1
TX1
TX1 TX1
SCL
SCL
SCL
SCL SCL
SDA
SDA
SDA
SDA SDA
1
1
1
1 1
AN2
AN2
AN2
AN2 AN2
RST2
RST2
RST2
RST2 RST2
CS2
CS2
CS2
CS2 CS2
SCK
SCK
SCK
SCK SCK
MISO
MISO
MISO
MISO MISO
MOSI
MOSI
MOSI
MOSI MOSI
+3.3V
+3.3V
+3.3V
+3.3V +3.3V
GND
GND
GND
GND GND
+5V
+5V
+5V
+5V +5V
GND
GND
GND
GND GND
PWM2
PWM2
PWM2
PWM2 PWM2
INT2
INT2
INT2
INT2 INT2
RX2
RX2
RX2
RX2 RX2
TX2
TX2
TX2
TX2 TX2
SCL
SCL
SCL
SCL SCL
SDA
SDA
SDA
SDA SDA
2
2
2
2 2
Curiosity Nano Adapter
USB
USB
USB
USBUSB
DEBUGGER
DEBUGGER
DEBUGGER
DEBUGGERDEBUGGER
A
A
A
AA
Ttiny1627
Ttiny1627
Ttiny1627
Ttiny1627Ttiny1627
SW0
SW0
SW0
SW0SW0
LED0
LED0
LED0
LED0LED0
PS LED
PS LED
PS LED
PS LEDPS LED
NC
NC
NC
NCNC ID
ID
ID
IDID CDCRX
CDCRX
CDCRX
CDCRXCDCRXCDCTX
CDCTX
CDCTX
CDCTXCDCTX DBG1
DBG1
DBG1
DBG1DBG1 DBG2
DBG2
DBG2
DBG2DBG2 PA1
PA1
PA1
PA1PA1 PA2
PA2
PA2
PA2PA2 PB1
PB1
PB1
PB1PB1 PB0
PB0
PB0
PB0PB0 PC2
PC2
PC2
PC2PC2 PC1
PC1
PC1
PC1PC1 PC0
PC0
PC0
PC0PC0 PC3
PC3
PC3
PC3PC3 GND
GND
GND
GNDGND PB2
PB2
PB2
PB2PB2 PB3
PB3
PB3
PB3PB3
VBUS
VBUS
VBUS
VBUSVBUS VOFF
VOFF
VOFF
VOFFVOFF DBG3
DBG3
DBG3
DBG3DBG3 DBG0
DBG0
DBG0
DBG0DBG0 GND
GND
GND
GNDGND VTG
VTG
VTG
VTGVTG PB6
PB6
PB6
PB6PB6 PB5
PB5
PB5
PB5PB5 PB4
PB4
PB4
PB4PB4 PA3
PA3
PA3
PA3PA3 PA4
PA4
PA4
PA4PA4 PA5
PA5
PA5
PA5PA5 PA6
PA6
PA6
PA6PA6 PA7
PA7
PA7
PA7PA7 GND
GND
GND
GNDGND PC5
PC5
PC5
PC5PC5 PC4
PC4
PC4
PC4PC4
ATtiny1627
CURIOSITY NANO
Table 3-1. Pin Configuration and Click board™
Curiosity Nano Adapter
Slot
Click board™Curiosity Nano Adapter Pin Name MCU Pin
Name
Slot 3 Modified PIR Click AN3 PA5
PWM3 PB4
3.4 Software Prerequisite
• Atmel Studio 7 (Version 7.0.1931 or later)
• Atmel Studio ATtiny_DFP version 1.4.310 or later
• Data Visualizer, standalone or Extension in Atmel Studio
3.5 Running the Example
1. Connect the modified Click board to slot 3 of the Curiosity Nano Base for Click boards
™.
2. Connect the ATtiny1627 Curiosity Nano to the Curiosity Nano Base for Click boards
™.
3. Connect the ATtiny1627 Curiosity Nano to a computer using a USB cable.
AN3641
Demo Operation
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 13
4. Download the application, as explained in Section 8. Get Code Examples from Atmel START 9. or Section
Get Code Examples from GitHub.
5. Choose one of the two code projects and program the ATtiny1627.
6. Wave a hand or walk in front of the sensor and observe the red LED flashing.
AN3641
Demo Operation
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 14
4. Source Code Overview
4.1 MCU and Peripheral Configuration
• CPU clock: 10 MHz in Active mode, 5 MHz in Standby sleep mode
• Peripherals used:
– ADC in differential mode
• PGA set to 16x gain
• ADC MUXPOS input channel is AIN9: pin PB4
• ADC MUXNEG input channel is AIN5: pin PA5
• ADC reference voltage: 1.024V
• ADC clock: 2.5 MHz (FCPU/2)
• ADC triggered trough the Event System
– RTC/PIT:
• RTC clock running at 1024 Hz
• PIT/256 connected to ADC trough the event system. The conversion trigger rate is 4 Hz.
• PIT/256 connected to the event out pin PB7. LED0 toggle rate is 4 Hz when movement is detected.
• PIT/1024 connected to the event out pin PB7. LED0 toggle rate is 1 Hz during warm-up.
– Event System
• Connects PIT to ADC
• Connects PIT to LED0
– USART0:
• TXD: PB2
• RXD: PB3
• Baud rate: 115200, ADC result is sent to the serial terminal
AN3641
Source Code Overview
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 15
4.2 Flow Chart
Figure 4-1. Firmware Flowchart
AN3641
Source Code Overview
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 16
4.3 Timing Diagram
Figure 4-2. Firmware Timing Diagram
4.4 Code Implementation
The code consists of these main parts:
• Peripheral initialization
• Main loop
• Warm-up and filter creation
• Updating average filters
• Checking for movement
• Handling the ADC interrupt routine
4.4.1 Peripheral Initialization
The initialization of the peripherals in ATtiny1627 can be done using START drivers. As this application note is
focusing on the ADC, only the bare metal ADC initialization is detailed here.
void ADC_0_init()
{
ADC0 CTRLB ADC_PRESC_DIV2_gc .=;/* System clock divided by 2 */
ADC0 CTRLF PIR_OVERSAMPLE_RATE .=;/* Samples accumulation */
ADC0 CTRLC ADC_REFSEL_1024MV_gc .=/* Internal 1.024V Reference */
TIMEBASE_VALUE ADC_TIMEBASE_gp |(<< ); /* timebase value */
ADC0 CTRLE .=0x12;/* Sample Duration */
ADC0 DBGCTRL ADC_DBGRUN_bm .=;/* Debug run: enabled */
ADC0 COMMAND ADC_DIFF_bm .=/* Differential ADC Conversion enabled */
ADC_MODE_BURST_gc |;/* Burst Accumulation */
ADC0 INTCTRL ADC_RESRDY_bm .=;/* Result Ready Interrupt enabled */
AN3641
Source Code Overview
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 17
ADC0 MUXNEG ADC_VIA_PGA_gc .=/* Via PGA */
ADC_MUXNEG_AIN5_gc |;/* ADC input pin 5, PIR sensor */
ADC0 MUXPOS ADC_VIA_PGA_gc .=/* Via PGA */
ADC_MUXPOS_AIN9_gc |;/* ADC input pin 9, PIR sensor */
ADC0 PGACTRL ADC_PGAEN_bm .=/* PGA Enabled */
PIR_PGA_GAIN |/* Gain */
ADC_ADCPGASAMPDUR_15CLK_gc |/* PGA Sample Duration */
ADC_PGABIASSEL_1_2X_gc |;/* PGA bias set to 1/2 */
ADC0 CTRLA ADC_ENABLE_bm .=/* Enable ADC */
ADC_RUNSTDBY_bm |;/* Enable ADC to run in Standby Sleep mode */
ADC0 INTFLAGS ADC_RESRDY_bm .=;/* Clear ADC Result Ready Interrupt Flag */
}
In the example application, the ADC is initialized in .ADC_0_init()
The PGA has been enabled to amplify the signal coming from the PIR sensor, and the gain can be adjusted by
changing the define found in . The ADC is running in and toPIR_PGA_GAIN main.c Differential mode Burst mode
accumulate samples and amplify the signal further. The number of accumulated samples can be adjusted in
PIR_OVERSAMPLE_RATE.
In , the voltage difference between the two inputs is measured by the ADC.Differential mode
In , a burst of conversions is accumulated as fast as possible after a single trigger, and the conversionBurst mode n
results are accumulated into a single ADC result.
4.4.2 Main Code
In the main function, after initialization, the MCU will enter a while(1) loop where it will go into Standby sleep mode.
The MCU is only woken when the ADC conversion is complete. The filters are then updated, and movement
determination is made. To reduce the average power consumption, the peripheral clock speed is reduced before
entering Standby sleep mode and increased again after waking up, to speed up updating the filters, and determine if
any movement has occurred. This change in clock speed is only effective if the number of accumulated ADC samples
configured in is greater than eight. Otherwise, the time to change the clock speed is longerPIR_OVERSAMPLE_RATE
than the time spent doing the ADC conversions, thus increasing the power consumption.
int void main( )
{
/* Initial clock set to
5MHz.
*/
/* If number of samples accumulated by the ADC is less than 16, CLKCTRL_PDIV_2X_gc in
MCLKCTRLB and ADC0.CTRLB = ADC_PRESC_DIV4_gc; */
/* should be used to lower average power
consumption
*/
CLKCTRL MCLKCTRLB CLKCTRL_PDIV_4X_gc CLKCTRL_PEN_bp ccp_write_io((void * &) ( . ), |1<< ); /*
Divide main clock by 4x*/
IO_init(); /* Initialize IO pins */
EVENT_SYSTEM_0_init(); /* Initialize Event System */
RTC_init(); /* Initialize PIT */
#ifdef PIR_DEBUG_MESSAGES
USART_init(); /* Initialize USART if SEND_SERIAL_DATA is defined */
#endif
ADC_0_init(); /* Initialize the ADC */
SLPCTRL CTRLA SLPCTRL_SMODE_STDBY_gc .=;/* Select standby sleep mode */
sei(); /* Enable global interrupt */
warm_up_and_filter_creation(); /* Run warm-up and collect ADC data to create
filters */
while ( )1
{
AN3641
Source Code Overview
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 18
/* Reduce main clock speed to lower peripheral clock tree power consumption when ADC
is running in sleep mode */
/* Only effective if ADC is configured to accumulate more that 8
samples. */
/* If number of samples accumulated is less than 16, the line below should be
removed */
CLKCTRL MCLKCTRLB CLKCTRL_PDIV_4X_gc ccp_write_io((void * &) ( . ), |1<<
CLKCTRL_PEN_bp ); /* Divide main clock by 4x */
sleep_mode(); /* Enter sleep mode */
/* Increase main clock to run the code faster in active mode, this will reduce the
average power consumption */
/* If number of samples accumulated is less than 16, the line below should be
removed */
CLKCTRL MCLKCTRLB CLKCTRL_PDIV_2X_gc ccp_write_io((void * &) ( . ), |1<<
CLKCTRL_PEN_bp ); /* Divide main clock by 2x */
update_average_filters(); /* Update filters with new measurement */
check_for_movement(); /* Check if movement has happened by examining delta
between filters */
#ifdef PIR_DEBUG_MESSAGES /* Send data to data visualizer if SEND_SERIAL_DATA is
defined */
adc_result long_term_average short_term_average filter_deltausart_tx( , , , );
#endif
}
}
4.4.3 Warm-up and Filter Creation
This section of the code is run before the main loop as part of the initialization of the system. The warm-up time is just
a delay loop to give the PIR sensor time to adjust to the IR radiation and can be adjusted in the define
PIR_WARMUP_TIME_MS. The PIT is connected through the Event System to LED 0 on the ATtiny1627 Nano and will
flash at 1 Hz through the entire warm-up and filter creation part of the code to indicate to the user that the system is
warming up.
When the warm-up time is over, the ADC is started using the following command:
ADC0.COMMAND |= ADC_START_EVENT_TRIGGER_gc;
and the MCU is put in Standby sleep mode.
The PIT is configured to start the ADC conversion through the Event System. When the PIT sends the event to start
an ADC conversion, the ADC is started and performs the configured conversion and issues a result ready (REDRDY)
interrupt on completion that wakes the MCU. How often the PIT sends an event can be configured in the define
PIR_SAMPLE_RATE_PER_SECOND.
void warm_up_and_filter_creation()
{
uint8_t i =0;
EVSYS USEREVSYSEVOUTB EVSYS_USER_CHANNEL2_gc .=;/* Flash LED0 at 1 Hz to indicate warm-up
*/
/* Put warm-up delay/code here */
PIR_WARMUP_TIME_MS_delay_ms( );
/* End of warm-up delay/code */
ADC0 COMMAND ADC_START_EVENT_TRIGGER_gc .|= ;/* Enable ADC to be triggered by
Event*/
i PIR_LONG_TERM_FILTER_RANGEwhile(<)
{
sleep_mode(); /* Enter Standby sleep mode and wait for
ADC to complete*/
accumulated_long_term_average accumulated_long_term_average adc_result = + ;
i PIR_SHORT_TERM_FILTER_RANGEif (<)
{
accumulated_short_term_average accumulated_short_term_average adc_result= + ;
AN3641
Source Code Overview
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 19
}
i++;
}
EVSYS USEREVSYSEVOUTB .=0;/* Warm up and average filter creation complete, stop flashing
LED0 */
}
4.4.4 Update Average Filters
These filters are used to average out the noise in the PIR sensor measurement. This will reduce the chance of false
detections when the noise is high. The filter is the average of the four most recent ADCshort_term_average
measurements and can be adjusted in the define . For each new ADCPIR_SHORT_TERM_FILTER_RANGE
measurement, 1/(size of filter) is subtracted, and the new ADC measurement is added. This way, the filter is
constantly updated and will be able to track rapid changes in the ADC measurements, such as movement. The
long_term_average long_term_average filter swap out 1/(size of filter) for each new ADC measurement. The
filter is slower but will track and average the noise in the measurement in addition to the slower drift in the system.
When there is no movement, the filters will converge around the same value, and the will stay low.filter_delta
When movement happens, the signal from the PIR will change, and the will increase since thefilter_delta
short_term_average long_term_average will follow the changing signal faster than the .
void update_average_filters()
{
accumulated_long_term_average
accumulated_long_term_average PIR_LONG_TERM_FILTER_RANGE -= / ;/* Subtract 1/x from
accumulated filter value */
accumulated_short_term_average
accumulated_short_term_average PIR_SHORT_TERM_FILTER_RANGE -= / ;/* Subtract 1/y from
accumulated filter value */
accumulated_long_term_average adc_result += ;/* Add new ADC measurement (1/x) to
accumulated filter value */
accumulated_short_term_average adc_result += ;/* Add new ADC measurement (1/y) to
accumulated filter value */
long_term_average accumulated_long_term_average PIR_LONG_TERM_FILTER_RANGE = / ;/* Divide
the accumulated_long_term_average on X to create long_term_average */
short_term_average accumulated_short_term_average PIR_SHORT_TERM_FILTER_RANGE = / ;/*
Divide the accumulated_long_term_average on Y to create long_term_average */
filter_delta long_term_average short_term_average =-;/* Find delta between filters */
}
4.4.5 Check for Movement
This part of the code will compare the absolute value of to the tofilter_delta PIR_DETECTION_THRESHOLD
determine if any movement has occurred or not. If movement has been detected, the PIT will be connected to LED0
and flash at 4 Hz as long as the movement is present.
void check_for_movement()
{
filter_delta PIR_DETECTION_THRESHOLD if ( (abs )>)
{
EVSYS USEREVSYSEVOUTB EVSYS_USER_CHANNEL3_gc .=;/* Movement detected, flash LED0
*/
}
else
{
EVSYS USEREVSYSEVOUTB .=0;/* No movement, stop flashing LED0 */
}
}
AN3641
Source Code Overview
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 20
4.4.6 ADC Result Ready Interrupt Routine
The ADC Result Ready interrupt routine will be called when the ADC conversion is complete, and the configured
number of samples have been accumulated. Once a conversion is complete, the result will be read and stored in
adc_result.
ISR( )ADC0_RESRDY_vect
{
adc_result ADC0 RESULT =. ; /* Read ADC result register */
ADC0 INTFLAGS ADC_RESRDY_bm .=;/* Clear ADC result ready interrupt flag */
}
AN3641
Source Code Overview
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 21
5. Summary
The example described in this application note shows how the built-in 12-bit differential ADC and Programmable Gain
Amplifier in the ATtiny1627 and the tinyAVR
® 2 Family can be used to design a low-power, cost-efficient, and small
footprint PIR motion detection solution.
Compared to the original Click board configuration, this solution will offer a significant reduction in BOM cost and
board space, saving two op amps, six resistors, and four capacitors in the signal chain. Other significant benefits
compared to the original solutions is that the sensitivity, detection threshold, filtering, and signal gain can be adjusted
in firmware and not in hardware, giving the developer the possibility to create the code in a way that it can adapt and
change based on the needs of the application. Having access to the PIR signal gives the developer the possibility to
implement smart algorithms in firmware to enable product differentiation.
The total power consumption of the system in the selected configuration is about 13.7 µA at ambient temperature.
The Click board contributes about 11 µA to the total current consumption with the original bias configuration.
Increasing the bias resistor R6 will reduce the Click board power consumption, at the expense of reduced output
signal from the sensor.
The power consumption is application dependent and will vary based on the configuration of the PIR sensor, sample
acquisition, and filtering parameters, which will also affect the detection range and/or sensitivity. Consider adjusting
these parameters to further reduce the power consumption in times were the demands of the application are lower.
In the table below, there are some examples of settings and the respective MCU power consumption.
Table 5-1. MCU Average Power Consumption
Setting MCU Power Consumption
PIR_SAMPLE_RATE_PER_SECOND = 4 Hz
PIR_OVERSAMPLE_RATE = 2
1.5 µA
PIR_SAMPLE_RATE_PER_SECOND = 8 Hz
PIR_OVERSAMPLE_RATE = 2
2.1 µA
PIR_SAMPLE_RATE_PER_SECOND = 16 Hz
PIR_OVERSAMPLE_RATE = 2
3.5 µA
PIR_SAMPLE_RATE_PER_SECOND = 4 Hz
PIR_OVERSAMPLE_RATE = 16
2.7 µA
PIR_SAMPLE_RATE_PER_SECOND = 8 Hz
PIR_OVERSAMPLE_RATE = 16
4.6 µA
PIR_SAMPLE_RATE_PER_SECOND = 16 Hz
PIR_OVERSAMPLE_RATE = 16
8.6 µA
PIR_SAMPLE_RATE_PER_SECOND = 4 Hz
PIR_OVERSAMPLE_RATE = 64
6.1 µA
PIR_SAMPLE_RATE_PER_SECOND = 8 Hz
PIR_OVERSAMPLE_RATE = 64
11.4 µA
PIR_SAMPLE_RATE_PER_SECOND = 16 Hz
PIR_OVERSAMPLE_RATE = 64
21.7 µA
AN3641
Summary
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 22
6. Plotting Graph in Data Visualizer
The following instructions show how to plot USART data in Data Visualizer by using the Data Stream protocol.
Note:  Data Visualizer, For detailed information on refer to the Data Visualizer User's Guide.
1. Open .Data Visualizer
2. Open in .Configuration > External Connection > Serial Port Data Visualizer
3. Select the Curiosity Virtual COM port, Baud rate: 115200, and then select . Connect
4. Open .Configuration > Protocols > Data Streamer
5. In the , under , browse to the configuration file and then selectData Stream Control Panel Configuration
Load.
Note:  In this case, the configuration file is and can be found in thetiny2_PIR_datastreamer.cfg
example source code project folder.
Note:  Data Stream Protocol For more details on the , refer to Data Visualizer User's Guide, Data Stream
Protocol section.
1. Open Configuration > Visualization > Graph.
2. Drag the connections, as shown with red arrows in the figure below, to plot the graph.
Figure 6-1. Data Stream Graph in Data Visualizer
To adjust the Y-axis in the graph, follow the steps below:
1. Under in , deselect .Configuration Graph Automatically Fit Y
2. Click somewhere inside the plot area.
3. Scroll the mouse-wheel while pressing or holding the key.Ctrl
To adjust the X-axis in the graph, follow the steps below:
1. Click somewhere inside the plot area.
2. Scroll the mouse-wheel while pressing or holding the key.Shift
Note:  For more details on Data Visualizer > Graph, refer to the Data Visualizer User's Guide, Graph section.
AN3641
Plotting Graph in Data Visualizer
© 2020 Microchip Technology Inc. Application Note DS00003641A-page 23


Product specificaties

Merk: Microchip
Categorie: Niet gecategoriseerd
Model: ATTINY824

Heb je hulp nodig?

Als je hulp nodig hebt met Microchip ATTINY824 stel dan hieronder een vraag en andere gebruikers zullen je antwoorden




Handleiding Niet gecategoriseerd Microchip

Handleiding Niet gecategoriseerd

Nieuwste handleidingen voor Niet gecategoriseerd