rssi-cpp - cpp
Embed
You can embed this paste into a blog or website with this code:
<iframe class="codetidy" type="text/html" width="100%" src="http://codetidy.com/paste/embed/913" frameborder="0"></iframe>
#include "WProgram.h"
#include "rssi.h"
#include <SPI.h>
rssi
::rssi()
{
pinMode
(10, OUTPUT
);
SPI.
begin();
SPI.
setBitOrder(MSBFIRST
); // Send most significant bit first when transferring a byte.
SPI.
setDataMode(SPI_MODE0
); // Base value of clock is 0, data is captured on clock's rising edge.
SPI.
setClockDivider(SPI_CLOCK_DIV8
); // Set SPI data rate to 16mhz/8. IE: 2mhz.
}
// Reset Radio Module
void rssi
::RADIO_Init(void){
// Set Radio output pins
pinMode
(9,OUTPUT
);
pinMode
(8,OUTPUT
);
// Reset the radio
digitalWrite
(8, HIGH
);
digitalWrite
(9, HIGH
);
// Radio module initialization
RADIO_Write
(REG_CLOCK_MANUAL,
0x41);
RADIO_Write
(REG_CLOCK_ENABLE,
0x41);
RADIO_Write
(REG_ANALOG_CTL,
0x44); 
RADIO_Write
(REG_CRYSTAL_ADJ,
0x40);
RADIO_Write
(REG_VCO_CAL,
0xC0);
}
// Read data from radio module
unsigned char rssi
::RADIO_Read(unsigned char address
){
digitalWrite
(8, LOW
); // Enable module 
unsigned char value
;
SPI.
transfer(address
);
value
= SPI.
transfer(0x00);
digitalWrite
(8, HIGH
);// Disable module
return value
;
}
// Write data to radio module
void rssi
::RADIO_Write(unsigned char address,
unsigned char value
){
// Enable module
digitalWrite
(8, LOW
); // Enable module 
// Send data
SPI.
transfer(0x80|address
);
SPI.
transfer(value
);
// Disable module
digitalWrite
(8, HIGH
);
}
// Returns RSSI (0..31) for given channel
unsigned char rssi
::RADIO_RSSI(unsigned char channel
){
byte value
;
// Set channel
RADIO_Write
(REG_CHANNEL,channel
);
// Turn receiver on
RADIO_Write
(REG_CONTROL,
0x80);
// Wait to receiver start-up
// SYNTH_SETTLE (200) + RECEIVER_READY (35) + RSSI_ADC_CONVERSION (50)
delayMicroseconds
(285);
while(1){
// Force conversion
RADIO_Write
(REG_CARRIER_DETECT,
0x00);
RADIO_Write
(REG_CARRIER_DETECT,
0x80);
// RSSI_ADC_CONVERSION (50)
delayMicroseconds
(50);
// Read RSSI
value
=RADIO_Read
(REG_RSSI
);
// Exit if valid
if (value
&0x20) break;
}
// Turn receiver off
RADIO_Write
(REG_CONTROL,
0x00);
// Return lower 4 bits
return (value
&0x0F);
}