Задача исходника. Поймать пакет данных
Пакет выглядит так преамбула+64бита
Преамбула
это последовательно 6 положительных и 6 отрицательных импульсво по 1мс
после нее идет информация. кодируется единица= высокий уровень 500мкс низкий уровень 500мкс
ноль кодируется=250мкс высокий 250 низкий
На косяки в исходники типа не используемых переменных не обращайте внимание и на тип особо. задача получить на оптимизацию я пока не смотрю даже.
Прилагаю файл в формате wav его можно в протеусе через AUDIO генератор подать на INT0 выставив напряжение вольт 6-7
это запись реального пакета.
В пакете последние 32 бита должны быть одинаковы это видно даже в любом wav редакторе, на деле (в протеусе) в УАРТ выводится при каждом пуске все что угодно но точно не нужные значения.
Нажмите для просмотра прикрепленного файла
CODE
/*****************************************************
This program was produced by the
CodeWizardAVR V2.03.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 02.12.2009
Author : Acer
Company :
Comments:
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8,000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#define start_timer() TCCR0=0x05 // запустить таймер
#define stop_timer() TCCR0=0x00 // остановить таймер
#define reset_timer() TCNT0=0x00 //обнулить таймер
int count;
int i;
int Buf[63];
int paket; //приняли пакет
int preamb;
int s;
int temp;
char byte[];
int a;
int x;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
if((MCUCR&0x04)!=0) {
/*прерывание произошло по положительному перепаду*/
stop_timer();
reset_timer();
start_timer();
MCUCR&=~0x04;}
else{ /*прерывание произошло по отрицательному перепаду*/
stop_timer();
i=TCNT0;
reset_timer();
MCUCR|=0x04;
if (i>13 && i<18 && preamb==0) {count++;};
if (count==5) {preamb=1;};
if (i>3 && i<=6 && preamb==1 && paket==0) {Buf[s]=1; s++; }; //Пришла еденица
if (i>7 && i<=10 && preamb==1 && paket==0) {Buf[s]=0; s++; }; // Пришел ноль
if (i<=3 && i>19 && preamb==1) {preamb=0; s=0; temp=237; }; // Срыв пакета
if (s==63) {paket=1; //пришел весь пакет
s=0; // сброс счетчика бит
preamb=0; // не в преамбуле
};
} // INT0 END
}
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A 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
PORTA=0x00;
DDRA=0x00;
// 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=0x00;
// Port C 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
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=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 8000,000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x01;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 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: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x03;
MCUCSR=0x00;
GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
if (paket==1 ) {
printf("Reseive signal:"); // выводим последние 8 бит должны быть одинаковы как и в wav файле
printf("%u%u%u%u%u%u%u%u",
Buf[56], Buf[57],Buf[58] ,Buf[59], Buf[60], Buf[61], Buf[62], Buf[63] );
printf("\n\r");
};
};
}
This program was produced by the
CodeWizardAVR V2.03.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 02.12.2009
Author : Acer
Company :
Comments:
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8,000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#define start_timer() TCCR0=0x05 // запустить таймер
#define stop_timer() TCCR0=0x00 // остановить таймер
#define reset_timer() TCNT0=0x00 //обнулить таймер
int count;
int i;
int Buf[63];
int paket; //приняли пакет
int preamb;
int s;
int temp;
char byte[];
int a;
int x;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
if((MCUCR&0x04)!=0) {
/*прерывание произошло по положительному перепаду*/
stop_timer();
reset_timer();
start_timer();
MCUCR&=~0x04;}
else{ /*прерывание произошло по отрицательному перепаду*/
stop_timer();
i=TCNT0;
reset_timer();
MCUCR|=0x04;
if (i>13 && i<18 && preamb==0) {count++;};
if (count==5) {preamb=1;};
if (i>3 && i<=6 && preamb==1 && paket==0) {Buf[s]=1; s++; }; //Пришла еденица
if (i>7 && i<=10 && preamb==1 && paket==0) {Buf[s]=0; s++; }; // Пришел ноль
if (i<=3 && i>19 && preamb==1) {preamb=0; s=0; temp=237; }; // Срыв пакета
if (s==63) {paket=1; //пришел весь пакет
s=0; // сброс счетчика бит
preamb=0; // не в преамбуле
};
} // INT0 END
}
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A 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
PORTA=0x00;
DDRA=0x00;
// 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=0x00;
// Port C 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
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=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 8000,000 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x01;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 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: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x03;
MCUCSR=0x00;
GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
if (paket==1 ) {
printf("Reseive signal:"); // выводим последние 8 бит должны быть одинаковы как и в wav файле
printf("%u%u%u%u%u%u%u%u",
Buf[56], Buf[57],Buf[58] ,Buf[59], Buf[60], Buf[61], Buf[62], Buf[63] );
printf("\n\r");
};
};
}