Ich habe jedenfall den code der Main funktion verstanden. Mir ist auch klar das in die If klammer eigentlich nichts reinkommt und man in else den code auswerten kann.
Das Problem ist nur es Funktioniert nicht. Und ich habe alles erdenkliche ausprobiert, schon bevor ich hier gepostet habe 
Also ich habe jetzt mal versucht das so schier wie möglich zu gestalten, würde mich sehr freuen wenn jemand die zeit finden könnte mal drüber zu gucken ob ich einen fehler drinn habe, denn ich weiß beim besten willen nicht mehr was es sein könnte
(sitze schon seit 2 Tagen daran)
Ich musste in der RC5.c noch SIGNAL (SIG_OVERFLOW0) in ISR(TIMER0_OVF_vect) außerdem
SIGNAL (SIG_INTERRUPT0) in ISR(INT0_vect) und SIGNAL (SIG_INTERRUPT1) in ISR(INT0_vect) umwandeln- damit der compiler nicht mehr meckert ( habe nachgelesen das die befehle veraltet waren)
application.c :
Code:
#define F_CPU 16000000UL
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include "rc5.h"
int main(void)
{ //Eingangsport Init
DDRB = 0b11111111;
DDRD &= ~(1<<PD2); // eingang fuer TSOP-DATA
//PORTD |= (1<<PD2); // Pull Up
/* der ensprechende INT-Port muss INPUT sein */
/* RC5 initialisieren, alle Adressen zulassen */
rc5_init (RC5_ALL);
/* Interrupts zulassen */
sei();
while(1)
{
/* Gibt's was Neues? */
if (-1 == rc5.flip)
{
/* Nein, dann mach irgendwas (oder nix) */
}
else
{
/* Ja, dann rc5.code merken und evtl. rc5.addr */
/* falls man die braucht und nicht sowieso schon kennt */
/*code = rc5.code;
addr = rc5.addr;*/
uint8_t code = rc5.code;
uint8_t addr = rc5.addr;
//Reaktion auf Tastendruck
PORTB = 0b11111111;
_delay_ms(10);
PORTB = 0b00000000;
_delay_ms(10);
PORTB = 0b11111111;
_delay_ms(10);
/* und auf naechstes Zeichen warten */
rc5.flip = -1;
/* code (evtl. addr) auswerten */
}
}
return 0;
}
rc5.h:
Code:
#ifndef _RC5_H_
#define _RC5_H_
#include <inttypes.h>
#define RC5_INT0 0
#define RC5_INT1 1
#define RC5_ALL 0xff
typedef struct
{
uint8_t code;
uint8_t addr;
volatile signed char flip;
} rc5_t;
extern rc5_t rc5;
extern void rc5_init (uint8_t addr);
#endif /* _RC5_H_ */ [/c]
rc5.c:
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include "rc5.h"
#define F_CPU 16000000UL
#ifndef RC5_INT
#define RC5_INT RC5_INT0
#endif /* RC5_INT */
#ifndef RC5_PRESCALE
#define RC5_PRESCALE 1024
#endif /* RC5_PRESCALE */
/* ******************************************************************************** */
rc5_t rc5;
/* ******************************************************************************** */
#ifndef F_CPU
#error Please define F_CPU
#endif /* !F_CPU */
/* µs for a whole bit of RC5 (first & second part) */
#define RC5_BIT_US (64*27)
#define RC5_TICKS \
((uint8_t) ((uint32_t) (F_CPU / 1000 * RC5_BIT_US / 1000 / RC5_PRESCALE)))
#define RC5_DELTA \
(RC5_TICKS / 6)
typedef union
{
uint16_t w;
uint8_t b[2];
} code_t;
static code_t code;
static uint8_t rc5_addr;
/* Number of Bits received so far */
/* Number of Interrupts occured so far */
static uint8_t nbits;
static uint8_t nint;
/* ******************************************************************************** */
void rc5_init (uint8_t addr)
{
nint = 0;
nbits = 0;
rc5.flip = -1;
rc5_addr = addr;
#if (RC5_PRESCALE==1024)
TCCR0 = (1 << CS02) | (1 << CS00);
#elif (RC5_PRESCALE==256)
TCCR0 = (1 << CS02);
#elif (RC5_PRESCALE==64)
TCCR0 = (1 << CS01) | (1 << CS00);
#else
#error This RC5_PRESCALE is not supported
#endif /* RC5_PRESCALE */
/* INTx on falling edge */
/* clear pending INTx */
/* enable INTx interrupt */
#if (RC5_INT == RC5_INT0)
MCUCR = (MCUCR | (1 << ISC01)) & ~ (1 << ISC00);
GIFR = (1 << INTF0);
GICR |= (1 << INT0);
#elif (RC5_INT == RC5_INT1)
MCUCR = (MCUCR | (1 << ISC11)) & ~ (1 << ISC10);
GIFR = (1 << INTF1);
GICR |= (1 << INT1);
#else
#error please define RC5_INT
#endif /* RC5_INT */
}
/* ******************************************************************************** */
ISR(TIMER0_OVF_vect)
{
TIMSK &= ~(1 << TOIE0);
uint8_t _nbits = nbits;
code_t _code = code;
if (26 == _nbits)
{
_nbits++;
_code.w <<= 1;
}
if (27 == _nbits
&& _code.b[1] >= 0x30 /* AGC == 3 */
&& 0 > rc5.flip)
{
uint8_t _rc5_code;
uint8_t _rc5_addr;
/* we do the bit manipulation stuff by hand, because of code size */
_rc5_code = _code.b[0] & 0x3f; /* 0b00111111 : #0..#5 */
_code.w <<= 2;
_rc5_addr = _code.b[1] & 0x1f; /* 0b00011111 : #6..#10 */
if (rc5_addr & 0x80
|| rc5_addr == _rc5_addr)
{
rc5.code = _rc5_code;
rc5.addr = _rc5_addr;
signed char flip = 0;
if (_code.b[1] & 0x20) /* 0b00100000 : #11 */
flip = 1;
rc5.flip = flip;
}
}
nint = 0;
nbits = 0;
/* INTx on falling edge */
/* clear pending INTx */
/* enable INTx interrupt */
#if (RC5_INT == RC5_INT0)
MCUCR = (MCUCR | (1 << ISC01)) & ~ (1 << ISC00);
GIFR = (1 << INTF0);
GICR |= (1 << INT0);
#elif (RC5_INT == RC5_INT1)
MCUCR = (MCUCR | (1 << ISC11)) & ~ (1 << ISC10);
GIFR = (1 << INTF1);
GICR |= (1 << INT1);
#endif
}
/* ******************************************************************************** */
#if (RC5_INT == RC5_INT0)
ISR(INT0_vect)
#elif (RC5_INT == RC5_INT1)
ISR(INT1_vect)
#endif /* RC5_INT */
{
code_t _code = code;
uint8_t _nint = nint;
uint8_t tcnt0 = TCNT0;
TCNT0 = 0;
if (0 == _nint)
{
/* INTx on both edges */
#if (RC5_INT == RC5_INT0)
MCUCR = (MCUCR | (1 << ISC00)) & ~ (1 << ISC01);
#elif (RC5_INT == RC5_INT1)
MCUCR = (MCUCR | (1 << ISC10)) & ~ (1 << ISC11);
#endif /* RC5_INT */
TIFR = (1 << TOV0);
TIMSK |= (1 << TOIE0);
_code.w = 0;
}
else
{
/* Number of bits of the just elapsed period */
uint8_t n = 1;
/* Bits received so far */
uint8_t _nbits = nbits;
/* is TCNT0 close to RC5_TICKS or RC5_TICKS/2 ? */
if (tcnt0 > RC5_TICKS + RC5_DELTA)
goto invalid;
else if (tcnt0 < RC5_TICKS/2 - RC5_DELTA)
goto invalid;
else if (tcnt0 > RC5_TICKS - RC5_DELTA)
n = 2;
else if (tcnt0 > RC5_TICKS/2 + RC5_DELTA)
goto invalid;
/* store the just received 1 or 2 bits */
do
{
_nbits++;
if (_nbits & 1)
{
_code.w <<= 1;
_code.b[0] |= _nint & 1;
}
}
while (--n);
if (0)
{
invalid:
/* disable INTx, run into Overflow0 */
#if (RC5_INT == RC5_INT0)
GICR &= ~(1 << INT0);
#elif (RC5_INT == RC5_INT1)
GICR &= ~(1 << INT1);
#endif /* RC5_INT */
_nbits = 0;
}
nbits = _nbits;
}
code = _code;
nint = 1+_nint;
}
Ich habe auch via If anweisung versucht den code 12 (PWR Taste) abzufragen.. klappt auch nicht.
Was allerding funktioniert ist wenn ich den Port INT0 (PD2) einfach auf LOW abfrage, dann bekomme ich Flanken angezeigt auf dem Oscilloscope beim Tastendruck.
Lesezeichen