// AD9740控制寄存器定义 #define AD9740_MODE_REG 0x00 // 模式控制寄存器 #define AD9740_IOUT_REG 0x01 // 输出电流控制 #define AD9740_PLL_REG 0x02 // PLL配置寄存器 // DAC初始化配置 void ad9740_init(void) { // 配置输出电流为20mA spi_write(AD9740_IOUT_REG, 0xFF); // 使能8倍插值模式 spi_write(AD9740_MODE_REG, 0x80); // 配置PLL倍频比 spi_write(AD9740_PLL_REG, 0x12); // 等待PLL锁定 while(!GPIO_ReadInputDataBit(PLL_LOCK_PORT, PLL_LOCK_PIN)); } // 正弦波生成(DDS算法) void generate_sine_wave(uint32_t frequency) { const uint16_t SINE_LUT[4096]; // 正弦波查找表 uint32_t phase_acc = 0; uint32_t phase_inc = (frequency * 4096) / DAC_SAMPLE_RATE; while(1) { // 输出正弦波采样点 uint16_t dac_code = SINE_LUT[phase_acc >> 20]; output_dac_data(dac_code); // 相位累加 phase_acc += phase_inc; // 等待DAC时钟 wait_dac_clock(); } }