在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

ATtiny單片機電子蠟燭,ATtiny candle

454398 ? 2018-09-20 19:47 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

ATtiny單片機電子蠟燭,ATtiny candle

關鍵字:ATTINY85,電子蠟燭電路

想想當你好不容易跟女朋友共度燭光晚餐,卻因為蠟燭點沒了或打翻著火了,那是一件多么坑爹的事啊!今天為你分享一款自己diy的超自然的燭光蠟燭。
ATtiny 電子蠟燭,皮特?米爾斯開發這個偉大的蠟燭,正如我們圖片所見到的一樣,但怎樣讓這蠟燭的光芒像傳統的蠟燭一樣閃爍呢。
皮特使用一個高亮的LED和一些模擬的輔助軟件,這樣就使得ATtiny 電子蠟燭的燭光和傳統蠟燭擁有一樣的閃爍的燭光,并且優于傳統蠟燭,因為它不伴有明火的危險。
ATtiny 電子蠟燭最難的部分就閃爍神態逼真,所以皮特做了一個蠟燭光檢測電阻( LDR )和固定電阻作為一個分壓器。這是作為ATTINY85 ADC之中的一個輸入端,并離散時間間隔的進行采樣。采樣速率為100毫秒。然后將采集的8bit的電頻值存儲到EEPROM中,以便記錄蠟燭的閃爍圖譜,驅動將其連接的LED、PWM形成通路。在用三節干電池供電。最后您只需編程程序,然后通過開關進行控制。
下面是ATtiny 電子蠟燭的電路圖
下面是程序的代碼以及寫入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 );  
 }  
   
}    
 }  
   
}    
 }  
   
}    
 }  
   
}    }  
   
}    }  
   
}    }  
   
}       
EEPROM的數據
rom.rar
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    STC單片機范例程序

    電子發燒友網站提供《STC單片機范例程序.zip》資料免費下載
    發表于 06-04 16:27 ?0次下載

    單片機科普總結,建議收藏!

    單片機(MicrocontrollerUnit,MCU)作為嵌入式系統的核心之一,在現代電子產品中無處不在。從智能家居、汽車電子,到工業控制、醫療設備,單片機支撐著無數智能化應用的發展
    的頭像 發表于 04-07 11:58 ?1604次閱讀
    <b class='flag-5'>單片機</b>科普總結,建議收藏!

    STM32單片機電機控制的仿真包

    免費分享,限時零積分可下載STM32單片機電機控制的仿真包
    發表于 03-10 17:15

    stm32L0單片機電源管腳對地電阻異常是什么原因導致的?

    部分stm32L0單片機電源管腳對地電阻異常,有的200歐姆左右,有的500歐姆左右。導致功耗變大,什么原因會導致電源管腳對地電阻變低異常。
    發表于 03-07 07:19

    單片機電子技術中的應用及發展

    單片機作為一種高度集成的微控制器,在電子技術領域有著廣泛的應用。本文首先介紹了單片機在多個領域的具體應用,包括自動化儀器儀表、家用電器、醫用設備、通信設備、汽車電子控制與檢測以及模塊化
    的頭像 發表于 01-15 10:30 ?697次閱讀

    如何優化單片機項目的功耗

    在現代電子設計中,功耗優化已成為一個不可忽視的重要議題。對于單片機(MCU)項目而言,功耗不僅關系到產品的能效比,還直接影響到電池壽命和熱管理。 硬件層面的功耗優化 1. 選擇合適的單片機 選擇一個
    的頭像 發表于 11-01 14:16 ?1184次閱讀

    單片機編程語言有哪些選擇

    單片機(Microcontroller Unit,MCU)編程是指為單片機編寫程序的過程,這些程序控制單片機的行為和功能。單片機廣泛應用于嵌入式系統,如家用電器、汽車
    的頭像 發表于 11-01 14:13 ?2285次閱讀

    單片機的中斷機制

    單片機的中斷機制是一種重要的處理方式,它允許單片機在執行主程序的過程中,能夠暫停當前任務,轉而處理外部或內部緊急事件。這種機制極大地提高了系統的響應速度和處理能力,使得單片機在各種應用領域中得到廣泛應用。以下是對
    的頭像 發表于 10-17 18:03 ?1883次閱讀

    單片機電路圖用什么軟件畫

    單片機電路圖的設計和繪制是一個復雜的過程,涉及到電路設計、電子元件的選擇、電路板布局、信號完整性分析等多個方面。 1. 選擇合適的軟件 繪制單片機電路圖,你可以選擇多種軟件,每種軟件都有其特點和優勢
    的頭像 發表于 10-17 09:43 ?3294次閱讀

    電子產品方案開發公司常用的15個單片機經典電路分享!

    : 13、復位電路: 14、AD/DA/光敏/熱敏: 15、CPU最小系統工作電路: 單片機作為電子產品開發中的核心控制單元,其經典電路在各個領域發揮著重要作用。隨著科技的不斷發展,單片機電
    發表于 09-25 14:43

    單片機方案開發:如何給電子產品單片機選型?

    在給電子項目做IC方案開發時,總是需要考慮到許多關鍵因素。在本文中,對于給即將開發的電子產品做好單片機選型。英銳恩單片機開發工程師將從以下幾點進行介紹。 一、選擇制造商: 許多生產
    發表于 09-25 10:56

    51單片機驅動

    電子發燒友網站提供《51單片機驅動.exe》資料免費下載
    發表于 09-20 11:46 ?5次下載

    51單片機晶振的問題總結

    電子發燒友網站提供《51單片機晶振的問題總結.doc》資料免費下載
    發表于 09-07 09:32 ?0次下載

    單片機燒錄程序的基本步驟是什么

    單片機燒錄程序是單片機開發過程中非常重要的一步,它涉及到將編寫好的程序代碼通過一定的方式傳輸到單片機內部的存儲器中,使單片機能夠按照預定的邏輯執行任務。 一、硬件準備
    的頭像 發表于 09-02 09:47 ?2595次閱讀

    一文讀懂什么單片機:組成結構與應用

    需要了解什么是單片機單片機與微處理器類似,但它在同一芯片上集成了一些額外的組件。什么是單片機單片機是一種超大規模集成電路(VLSI),它包含電子
    的頭像 發表于 08-09 11:49 ?2336次閱讀
    一文讀懂什么<b class='flag-5'>單片機</b>:組成結構與應用
    主站蜘蛛池模板: 久久婷婷国产精品香蕉 | 日日操夜夜操天天操 | 四虎影院免费在线播放 | 亚洲欧美日韩在线观看你懂的 | 国产精品爽爽影院在线 | 午夜毛片不卡高清免费 | 欧洲人体超大胆露私视频 | 天堂网www在线资源中文 | 日韩毛片免费视频一级特黄 | 色婷婷激婷婷深爱五月老司机 | 天堂在线网站 | 国产农村一一级特黄毛片 | 91大神大战高跟丝袜美女 | 爱爱帝国亚洲一区二区三区 | 香蕉视频网站在线播放 | 最近国语剧情视频在线观看 | 天天操天天爱天天干 | 色婷婷综合在线视频最新 | a级毛片免费网站 | 最新国产在线播放 | 天天色天天射综合网 | 欧美一级日韩在线观看 | 手机在线精品视频 | 欧美日本一区二区三区生 | 午夜精品久视频在线观看 | 中国性猛交xxxxx免费看 | 日本xxxxxxxxx18| 三级黄网站 | 色5月婷婷| 天天看片夜夜爽 | 亚洲va欧美va国产综合久久 | 老头天天吃我奶躁我的动图 | 亚洲狠狠网站色噜噜 | 永久精品免费影院在线观看网站 | 亚洲午夜久久久精品影院视色 | 天天拍天天干天天操 | 丁香激情综合 | 免费观看黄色在线视频 | 激情五月激情综合网 | 国产女人和拘做受视频免费 | 99草在线观看 |