rf02.c


1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
#include "global.h"
4
#include "rf02.h"
5
6
#define F_CPU 16000000UL
7
#include <util/delay.h>
8
9
#define RF_PORT  PORTB
10
#define RF_DDR  DDRB
11
#define RF_PIN  PINB
12
13
#define SDI    0  // SDI,  -> RF02
14
#define SCK    3  // SCK,  -> RF02
15
#define CS    2  // nSEL, -> RF02
16
#define IRQ    5  // nIRQ, <- RF02
17
18
// FSK: Pullup oder Pulldown
19
20
void rf02_trans(unsigned short wert)
21
{  unsigned char i;
22
23
  cbi(RF_PORT, CS);
24
  for (i=0; i<16; i++)
25
  {  if (wert&32768)
26
      sbi(RF_PORT, SDI);
27
    else
28
      cbi(RF_PORT, SDI);
29
    sbi(RF_PORT, SCK);
30
    wert<<=1;
31
    _delay_us(0.3);
32
    cbi(RF_PORT, SCK);
33
  }
34
  sbi(RF_PORT, CS);
35
}
36
37
void rf02_init(void)
38
{
39
  RF_PORT=(1<<CS);
40
  RF_DDR=(1<<SDI)|(1<<SCK)|(1<<CS);
41
42
  for (unsigned char i=0; i<15; i++)
43
    _delay_ms(10);      // wait until POR done
44
  rf02_trans(0xC0E0);      // power settings
45
  rf02_trans(0x8F80);
46
  rf02_trans(0xC2A0);      // enable tx sync bit, disable low bat detector
47
}
48
49
void rf02_setmodfreq(unsigned char bandwidth)
50
{
51
  rf02_trans(0x9780|(bandwidth&7));      //früher stand hier rf02_trans(0x8F80|(bandwidth&7));
52
}
53
54
void rf02_setfreq(unsigned short freq)
55
{  if (freq<96)        // 430,2400MHz
56
    freq=96;
57
  else if (freq>3903)      // 439,7575MHz
58
    freq=3903;
59
  rf02_trans(0xA000|freq);
60
}
61
62
void rf02_setpower(unsigned char power)
63
{
64
  rf02_trans(0xB000|((power&7)<<8));
65
}
66
67
void rf02_setbaud(unsigned short baud)
68
{
69
  if (baud<1345)
70
    baud=1345;
71
  if (baud<19000)
72
    rf02_trans(0xD240);    // 25% PLL current
73
  else if (baud<37000)
74
    rf02_trans(0xD2C0);    // 33% PLL current
75
  else
76
    rf02_trans(0xD200);    // 50% PLL current
77
  rf02_trans(0xC800|((344828UL/baud)-1));  // Baudrate= 344827,59/(R+1)
78
}
79
80
void rf02_txdata(unsigned char *data, unsigned char number)
81
{  unsigned char i,j,wert;
82
  wert=0xC6;
83
  cbi(RF_PORT, CS);
84
  for (i=0; i<8; i++)
85
  {  if (wert&128)
86
      sbi(RF_PORT, SDI);
87
    else
88
      cbi(RF_PORT, SDI);
89
    sbi(RF_PORT, SCK);
90
    wert<<=1;
91
    _delay_us(0.2);
92
    cbi(RF_PORT, SCK);
93
  }
94
    rf02_shiftout(0xAA);
95
    rf02_shiftout(0xAA);
96
    rf02_shiftout(0xAA);
97
    rf02_shiftout(0x2D);
98
    rf02_shiftout(0xD4);
99
  for (i=0; i<number; i++)
100
    rf02_shiftout(*data++);
101
  sbi(RF_PORT, CS);
102
  while(RF_PIN&(1<<IRQ));    // wait until transfer done
103
  rf02_trans(0xC464);      // TX off after 10us
104
}
105
106
void rf02_shiftout(unsigned char wert)
107
{  unsigned char j;
108
  for (j=0; j<8; j++)
109
  {  while(RF_PIN&(1<<IRQ));
110
      while(!(RF_PIN&(1<<IRQ)));
111
    if (wert&128)
112
        sbi(RF_PORT, SDI);
113
      else
114
        cbi(RF_PORT, SDI);
115
      wert<<=1;
116
    }
117
}