• 6.jpg
  • 7.jpg

第4步编程

/* ----------------------------------------------------------------------- 
 * Title:    flick led 
 * Author:   Alexander Weber <alexander.weber.0 at gmail.com> 
 * Date:     21.02.2007 
 * Hardware: ATtiny13v 
 * Software: WinAVR 20060421 
 */
    
// define as -D switch 
//#define F_CPU 1200000 // Taktfrequenz: 1.2MHz, internal oscillator 
    
    
#include <avr/io.h> 
#include <util/delay.h> 
    
#define TRUE 1 
#define FALSE 0  
#define ON 1 
#define OFF 0 
    
// use PB2 for led, pin 7 
#define LED_BIT 2 
// use PB3 to enable ldr, pin 2 
#define ADC_ENA_BIT 3 
// pause 
#define PAUSE 25 
// select ADC2, PB4, pin 3 
#define CHANNEL 2 
// values over threshold are recorded as "on" 
#define THRESHOLD 800 
// storage for recorded values 
#define MAX 400 
static uint8_t values[MAX / 8]; 
    
    
/* 
 * get_adc 
 * Return the 10bit value of the selected adc channel. 
 */
uint16_t get_adc(uint8_t channel) { 
    
    // enable voltage for adc  
    PORTB |= (1 << ADC_ENA_BIT);   
        
    // ADC setup 
    ADCSRA =  
        (1 << ADEN) |                 // enable ADC 
        (1 << ADPS1) | (1 << ADPS0);    // set prescaler to 8    
            
    // select channel 
    ADMUX = channel; 
        
    // select reference voltage 
    // ADMUX |= (1 << REFS0); // use internal reference 
    
    // warm up the ADC, discard the first conversion 
    ADCSRA |= (1 << ADSC); 
    while (ADCSRA & (1 << ADSC));  
        
    ADCSRA |= (1 << ADSC);                // start single conversion 
    while (ADCSRA & (1 << ADSC));         // wait until conversion is done 
            
    PORTB &= ~(1 << ADC_ENA_BIT);     // disable voltage for adc 
            
    return ADCW; 
} 
    
    
    
int main(void) { 
        
    uint16_t i = 0; 
    uint16_t count = 0; 
    uint8_t light = OFF; 
    uint8_t last_light = OFF; 
    uint16_t last_time = 0; 
    uint8_t programming = TRUE; 
        
    // define LED and ADC enable as outputs 
    DDRB |= (1 << LED_BIT) | 
            (1 <<ADC_ENA_BIT);     
        
    // intro 
    for (i = 0; i < 5; i++) { 
        PORTB |= (1 << LED_BIT);   
        _delay_ms(200); 
        PORTB &= ~(1 << LED_BIT); 
        _delay_ms(200); 
    } 
    for (i = 0; i < 5; i++) { 
        _delay_ms(100); 
    } 
    
    while (1) {  
                
        if (programming) { 
            // signal that we are ready to program 
            for (i = 0; i < 5; i++) { 
                PORTB |= (1 << LED_BIT);   
                _delay_ms(40); 
                PORTB &= ~(1 << LED_BIT); 
                _delay_ms(40); 
            } 
            // now read the ldr and store it 
            for (i = 0; i < MAX; i++) { 
                if (get_adc(CHANNEL) > THRESHOLD) { 
                    values[i / 8] |= (1 << (i % 8)); 
                    PORTB |= (1 << LED_BIT);   
                } 
                else { 
                    values[i / 8] &= ~(1 << (i % 8)); 
                    PORTB &= ~(1 << LED_BIT); 
                }                            
                _delay_ms(PAUSE); 
            } 
            // signal that we are finished with programming 
            for (i = 0; i < 5; i++) { 
                PORTB |= (1 << LED_BIT);   
                _delay_ms(40); 
                PORTB &= ~(1 << LED_BIT); 
                _delay_ms(40); 
            } 
            // switch to playback 
            programming = FALSE; 
            count = 0;               
        }        
        else {   
            // have we detected a change? 
            light = (get_adc(CHANNEL) > THRESHOLD) ? ON : OFF;            
            if (light != last_light) { 
                // when was the last change? 
                if ((count - last_time) == 10) { 
                    programming = TRUE;         // switch to programming mode 
                } 
                else { 
                    last_time = count; 
                } 
            } 
            last_light = light; 
                
            // replay recorded lights 
            if (values[count / 8] & (1 << (count % 8))) { 
                PORTB |= (1 << LED_BIT);   
            } 
            else { 
                PORTB &= ~(1 << LED_BIT); 
            } 
            _delay_ms(PAUSE); 
            count++; 
            if (count == MAX) { 
                count = 0; 
            }        
        } 
            
    } 
    
    return 0; 
        
}
:100000000CC026C025C024C023C022C021C020C0EF 
:100010001FC01EC01DC01CC01BC011241FBECFE5C9 
:10002000D2E0DEBFCDBF10E0A0E6B0E0E4E8F2E051 
:1000300003C0C89531960D92A036B107D1F710E0F4 
:10004000A0E6B0E001C01D92A239B107E1F710C0EF 
:10005000D7CF982FC39A83E886B997B9369A36993D 
:10006000FECF369A3699FECFC39884B195B10895E4 
:10007000CFE5D2E0DEBFCDBF40E050E0FF24C42E8C 
:10008000D52E61E087B38C6087BBE0E6FAEE24E012 
:1000900030E0C29ACE2FDF2F2197F1F7C298AE2F12 
:1000A000BF2F1197F1F72150304037FFF2CF80E397 
:1000B00095E7F92FE82F3197F1F7D92FC82F21971E 
:1000C000F1F7B92FA82F1197F1F7F92FE82F3197F2 
:1000D000F1F70197F1F772E0E72E3CE4832E3DE162 
:1000E000932EA12CB12C662309F468C0A0EEBEE2C9 
:1000F000C0E0D0E024E030E0C29ADB2FCA2F219785 
:10010000F1F7C2988A2F9B2F0197F1F721503040C9 
:1001100037FFF2CF40E050E012E003E861E070E02A 
:10012000ACE4BDE1C0E0D0E0C39A06B917B9369A95 
:100130003699FECF369A3699FECFC398E4B1F5B121 
:10014000E152F34008F47EC0D52FC42FD695C79551 
:10015000D695C795D695C795C05ADF4F952F842F52 
:1001600087709070262F372F02C0220F331F8A9579 
:10017000E2F7A880A22AA882C29A8A2F9B2F019711 
:10018000F1F74F5F5F4F31E04039530768F2A0EE5F 
:10019000BEE2C0E0D0E024E030E0C29AFB2FEA2FBC 
:1001A0003197F1F7C298DB2FCA2F2197F1F7215031 
:1001B000304037FFF2CF60E040E050E0262FC39A96 
:1001C00003E806B9E7B8369A3699FECF369A3699DB 
:1001D000FECFC398A4B1B5B1A152B34008F021E05D 
:1001E0002F1549F0952F842F8C199D090A9709F432 
:1001F00047C0C42ED52EF22ED52FC42FD695C79525 
:10020000D695C795D695C795C05ADF4F888199274F 
:10021000242F352F2770307002C0959587952A9529 
:10022000E2F780FF2BC0C29AF92DE82D3197F1F744 
:100230004F5F5F4F81E04039580709F054CF40E0ED 
:1002400050E051CFF52FE42FF695E795F695E79519 
:10025000F695E795E05AFF4F952F842F87709070A1 
:10026000262F372F02C0220F331F8A95E2F7922FD5 
:100270009095B080B922B082C2987FCFC298D4CF77 
:0402800061E0B9CFB1 
:00000001FF

下一步上一步

评 论

e

选择昵称后请轻按空格键

    提 交

    请勿进行人身攻击,谩骂以及任何违法国家相关法律法规的言论。

    信息

    54151

    浏览
    5
    asd周

    作者:asd周

    分享:19

    关键词:led微控制器可编程

    猜你会喜欢

    简单的光线探测器

    光线探测器是最普遍的传感器之一,并广泛应...

    无线供电的LED旋转时钟

    这次的制作结合了无线供电的原理和时钟功能...

    发光圣诞树

    圣诞节就要来了,你还没有圣诞树么?现在就...

    高手打造的智能无线供电台灯

    无线供电是一个很吸引人的制作课题,许多电...

    有爱的碗

    该文是在Chinked-out工作室翻译...