Created
March 18, 2016 22:20
-
-
Save raicem/6ace672297a581bb182b to your computer and use it in GitHub Desktop.
AVR Attiny13 Bisiklet Işığı Kodu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Created: 24.12.2014 12:32:24 | |
* Author: Cem | |
* Versiyon Bilgisi: isikModu = 0 konumunda iken uyku moduna geçiliyor. | |
* islemci Hizi: 9.6 mHz | |
* avrdude -c usbasp -p attiny13 -U flash:w:main.hex | |
* -U lfuse:w:0x7a:m -U hfuse:w:0xff:m //CLKDIV8 sigortası kaldırıldı. | |
*/ | |
#include <avr/io.h> | |
#include <util/delay.h> | |
#include <avr/sleep.h> | |
#include <avr/interrupt.h> | |
#define led1 0 | |
#define led2 2 | |
#define tus 1 | |
char isikModu = 0; | |
ISR(INT0_vect) | |
{ | |
} | |
void initTimers(void) | |
{ | |
TCCR0B |= (1<<CS00); //Saat prescale 1024 9.600.000/1 | |
TCCR0A |= (1 << COM0A1)| (1 << WGM01) | (1 << WGM00); | |
} | |
void initADC2(void) | |
{ | |
ADMUX |= (1<<MUX1) | (1<<MUX0); // MUX: 11 seçildi, ADC3 kanali. Referans voltaj Vcc seçildi. | |
ADCSRA |= (1<<ADPS2) | (1<<ADPS1) | (1<<ADEN); //Prescaler = 64. 9.600.000 / 64 = 150000 | |
} | |
int adcOkuma(void) | |
{ | |
ADCSRA |= (1<<ADSC); | |
while (ADCSRA & (1 << ADSC)); | |
return ADC; | |
} | |
void parlaklikAyarlama(uint8_t x) | |
{ | |
OCR0A = x; | |
} | |
void uykuModu(void) | |
{ | |
cli(); | |
GIMSK |= (1<<INT0); | |
ADCSRA &= ~(1<<ADEN); | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); | |
_delay_ms(75); | |
sei(); | |
sleep_mode(); | |
ADCSRA |= (1<<ADEN); | |
isikModu = 1; | |
_delay_ms(75); | |
} | |
int main(void) | |
{ | |
unsigned long sayac = 0; | |
unsigned long sayac2 = 0; | |
int okuma = 0; | |
char i; | |
unsigned long ortalama = 500; | |
int esikDeger = 500; | |
DDRB |= (1<<led2); | |
PORTB |= (1<<tus); | |
initTimers(); | |
initADC2(); | |
while(1) | |
{ | |
if (bit_is_clear(PINB, tus)) | |
{ | |
sayac++; | |
if (sayac == 10000) | |
{ | |
cli(); | |
esikDeger = 0; | |
isikModu = 5; | |
for (i = 0; i < 9; i++) | |
{ | |
esikDeger = esikDeger + adcOkuma(); | |
DDRB ^= (1<<led1); | |
_delay_ms(50); | |
} | |
esikDeger = (esikDeger >> 3); | |
isikModu = 1; | |
sei(); | |
} | |
} | |
if (bit_is_set(PINB, tus)) | |
{ | |
if (sayac > 20 && sayac < 10000) | |
{ | |
isikModu++; | |
if (isikModu == 2) | |
{ | |
parlaklikAyarlama(255); | |
} | |
if (isikModu == 3) | |
{ | |
parlaklikAyarlama(40); | |
} | |
if (isikModu == 4) | |
{ | |
isikModu = 0; | |
} | |
} | |
sayac = 0; | |
} | |
if (isikModu == 0) | |
{ | |
DDRB &= ~(1<<led1); | |
uykuModu(); | |
} | |
if (isikModu == 3) | |
{ | |
if (TCNT0 > 40) | |
{ | |
sayac2++; | |
if (sayac2 > 20000) | |
{ | |
sayac2 = 0; | |
DDRB ^= (1<<led1); | |
} | |
} | |
} | |
if (isikModu == 2) | |
{ | |
DDRB |= (1<<led1); | |
} | |
if (isikModu == 1) | |
{ | |
okuma = adcOkuma(); | |
ortalama = (okuma) + (ortalama << 6) - (((ortalama << 6) - 32) >> 6); | |
ortalama = (ortalama >> 6); | |
if (ortalama > esikDeger) | |
{ | |
parlaklikAyarlama(40); | |
sayac2++; | |
if (sayac2 > 1000) | |
{ | |
DDRB ^= (1<<led1); | |
sayac2 = 0; | |
} | |
} | |
else | |
{ | |
parlaklikAyarlama(255); | |
DDRB |= (1<<led1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment