⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 KEIL环境下编译的ADUC7026的DAC实验源码
💻 C
字号:
#ifdef ADUC7020
#include <ADUC7020.H>
#endif
#ifdef ADUC7021
#include <ADUC7021.H>
#endif
#ifdef ADUC7024
#include <ADUC7024.H>
#endif
#ifdef ADUC7026
#include <ADUC7026.H>
#endif

#define CLOCK  22544384   // CPU configured for 22.544384 MHz clock
#define T0_Freq  200       // Timer 0 Reload Frequency
#define T0_LD ((unsigned short )(CLOCK / 16 / T0_Freq))

/*-----------------------------------------------
 Sine Wave Table 
-----------------------------------------------*/
unsigned char sintab [] = {
0x00, 0x01, 0x03, 0x04, 0x06, 0x07, 0x09, 0x0A, 
0x0C, 0x0E, 0x0F, 0x11, 0x12, 0x14, 0x15, 0x17, 
0x18, 0x1A, 0x1C, 0x1D, 0x1F, 0x20, 0x22, 0x23, 
0x25, 0x26, 0x28, 0x29, 0x2B, 0x2C, 0x2E, 0x2F, 
0x30, 0x32, 0x33, 0x35, 0x36, 0x38, 0x39, 0x3A, 
0x3C, 0x3D, 0x3F, 0x40, 0x41, 0x43, 0x44, 0x45, 
0x47, 0x48, 0x49, 0x4A, 0x4C, 0x4D, 0x4E, 0x4F, 
0x51, 0x52, 0x53, 0x54, 0x55, 0x57, 0x58, 0x59, 
0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 
0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 
0x6A, 0x6B, 0x6C, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 
0x70, 0x71, 0x72, 0x73, 0x73, 0x74, 0x75, 0x75, 
0x76, 0x76, 0x77, 0x77, 0x78, 0x79, 0x79, 0x7A, 
0x7A, 0x7A, 0x7B, 0x7B, 0x7C, 0x7C, 0x7C, 0x7D, 
0x7D, 0x7D, 0x7E, 0x7E, 0x7E, 0x7E, 0x7F, 0x7F, 
0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F };


static unsigned char DAC0_next_out;
static unsigned char DAC1_next_out;
static unsigned char DAC2_next_out;
static unsigned char DAC3_next_out;
static unsigned int i = 0x00;

void IRQ_Handler (void) __irq {
  if (IRQSIG & 0x00000004)  {                     /* Timer0 Interrupt          */
  /*-----------------------------------------------
  Output D/A Value 
  -----------------------------------------------*/
  DAC0DAT = DAC0_next_out << 20;                    /* Convert last D/A value    */
  DAC1DAT = DAC1_next_out << 20;
  DAC2DAT = DAC2_next_out << 20;
  DAC3DAT = DAC3_next_out << 20;
  
  /*-----------------------------------------------
  Calculate next D/A Value
  -----------------------------------------------*/
  if (i++ >= 511) i = 0;
 
  if (i >= 384){

   DAC0_next_out = 127 - sintab[127 - (i % 128)];   /* 180 - 270 quadrant        */
   DAC1_next_out = 1023 - 2*i;
   DAC2_next_out = 0;
   DAC3_next_out = i - 255;
   }
  else if (i >= 256){

    DAC0_next_out = 127 - sintab[i % 128];          /* 90 - 180 quadrant         */
    DAC1_next_out = (i - 256) * 2;
    DAC2_next_out = 255;
    DAC3_next_out = i - 255;
    }
  else if (i >= 128) {
    DAC0_next_out = 128 + sintab[127 - (i % 128)];  /* 0 - 90 quadrant           */
    DAC1_next_out = 511 - i * 2;
    DAC2_next_out = 255;
    DAC3_next_out = i;
    }
  else{
    DAC0_next_out = 128 + sintab[i];                /* 270 - 0 quadrant          */
    DAC1_next_out = i * 2;
    DAC2_next_out = 0;
    DAC3_next_out = i;
    }
  T0CLRI = 1;                                     /* Clear Timer 0 interrupt   */
 }
}

void main (void)
{
/*-------------------------------------
Configure the D/A converter:
  normal mode, 0-VDD range,
-------------------------------------*/
DAC0CON = 0x13;
DAC1CON = 0x13;
DAC2CON = 0x13;
DAC3CON = 0x13;


/*-------------------------------------
Initialize Timer 0 Interrupt 
-------------------------------------*/
  IRQEN = 0x00000004;         /* Configure Timer 0                               */
  T0LD  = T0_LD;              /* Timer reload value                              */
  T0CON = 0xC0;               /* Enable Timer 0, Mode: periodic, prescaler = 1   */

while (1){
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -