@Ralf:
Ok, ich würde den RC5-Code in die Lib in die ISR(TIMER2_COMP_vect), also in den 100us Interrupt einklinken.
Beispiel:
Code:
// Dies VOR die ISR:
volatile RC5data_t RC5_newdata; // Decoded RC5 data
// Dies hier IN die ISR (ans Ende!):
// ----------------------------------------------------------------------------
// RC5 reception code (adapted for Robot Arm from Asuro Lib):
#define F_ISR 10e3 // 10kHz (100µs)
#define RC5_TIME 1.778e-3 // 1.778ms
#define RC5_SAMPLES_PER_BIT_MIN (uint8_t)(F_ISR * RC5_TIME * 0.4)
#define RC5_SAMPLES_PER_BIT_EARLY (uint8_t)(F_ISR * RC5_TIME * 0.75)
#define RC5_SAMPLES_PER_BIT_LATE (uint8_t)(F_ISR * RC5_TIME * 1.25)
#define RC5_PAUSE_SAMPLES 250 // 25ms (RC5 pause: 88.889ms)
static uint8_t RC5_lastsample = 0; // Last read sample
static uint8_t RC5_timecounter = 0; // Bit time counter
static uint16_t RC5_bitstream = 0; // RC5 bit stream
static uint8_t RC5_bitcounter = 0; // RC5 bit counter
uint8_t sample = 1;
if(PIND & RESERVE_PD7) // Read RC5 input (PD7)
sample = 0;
if(RC5_timecounter < 255) // Time counter limited to 8 bit
RC5_timecounter++;
if(RC5_lastsample != sample) { // Detect edges
if(RC5_timecounter <= RC5_SAMPLES_PER_BIT_MIN)
RC5_bitcounter = 0; // Edge to early
else {
if(RC5_bitcounter == 0) { // RC5 start bit
if((sample == 1)
&& (RC5_timecounter > RC5_PAUSE_SAMPLES)) {
RC5_bitstream = 1; // Store start bit
RC5_bitcounter++;
}
else
RC5_bitstream = 0; // To long pause: Error
RC5_timecounter = 0; // Reset bit time counter
}
else { // RC5 bits 2..14:
if(RC5_timecounter >= RC5_SAMPLES_PER_BIT_EARLY) {
if(RC5_timecounter <= RC5_SAMPLES_PER_BIT_LATE) {
RC5_bitstream = (RC5_bitstream << 1) | sample;
RC5_bitcounter++;
}
else
RC5_bitcounter = 0; // To late: Error
RC5_timecounter = 0; // Reset bit time counter
}
}
}
}
else {
if(RC5_timecounter > RC5_SAMPLES_PER_BIT_LATE) { // No edge in bit?
if(RC5_bitcounter == 14) { // All 14 RC5 bits read?
RC5_newdata.data = RC5_bitstream;
RC5_Ready = 1; // New data received!
}
RC5_bitcounter = 0; // Reset RC5 bit counter
}
}
RC5_lastsample = sample; // Store sample in sample buffer
// ----------------------------------------------------------------------------
Dazu müßte man im Header der Lib die Variablen definieren:
Code:
typedef union {
uint16_t data; // RC5 data structure (14 bits):
struct {
unsigned key_code : 6; // RC5 key code C0..C5
unsigned device : 5; // RC5 device address A0..A4
unsigned toggle_bit : 1; // RC5 toggle bit F
unsigned key_codeC6 : 1; // RC5 key code bit 6 !(C6)
unsigned start_bit : 1; // RC5 start bit 1
unsigned notused : 2;
};
} RC5data_t;
// RC5data_t RC5_data; // Received RC5 data
volatile uint8_t RC5_Ready; // RC5 data ready (true/false)
Probier das mal! Wenn RC5_Ready true (1) wird, wurden Daten in RC5_newdata empfangen. Das Hauptprogramm müßte also RC5_Ready ständig pollen (z.B. in einer task_RC5 in der Hauptschleife). Nachdem die Daten aus RC5_newdata gelesen wurden, muss man noch RC5_Ready false setzen, damit die nächsten Daten empfangen werden können.
Als Eingangspin habe ich PD7 genommen.
Lesezeichen