1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #include <avr/io.h> #include <avr/interrupt.h> #include <avr/pgmspace.h> #include <avr/wdt.h> #include <util/delay.h> #define F_CPU 1.0E6 //1MHz #define ADC_VREF 0x45 /* 0100 0101 vcc:10bit:PC5: ADCW */ int main( void ){ unsigned char i; int th; ADMUX=ADC_VREF; th=0; for (i=0;i<8;i++){ //8 times measurement ADCSRA=0xC6; //1100 0110 ADC enabled start clock/64 while (( ADCSRA & 0x10)==0){} // PC5 is ADC input th = th+ADCW; ADCSRA=0x96; //1001 0110 } th=th>>3; //average generated voltage for (;;){ ADCSRA=0xC6; //ADC enabled start clock/64 while (( ADCSRA & 0x10)==0){} ADMUX=0; //avoid analog in order to activate digital output if (ADCW < th-605 ){ DDRC=0x20; for (i=0;i<4;i++){ PORTC=0x20; _delay_us(10); PORTC=0x00; _delay_us(10); } } else if (ADCW>= th-600){ DDRC=0x20; for (i=0;i<20;i++){ PORTC=0x00; _delay_us(10); } } DDRC=0; //avoid digital output PORTC=0; ADMUX=ADC_VREF; ADCSRA=0x96; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | PROJECT=ledsense328p SOURCES=$(PROJECT).c MMCU=atmega328p F_CPU = 1000000 CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=$(F_CPU) $(PROJECT).hex: $(PROJECT).out avr-objcopy -O ihex $(PROJECT).out $(PROJECT).c.hex;\ avr-size --mcu=$(MMCU) --format=avr $(PROJECT).out $(PROJECT).out: $(SOURCES) avr-gcc $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES) program-bsd: $(PROJECT).hex avrdude -p m328p -c bsd -U flash:w:$(PROJECT).c.hex program-dasa: $(PROJECT).hex avrdude -p m328p -P /dev/ttyUSB0 -c dasa -U flash:w:$(PROJECT).c.hex program-avrisp2: $(PROJECT).hex avrdude -p m328p -P usb -c avrisp2 -U flash:w:$(PROJECT).c.hex program-avrisp2-fuses: $(PROJECT).hex avrdude -p m328p -P usb -c avrisp2 -U lfuse:w:0x7E:m program-usbtiny: $(PROJECT).hex avrdude -p m328p -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex program-usbtiny-fuses: $(PROJECT).hex avrdude -p m328p -P usb -c usbtiny -U lfuse:w:0x7E:m program-dragon: $(PROJECT).hex avrdude -p m328p -P usb -c dragon_isp -U flash:w:$(PROJECT).c.hex |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <avr/io.h> #include <avr/interrupt.h> #include <avr/pgmspace.h> #include <avr/wdt.h> #include <util/delay.h> #define ADC_VREF 0x43 /* 0100 0011 vcc:10bit:PB3: ADCW */ int main( void ){ unsigned char i; int th; ADMUX=ADC_VREF; th=0; for (i=0;i<8;i++){ //8 times measurement ADCSRA=0xC6; //1100 0110 ADC enabled start clock/64 while (( ADCSRA & 0x10)==0){} //PB3 is ADC input th = th+ADCW; ADCSRA=0x96; //1001 0110 } th=th>>3; //average generated voltage for (;;){ ADCSRA=0xC6; //1100 0110 ADC enabled start clock/64 while (( ADCSRA & 0x10)==0){} ADMUX=0; //avoid analog in order to activate digital output if (ADCW < th-605){ DDRB=0x08; for (i=0;i<4;i++){ PORTB=0x08; _delay_us(10); PORTB=0x00; _delay_us(10); } } else if (ADCW >= (th-600)){ DDRB=0x08; for (i=0;i<20;i++){ PORTB=0x00; _delay_us(10); } } DDRB=0; //avoid digital output PORTB=0; ADMUX=ADC_VREF; ADCSRA=0x96; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | PROJECT=ledsense45 SOURCES=$(PROJECT).c MMCU=attiny45 F_CPU = 1000000 CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=$(F_CPU) $(PROJECT).hex: $(PROJECT).out avr-objcopy -O ihex $(PROJECT).out $(PROJECT).c.hex;\ avr-size --mcu=$(MMCU) --format=avr $(PROJECT).out $(PROJECT).out: $(SOURCES) avr-gcc $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES) program-bsd: $(PROJECT).hex avrdude -p t45 -c bsd -U flash:w:$(PROJECT).c.hex program-dasa: $(PROJECT).hex avrdude -p t45 -P /dev/ttyUSB0 -c dasa -U flash:w:$(PROJECT).c.hex program-avrisp2: $(PROJECT).hex avrdude -p t45 -P usb -c avrisp2 -U flash:w:$(PROJECT).c.hex program-usbtiny: $(PROJECT).hex avrdude -p t45 -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex program-dragon: $(PROJECT).hex avrdude -p t45 -P usb -c dragon_isp -U flash:w:$(PROJECT).c.hex |