This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

about D/A C

/*******************************************************************/
#include "exp1_2407.h"
#include "math.h"
#define pi 3.1415926
/*******************************************************************/

unsigned int data=0;
unsigned int curve[128];
unsigned int curve1[128];
unsigned int curve2[128];

void sys_ini()                   //系统初始化子程序
{
     asm(" setc INTM");         //禁止所有中断
     asm(" setc SXM");          //抑制符号位扩展
     asm(" clrc OVM");          //累加器中结果正常溢出
     WSGR=0x0049;               //io、ram、program都设为0等待读写
     * WDCR=0x00E8;             //禁止看门狗
     * SCSR1=0x0021;            //CLKIN=7.3728M,CLKOUT=29.4912M,使能spi外围模块
     * MCRB=0xFE3F;             //使能spi引脚
     * IFR=0xFFFF;              //清所有中断标志位
}  

void spi_ini()
{
     * SPICCR&=0x007F;          //复位spi
     * SPICCR=0x004F;           //CLOCK POLARITY=1,16位数据格式
     * SPICTL=0x000E;           //禁止溢出中断,CLOCK PHASE=1,禁用spi中断,主模式,使能数据发送
     * SPISTS=0x0080;           //清除接收溢出中断标志
     * SPIBRR=0x0000;           //波特率为29.4912/4=7.3728M
     * SPICCR|=0x0080;          //启动spi工作
}

void delay()
{
     unsigned int k;
     for(k=0;k<5;k++);
}

interrupt void nothing()        //哑中断子程序
{
     return;         
}
 
void main()
{
  int i,p,data;
  sys_ini(); 
  spi_ini();

for(i=0; i<128;i++) 
{
  data=(int)(511.5*(1+sin(1+sin(2*pi*i/127)));
  curve[i]=data;                 //的值换算到
  curve1[i]=data<<2|0xC000;      //tlv5617的da值范围
  curve2[i]=data<<2|0x5000;
}

  for(;;)
  {
    for(p=0;p<128;p++)
      {
        * SPITXBUF=curve2[p];    //将数据写入tlv5617的buffer
        delay();
        * SPITXBUF=curve1[p];    //将数据写入tlv5617的OUTA,同时用buffer值更新OUTB的值。
        delay();
      }
  }
}

 

this is a source procedure ,output waveform is Sine wave. How to change procedure so that output waveform is Triangular wave.

  • Try this:

    unsigned int triangle[128];  // Add another array at the top of your code

    for (i=0; i<128;i++)
    {
    data=(int)(511.5*(1+sin(1+sin(2*pi*i/127))));
    curve[i]=data; //?????
    curve1[i]=data<<2|0xC000; //tlv5617?da???
    curve2[i]=data<<2|0x5000;
    if (i<= 64)  // or define a 'half period'
    (triangle[i] = 511.5 * i);
    else
    (triangle[i] = 511.5 * (128-i));  // decrement your index by the max elements after passing the peak value
    }