Помощь - Поиск - Пользователи - Календарь
Полная версия этой страницы: NEC IR Decoder
Форум разработчиков электроники ELECTRONIX.ru > Микроконтроллеры (MCs) > AVR
dvm11111111
Доброго всем времени суток. Вот решил пульт с кодировкой NEC прикрутить к 88 меге. Первоисходник http://www.mcselec.com/index.php?option=co...3&Itemid=57 на BASCOM. Переделал под WINAVR, НО ВОТ РАБОТАЕТ ЭТО ВСЕ КАК-ТО НЕПОНЯТНО. Исходник прилагаю мега работает от внутреннего 8МГц, приемник на INT0, светодиод на POTB.1. ГУРУ посмотрите что не так на первый взгляд.

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);

}*/

}
}
dvm11111111
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(0,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(1,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(2,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 >= 1 && Tik < 15 && Start_flag == 1){ //'if has happenned from 10 before 21 teaks - have taken "0"
eeprom_write_byte(3,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 == 31){
Address = Address_1;
Command = Command_1;
Repeat_flag = 0;
Start_flag = 0;
Recive_flag = 1;
TCCR0B=0;
Byt=0;
PORTB=1;
}

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=0;
DDRB=0x03;

// 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;

for(Byt=0;Byt<4;Byt++){
if(PORTB==0) PORTB=1;
else PORTB=0;
_delay_ms(500);
}

Byt=0;
sei();



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);

}
*/
}
}



Вот воткнул запись временных интервалов, значения получаются такие 0x8C 0x75 0x11 0x06. По интервалам в роди все должно проходить. Повтор нажатия срабатывает нормально, а вот прием данных ну ни как работать не хочет. Такое впечатление что Bit не увеличивается.
Прерывания все нормально работают проверял тестами.
hd44780
http://chipenable.ru/index.php/how-connect...k-signalov.html

Есть и на WinAVR. Правда работает оно или нет, я не проверял.
dvm11111111
Это я уже видел. Только вот есть одна загвоздка мне таймер1 понадобиться в дальнейшем. а с таймером 0 этот способ не прокатит.


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 >= 130 && Tik < 150){ //if has happenned from 139 before 150 teaks - "START"
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 < 130) { //'if has happenned from 116 before 138 teaks - "REPETITION"
Repeat_flag = 1;
Start_flag = 0;
Recive_flag = 0;

}


if (Tik >= 19 && Tik < 30 && Start_flag == 1) {// 'if has happenned from 22 before 115 teaks - have taken "1"
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 >= 1 && Tik < 19 && Start_flag == 1){ //'if has happenned from 10 before 21 teaks - have taken "0"
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;
TCNT0 = 253;
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;
TCNT0 = 253;
TCCR0B=0; //stop timer
}


}

// Declare your global variables here

int main(void)
{
// 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=0;
DDRB=0x03;

// 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;

for(Byt=0;Byt<4;Byt++){
if(PORTB==0) PORTB=1;
else PORTB=0;
_delay_ms(500);
}

Byt=0;
sei();



while (1)
{
/*

if(Recive_flag == 1)
{
PORTB=1;
_delay_ms(500);
PORTB=0;Repeat_flag
}
*/

if( Recive_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);

}

}
}





Ну в общем вот. Заработало только кривоватенько пока. Репит срабатывает, но только при первом нажатии кнопки адрес и команда =0. при повторном нажатии все норм получается и так при нажатии на любую кнопку. ППЦ с RC-5 както проще было ))) первый же пример заработал.

Гуру ну ткните носом че за трабла.
ARV
вот тут я описал алгоритм, который можно реализовать вообще без использования прерываний, таймеров и т.п. взяв за основу идею, можно сделать и по прерываниям, чтобы принимать команды "в фоне". минус - коды декодируются нестандартно, т.е. кнопки различить легко друг от друга, но их коды стандартным скорее всего соответствовать не будут.
Marian
Делал на Атмега16 прием посылки с пульта.
(Принимает посылку с пульта и отправляет на "СОМ" порт)
Данные отправляются в виде длина импульса, длина паузы и.т.д.
Декодирование придется делать самому.

Пример как это все используется здесь ИР то СОМ
dvm11111111
Заработало. Судя по всему проблемы были при записи в епром. поубирал их и вуаля. Держите исходник кому надо.
Для просмотра полной версии этой страницы, пожалуйста, пройдите по ссылке.
Invision Power Board © 2001-2025 Invision Power Services, Inc.