ATtiny單片機(jī)電子蠟燭,ATtiny candle
關(guān)鍵字:ATTINY85,電子蠟燭電路
想想當(dāng)你好不容易跟女朋友共度燭光晚餐,卻因?yàn)橄灎T點(diǎn)沒(méi)了或打翻著火了,那是一件多么坑爹的事啊!今天為你分享一款自己diy的超自然的燭光蠟燭。
ATtiny 電子蠟燭,皮特?米爾斯開(kāi)發(fā)這個(gè)偉大的蠟燭,正如我們圖片所見(jiàn)到的一樣,但怎樣讓這蠟燭的光芒像傳統(tǒng)的蠟燭一樣閃爍呢。
皮特使用一個(gè)高亮的LED和一些模擬的輔助軟件,這樣就使得ATtiny 電子蠟燭的燭光和傳統(tǒng)蠟燭擁有一樣的閃爍的燭光,并且優(yōu)于傳統(tǒng)蠟燭,因?yàn)樗话橛忻骰鸬奈kU(xiǎn)。
ATtiny 電子蠟燭最難的部分就閃爍神態(tài)逼真,所以皮特做了一個(gè)蠟燭光檢測(cè)電阻( LDR )和固定電阻作為一個(gè)分壓器。這是作為ATTINY85 ADC之中的一個(gè)輸入端,并離散時(shí)間間隔的進(jìn)行采樣。采樣速率為100毫秒。然后將采集的8bit的電頻值存儲(chǔ)到EEPROM中,以便記錄蠟燭的閃爍圖譜,驅(qū)動(dòng)將其連接的LED、PWM形成通路。在用三節(jié)干電池供電。最后您只需編程程序,然后通過(guò)開(kāi)關(guān)進(jìn)行控制。
下面是ATtiny 電子蠟燭的電路圖
下面是程序的代碼以及寫(xiě)入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)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。
舉報(bào)投訴
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
熱點(diǎn)推薦
基于單片機(jī)的額溫槍設(shè)計(jì)
電子發(fā)燒友網(wǎng)站提供《基于單片機(jī)的額溫槍設(shè)計(jì).docx》資料免費(fèi)下載
發(fā)表于 03-01 15:36
?0次下載
ATtiny13/ATtiny13V:低功耗8位微控制器的強(qiáng)大之選
ATtiny13/ATtiny13V:低功耗8位微控制器的強(qiáng)大之選 在電子設(shè)計(jì)領(lǐng)域,一款性能卓越且低功耗的微控制器往往是工程師們的“心頭好”。今天,我們就來(lái)深入了解一下 Atmel 公司推出
ATtiny28系列微控制器:低功耗與高性能的完美結(jié)合
ATtiny28系列微控制器:低功耗與高性能的完美結(jié)合 引言 在電子設(shè)計(jì)領(lǐng)域,對(duì)于微控制器的需求往往是在低功耗和高性能之間尋求平衡。ATtiny28系列微控制器基于AVR RISC架構(gòu),為我們提供了
ATtiny24A/44A/84A:高性能低功耗8位微控制器的全方位解析
ATtiny24A/44A/84A:高性能低功耗8位微控制器的全方位解析 在電子設(shè)計(jì)的廣闊領(lǐng)域中,微控制器(MCU)宛如一顆璀璨的明星,憑借其強(qiáng)大的功能和靈活的應(yīng)用,在各種電子設(shè)備里扮演著至關(guān)重要
探索ATtiny441/841:高性能低功耗8位AVR微控制器的卓越之選
探索ATtiny441/841:高性能低功耗8位AVR微控制器的卓越之選 在電子設(shè)計(jì)領(lǐng)域,選擇一款合適的微控制器至關(guān)重要。Atmel公司的ATtiny441/841 8位AVR微控制器憑借其高性能
請(qǐng)問(wèn)什么是單片機(jī)的靜態(tài)電流和待機(jī)電流?
請(qǐng)問(wèn)下什么是單片機(jī)的靜態(tài)電流和待機(jī)電流?
發(fā)表于 12-22 11:07
浮思特 | 電子溫度計(jì)單片機(jī)如何選型?ABOV單片機(jī)提供幾點(diǎn)很關(guān)鍵!
一個(gè)優(yōu)秀的電子溫度計(jì),其核心往往在于內(nèi)部單片機(jī)(MCU)的性能與匹配度。如何選擇一款合適的單片機(jī),實(shí)現(xiàn)精準(zhǔn)、穩(wěn)定、低功耗的溫度測(cè)量與交互,是產(chǎn)品開(kāi)發(fā)中的重要課題。本文將從技術(shù)維度,為大家梳理選型
廣州唯創(chuàng)電子單片機(jī)語(yǔ)音芯片:智能設(shè)備的聲音靈魂與技術(shù)核心
。單片機(jī)(MicrocontrollerUnit,MCU)是集成處理器、存儲(chǔ)器和多種外圍接口的微型計(jì)算機(jī)系統(tǒng)。它以其高度集成、低功耗、強(qiáng)控制能力等特點(diǎn),成為現(xiàn)代電子
單片機(jī)電路設(shè)計(jì)必讀:電容選用的五大關(guān)鍵原則
表現(xiàn)。電容在單片機(jī)電路中的核心作用單片機(jī)的穩(wěn)定運(yùn)行離不開(kāi)電容的保駕護(hù)航。去耦電容用于消除電源噪聲,耦合電容負(fù)責(zé)信號(hào)傳輸,起振電容確保時(shí)鐘精準(zhǔn),復(fù)位電容保障系統(tǒng)啟動(dòng)可靠。這些看似簡(jiǎn)單的
ATtiny3224/3226/3227:高性能低功耗的tinyAVR? 2系列微控制器
Microchip Technology ATtiny3224、ATtiny3226與ATtiny3227 8位微控制器 (MCU) 采用帶硬件乘法器的AVR? CPU,運(yùn)行速率高達(dá)20MHz,具有
從蠟燭燈到數(shù)顯表!EN8F1801單片機(jī)適配多場(chǎng)景超給力!#8位單片機(jī)#8位MCU#蠟燭燈芯片#數(shù)顯表芯片
單片機(jī)
英銳恩科技
發(fā)布于 :2025年08月02日 11:14:48
怎么測(cè)單片機(jī)系統(tǒng)頻率
單片機(jī)系統(tǒng)頻率是指單片機(jī)工作時(shí)的時(shí)鐘頻率,它直接影響單片機(jī)的運(yùn)行速度和處理能力,準(zhǔn)確測(cè)量系統(tǒng)頻率對(duì)單片機(jī)應(yīng)用開(kāi)發(fā)、程序調(diào)試和性能優(yōu)化具有重要意義。測(cè)量
單片機(jī)科普總結(jié),建議收藏!
單片機(jī)(MicrocontrollerUnit,MCU)作為嵌入式系統(tǒng)的核心之一,在現(xiàn)代電子產(chǎn)品中無(wú)處不在。從智能家居、汽車(chē)電子,到工業(yè)控制、醫(yī)療設(shè)備,單片機(jī)支撐著無(wú)數(shù)智能化應(yīng)用的發(fā)展
stm32L0單片機(jī)電源管腳對(duì)地電阻異常是什么原因?qū)е碌模?/a>
部分stm32L0單片機(jī)電源管腳對(duì)地電阻異常,有的200歐姆左右,有的500歐姆左右。導(dǎo)致功耗變大,什么原因會(huì)導(dǎo)致電源管腳對(duì)地電阻變低異常。
發(fā)表于 03-07 07:19
ATtiny單片機(jī)電子蠟燭,ATtiny candle
評(píng)論