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
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    Atmel ATtiny102和ATtiny104 MCU:小身材大性能

    Atmel ATtiny102和ATtiny104 MCU:小身材大性能 在电子设计领域,我们常常追求在小尺寸设备中实现高性能。Atmel推出的基于8位RISC架构的ATtiny
    的头像 发表于 04-27 17:20 432次阅读

    ATtiny2313:低功耗8位微控制器的技术解析与应用潜力

    ATtiny2313:低功耗8位微控制器的技术解析与应用潜力 在嵌入式系统的设计领域,低功耗、高性能的微控制器始终是工程师们追求的目标。ATtiny2313作为Atmel公司推出的一款8位微控制器
    的头像 发表于 04-26 16:25 335次阅读

    Atmel ATtiny87/ATtiny167:8位AVR微控制器的卓越之选

    Atmel ATtiny87/ATtiny167:8位AVR微控制器的卓越之选 在电子设计领域,选择一款合适的微控制器对于项目的成功至关重要。Atmel的ATtiny87/
    的头像 发表于 04-07 11:10 158次阅读

    ATtiny13/ATtiny13V:低功耗8位微控制器的卓越之选

    ATtiny13/ATtiny13V:低功耗8位微控制器的卓越之选 在电子设计领域,低功耗、高性能的微控制器一直是工程师们追求的目标。ATtiny13和
    的头像 发表于 04-07 09:35 164次阅读

    深入解析Atmel ATtiny102/ATtiny104 8位AVR微控制器

    深入解析Atmel ATtiny102/ATtiny104 8位AVR微控制器 引言 在电子设计领域,微控制器是众多项目的核心组件。Atmel的ATtiny102/
    的头像 发表于 04-07 09:05 156次阅读

    ATtiny28L/V:低功耗8位微控制器的卓越之选

    ATtiny28L/V:低功耗8位微控制器的卓越之选 在嵌入式控制应用领域,低功耗、高性能的微控制器一直是工程师们追求的目标。今天,我们就来深入了解一下Atmel公司推出的ATtiny28L/V这款
    的头像 发表于 04-06 15:45 1111次阅读

    ATtiny26(L) 8位微控制器深度解析:特性、应用与设计要点

    增强RISC架构,凭借其高性能、低功耗的特点,在众多应用场景中展现出独特的优势。本文将深入剖析ATtiny26(L)的各项特性、应用领域以及设计要点,为电子工程师们提供全面的参考。 文件下载
    的头像 发表于 04-06 15:40 1076次阅读

    Atmel ATtiny25/45/85汽车级8位微控制器的深度剖析

    Atmel ATtiny25/45/85汽车级8位微控制器的深度剖析 在汽车电子飞速发展的今天,高性能、低功耗且稳定可靠的微控制器成为了众多电子工程师设计中的核心需求。Atmel 公司
    的头像 发表于 04-06 15:30 719次阅读

    Atmel ATtiny87/ATtiny167:高性能8位AVR微控制器的技术剖析

    Atmel ATtiny87/ATtiny167:高性能8位AVR微控制器的技术剖析 一、引言 在当今的电子设计领域,对于高性能、低功耗微控制器的需求日益增长。Atmel的ATtiny
    的头像 发表于 04-06 15:15 717次阅读

    深入剖析ATtiny15L:低功耗8位微控制器的卓越之选

    深入剖析ATtiny15L:低功耗8位微控制器的卓越之选 在嵌入式系统设计领域,低功耗、高性能的微控制器一直是工程师们追求的目标。ATtiny15L作为Atmel公司推出的一款8位微控制器,凭借其先
    的头像 发表于 04-06 15:15 673次阅读

    ATtiny13/ATtiny13V 8位微控制器深度解析

    ATtiny13/ATtiny13V 8位微控制器深度解析 在电子设计领域,选择一款合适的微控制器对于项目的成功至关重要。ATtiny13/ATti
    的头像 发表于 04-06 15:05 765次阅读

    ATtiny13/ATtiny13V:低功耗8位微控制器的强大之选

    ATtiny13/ATtiny13V:低功耗8位微控制器的强大之选 在电子设计领域,一款性能卓越且低功耗的微控制器往往是工程师们的“心头好”。今天,我们就来深入了解一下 Atmel 公司推出
    的头像 发表于 02-28 15:15 538次阅读

    ATtiny28系列微控制器:低功耗与高性能的完美结合

    ATtiny28系列微控制器:低功耗与高性能的完美结合 引言 在电子设计领域,对于微控制器的需求往往是在低功耗和高性能之间寻求平衡。ATtiny28系列微控制器基于AVR RISC架构,为我们提供了
    的头像 发表于 02-09 16:25 308次阅读

    探索ATtiny441/841:高性能低功耗8位AVR微控制器的卓越之选

    探索ATtiny441/841:高性能低功耗8位AVR微控制器的卓越之选 在电子设计领域,选择一款合适的微控制器至关重要。Atmel公司的ATtiny441/841 8位AVR微控制器凭借其高性能
    的头像 发表于 02-09 14:05 532次阅读

    ATtiny3224/3226/3227:高性能低功耗的tinyAVR® 2系列微控制器

    Microchip Technology ATtiny3224、ATtiny3226与ATtiny3227 8位微控制器 (MCU) 采用带硬件乘法器的AVR® CPU,运行速率高达20MHz,具有
    的头像 发表于 10-14 16:26 1009次阅读
    <b class='flag-5'>ATtiny</b>3224/3226/3227:高性能低功耗的tinyAVR® 2系列微控制器