CODE
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/eeprom.h>
volatile int Tik;
volatile char Byt; //'counter accepted bit
volatile char Repeat_flag, Recive_flag; //'flag of repetition
volatile char Start_flag; //'flag of start condition
volatile char Address; //'byte of address
volatile char Address_1; //'direct byte of address
volatile char Command;
volatile char Command_1; //'direct byte of command
volatile char Address_0; //'indirect byte of address
volatile char Command_0; //'indirect byte of command
// volatile int Summa;
// External Interrupt 0 service routine
SIGNAL (SIG_INTERRUPT0)
{
TCNT0 = 253;
TCCR0B=4;
if (Tik >= 125 && Tik < 150){ //if has happenned from 139 before 150 teaks - "START"
eeprom_write_byte(10,Tik);
Address = 1;
Repeat_flag = 0;
Recive_flag = 0;
Start_flag = 1;
Address_1 = 0;
Command_1 = 0;
Address_0 = 0;
Command_0 = 0;
Byt=0;
}
if (Tik >= 100 && Tik < 125) { //'if has happenned from 116 before 138 teaks - "REPETITION"
eeprom_write_byte(11,Tik);
Address = 2;
Repeat_flag = 1;
Start_flag = 0;
Recive_flag = 0;
}
if (Tik >= 15 && Tik < 30 && Start_flag == 1) {// 'if has happenned from 22 before 115 teaks - have taken "1"
eeprom_write_byte(12,Tik);
Byt=Byt+1;
if ( Byt < 9){ Address_1<<=1; Address_1|=1; }
if ( Byt >= 9 && Byt < 17){ Address_0<<=1;Address_0|=1; }
if (Byt >= 17 && Byt < 25){ Command_1<<=1;Command_1|=1; }
if (Byt >= 25){Command_0<<=1; Command_0|=1;}
}
if ( Tik >= 5 && Tik < 15 && Start_flag == 1){ //'if has happenned from 10 before 21 teaks - have taken "0"
eeprom_write_byte(13,Tik);
Byt=Byt+1;
if ( Byt < 9) Address_1<<=1;
if ( Byt >= 9 && Byt < 17) Address_0<<=1;
if (Byt >= 17 && Byt < 25) Command_1<<=1;
if (Byt >= 25) Command_0<<=1;
}
if (Byt == 32){
Address = Address_1;
Command = Command_1;
Repeat_flag = 0;
Start_flag = 0;
Recive_flag = 1;
TCCR0B=0;
Byt=0;
}
Tik = 0;
}
// Timer 0 overflow interrupt service routine
SIGNAL (SIG_OVERFLOW0)
{
TCNT0 = 253; // 31250/(256-253)=10416,66 Hz (96 een)
Tik=Tik+1;
if(Tik >= 1200){ //'if 1200 teaks, have thrown all in source condition
Tik = 0;
Repeat_flag = 0;
Start_flag = 0;
Recive_flag = 0;
Address_1 = 0;
Command_1 = 0;
Address_0 = 0;
Command_0 = 0;
Address = 0;
Command = 0;
PORTB=0;
Byt = 0;
TCCR0B=0; //stop timer
}
}
// Declare your global variables here
int main(void)
{
// Declare your local variables here
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x07;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x04;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 31,250 kHz
// Mode: Normal top=0xFF
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x04;
OCR0A=0x00;
OCR0B=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2A output: Disconnected
// OC2B output: Disconnected
ASSR=0x00;
TCCR2A=0x00;
TCCR2B=0x00;
TCNT2=0x00;
OCR2A=0x00;
OCR2B=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-14: Off
// Interrupt on any change on pins PCINT16-23: Off
EICRA=0x02;
EIMSK=0x01;
EIFR=0x01;
PCICR=0x00;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x01;
// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x00;
// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x00;
// USART initialization
// USART disabled
UCSR0B=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
ADCSRB=0x00;
DIDR1=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Global enable interrupts
//PORTB=1;
TCCR0B=0;
TCNT0 = 253;
/*
PORTB=1;
_delay_ms(500);
PORTB=0;
_delay_ms(500);
PORTB=1;
_delay_ms(500);
PORTB=0;
*/
sei();
Byt=0;
while (1)
{
if(Recive_flag == 1)
{
PORTB=1;
_delay_ms(500);
PORTB=0;
}
/*
if(Repeat_flag == 1){
PORTB=1;
_delay_ms(100);
PORTB=0;
_delay_ms(100);
PORTB=1;
_delay_ms(100);
PORTB=0;
Repeat_flag =0;
eeprom_write_byte(5,Address);
eeprom_write_byte(7,Command);
eeprom_write_byte(6,Address_0);
eeprom_write_byte(8,Command_0);
}*/
}
}
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/eeprom.h>
volatile int Tik;
volatile char Byt; //'counter accepted bit
volatile char Repeat_flag, Recive_flag; //'flag of repetition
volatile char Start_flag; //'flag of start condition
volatile char Address; //'byte of address
volatile char Address_1; //'direct byte of address
volatile char Command;
volatile char Command_1; //'direct byte of command
volatile char Address_0; //'indirect byte of address
volatile char Command_0; //'indirect byte of command
// volatile int Summa;
// External Interrupt 0 service routine
SIGNAL (SIG_INTERRUPT0)
{
TCNT0 = 253;
TCCR0B=4;
if (Tik >= 125 && Tik < 150){ //if has happenned from 139 before 150 teaks - "START"
eeprom_write_byte(10,Tik);
Address = 1;
Repeat_flag = 0;
Recive_flag = 0;
Start_flag = 1;
Address_1 = 0;
Command_1 = 0;
Address_0 = 0;
Command_0 = 0;
Byt=0;
}
if (Tik >= 100 && Tik < 125) { //'if has happenned from 116 before 138 teaks - "REPETITION"
eeprom_write_byte(11,Tik);
Address = 2;
Repeat_flag = 1;
Start_flag = 0;
Recive_flag = 0;
}
if (Tik >= 15 && Tik < 30 && Start_flag == 1) {// 'if has happenned from 22 before 115 teaks - have taken "1"
eeprom_write_byte(12,Tik);
Byt=Byt+1;
if ( Byt < 9){ Address_1<<=1; Address_1|=1; }
if ( Byt >= 9 && Byt < 17){ Address_0<<=1;Address_0|=1; }
if (Byt >= 17 && Byt < 25){ Command_1<<=1;Command_1|=1; }
if (Byt >= 25){Command_0<<=1; Command_0|=1;}
}
if ( Tik >= 5 && Tik < 15 && Start_flag == 1){ //'if has happenned from 10 before 21 teaks - have taken "0"
eeprom_write_byte(13,Tik);
Byt=Byt+1;
if ( Byt < 9) Address_1<<=1;
if ( Byt >= 9 && Byt < 17) Address_0<<=1;
if (Byt >= 17 && Byt < 25) Command_1<<=1;
if (Byt >= 25) Command_0<<=1;
}
if (Byt == 32){
Address = Address_1;
Command = Command_1;
Repeat_flag = 0;
Start_flag = 0;
Recive_flag = 1;
TCCR0B=0;
Byt=0;
}
Tik = 0;
}
// Timer 0 overflow interrupt service routine
SIGNAL (SIG_OVERFLOW0)
{
TCNT0 = 253; // 31250/(256-253)=10416,66 Hz (96 een)
Tik=Tik+1;
if(Tik >= 1200){ //'if 1200 teaks, have thrown all in source condition
Tik = 0;
Repeat_flag = 0;
Start_flag = 0;
Recive_flag = 0;
Address_1 = 0;
Command_1 = 0;
Address_0 = 0;
Command_0 = 0;
Address = 0;
Command = 0;
PORTB=0;
Byt = 0;
TCCR0B=0; //stop timer
}
}
// Declare your global variables here
int main(void)
{
// Declare your local variables here
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x07;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x04;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 31,250 kHz
// Mode: Normal top=0xFF
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x04;
OCR0A=0x00;
OCR0B=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2A output: Disconnected
// OC2B output: Disconnected
ASSR=0x00;
TCCR2A=0x00;
TCCR2B=0x00;
TCNT2=0x00;
OCR2A=0x00;
OCR2B=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-14: Off
// Interrupt on any change on pins PCINT16-23: Off
EICRA=0x02;
EIMSK=0x01;
EIFR=0x01;
PCICR=0x00;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x01;
// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x00;
// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x00;
// USART initialization
// USART disabled
UCSR0B=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
ADCSRB=0x00;
DIDR1=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Global enable interrupts
//PORTB=1;
TCCR0B=0;
TCNT0 = 253;
/*
PORTB=1;
_delay_ms(500);
PORTB=0;
_delay_ms(500);
PORTB=1;
_delay_ms(500);
PORTB=0;
*/
sei();
Byt=0;
while (1)
{
if(Recive_flag == 1)
{
PORTB=1;
_delay_ms(500);
PORTB=0;
}
/*
if(Repeat_flag == 1){
PORTB=1;
_delay_ms(100);
PORTB=0;
_delay_ms(100);
PORTB=1;
_delay_ms(100);
PORTB=0;
Repeat_flag =0;
eeprom_write_byte(5,Address);
eeprom_write_byte(7,Command);
eeprom_write_byte(6,Address_0);
eeprom_write_byte(8,Command_0);
}*/
}
}