ATtniy单片机电子蜡烛.docx
- 文档编号:9803731
- 上传时间:2023-05-21
- 格式:DOCX
- 页数:20
- 大小:369.44KB
ATtniy单片机电子蜡烛.docx
《ATtniy单片机电子蜡烛.docx》由会员分享,可在线阅读,更多相关《ATtniy单片机电子蜡烛.docx(20页珍藏版)》请在冰点文库上搜索。
ATtniy单片机电子蜡烛
ATtiny单片机电子蜡烛,ATtinycandle
关键字:
ATTINY85,电子蜡烛电路
想想当你好不容易跟女朋友共度烛光晚餐,却因为蜡烛点没了或打翻着火了,那是一件多么坑爹的事啊!
今天为你分享一款自己diy的超自然的烛光蜡烛。
ATtiny电子蜡烛,皮特•米尔斯开发这个伟大的蜡烛,正如我们图片所见到的一样,但怎样让这蜡烛的光芒像传统的蜡烛一样闪烁呢。
皮特使用一个高亮的LED和一些模拟的辅助软件,这样就使得ATtiny电子蜡烛的烛光和传统蜡烛拥有一样的闪烁的烛光,并且优于传统蜡烛,因为它不伴有明火的危险。
ATtiny电子蜡烛最难的部分就闪烁神态逼真,所以皮特做了一个蜡烛光检测电阻(LDR)和固定电阻作为一个分压器。
这是作为ATTINY85ADC之中的一个输入端,并离散时间间隔的进行采样。
采样速率为100毫秒。
然后将采集的8bit的电频值存储到EEPROM中,以便记录蜡烛的闪烁图谱,驱动将其连接的LED、PWM形成通路。
在用三节干电池供电。
最后您只需编程程序,然后通过开关进行控制。
下面是ATtiny电子蜡烛的电路图
下面是程序的代码以及写入EEPROM的数据
viewplainprint?
/*
ProgramDescription:
ThisprogramreadsalightdetectingresistorthruaninternalADCandstoresthevalue,
afterscalingit,toeeprom.ThisADCvalueissenttoaPWMchannelwithattachedled.Thisisessentiallyadatalogger
forlightandreplaybyLED.If,ifyouaimtheLDRataflickeringcandleduringitsrecordingphase,youhaveaflickering
ledcandle.
Acircuitdescriptionandotherdetailscanbefoundat
Filename:
ATTiny_Candle_v1.0.c
Author:
PeteMills
Int.RCOsc.8MHz;Start-uptimePWRDWN/RESET:
6CK/14CK+64ms
*/
//**********Includes**********
#include
#include
#include
//**********Definitions**********
//LEDforflamesimulation
#defineLEDPB0
#defineLED_PORTPORTB
#defineLED_DDRDDRB
//LightDetectingResistorforrecordingaliveflame
#defineLDRPINB3
#defineLDR_PORTPINB
#defineLDR_DDRDDRB
//TactileSwitchInput
#defineSW1PINB4
#defineSW1_PORTPINB
#defineSW1_DDRDDRB
#defineARRAY_SIZE500//sizeoftheflickerarray
#defineSAMPLE_RATE100//msdelayforcollectingandreproducingtheflicker
//**********FunctionPrototypes**********
voidsetup(void);
voidtoggle_led(void);
voidprogram_flicker(void);
voidled_alert(void);
voideeprom_save_array(void);
voideeprom_read_array(void);
voidscale_array(void);
uint8_tget_adc(void);
uint8_tscale(uint8_tinput,uint8_tinp_low,uint8_tinp_hi,uint8_toutp_low,uint8_toutp_hi);
uint8_tis_input_low(charport,charchannel,uint8_tdebounce_time,intinput_block);
//**********GlobalVariables**********
uint8_tflicker_array[ARRAY_SIZE]={0};
uint8_tEEMEMee_flicker_array[ARRAY_SIZE]={0};
intmain(void)
{
uint16_treplay=0;
setup();
eeprom_read_array();
while
(1)
{
if(is_input_low(SW1_PORT,SW1,25,250))
{
//programtheflicker
//afterenteringanduponcompletion,apredeterminedflashpatternwilloccurasdescribedinled_alert()
//aimtheldrataflickeringcandleoranyotherlightsource(likealaser)youwanttorecordduringthistime
//anduponcompletionthevaluesarestoredtoeeprom.Theyareplayedbackimmediatelyaswell
//asbeingrecalledfromeepromuponfirststartup
led_alert();
program_flicker();
scale_array();
eeprom_save_array();
led_alert();
}
//replaytherecordedflickerpattern
OCR0A=flicker_array[replay];
++replay;
if(replay>=(ARRAY_SIZE-13))//iftheendofthestoredarrayhasbeenreached
{
replay=0;//startagainfromthebeginning
//led_alert();
}
_delay_ms(SAMPLE_RATE);
_delay_ms(3);//ADCConversiontime
}
}
//**********Functions**********
voidsetup(void)
{
//*********PortConfig*********
LED_DDR|=(1< LED_PORT&=~(1< LDR_DDR&=~(1< LDR_PORT|=(1< SW1_DDR&=~(1< SW1_PORT|=(1< //**********PWMConfig********* TCCR0A|=((1< TCCR0B|=(1< //**********ADCConfig********** ADMUX|=((1< ADCSRA|=((1< DIDR0|=(1< } voidtoggle_led() { LED_PORT^=(1< } uint8_tis_input_low(charport,charchannel,uint8_tdebounce_time,intinput_block) { /* Thisfunctionisfordebouncingaswitchinput Debouncetimeisablockingintervaltowaituntiltheinputistestedagain. Iftheinputtestslowagain,adelayequaltoinput_blockisexecutedandthefunctionreturns (1) */ if(bit_is_clear(port,channel)) { _delay_ms(debounce_time); if(bit_is_clear(port,channel)) { _delay_ms(input_block); return1; } } return0; } uint8_tget_adc() { ADCSRA|=(1< while(ADCSRA&(1< return~ADCH;//returntheinverted8-bitleftadjustedadcval } voidprogram_flicker() { //buildtheflickerarray for(inti=0;i { flicker_array[i]=get_adc(); _delay_ms(SAMPLE_RATE); } } voidled_alert() { //thisisafunctiontocreateavisualalertthataneventhasoccuredwithintheprogram //ittogglestheled10times. for(inti=0;i<10;i++) { OCR0A=0; _delay_ms(40); OCR0A=255; _delay_ms(40); } } voideeprom_save_array() { for(inti=0;i { eeprom_write_byte(&ee_flicker_array[i],flicker_array[i]); } } voideeprom_read_array() { for(inti=0;i { flicker_array[i]=eeprom_read_byte(&ee_flicker_array[i]); } } uint8_tscale(uint8_tinput,uint8_tinp_low,uint8_tinp_hi,uint8_toutp_low,uint8_toutp_hi) { return(((input-inp_low)*(outp_hi-outp_low))/((inp_hi-inp_low)+outp_low)); } voidscale_array() { uint8_tarr_min=255; uint8_tarr_max=0; uint8_tout_low=20; uint8_tout_high=255; //findtheminandmaxvalues for(inti=0;i { if(flicker_array[i] arr_min=flicker_array[i]; if(flicker_array[i]>arr_max) arr_max=flicker_array[i]; } //nowthatweknowtherange,scaleit for(inti=0;i { flicker_array[i]=scale(flicker_array[i],arr_min,arr_max,out_low,out_high); } }igh); } }igh); } } } } } } } }} }} }} } EEPROM数据 : 10000000777B7D7B78BA95535E3E3E4352353E7595 : 100010004B657B5263586B5562777287858C5D7A2E : 10002000535D5062556F6758784E55956B6D7D7373 : 100030007D5B6B686A6A606B7777987A87605B6BC9 : 10004000534A5368453B65679C6067537375638A81 : 100050007F8388806358586B7A787B838A878A8508 : 1000600083888A8A8A8A8A8C8A8A8A8A8A88837F0B : 100070007D7B7A78777570707270704D416D6860B5 : 1000800035353D3B4145525E41535D60656A5048A0 : 100090004B4E3535313333363B40504E525D605315 : 1000A000564B352D2E2E353838393B383158406077 : 1000B0004D505A5D434053585A554E31312B2E33D3 : 1000C0003136353638393938404A413B506240364E : 1000D000292D455E5D523E333B433545383531333E : 1000E00036363936383B4136363039332B29335A98 : 1000F0006356413D5052556065553B302E303B4E66 : 10010000362E2B3B393D4A503D45584E4B4E4A45C5 : 10011000584B555D5B56585E60775E385A52464B79 : 10012000504A4A354E412E363638524B463B3340C4 : 100130004E605A504D434A504B48403D4046525BFA : 100140006263635B52465B43554526353B5B434DDB : 100150004056585A5D50464545413B437287908A08 : 100160008F979D9573656B4D464555554156555531 : 10017000565A5A5B5E56625565585A62686D6D6B89 : 10018000686A6F656D316F55485055675A41555EC5 : 100190006065686863606A60676A7F838C8788923D : 1001A0008D8F888C8C85826A4E35231119433B4193 : 1001B000674A4A3B2E3045414A5848705B6D72622F : 1001C0007567565A5E554D77532D36415D55404003 : 1001D0004040403E415E82928888909488857B634F : 1001E000555356555053550334013A7EFF01603E36 : 1001F0003E28018EFFFFFFFFFFFFFFFFFFFFFFFF16 : 00000001FFFFFFFFFFFFFFFFFF16 : 00000001FF: 00000001FF: 00000001FF: 00000001FF: 00000001FF0001FF0001FF0001FF0001FF0001FF0001FF0001FF001FF001FF001FF001FF001FF001FF001FF001FF01FF01FF01FF01FF01FF01FF01FF01FF01FFFFFFFFFFFFFFFFFFFF
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ATtniy 单片机 电子 蜡烛
![提示](https://static.bingdoc.com/images/bang_tan.gif)