Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Chain oiler - tomko222
- attiny24/44/84
- PA0 - switch
- PA1, PA2, PA3 - LEDs
- PA7 - valve
- */
- #define F_CPU 1000000L
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/eeprom.h>
- #include <avr/interrupt.h>
- void delay(int time);
- void ledBlink(int cont);
- struct mode
- {
- volatile int wait_time;
- volatile int open_time;
- };
- //modes - temp values!!!
- struct mode modeLow = {.wait_time = 60, .open_time = 5};
- struct mode modeMid = {.wait_time = 45, .open_time = 7};
- struct mode modeHigh = {.wait_time = 30, .open_time = 10};
- volatile struct mode* currentModeP;
- int volatile modeNumber = 0;//0 - off, 1 - modeLow, 2 - modeMid, 3 - modeHigh
- int volatile initializationFinished = 0;
- void setCurrentMode(int number);
- void init();
- int buttonPushed = 0;
- void init()
- {
- DDRA |= _BV(1) |_BV(2) |_BV(3);//LEDs - out
- DDRA &= ~_BV(0);//PA0 (INT0) - switch - in
- DDRA |= _BV(7);//valve - out
- //interrupts
- GIMSK |= (1 << PCIE0);
- PCMSK0 = (1<<PCINT0);
- sei();
- ledBlink(3);
- initializationFinished = 1;
- }
- int main()
- {
- initializationFinished = 0;
- init();
- while(1)
- {
- if(modeNumber > 0 && modeNumber <= 3)
- {
- delay((*currentModeP).wait_time);//wait wait_time
- PORTA |= _BV(7);//open
- delay((*currentModeP).open_time);//wait open_time
- PORTA &= ~_BV(7);//close
- }else {
- PORTA &= ~_BV(7);//close
- }
- }
- return 0;
- }
- ISR(PCINT0_vect)
- {
- if(!buttonPushed)
- buttonPushed = 1;
- else
- buttonPushed = 0;
- if(initializationFinished == 1)
- {
- PORTA &= ~_BV(7);
- if(buttonPushed)
- {
- if(modeNumber >= 0 && modeNumber < 3)
- {
- setCurrentMode(++modeNumber);
- }else if(modeNumber == 3)
- {
- setCurrentMode(0);
- }
- }
- }
- }
- void delay(int time)
- {
- while(time--)
- _delay_ms(100);
- }
- void ledBlink(int count)
- {
- for(; count; count--)
- {
- _delay_ms(300);
- PORTA &= ~_BV(1) & ~_BV(2) & ~_BV(3);//turn on
- _delay_ms(300);
- PORTA |= _BV(1) |_BV(2) |_BV(3);//turn off
- }
- }
- void setCurrentMode(int number)
- {
- if(number >=0 && number <=4)
- {
- if(number == 0)
- {
- PORTA &= ~_BV(7);
- modeNumber = 0;
- //LEDs
- PORTA |= _BV(1) |_BV(2) |_BV(3);
- }else if(number == 1)
- {
- modeNumber = 1;
- currentModeP = &modeLow;
- //LEDs
- PORTA |= _BV(2) |_BV(3);
- PORTA &= ~_BV(1);
- }else if(number == 2)
- {
- modeNumber = 2;
- currentModeP = &modeMid;
- //LEDs
- PORTA |= _BV(1) |_BV(3);
- PORTA &= ~_BV(2);
- }else if(number == 3)
- {
- modeNumber = 3;
- currentModeP = &modeHigh;
- //LEDs
- PORTA |= _BV(1) |_BV(2);
- PORTA &= ~_BV(3);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement