usrp b210如何用pps设置同步,c代码
时间: 2024-05-08 16:15:42 浏览: 177
fpga-b210-verilog.zip_b210_usrp_usrp b210 fpga_usrp fpga_usrp fp
5星 · 资源好评率100%
以下是使用C代码设置USRP B210同步的步骤:
1.包含必要的头文件:
```
#include <uhd/usrp/multi_usrp.hpp>
#include <uhd/utils/thread.hpp>
#include <uhd/utils/safe_main.hpp>
#include <boost/program_options.hpp>
#include <boost/format.hpp>
#include <iostream>
#include <csignal>
#include <cmath>
```
2.定义全局变量:
```
uhd::usrp::multi_usrp::sptr usrp;
uhd::time_spec_t start_time;
```
3.定义设置同步的函数:
```
void setup_sync()
{
std::cout << boost::format("Setting up synchronization with PPS...\n");
// Lock mboard clocks
usrp->set_clock_source("external");
usrp->set_time_source("external");
// Set time to zero (actually the time of the next PPS edge)
usrp->set_time_now(uhd::time_spec_t(0.0));
// Wait for PPS edge
while (true) {
if (usrp->get_time_last_pps().get_real_secs() > 0.0) {
break;
}
uhd::this_thread::sleep_for(uhd::time_spec_t(0.1));
}
// Get time of PPS edge
start_time = usrp->get_time_last_pps();
std::cout << boost::format("Synchronization successful!\n");
}
```
4.定义主函数:
```
int UHD_SAFE_MAIN(int argc, char* argv[])
{
// Set up program options
namespace po = boost::program_options;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("freq", po::value<double>()->default_value(100e6), "set center frequency in Hz")
("gain", po::value<double>()->default_value(0), "set gain in dB")
("ppstime", "set up synchronization with PPS")
;
// Parse program options
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << std::endl;
return 0;
}
double freq = vm["freq"].as<double>();
double gain = vm["gain"].as<double>();
bool use_pps = vm.count("ppstime") > 0;
// Create USRP object
usrp = uhd::usrp::multi_usrp::make("");
usrp->set_rx_subdev_spec("A:A", 0);
usrp->set_rx_rate(1e6);
usrp->set_rx_freq(freq);
usrp->set_rx_gain(gain);
// Set up synchronization with PPS
if (use_pps) {
setup_sync();
}
// Start receiving samples
uhd::stream_args_t stream_args("fc32");
uhd::rx_streamer::sptr rx_stream = usrp->get_rx_stream(stream_args);
uhd::rx_metadata_t rx_md;
std::vector<std::complex<float>> buff(1000);
int num_rx_samples = rx_stream->recv(&buff[0], buff.size(), rx_md);
return 0;
}
```
在主函数中,首先解析命令行参数,然后创建USRP对象并设置接收参数。如果使用PPS同步,则调用setup_sync()函数进行同步设置。最后,启动接收并获取接收到的样本。
阅读全文