当前位置:首页 > 芯闻号 > 电机控制电路
[导读]ATtiny 电子蜡烛最难的部分就闪烁神态逼真,所以皮特做了一个蜡烛光检测电阻( LDR )和固定电阻作为一个分压器。这是作为ATTINY85 ADC之中的一个输入端,并离散时间间隔的进行采样。采样速率为100毫秒。然后将采集的8bit的电频值存储到EEPROM中,以便记录蜡烛的闪烁图谱,驱动将其连接的LED、PWM形成通路。在用三节干电池供电。最后您只需编程程序,然后通过开关进行控制。

想想当你好不容易跟女朋友共度烛光晚餐,却因为蜡烛点没了或打翻着火了,那是一件多么坑爹的事啊!今天为你分享一款自己diy的超自然的烛光蜡烛。

 

WP_000356.jpg

ATtiny 电子蜡烛,皮特•米尔斯开发这个伟大的蜡烛,正如我们图片所见到的一样,但怎样让这蜡烛的光芒像传统的蜡烛一样闪烁呢。

 

WP_000370.jpg

皮特使用一个高亮的LED和一些模拟的辅助软件,这样就使得ATtiny 电子蜡烛的烛光和传统蜡烛拥有一样的闪烁的烛光,并且优于传统蜡烛,因为它不伴有明火的危险。

 

WP_000376.jpg

ATtiny 电子蜡烛最难的部分就闪烁神态逼真,所以皮特做了一个蜡烛光检测电阻( LDR )和固定电阻作为一个分压器。这是作为ATTINY85 ADC之中的一个输入端,并离散时间间隔的进行采样。采样速率为100毫秒。然后将采集的8bit的电频值存储到EEPROM中,以便记录蜡烛的闪烁图谱,驱动将其连接的LED、PWM形成通路。在用三节干电池供电。最后您只需编程程序,然后通过开关进行控制。

 

WP_000345.jpg

下面是ATtiny 电子蜡烛的电路图

 

ATTiny Candle Sch.jpg

下面是程序的代码以及写入EEPROM的数据

view plainprint?

/*

Program Description: This program reads a light detecting resistor thru an internal ADC and stores the value,

after scaling it, to eeprom. This ADC value is sent to a PWM channel with attached led. This is essentially a data logger

for light and replay by LED. If, if you aim the LDR at a flickering candle during its recording phase, you have a flickering

led candle.

A circuit description and other details can be found at http://petemills.blogspot.com

Filename: ATTiny_Candle_v1.0.c

Author: Pete Mills

Int. RC Osc. 8 MHz; Start-up time PWRDWN/RESET: 6 CK/14 CK + 64 ms

*/

//********** Includes **********

#include

#include

#include

//********** Definitions **********

// LED for flame simulation

#define LED PB0

#define LED_PORT PORTB

#define LED_DDR DDRB

// Light Detecting Resistor for recording a live flame

#define LDR PINB3

#define LDR_PORT PINB

#define LDR_DDR DDRB

// Tactile Switch Input

#define SW1 PINB4

#define SW1_PORT PINB

#define SW1_DDR DDRB

#define ARRAY_SIZE 500 // size of the flicker array

#define SAMPLE_RATE 100 // ms delay for collecting and reproducing the flicker

//********** Function Prototypes **********

void setup(void);

void toggle_led(void);

void program_flicker(void);

void led_alert(void);

void eeprom_save_array(void);

void eeprom_read_array(void);

void scale_array(void);

uint8_t get_adc(void);

uint8_t scale( uint8_t input, uint8_t inp_low, uint8_t inp_hi, uint8_t outp_low, uint8_t outp_hi);

uint8_t is_input_low(char port, char channel, uint8_t debounce_time, int input_block);

//********** Global Variables **********

uint8_t flicker_array[ ARRAY_SIZE ] = { 0 };

uint8_t EEMEM ee_flicker_array[ ARRAY_SIZE ] = { 0 };

int main(void)

{

uint16_t replay = 0;

setup();

eeprom_read_array();

while(1)

{

if( is_input_low( SW1_PORT, SW1, 25, 250 ) )

{

// program the flicker

// after entering and upon completion, a predetermined flash pattern will occur as described in led_alert()

// aim the ldr at a flickering candle or any other light source ( like a laser ) you want to record during this time

// and upon completion the values are stored to eeprom. They are played back immediately as well

// as being recalled from eeprom upon first start up

led_alert();

program_flicker();

scale_array();

eeprom_save_array();

led_alert();

}

// replay the recorded flicker pattern

OCR0A = flicker_array[ replay ];

++replay;

if( replay >= ( ARRAY_SIZE - 13 ) ) // if the end of the stored array has been reached

{

replay = 0; // start again from the beginning

//led_alert();

}

_delay_ms( SAMPLE_RATE );

_delay_ms( 3 ); // ADC Conversion time

}

}

//********** Functions **********

void setup(void)

{

//********* Port Config *********

LED_DDR |= ( 1 << LED); // set PB0 to "1" for output

LED_PORT &= ~( 1 << LED ); // turn the led off

LDR_DDR &= ~( 1 << LDR ); // set LDR pin to 0 for input

LDR_PORT |= ( 1 << LDR ); // write 1 to enable internal pullup

SW1_DDR &= ~( 1 << SW1 ); // set sw1 pin to 0 for input

SW1_PORT |= ( 1 << SW1 ); // write a 1 to sw1 to enable the internal pullup

//********** PWM Config *********

TCCR0A |= ( ( 1 << COM0A1 ) | ( 1 << WGM01 ) | ( 1 << WGM00 ) ); // non inverting fast pwm

TCCR0B |= ( 1 << CS00 ); // start the timer

//********** ADC Config **********

ADMUX |= ( ( 1 << ADLAR ) | ( 1 << MUX1 ) | ( 1 << MUX0 ) ); // left adjust and select ADC3

ADCSRA |= ( ( 1 << ADEN ) | ( 1 << ADPS2 ) | ( 1 << ADPS1 ) ); // ADC enable and clock divide 8MHz by 64 for 125khz sample rate

DIDR0 |= ( 1 << ADC3D ); // disable digital input on analog input channel to conserve power

}

void toggle_led()

{

LED_PORT ^= ( 1 << LED );

}

uint8_t is_input_low( char port, char channel, uint8_t debounce_time, int input_block )

{

/*

This function is for debouncing a switch input

Debounce time is a blocking interval to wait until the input is tested again.

If the input tests low again, a delay equal to input_block is executed and the function returns ( 1 )

*/

if ( bit_is_clear( port, channel ) )

{

_delay_ms( debounce_time );

if ( bit_is_clear( port, channel ) )

{

_delay_ms( input_block );

return 1;

}

}

return 0;

}

uint8_t get_adc()

{

ADCSRA |= ( 1 << ADSC ); // start the ADC Conversion

while( ADCSRA & ( 1 << ADSC )); // wait for the conversion to be complete

return ~ADCH; // return the inverted 8-bit left adjusted adc val

}

void program_flicker()

{

// build the flicker array

for( int i = 0; i < ARRAY_SIZE; i++ )

{

flicker_array[ i ] = get_adc();

_delay_ms( SAMPLE_RATE );

}

}

void led_alert()

{

// this is a function to create a visual alert that an event has occured within the program

// it toggles the led 10 times.

for( int i = 0; i < 10; i++ )

{

OCR0A = 0;

_delay_ms( 40 );

OCR0A = 255;

_delay_ms( 40 );

}

}

void eeprom_save_array()

{

for( int i = 0; i < ARRAY_SIZE; i++ )

{

eeprom_write_byte( &ee_flicker_array[ i ], flicker_array[ i ] );

}

}

void eeprom_read_array()

{

for( int i = 0; i < ARRAY_SIZE; i++ )

{

flicker_array[ i ] = eeprom_read_byte( &ee_flicker_array[ i ] );

}

}

uint8_t scale( uint8_t input, uint8_t inp_low, uint8_t inp_hi, uint8_t outp_low, uint8_t outp_hi)

{

return ( ( ( input - inp_low ) * ( outp_hi - outp_low ) ) / ( ( inp_hi - inp_low ) + outp_low ) );

}

void scale_array()

{

uint8_t arr_min = 255;

uint8_t arr_max = 0;

uint8_t out_low = 20;

uint8_t out_high = 255;

// find the min and max values

for( int i = 0; i < ARRAY_SIZE; i++ )

{

if( flicker_array[ i ] < arr_min )

arr_min = flicker_array[ i ];

if( flicker_array[ i ] > arr_max )

arr_max = flicker_array[ i ];

}

// now that we know the range, scale it

for( int i = 0; i < ARRAY_SIZE; i++ )

{

flicker_array[ i ] = scale( flicker_array[ i ], arr_min, arr_max, out_low, out_high );

}

} igh );

}

} igh );

}

}

}

}

}

}

}

} }

} }

} }

}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

在现代电子技术的快速发展中,单片机以其高度的集成性、稳定性和可靠性,在工业自动化、智能家居、医疗设备、航空航天等诸多领域得到了广泛应用。S32单片机,作为其中的佼佼者,其引脚功能丰富多样,是实现与外部设备通信、控制、数据...

关键字: s32单片机引脚 单片机

在微控制器领域,MSP430与STM32无疑是两颗璀璨的明星。它们各自凭借其独特的技术特点和广泛的应用领域,在市场上占据了重要的位置。本文将深入解析MSP430与STM32之间的区别,探讨它们在不同应用场景下的优势和局限...

关键字: MSP430 STM32 单片机

该系列产品有助于嵌入式设计人员在更广泛的系统中轻松实现USB功能

关键字: 单片机 嵌入式设计 USB

单片机编程语言是程序员与微控制器进行交流的桥梁,它们构成了单片机系统的软件开发基石,决定着如何有效、高效地控制和管理单片机的各项资源。随着微控制器技术的不断发展,针对不同应用场景的需求,形成了丰富多样的编程语言体系。本文...

关键字: 单片机 微控制器

单片机,全称为“单片微型计算机”或“微控制器”(Microcontroller Unit,简称MCU),是一种高度集成化的电子器件,它是现代科技领域的关键组件,尤其在自动化控制、物联网、消费电子、汽车电子、工业控制等领域...

关键字: 单片机 MCU

STM32是由意法半导体公司(STMicroelectronics)推出的基于ARM Cortex-M内核的32位微控制器系列,以其高性能、低功耗、丰富的外设接口和强大的生态系统深受广大嵌入式开发者喜爱。本文将详细介绍S...

关键字: STM32 单片机

在当前的科技浪潮中,单片机作为嵌入式系统的重要组成部分,正以其强大的功能和广泛的应用领域受到越来越多行业的青睐。在众多单片机中,W79E2051以其卓越的性能和稳定的工作特性,成为市场上的明星产品。本文将深入探讨W79E...

关键字: 单片机 w79e2051单片机

单片机,又称为微控制器或微处理器,是现代电子设备中的核心部件之一。它集成了中央处理器、存储器、输入输出接口等电路,通过外部信号引脚与外部设备进行通信,实现对设备的控制和管理。本文将详细介绍单片机的外部信号引脚名称及其功能...

关键字: 单片机 微控制器 中央处理器

随着科技的飞速发展,单片机和嵌入式系统在现代电子设备中的应用越来越广泛。它们不仅提高了设备的智能化水平,还推动了各行各业的创新与发展。在单片机和嵌入式系统的开发中,编程语言的选择至关重要。本文将深入探讨单片机和嵌入式系统...

关键字: 单片机 嵌入式系统 电子设备

PLC(可编程逻辑控制器)和单片机是两种不同的控制设备,它们之间存在明显的区别:

关键字: 单片机 plc 控制器
关闭
关闭