ATtiny單片機(jī)電子蠟燭,ATtiny candle
關(guān)鍵字:ATTINY85,電子蠟燭電路
想想當(dāng)你好不容易跟女朋友共度燭光晚餐,卻因?yàn)橄灎T點(diǎn)沒了或打翻著火了,那是一件多么坑爹的事啊!今天為你分享一款自己diy的超自然的燭光蠟燭。
ATtiny 電子蠟燭,皮特?米爾斯開發(fā)這個(gè)偉大的蠟燭,正如我們圖片所見到的一樣,但怎樣讓這蠟燭的光芒像傳統(tǒng)的蠟燭一樣閃爍呢。
皮特使用一個(gè)高亮的LED和一些模擬的輔助軟件,這樣就使得ATtiny 電子蠟燭的燭光和傳統(tǒng)蠟燭擁有一樣的閃爍的燭光,并且優(yōu)于傳統(tǒng)蠟燭,因?yàn)樗话橛忻骰鸬奈kU(xiǎn)。
ATtiny 電子蠟燭最難的部分就閃爍神態(tài)逼真,所以皮特做了一個(gè)蠟燭光檢測電阻( LDR )和固定電阻作為一個(gè)分壓器。這是作為ATTINY85 ADC之中的一個(gè)輸入端,并離散時(shí)間間隔的進(jìn)行采樣。采樣速率為100毫秒。然后將采集的8bit的電頻值存儲到EEPROM中,以便記錄蠟燭的閃爍圖譜,驅(qū)動將其連接的LED、PWM形成通路。在用三節(jié)干電池供電。最后您只需編程程序,然后通過開關(guān)進(jìn)行控制。
下面是ATtiny 電子蠟燭的電路圖
下面是程序的代碼以及寫入EEPROM的數(shù)據(jù)
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 ); } } } } } } } } } } } } } }
EEPROM的數(shù)據(jù)
rom.rar
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報(bào)投訴
發(fā)布評論請先 登錄
相關(guān)推薦
熱點(diǎn)推薦
單片機(jī)科普總結(jié),建議收藏!
單片機(jī)(MicrocontrollerUnit,MCU)作為嵌入式系統(tǒng)的核心之一,在現(xiàn)代電子產(chǎn)品中無處不在。從智能家居、汽車電子,到工業(yè)控制、醫(yī)療設(shè)備,單片機(jī)支撐著無數(shù)智能化應(yīng)用的發(fā)展

stm32L0單片機(jī)電源管腳對地電阻異常是什么原因?qū)е碌模?/a>
部分stm32L0單片機(jī)電源管腳對地電阻異常,有的200歐姆左右,有的500歐姆左右。導(dǎo)致功耗變大,什么原因會導(dǎo)致電源管腳對地電阻變低異常。
發(fā)表于 03-07 07:19
單片機(jī)中斷技術(shù)詳解
在現(xiàn)代電子設(shè)備中,單片機(jī)作為控制核心發(fā)揮著舉足輕重的作用。而在其高效運(yùn)作的背后,中斷機(jī)制是推動單片機(jī)實(shí)現(xiàn)實(shí)時(shí)響應(yīng)與高效執(zhí)行的關(guān)鍵因素。本文將深入探討單片機(jī)中的中斷概念、中斷系統(tǒng)的結(jié)構(gòu)、
單片機(jī)在電子技術(shù)中的應(yīng)用及發(fā)展
單片機(jī)作為一種高度集成的微控制器,在電子技術(shù)領(lǐng)域有著廣泛的應(yīng)用。本文首先介紹了單片機(jī)在多個(gè)領(lǐng)域的具體應(yīng)用,包括自動化儀器儀表、家用電器、醫(yī)用設(shè)備、通信設(shè)備、汽車電子控制與檢測以及模塊化
如何優(yōu)化單片機(jī)項(xiàng)目的功耗
在現(xiàn)代電子設(shè)計(jì)中,功耗優(yōu)化已成為一個(gè)不可忽視的重要議題。對于單片機(jī)(MCU)項(xiàng)目而言,功耗不僅關(guān)系到產(chǎn)品的能效比,還直接影響到電池壽命和熱管理。 硬件層面的功耗優(yōu)化 1. 選擇合適的單片機(jī) 選擇一個(gè)
單片機(jī)編程語言有哪些選擇
單片機(jī)(Microcontroller Unit,MCU)編程是指為單片機(jī)編寫程序的過程,這些程序控制單片機(jī)的行為和功能。單片機(jī)廣泛應(yīng)用于嵌入式系統(tǒng),如家用電器、汽車
單片機(jī)電路圖用什么軟件畫
單片機(jī)電路圖的設(shè)計(jì)和繪制是一個(gè)復(fù)雜的過程,涉及到電路設(shè)計(jì)、電子元件的選擇、電路板布局、信號完整性分析等多個(gè)方面。 1. 選擇合適的軟件 繪制單片機(jī)電路圖,你可以選擇多種軟件,每種軟件都有其特點(diǎn)和優(yōu)勢
電子產(chǎn)品方案開發(fā)公司常用的15個(gè)單片機(jī)經(jīng)典電路分享!
:
13、復(fù)位電路:
14、AD/DA/光敏/熱敏:
15、CPU最小系統(tǒng)工作電路:
單片機(jī)作為電子產(chǎn)品開發(fā)中的核心控制單元,其經(jīng)典電路在各個(gè)領(lǐng)域發(fā)揮著重要作用。隨著科技的不斷發(fā)展,單片機(jī)電
發(fā)表于 09-25 14:43
單片機(jī)方案開發(fā):如何給電子產(chǎn)品單片機(jī)選型?
在給電子項(xiàng)目做IC方案開發(fā)時(shí),總是需要考慮到許多關(guān)鍵因素。在本文中,對于給即將開發(fā)的電子產(chǎn)品做好單片機(jī)選型。英銳恩單片機(jī)開發(fā)工程師將從以下幾點(diǎn)進(jìn)行介紹。
一、選擇制造商:
許多生產(chǎn)
發(fā)表于 09-25 10:56
單片機(jī)燒錄程序的基本步驟是什么
單片機(jī)燒錄程序是單片機(jī)開發(fā)過程中非常重要的一步,它涉及到將編寫好的程序代碼通過一定的方式傳輸?shù)?b class='flag-5'>單片機(jī)內(nèi)部的存儲器中,使單片機(jī)能夠按照預(yù)定的邏輯執(zhí)行任務(wù)。 一、硬件準(zhǔn)備
一文讀懂什么單片機(jī):組成結(jié)構(gòu)與應(yīng)用
需要了解什么是單片機(jī)。單片機(jī)與微處理器類似,但它在同一芯片上集成了一些額外的組件。什么是單片機(jī)?單片機(jī)是一種超大規(guī)模集成電路(VLSI),它包含電子

基于51單片機(jī)電子稱電路圖及程序
本資源內(nèi)容概要:? ? ? ?這是基于51單片機(jī)電子稱電路圖及程序設(shè)計(jì)包含了電路圖源文件(Altiumdesigner軟件打開)、C語言程序源代碼(keil軟件打開)。本資源適合人群
發(fā)表于 06-21 14:33
?0次下載
基于51單片機(jī)矩陣鍵盤音樂電子琴電路圖proteus仿真及程序
本資源內(nèi)容概要:? ? ? ?這是基于51單片機(jī)矩陣鍵盤音樂電子琴電路圖proteus仿真及程序設(shè)計(jì)包含了電路圖源文件(Altiumdesigner軟件打開)、C語言程序源代碼(keil軟件打開
發(fā)表于 06-21 14:32
?4次下載
評論