Created
May 16, 2016 05:23
-
-
Save raicem/a64160d319da43ae16e2dc1df2509f89 to your computer and use it in GitHub Desktop.
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
#include <avr/io.h> | |
#include <util/delay.h> | |
#include <avr/sleep.h> | |
#include <avr/interrupt.h></p> | |
#define led1 0 | |
#define led2 2 | |
#define tus 1</p> | |
uint8_t isikModu = 0;</p> | |
ISR(INT0_vect) //This is needed even if it does nothing in brackets. | |
{} | |
static void initTimers(void) | |
{ | |
TCCR0B |= (1<< CS00); //Saat prescale 1024 9.600.000/1 | |
TCCR0A |= (1 << COM0A1)| (1 << WGM01) | (1 << WGM00); | |
} | |
static void initADC2(void) | |
{ | |
ADMUX |= (1<< MUX1) | (1<< MUX0); // ADC with referance of Vcc | |
ADCSRA |= (1<< ADPS2) | (1<< ADPS1) | (1<< ADEN); //Prescaler = 64. 9.600.000 / 64 = 150000 | |
} | |
static int adcOkuma(void) // adcRead function | |
{ | |
ADCSRA |= (1 << ADSC); | |
while (ADCSRA & (1 << ADSC)); | |
return ADC; | |
} | |
static void parlaklikAyarlama(uint8_t x) // Setting the OCROA to control brightness | |
{ | |
OCR0A = x; | |
} | |
static void uykuModu(void) // Sleep mode | |
{ | |
cli(); | |
GIMSK |= (1 << INT0); | |
ADCSRA &= ~(1 << ADEN); | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); | |
_delay_ms(10); | |
sei(); | |
sleep_mode(); | |
GIMSK &= ~(1<< INT0); | |
ADCSRA |= (1<< ADEN); | |
loop_until_bit_is_set(PINB, tus); // This makes it wait until the button is released | |
isikModu = 1; | |
_delay_ms(10); | |
} | |
int main(void) | |
{ | |
uint8_t i; | |
uint16_t esikDeger = 500; | |
uint32_t sayac = 0, sayac2 = 0, sayac3 = 0, ortalama = 0; | |
PORTB |= (1<< tus); | |
initTimers(); | |
initADC2(); | |
CLKPR = 0b10000000; | |
CLKPR = 0b00000010; | |
while(1) | |
{ | |
if (bit_is_clear(PINB, tus)) | |
{ | |
sayac++; | |
if (sayac == 50000 && isikModu == 1) | |
{ | |
cli(); | |
esikDeger = 0; | |
isikModu = 5; | |
for (i = 8; i != 0; --i) | |
{ | |
esikDeger = esikDeger + adcOkuma(); | |
DDRB ^= (1<< led1); | |
_delay_ms(100); | |
} | |
esikDeger = (esikDeger >> 3); | |
isikModu = 1; | |
sei(); | |
} | |
} | |
else | |
{ | |
if (sayac > 1600 && sayac < 50000) | |
{ | |
isikModu++; | |
switch(isikModu) { // This switch statement assignes brightness level | |
case 2: | |
parlaklikAyarlama(255); | |
break; | |
case 3: | |
parlaklikAyarlama(30); | |
break; | |
case 4: | |
isikModu = 0; | |
break; | |
} | |
} | |
sayac = 0; | |
} | |
if (isikModu == 0) | |
{ | |
DDRB &= ~(1<< led1); | |
uykuModu(); | |
} | |
else if (isikModu == 3) | |
{ | |
if (TCNT0 > 40) | |
{ | |
sayac2++; | |
if (sayac2 > 5000) | |
{ | |
sayac2 = 0; | |
DDRB ^= (1<< led1); | |
} | |
} | |
} | |
else if (isikModu == 2) | |
{ | |
DDRB |= (1<< led1); | |
} | |
else if (isikModu == 1) | |
{ | |
sayac3++; | |
if (sayac3 == 30000) | |
{ | |
for (i = 0; i <64; i++) | |
{ | |
ortalama = ortalama + adcOkuma(); | |
} | |
ortalama = ortalama >> 6; | |
sayac3 = 0; | |
} | |
if (ortalama > esikDeger) | |
{ | |
parlaklikAyarlama(30); | |
sayac2++; | |
if (sayac2 > 4000) | |
{ | |
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