Спойлер
Код: Выделить всё
#define F_CPU 1200000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#define T_POLL 136
#define TRANSON _BV(2)
#define TRANSOFF _BV(4)
#define LED_R _BV(3)
#define BTN _BV(1)
char mod = 0; // vatiable for 0 to 1
volatile uint8_t amount; //vatiable for 0 to 12 need for 12 reps ot one side and back
volatile uint8_t i; //vatiable for 0 to 10 need fo timer
volatile uint16_t Sec; //variable for count the number of second
volatile uint16_t SecF=0;// variable for make 5 second delay working mode, and than off.
int rantime; // variable for make random number.
ISR(TIM0_OVF_vect){ //timer
TCNT0 = T_POLL;
i++;
if (i>=10) //period 1s
{
Sec++;
i=0;
}
}
static void pulcesDREAMon(void){
for ( amount = 0; amount < 12; amount++ ) //this function make 11 times on/off ransistor of number 1
{
PORTB |= TRANSON;
_delay_ms(500);
PORTB &= ~TRANSON;
_delay_ms(1000);
}
}
static void pulcesDREAMoff(void){
for ( amount = 0; amount < 12; amount++ ) //this function make 11 times on/off ransistor of number 2
{
PORTB |= TRANSOFF;
_delay_ms(500);
PORTB &= ~TRANSOFF;
_delay_ms(1000);
}
}
static void pulce_led(void){
PORTB |= LED_R;
_delay_ms(10);
PORTB &= ~LED_R;
}
static void povtor_per(void){ //repeating a piece of text repeat
SecF=5; //this variable need for check how much time passed
Sec = 0;
}
static void povtor_per2(void){ //repeating a piece of text repeat
SecF=3000; //this variable need for disable funtion which off the mode
Sec = 0;
}
int main(void){
PRR = (1<<PRADC); // shut down ADC
TIMSK0 = (1<<TOIE0); // timer0 overflow interrupt enable
TCCR0B = (1<<CS02) | (1<<CS00); // prescaler 1/1024
sei();
DDRB = TRANSON | TRANSOFF | LED_R; //work for the exit
rantime = 1800 + random() % 5400; //make random number between 30 min and 2 hours.
while (1){
if (mod==0){
if (Sec>=1800){
pulcesDREAMon(); //on the need mode and give them to work 5 sek.
povtor_per();
}
if (Sec>=SecF){ //check how much time passed
pulcesDREAMoff();
povtor_per2();
}
}
if (mod==1){
if (Sec>=rantime){
pulcesDREAMon(); //on the need mode and give them to work 5 sek.
povtor_per();
rantime = 1800 + random() % 5400;
}
if (Sec>=SecF){
pulcesDREAMoff(); //check how much time passed
povtor_per2();
}
}
if (PINB & BTN){ //just a function button processing
cli();
Sec = 0;
if (mod==0){
mod = 1;
pulce_led();
} else {
mod = 0;
pulce_led();
_delay_ms(100);
pulce_led();
SecF=5400;
}
_delay_ms(1000);
sei();
}
}
}



