Probier die mal:
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include "rfm12.h"
#define F_CPU 1000000UL
#include <util/delay.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define IRQ_Port PORTD
#define IRQ_Pin PIND
#define IRQ_DDR DDRD
#define IRQ 2
#define IRQ_Output() IRQ_DDR |= (1<<IRQ)
#define IRQ_Input() IRQ_DDR&=~ (1<<IRQ)
#define IRQ_High() IRQ_Port|= (1<<IRQ)
#define IRQ_Low() IRQ_Port&=~(1<<IRQ)
#define IRQ_Wait_Low() while(IRQ_Pin&(1<<IRQ))
#define RF_PORT PORTB
#define RF_DDR DDRB
#define RF_PIN PINB
#define SDI 5
#define SCK 7
#define CS 4
#define SDO 6
void _delay_s(int s)
{
for (unsigned char i=0; i<s*100; i++)
_delay_ms(10);
}
unsigned short rf12_trans(unsigned short wert)
{ unsigned short werti=0;
unsigned char i;
cbi(RF_PORT, CS);
for (i=0; i<16; i++)
{ if (wert&32768)
sbi(RF_PORT, SDI);
else
cbi(RF_PORT, SDI);
werti<<=1;
if (RF_PIN&(1<<SDO))
werti|=1;
sbi(RF_PORT, SCK);
wert<<=1;
_delay_us(0.3);
cbi(RF_PORT, SCK);
}
sbi(RF_PORT, CS);
return werti;
}
void rf12_init(void)
{
RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS);
RF_PORT=(1<<CS);
IRQ_Input();
for (unsigned char i=0; i<10; i++)
_delay_ms(10); // wait until POR done
rf12_trans(0xC0E0); // AVR CLK: 10MHz
rf12_trans(0x80D7); // Enable FIFO
rf12_trans(0xC2AC); // Data Filter: internal
rf12_trans(0xCA81); // Set FIFO mode
rf12_trans(0xE000); // disable wakeuptimer
rf12_trans(0xC800); // disable low duty cycle
rf12_trans(0xC4F7); // AFC settings: autotuning: -10kHz...+7,5kHz
rf12_trans(0x9850); // 0 dB, fsk:15kHz
rf12_trans(0xC623); // 10 kbps
}
void rf12_ready(void)
{ cbi(RF_PORT, CS);
while (!(RF_PIN&(1<<SDO))); // wait until FIFO ready
}
void rf12_txdata(unsigned char *data, unsigned char number)
{ unsigned char i;
rf12_trans(0x8238); // TX on
rf12_ready();
rf12_trans(0xB8AA);
rf12_ready();
rf12_trans(0xB8AA);
rf12_ready();
rf12_trans(0xB8AA);
rf12_ready();
rf12_trans(0xB82D);
rf12_ready();
rf12_trans(0xB8D4);
for (i=0; i<number; i++)
{ rf12_ready();
rf12_trans(0xB800|(*data++));
}
rf12_ready();
rf12_trans(0xB8AA);
rf12_ready();
rf12_trans(0x8208); // TX off
}
void rf12_rxdata(unsigned char *data, unsigned char number)
{ unsigned char i;
rf12_trans(0x82C8); // RX on
rf12_trans(0xCA81); // set FIFO mode
rf12_trans(0xCA83); // enable FIFO
for (i=0; i<number; i++)
{ rf12_ready();
*data++=rf12_trans(0xB000);
}
rf12_trans(0x8208); // RX off
}
Die habe ich irgendwo auf http://www.mikrocontroller.net/articles/RFM12 gefunden. und ein bisschen an der Konfiguration rumgespielt.
Lesezeichen