{"id":1722,"date":"2018-12-12T01:20:51","date_gmt":"2018-12-12T01:20:51","guid":{"rendered":""},"modified":"2018-12-12T09:21:26","modified_gmt":"2018-12-12T01:21:26","slug":"attiny%e5%8d%95%e7%89%87%e6%9c%ba%e7%94%b5%e5%ad%90%e8%9c%a1%e7%83%9b","status":"publish","type":"post","link":"http:\/\/www.szryc.com\/?p=1722","title":{"rendered":"ATtiny\u5355\u7247\u673a\u7535\u5b50\u8721\u70db"},"content":{"rendered":"

\n\t\u60f3\u60f3\u5f53\u4f60\u597d\u4e0d\u5bb9\u6613\u8ddf\u5973\u670b\u53cb\u5171\u5ea6\u70db\u5149\u665a\u9910\uff0c\u5374\u56e0\u4e3a\u8721\u70db\u70b9\u6ca1\u4e86\u6216\u6253\u7ffb\u7740\u706b\u4e86\uff0c\u90a3\u662f\u4e00\u4ef6\u591a\u4e48\u5751\u7239\u7684\u4e8b\u554a!\u4eca\u5929\u4e3a\u4f60\u5206\u4eab\u4e00\u6b3e\u81ea\u5df1diy\u7684\u8d85\u81ea\u7136\u7684\u70db\u5149\u8721\u70db\u3002<\/p>\n

\n\t <\/p>\n


\n\t\"WP_000356.jpg\"<\/center><\/p>\n

\n\tATtiny \u7535\u5b50\u8721\u70db\uff0c\u76ae\u7279•\u7c73\u5c14\u65af\u5f00\u53d1\u8fd9\u4e2a\u4f1f\u5927\u7684\u8721\u70db\uff0c\u6b63\u5982\u6211\u4eec\u56fe\u7247\u6240\u89c1\u5230\u7684\u4e00\u6837\uff0c\u4f46\u600e\u6837\u8ba9\u8fd9\u8721\u70db\u7684\u5149\u8292\u50cf\u4f20\u7edf\u7684\u8721\u70db\u4e00\u6837\u95ea\u70c1\u5462\u3002<\/p>\n

\n\t <\/p>\n


\n\t\"WP_000370.jpg\"<\/center><\/p>\n

\n\t\u76ae\u7279\u4f7f\u7528\u4e00\u4e2a\u9ad8\u4eae\u7684LED\u548c\u4e00\u4e9b\u6a21\u62df\u7684\u8f85\u52a9\u8f6f\u4ef6\uff0c\u8fd9\u6837\u5c31\u4f7f\u5f97ATtiny \u7535\u5b50\u8721\u70db\u7684\u70db\u5149\u548c\u4f20\u7edf\u8721\u70db\u62e5\u6709\u4e00\u6837\u7684\u95ea\u70c1\u7684\u70db\u5149\uff0c\u5e76\u4e14\u4f18\u4e8e\u4f20\u7edf\u8721\u70db\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4f34\u6709\u660e\u706b\u7684\u5371\u9669\u3002<\/p>\n

\n\t <\/p>\n


\n\t\"WP_000376.jpg\"<\/center><\/p>\n

\n\tATtiny \u7535\u5b50\u8721\u70db\u6700\u96be\u7684\u90e8\u5206\u5c31\u95ea\u70c1\u795e\u6001\u903c\u771f\uff0c\u6240\u4ee5\u76ae\u7279\u505a\u4e86\u4e00\u4e2a\u8721\u70db\u5149\u68c0\u6d4b\u7535\u963b( LDR )\u548c\u56fa\u5b9a\u7535\u963b\u4f5c\u4e3a\u4e00\u4e2a\u5206\u538b\u5668\u3002\u8fd9\u662f\u4f5c\u4e3aATTINY85 ADC\u4e4b\u4e2d\u7684\u4e00\u4e2a\u8f93\u5165\u7aef\uff0c\u5e76\u79bb\u6563\u65f6\u95f4\u95f4\u9694\u7684\u8fdb\u884c\u91c7\u6837\u3002\u91c7\u6837\u901f\u7387\u4e3a100\u6beb\u79d2\u3002\u7136\u540e\u5c06\u91c7\u96c6\u76848bit\u7684\u7535\u9891\u503c\u5b58\u50a8\u5230EEPROM\u4e2d\uff0c\u4ee5\u4fbf\u8bb0\u5f55\u8721\u70db\u7684\u95ea\u70c1\u56fe\u8c31\uff0c\u9a71\u52a8\u5c06\u5176\u8fde\u63a5\u7684LED\u3001PWM\u5f62\u6210\u901a\u8def\u3002\u5728\u7528\u4e09\u8282\u5e72\u7535\u6c60\u4f9b\u7535\u3002\u6700\u540e\u60a8\u53ea\u9700\u7f16\u7a0b\u7a0b\u5e8f\uff0c\u7136\u540e\u901a\u8fc7\u5f00\u5173\u8fdb\u884c\u63a7\u5236\u3002<\/p>\n

\n\t <\/p>\n


\n\t\"WP_000345.jpg\"<\/center><\/p>\n

\n\t\u4e0b\u9762\u662fATtiny \u7535\u5b50\u8721\u70db\u7684\u7535\u8def\u56fe<\/p>\n

\n\t <\/p>\n


\n\t\"ATTiny<\/center><\/p>\n

\n\t\u4e0b\u9762\u662f\u7a0b\u5e8f\u7684\u4ee3\u7801\u4ee5\u53ca\u5199\u5165EEPROM\u7684\u6570\u636e<\/p>\n

\n\tview plainprint?<\/p>\n

\n\t\/*<\/p>\n

\n\tProgram Description: This program reads a light detecting resistor thru an internal ADC and stores the value,<\/p>\n

\n\tafter scaling it, to eeprom. This ADC value is sent to a PWM channel with attached led. This is essentially a data logger<\/p>\n

\n\tfor light and replay by LED. If, if you aim the LDR at a flickering candle during its recording phase, you have a flickering<\/p>\n

\n\tled candle.<\/p>\n

\n\tA circuit description and other details can be found at http:\/\/petemills.blogspot.com<\/p>\n

\n\tFilename: ATTiny_Candle_v1.0.c<\/p>\n

\n\tAuthor: Pete Mills<\/p>\n

\n\tInt. RC Osc. 8 MHz; Start-up time PWRDWN\/RESET: 6 CK\/14 CK + 64 ms<\/p>\n

\n\t*\/<\/p>\n

\n\t\/\/********** Includes **********<\/p>\n

\n\t#include<\/p>\n

\n\t#include<\/p>\n

\n\t#include<\/p>\n

\n\t\/\/********** Definitions **********<\/p>\n

\n\t\/\/ LED for flame simulation<\/p>\n

\n\t#define LED PB0<\/p>\n

\n\t#define LED_PORT PORTB<\/p>\n

\n\t#define LED_DDR DDRB<\/p>\n

\n\t\/\/ Light Detecting Resistor for recording a live flame<\/p>\n

\n\t#define LDR PINB3<\/p>\n

\n\t#define LDR_PORT PINB<\/p>\n

\n\t#define LDR_DDR DDRB<\/p>\n

\n\t\/\/ Tactile Switch Input<\/p>\n

\n\t#define SW1 PINB4<\/p>\n

\n\t#define SW1_PORT PINB<\/p>\n

\n\t#define SW1_DDR DDRB<\/p>\n

\n\t#define ARRAY_SIZE 500 \/\/ size of the flicker array<\/p>\n

\n\t#define SAMPLE_RATE 100 \/\/ ms delay for collecting and reproducing the flicker<\/p>\n

\n\t\/\/********** Function Prototypes **********<\/p>\n

\n\tvoid setup(void);<\/p>\n

\n\tvoid toggle_led(void);<\/p>\n

\n\tvoid program_flicker(void);<\/p>\n

\n\tvoid led_alert(void);<\/p>\n

\n\tvoid eeprom_save_array(void);<\/p>\n

\n\tvoid eeprom_read_array(void);<\/p>\n

\n\tvoid scale_array(void);<\/p>\n

\n\tuint8_t get_adc(void);<\/p>\n

\n\tuint8_t scale( uint8_t input, uint8_t inp_low, uint8_t inp_hi, uint8_t outp_low, uint8_t outp_hi);<\/p>\n

\n\tuint8_t is_input_low(char port, char channel, uint8_t debounce_time, int input_block);<\/p>\n

\n\t\/\/********** Global Variables **********<\/p>\n

\n\tuint8_t flicker_array[ ARRAY_SIZE ] = { 0 };<\/p>\n

\n\tuint8_t EEMEM ee_flicker_array[ ARRAY_SIZE ] = { 0 };<\/p>\n

\n\tint main(void)<\/p>\n

\n\t{<\/p>\n

\n\tuint16_t replay = 0;<\/p>\n

\n\tsetup();<\/p>\n

\n\teeprom_read_array();<\/p>\n

\n\twhile(1)<\/p>\n

\n\t{<\/p>\n

\n\tif( is_input_low( SW1_PORT, SW1, 25, 250 ) )<\/p>\n

\n\t{<\/p>\n

\n\t\/\/ program the flicker<\/p>\n

\n\t\/\/ after entering and upon completion, a predetermined flash pattern will occur as described in led_alert()<\/p>\n

\n\t\/\/ aim the ldr at a flickering candle or any other light source ( like a laser ) you want to record during this time<\/p>\n

\n\t\/\/ and upon completion the values are stored to eeprom. They are played back immediately as well<\/p>\n

\n\t\/\/ as being recalled from eeprom upon first start up<\/p>\n

\n\tled_alert();<\/p>\n

\n\tprogram_flicker();<\/p>\n

\n\tscale_array();<\/p>\n

\n\teeprom_save_array();<\/p>\n

\n\tled_alert();<\/p>\n

\n\t}<\/p>\n

\n\t\/\/ replay the recorded flicker pattern<\/p>\n

\n\tOCR0A = flicker_array[ replay ];<\/p>\n

\n\t++replay;<\/p>\n

\n\tif( replay >= ( ARRAY_SIZE - 13 ) ) \/\/ if the end of the stored array has been reached<\/p>\n

\n\t{<\/p>\n

\n\treplay = 0; \/\/ start again from the beginning<\/p>\n

\n\t\/\/led_alert();<\/p>\n

\n\t}<\/p>\n

\n\t_delay_ms( SAMPLE_RATE );<\/p>\n

\n\t_delay_ms( 3 ); \/\/ ADC Conversion time<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\t\/\/********** Functions **********<\/p>\n

\n\tvoid setup(void)<\/p>\n

\n\t{<\/p>\n

\n\t\/\/********* Port Config *********<\/p>\n

\n\tLED_DDR |= ( 1 << LED); \/\/ set PB0 to "1" for output<\/p>\n

\n\tLED_PORT &= ~( 1 << LED ); \/\/ turn the led off<\/p>\n

\n\tLDR_DDR &= ~( 1 << LDR ); \/\/ set LDR pin to 0 for input<\/p>\n

\n\tLDR_PORT |= ( 1 << LDR ); \/\/ write 1 to enable internal pullup<\/p>\n

\n\tSW1_DDR &= ~( 1 << SW1 ); \/\/ set sw1 pin to 0 for input<\/p>\n

\n\tSW1_PORT |= ( 1 << SW1 ); \/\/ write a 1 to sw1 to enable the internal pullup<\/p>\n

\n\t\/\/********** PWM Config *********<\/p>\n

\n\tTCCR0A |= ( ( 1 << COM0A1 ) | ( 1 << WGM01 ) | ( 1 << WGM00 ) ); \/\/ non inverting fast pwm<\/p>\n

\n\tTCCR0B |= ( 1 << CS00 ); \/\/ start the timer<\/p>\n

\n\t\/\/********** ADC Config **********<\/p>\n

\n\tADMUX |= ( ( 1 << ADLAR ) | ( 1 << MUX1 ) | ( 1 << MUX0 ) ); \/\/ left adjust and select ADC3<\/p>\n

\n\tADCSRA |= ( ( 1 << ADEN ) | ( 1 << ADPS2 ) | ( 1 << ADPS1 ) ); \/\/ ADC enable and clock divide 8MHz by 64 for 125khz sample rate<\/p>\n

\n\tDIDR0 |= ( 1 << ADC3D ); \/\/ disable digital input on analog input channel to conserve power<\/p>\n

\n\t}<\/p>\n

\n\tvoid toggle_led()<\/p>\n

\n\t{<\/p>\n

\n\tLED_PORT ^= ( 1 << LED );<\/p>\n

\n\t}<\/p>\n

\n\tuint8_t is_input_low( char port, char channel, uint8_t debounce_time, int input_block )<\/p>\n

\n\t{<\/p>\n

\n\t\/*<\/p>\n

\n\tThis function is for debouncing a switch input<\/p>\n

\n\tDebounce time is a blocking interval to wait until the input is tested again.<\/p>\n

\n\tIf the input tests low again, a delay equal to input_block is executed and the function returns ( 1 )<\/p>\n

\n\t*\/<\/p>\n

\n\tif ( bit_is_clear( port, channel ) )<\/p>\n

\n\t{<\/p>\n

\n\t_delay_ms( debounce_time );<\/p>\n

\n\tif ( bit_is_clear( port, channel ) )<\/p>\n

\n\t{<\/p>\n

\n\t_delay_ms( input_block );<\/p>\n

\n\treturn 1;<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\treturn 0;<\/p>\n

\n\t}<\/p>\n

\n\tuint8_t get_adc()<\/p>\n

\n\t{<\/p>\n

\n\tADCSRA |= ( 1 << ADSC ); \/\/ start the ADC Conversion<\/p>\n

\n\twhile( ADCSRA & ( 1 << ADSC )); \/\/ wait for the conversion to be complete<\/p>\n

\n\treturn ~ADCH; \/\/ return the inverted 8-bit left adjusted adc val<\/p>\n

\n\t}<\/p>\n

\n\tvoid program_flicker()<\/p>\n

\n\t{<\/p>\n

\n\t\/\/ build the flicker array<\/p>\n

\n\tfor( int i = 0; i < ARRAY_SIZE; i++ )<\/p>\n

\n\t{<\/p>\n

\n\tflicker_array[ i ] = get_adc();<\/p>\n

\n\t_delay_ms( SAMPLE_RATE );<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\tvoid led_alert()<\/p>\n

\n\t{<\/p>\n

\n\t\/\/ this is a function to create a visual alert that an event has occured within the program<\/p>\n

\n\t\/\/ it toggles the led 10 times.<\/p>\n

\n\tfor( int i = 0; i < 10; i++ )<\/p>\n

\n\t{<\/p>\n

\n\tOCR0A = 0;<\/p>\n

\n\t_delay_ms( 40 );<\/p>\n

\n\tOCR0A = 255;<\/p>\n

\n\t_delay_ms( 40 );<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\tvoid eeprom_save_array()<\/p>\n

\n\t{<\/p>\n

\n\tfor( int i = 0; i < ARRAY_SIZE; i++ )<\/p>\n

\n\t{<\/p>\n

\n\teeprom_write_byte( &ee_flicker_array[ i ], flicker_array[ i ] );<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\tvoid eeprom_read_array()<\/p>\n

\n\t{<\/p>\n

\n\tfor( int i = 0; i < ARRAY_SIZE; i++ )<\/p>\n

\n\t{<\/p>\n

\n\tflicker_array[ i ] = eeprom_read_byte( &ee_flicker_array[ i ] );<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\tuint8_t scale( uint8_t input, uint8_t inp_low, uint8_t inp_hi, uint8_t outp_low, uint8_t outp_hi)<\/p>\n

\n\t{<\/p>\n

\n\treturn ( ( ( input - inp_low ) * ( outp_hi - outp_low ) ) \/ ( ( inp_hi - inp_low ) + outp_low ) );<\/p>\n

\n\t}<\/p>\n

\n\tvoid scale_array()<\/p>\n

\n\t{<\/p>\n

\n\tuint8_t arr_min = 255;<\/p>\n

\n\tuint8_t arr_max = 0;<\/p>\n

\n\tuint8_t out_low = 20;<\/p>\n

\n\tuint8_t out_high = 255;<\/p>\n

\n\t\/\/ find the min and max values<\/p>\n

\n\tfor( int i = 0; i < ARRAY_SIZE; i++ )<\/p>\n

\n\t{<\/p>\n

\n\tif( flicker_array[ i ] < arr_min )<\/p>\n

\n\tarr_min = flicker_array[ i ];<\/p>\n

\n\tif( flicker_array[ i ] > arr_max )<\/p>\n

\n\tarr_max = flicker_array[ i ];<\/p>\n

\n\t}<\/p>\n

\n\t\/\/ now that we know the range, scale it<\/p>\n

\n\tfor( int i = 0; i < ARRAY_SIZE; i++ )<\/p>\n

\n\t{<\/p>\n

\n\tflicker_array[ i ] = scale( flicker_array[ i ], arr_min, arr_max, out_low, out_high );<\/p>\n

\n\t}<\/p>\n

\n\t} igh );<\/p>\n

\n\t}<\/p>\n

\n\t} igh );<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\t}<\/p>\n

\n\t} }<\/p>\n

\n\t} }<\/p>\n

\n\t} }<\/p>\n

\n\t}<\/p>\n","protected":false},"excerpt":{"rendered":"

\u60f3\u60f3\u5f53\u4f60\u597d\u4e0d\u5bb9\u6613\u8ddf\u5973\u670b\u53cb\u5171\u5ea6\u70db\u5149\u665a\u9910\uff0c\u5374\u56e0\u4e3a\u8721\u70db\u70b9\u6ca1\u4e86\u6216\u6253\u7ffb\u7740\u706b\u4e86\uff0c\u90a3\u662f\u4e00\u4ef6\u591a\u4e48\u5751\u7239\u7684\u4e8b\u554a!\u4eca\u5929\u4e3a\u4f60\u5206\u4eab\u4e00\u6b3e\u81ea\u5df1diy\u7684\u8d85\u81ea\u7136\u7684\u70db\u5149\u8721\u70db\u3002 ATtiny \u7535\u5b50\u8721\u70db\uff0c\u76ae\u7279\u7c73\u5c14\u65af\u5f00\u53d1<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/1722"}],"collection":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1722"}],"version-history":[{"count":0,"href":"http:\/\/www.szryc.com\/index.php?rest_route=\/wp\/v2\/posts\/1722\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1722"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.szryc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}