PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : RC5 IR Sender (Wen´s interessiert)



The Man
21.10.2009, 19:50
Hallo zusammen,

für ein laufendes Projekt habe ich den Kern Code für einen Sender geschrieben, der des RC5 Signal benutzt. Wichtig bei dieser Konfiguration ist, das die ISR mit 72kHz aufgerufen wird.
Vorallem ist da keine selbstgebaute Bibliothek eingebunden. Ich steh da nicht so drauf.



#include <avr/io.h>
#include <avr/interrupt.h>



volatile int zielsystem = 0;
volatile int befehl = 0;
volatile int mhh = 0;
volatile int mhl = 0;
volatile int mlh = 0;
volatile int mll = 0;
volatile int tmp_shift = 0;
volatile int puls_counter = 0;
volatile int shift_counter = 0;
volatile int byte_counter;
volatile int ubertrgung_aktiv = 0;
volatile int halb_bit_counter = 0;
volatile int command [4];
volatile int command_num = 0;
volatile int bit = 0;



SIGNAL(TIMER1_COMPA_vect){// TIMER2_COMP_vect
static int tmp = 0;
puls_counter++;

if (bit < 28){
if(command[command_num] > 127){
if(tmp == 2) tmp = 0;
else tmp = 2;

}
}
else tmp = 0;
PORTB = tmp;

if (puls_counter == 64) {
command[command_num] = command[command_num] <<1;
command[command_num] &= 0b11111110;
puls_counter = 0;
halb_bit_counter++;
bit ++;
}

}

void command_to_manch(){
char tmp;
zielsystem = 20;// CD Player
befehl = 53; //Play
tmp = zielsystem;
//***********High Highbyte******
/*if (tmp > 127){
command [0] |=0b01000000;
tmp -= 128;
}
else command [0] |= 0b10000000;

if (tmp > 63){
command [0] |=0b00010000;
tmp -= 64;
}
else command [0] |= 0b00100000;

if (tmp > 31){
command [0] |=0b00000100;
tmp -= 31;
}
else command [0] |= 0b00001000;
*/
command [0] |= 0b01010100;

if (tmp > 15){
command [0] |=0b00000001;
tmp -= 16;
}
else command [0] |= 0b00000010;

//*********High Lowbyte*******
if (tmp > 7){
command [1] |=0b01000000;
tmp -= 8;
}
else command [1] |= 0b10000000;

if (tmp > 3){
command [1] |=0b00010000;
tmp -= 4;
}
else command [1] |= 0b00100000;

if (tmp > 1){
command [1] |=0b00000100;
tmp -= 2;
}
else command [1] |= 0b00001000;

if (tmp > 0){
command [1] |=0b00000001;
tmp -= 1;
}
else command [1] |= 0b00000010;

//***********High Highbyte******
tmp = befehl;
if (tmp > 31){
command [2] |=0b01000000;
tmp -= 32;
}
else command [2] |= 0b10000000;

if (tmp > 15){
command [2] |=0b00010000;
tmp -= 16;
}
else command [2] |= 0b00100000;

if (tmp > 7){
command [2] |=0b00000100;
tmp -= 8;
}
else command [2] |= 0b00001000;

if (tmp > 3){
command [2] |=0b00000001;
tmp -= 4;
}
else command [2] |= 0b00000010;
//*********High Lowbyte*******
if (tmp > 1){
command [3] |=0b01000000;
tmp -= 2;
}
else command [3] |= 0b10000000;

if (tmp > 0){
command [3] |=0b00010000;
tmp -= 1;
}
else command [3] |= 0b00100000;

command [3] |= 0b00000000;
/*
if (tmp > 0){
command [3] |=0b00000100;
tmp -= 1;
}
else command [3] |= 0b00001000;

if (tmp > 0){
command [3] |=0b00000001;
}
else command [3] |= 0b00000010;
*/
}

void senden(){
command_to_manch();
command_num = 0;
halb_bit_counter = 0;
bit = 0;

sei();
while (command_num < 4) {
if(halb_bit_counter == 8){
command_num ++;
halb_bit_counter = 0;
}

}
command [0] = 0;
command [1] = 0;
command [2] = 0;
command [3] = 0;
cli();

}

int main (void)
{

TIMSK = 0b00010000;
DDRB = 2;
DDRC = 0b00000000;
PORTC = 0b00100000;
TCCR1A |= (0<<COM1A1)|(1<<WGM11);
TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
ICR1 = 220;
OCR1A = 125;

main2:

if ((PINC & 32) == 0) senden();

goto main2;
return 0;
}


mfg,
The Man