文章目录
- DAC转换输出波形
- 使用DA转换输出正弦波,三角波,锯齿波
- (1)仿真电路图
- (2)源代码
- (3)实验结果
DAC转换输出波形
使用DA转换输出正弦波,三角波,锯齿波
(1)仿真电路图
(2)源代码
absacc.h
‘‘absacc.h’’ 文件通常是keil C51编译器的一个头文件,用于访问特殊功能寄存器和实现对底层硬件的底层控制。这个头文件为底层寄存器提供了直接访问,允许程序员对特殊功能进行底层的控制。其中包含特殊的宏定义和内联汇编,用于直接操作芯片的寄存器。
#ifndef __ABSACC_H__
#define __ABSACC_H__
#define CBYTE ((unsigned char volatile code *) 0)
#define DBYTE ((unsigned char volatile data *) 0)
#if !defined (__CX2__)
#define PBYTE ((unsigned char volatile pdata *) 0)
#endif
#define XBYTE ((unsigned char volatile xdata *) 0)
#define CWORD ((unsigned int volatile code *) 0)
#define DWORD ((unsigned int volatile data *) 0)
#if !defined (__CX2__)
#define PWORD ((unsigned int volatile pdata *) 0)
#endif
#define XWORD ((unsigned int volatile xdata *) 0)
#if defined (__CX51__) || defined (__CX2__)
#define FVAR(object, addr) (*((object volatile far *) (addr)))
#define FARRAY(object, base) ((object volatile far *) (base))
#define FCVAR(object, addr) (*((object const far *) (addr)))
#define FCARRAY(object, base) ((object const far *) (base))
#else
#define FVAR(object, addr) (*((object volatile far *) ((addr)+0x10000L)))
#define FCVAR(object, addr) (*((object const far *) ((addr)+0x810000L)))
#define FARRAY(object, base) ((object volatile far *) ((base)+0x10000L))
#define FCARRAY(object, base) ((object const far *) ((base)+0x810000L))
#endif
#if defined (__CX2__)
#define HBYTE ((unsigned char volatile huge *) 0)
#define HWORD ((unsigned int volatile huge *) 0)
#define HVAR(object, addr) (*((object volatile huge *) (addr)))
#define HARRAY(object, base) ((object volatile huge *) (base))
#endif
#define CVAR(object, addr) (*((object volatile code *) (addr)))
#define CARRAY(object, base) ((object volatile code *) (base))
#define DVAR(object, addr) (*((object volatile data *) (addr)))
#define DARRAY(object, base) ((object volatile data *) (base))
#define XVAR(object, addr) (*((object volatile xdata *) (addr)))
#define XARRAY(object, base) ((object volatile xdata *) (base))
#endif
math.h
#ifndef __MATH_H__
#define __MATH_H__
#if defined __CX2__ && (__CX2__ >= 558 || __CX2__ == 556 && __CX2_MINOR__ >= 207)
#ifndef HUGE_VAL
#define HUGE_VAL __inf__
#endif // HUGE_VAL
#ifndef NAN
#define NAN __nan__
#endif // NAN
#pragma SAVE
#pragma FUNCTIONS(STATIC)
/* intrinsic functions are reentrant, but need static attribute */
extern int abs (int val);
#pragma RESTORE
#endif
#pragma SAVE
#pragma REGPARMS
#if !defined (__CX2__)
extern char cabs (char val);
extern int abs (int val);
extern long labs (long val);
#endif
extern float fabs (float val);
extern float sqrt (float val);
extern float exp (float val);
extern float log (float val);
extern float log10 (float val);
extern float sin (float val);
extern float cos (float val);
extern float tan (float val);
extern float asin (float val);
extern float acos (float val);
extern float atan (float val);
extern float sinh (float val);
extern float cosh (float val);
extern float tanh (float val);
extern float atan2 (float y, float x);
extern float ceil (float val);
extern float floor (float val);
extern float modf (float val, float *n);
extern float fmod (float x, float y);
extern float pow (float x, float y);
#if defined (__CX2__)
extern float frexp (float val, int *exp);
extern float ldexp (float val, int exp);
#endif
#pragma RESTORE
#endif
main.c
#include<reg51.h>
#include<absacc.h>
#include<math.h>
#define PI 3.1415926
sbit CS=P2^7;
sbit WR12=P3^6;
//正弦波函数
void sine_wave_da(void)
{
unsigned int i;
for(i=0;i<255;i++)
{
XBYTE[0x7fff] = 127+127*(sin(2*PI*i/127));
}
}
//三角波函数
void triangular_wave_da(void)
{
unsigned int i;
for(i=0;i<255;i++)
XBYTE[0x7fff] = i;
for(i = 255;i > 0; i--)
XBYTE[0x7fff] = i;
}
//锯齿波函数
void sawtooth_wave_da(void)
{
unsigned int i;
for(i=0;i<255;i++)
XBYTE[0x7fff] = i;
}
void main()
{
CS=0;
WR12=0;
while(1)
{
//输出正弦波
sine_wave_da();
//输出三角波
triangular_wave_da();
//输出锯齿波
sawtooth_wave_da();
}
}
(3)实验结果
正弦波仿真结果
三角波仿真结果
锯齿波仿真结果