1436|1

1237

帖子

66

TA的资源

纯净的硅(中级)

楼主
 

【先楫HPM6750EVK2测评】硬件spi驱动ad9833波形发生器芯片 [复制链接]

  本帖最后由 dql2016 于 2023-5-14 22:21 编辑

例程提供了SPI主机轮询的参考代码,根据例程修改,能够十分简单的将1SPI接口应用起来,这里驱动常见的SPI接口DDS芯片AD9833代码如下:

/*
 * Copyright (c) 2021 HPMicro
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#include "board.h"
#include "hpm_debug_console.h"
#include "hpm_spi_drv.h"
#include "hpm_clock_drv.h"

#define FMCLK 25000000        // Master Clock On AD9833
enum WaveType{SIN, SQR, TRI}; // Wave Selection Enum
uint16_t FRQLW = 0;    // MSB of Frequency Tuning Word
uint16_t FRQHW = 0;    // LSB of Frequency Tuning Word
uint32_t  phaseVal=0;  // Phase Tuning Value
uint8_t WKNOWN=0;      // Flag Variable
// -------------------------------- Functions --------------------------------

// ------------------------------------------------ Software SPI Function
void writeSPI(uint16_t word) 
{
    hpm_stat_t stat = spi_write_data(BOARD_APP_SPI_BASE, 2, &word, 2);
    if (stat == status_success) 
    {
        printf("SPI-Master transfer ends.\n");
    } 
    else 
    {
        printf("SPI-Master transfer error[%d]!\n", stat);
    }
}

// ------------------------------------------------ Sets Output Wave Type
void AD9833_SetWave(uint16_t Wave)
{
  switch(Wave){
  case 0:
    writeSPI(0x2000); // Value for Sinusoidal Wave
    WKNOWN=0;
    break;
  case 1:
    writeSPI(0x2028); // Value for Square Wave
    WKNOWN=1;
    break;
  case 2:
    writeSPI(0x2002); // Value for Triangle Wave
    WKNOWN=2;
    break;
  default:
    break;
  }
}

// ------------------------------------------------ Sets Wave Frequency & Phase (In Degree) In PHASE0 & FREQ0 Registers
void AD9833_SetWaveData(float Frequency,float Phase)
{
  // ---------- Tuning Word for Phase ( 0 - 360 Degree )
  if(Phase<0)Phase=0; // Changing Phase Value to Positive
  if(Phase>360)Phase=360; // Maximum value For Phase (In Degree)
  phaseVal  = ((int)(Phase*(4096/360)))|0XC000;  // 4096/360 = 11.37 change per Degree for Register And using 0xC000 which is Phase 0 Register Address
  // ---------- Tuning word for Frequency      
  long freq=0;
  freq=(int)(((Frequency*pow(2,28))/FMCLK)+1); // Tuning Word
  FRQHW=(int)((freq & 0xFFFC000) >> 14); // FREQ MSB
  FRQLW=(int)(freq & 0x3FFF);  // FREQ LSB 
  FRQLW |= 0x4000;
  FRQHW |= 0x4000; 
  // ------------------------------------------------ Writing DATA
  writeSPI(0x2100); // enable 16bit words and set reset bit
  writeSPI(FRQLW);
  writeSPI(FRQHW);
  writeSPI(phaseVal);
  writeSPI(0x2000); // clear reset bit 
  AD9833_SetWave(WKNOWN);
  return;
}

// ------------------------------------------------ Initializing AD9833
void AD9833_Init(uint16_t WaveType,float FRQ,float Phase)
{
  AD9833_SetWave(WaveType);                              // Type Of Wave 
  AD9833_SetWaveData(FRQ,Phase);                         // Frequency & Phase Set
  return;
}

int main(void)
{
    uint8_t wbuff[10] = {0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9};
    uint8_t rbuff[10] = {0};
    spi_timing_config_t timing_config = {0};
    spi_format_config_t format_config = {0};
    spi_control_config_t control_config = {0};
    hpm_stat_t stat;

    /* bsp initialization */
    board_init();
    board_init_spi_clock(BOARD_APP_SPI_BASE);
    board_init_spi_pins(BOARD_APP_SPI_BASE);
    printf("SPI-Master Polling Transfer Example\n");

    /* set SPI sclk frequency for master */
    spi_master_get_default_timing_config(&timing_config);
    timing_config.master_config.clk_src_freq_in_hz = clock_get_frequency(BOARD_APP_SPI_CLK_NAME);;
    timing_config.master_config.sclk_freq_in_hz = BOARD_APP_SPI_SCLK_FREQ;
    spi_master_timing_init(BOARD_APP_SPI_BASE, &timing_config);
    printf("SPI-Master transfer timing is configured.\n");
    printf("SPI-Master transfer source clock frequency: %dHz\n", timing_config.master_config.clk_src_freq_in_hz);
    printf("SPI-Master transfer sclk frequency: %dHz\n", timing_config.master_config.sclk_freq_in_hz);

    /* set SPI format config for master */
    spi_master_get_default_format_config(&format_config);
    format_config.common_config.data_len_in_bits = BOARD_APP_SPI_DATA_LEN_IN_BITS;
    format_config.common_config.mode = spi_master_mode;
    format_config.common_config.cpol = spi_sclk_high_idle;
    format_config.common_config.cpha = spi_sclk_sampling_even_clk_edges;
    spi_format_init(BOARD_APP_SPI_BASE, &format_config);
    printf("SPI-Master transfer format is configured.\n");

    /* set SPI control config for master */
    spi_master_get_default_control_config(&control_config);
    control_config.master_config.cmd_enable = false;  /* cmd phase control for master */
    control_config.master_config.addr_enable = false; /* address phase control for master */
    control_config.common_config.trans_mode = spi_trans_write_read_together;

    printf("SPI-Master transfer starts.\n");
    AD9833_Init(SIN,10000,0);

    while(1) 
    {
    }
    return 0;
}

 

最新回复

先把楼主分享的驱动常见的SPI接口DDS芯片AD9833代码收藏再说   详情 回复 发表于 2023-5-15 07:31
点赞 关注
 
 

回复
举报

6587

帖子

0

TA的资源

五彩晶圆(高级)

沙发
 

先把楼主分享的驱动常见的SPI接口DDS芯片AD9833代码收藏再说

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表