public void SetCarrierFrequency(double frequencyInMHz) { Log.Info(string.Format("Setting Carrier Frequency as {0} MHz", frequencyInMHz), 1); // bounds checking for cc1101 hardware limitations if (!(((frequencyInMHz >= 300) && (frequencyInMHz <= 348)) || ((frequencyInMHz >= 387) && (frequencyInMHz <= 464)) || ((frequencyInMHz >= 779) && (frequencyInMHz <= 928)))) { Log.Error("Frequency out of bounds! Use 300-348, 387-464, 779-928MHz only!"); return; } // trying to avoid any floating point issues double secondByteOverflow = frequencyInMHz % 26; double firstByteValue = (double)((frequencyInMHz - secondByteOverflow) / 26); double thirdByteOverflow = (secondByteOverflow * 255) % 26; double secondByteValue = (double)(((secondByteOverflow * 255) - thirdByteOverflow) / 26); double excessOverflow = (thirdByteOverflow * 255) % 26; double thirdByteValue = (double)(((thirdByteOverflow * 255) - excessOverflow) / 26); CCRegister.ConfigurationRegisterValues["FREQ2"] = (byte)firstByteValue; CCRegister.ConfigurationRegisterValues["FREQ1"] = (byte)secondByteValue; CCRegister.ConfigurationRegisterValues["FREQ0"] = (byte)thirdByteValue; SPI.WriteRegister(sp, CCRegister.ConfigurationRegisters["FREQ2"], CCRegister.ConfigurationRegisterValues["FREQ2"]); SPI.WriteRegister(sp, CCRegister.ConfigurationRegisters["FREQ1"], CCRegister.ConfigurationRegisterValues["FREQ1"]); SPI.WriteRegister(sp, CCRegister.ConfigurationRegisters["FREQ0"], CCRegister.ConfigurationRegisterValues["FREQ0"]); ShortWait(); }
// Set the baud rate of the device for tx/rx // in: baudrate in bauds // see http://www.ti.com/lit/ds/symlink/cc1101.pdf , page 35 public void SetBaudRate(double baudRate) { double clockFrequencyMHz = CCRegister.CC1101_CLOCK_FREQUENCY; double baudRateExponent = 0; double baudRateMantissa = 0; for (int tempExponent = 0; tempExponent < 16; tempExponent++) { int tempMantissa = (int)((baudRate * Math.Pow(2, 28) / (Math.Pow(2, tempExponent) * (clockFrequencyMHz * 1000000.0)) - 256) + .5); if (tempMantissa < 256) { baudRateExponent = tempExponent; baudRateMantissa = tempMantissa; break; } } baudRate = (1000000.0 * clockFrequencyMHz * (256 + baudRateMantissa) * Math.Pow(2, baudRateExponent) / Math.Pow(2, 28)); baudRate = Math.Round(baudRate, 4); CCRegister.ConfigurationRegisterValues["MDMCFG4"] = (byte)((CCRegister.ConfigurationRegisterValues["MDMCFG4"] & 0xf0) | (int)baudRateExponent); CCRegister.ConfigurationRegisterValues["MDMCFG3"] = (byte)baudRateMantissa; SPI.WriteRegister(sp, CCRegister.ConfigurationRegisters["MDMCFG4"], CCRegister.ConfigurationRegisterValues["MDMCFG4"]); SPI.WriteRegister(sp, CCRegister.ConfigurationRegisters["MDMCFG3"], CCRegister.ConfigurationRegisterValues["MDMCFG3"]); Log.Info(string.Format("Transmission Baud rate set to {0}. E: {1}, M: {2}", baudRate, baudRateExponent, baudRateMantissa), 1); }
public void SetupRegisters() { Log.Info("Writing Registers", 4); foreach (KeyValuePair <string, byte> configurationRegisterValue in CCRegister.ConfigurationRegisterValues) { SPI.WriteRegister(sp, CCRegister.ConfigurationRegisters[configurationRegisterValue.Key], configurationRegisterValue.Value); } }
public void Transmit(byte[] dataToTransmit) { List <byte> dataToTransmitInverted = new List <byte>(); for (int i = 0; i < dataToTransmit.Length; i++) { dataToTransmitInverted.Add(FlipByte(dataToTransmit[i])); } Log.Info("Writing TXFIFO", 2); SPI.WriteRegister(sp, CCRegister.CC1101_TXFIFO, (byte)dataToTransmit.Length); SPI.WriteBurstRegister(sp, CCRegister.CC1101_TXFIFO, dataToTransmit); EnterTransmitState(); ShortWait(); EnterIdleState(); Log.Success("Transmit Completed"); }