文章目录
- (1)DA转换
- (2)DAC0832简介
- (3)电路设计
- (4)参考例程
- (5)参考文献
(1)DA转换
单片机作为一个数字电路系统,当需要采集外界模拟量的使用需要进行AD转换,将模拟量转换成数字量,供单片机使用。51单片机需要外部配置一个AD转换芯片来进行模拟量的采集,如我之前写了一篇ADC0832的使用。高级的单片机如STC12和STM32已经集成了AD转换功能,只需简单配置一下,便可以采集到AD转换后的数据。
AD转换(Analog to Digital)是模拟量转换数字量,那么DA转换(Digital to Analog)便是数字量转换成模拟量。
使用DAC0832进行数字到模拟的转换,再使用定时器控制输出波形的频率便可以制作一个函数信号发生器。
(2)DAC0832简介
CS:低电平有效,片选输入线
WR1:低电平大于500ms有效,输入锁存器
WR2:高电平有效,与XFER结合可将输入锁存器的8位数据传输到DAC寄存器中
XFER:低电平有效,数据传输控制输入线
DI0~DI7:8位数据位并行输入,有效时间大于90ns有效
VCC:电源输入5V-15V
VERF:基准电压输入线-10V~10V
RFB:反馈信号输入,可以看成一个反馈监测
IOUT1:电流输出1,其值随着输入线性变化
IOUT2:电流输出2,与OUT1值之和为一个常数
(3)电路设计
单片机最小系统
波形切换控制
(这里由于博主时间有限,例程代码的LED显示功能就没有写了)
(4)参考例程
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define DI P0 //8位数据输入线
//sbit sine_wave_LED1 = P1^0; //正弦波指示灯
//sbit square_wave_LED2 = P1^1; //方波指示灯
//sbit triangular_wave_LED3 = P1^2; //三角波指示灯
//sbit sawtooth_wave_LED4 = P1^3; //锯齿波指示灯
sbit change_wave_Key = P1^4; //更换波形
uint freq = 3906; //定时数值×16^(-6)×256 = 波形频率 例程使用1KHZ
uint number=1,n=0,THHL,i;
//正弦波段码
unsigned code sin[256]={
0x80,0x83,0x85,0x88,0x8A,0x8D,0x8F,0x92,
0x94,0x97,0x99,0x9B,0x9E,0xA0,0xA3,0xA5,
0xA7,0xAA,0xAC,0xAE,0xB1,0xB3,0xB5,0xB7,
0xB9,0xBB,0xBD,0x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5B,0x5D,0x60,0x62,0x65,0x67,0x69,
0x6C,0x6E,0x71,0x73,0x76,0x78,0x7B,0x7D
};
//三角波段码
unsigned code thr[256]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,
0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,
0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,
0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
0xBF,0xBE,0xBD,0xBC,0xBB,0xBA,0xB9,0xB8,
0xB7,0xB6,0xB5,0xB4,0xB3,0xB2,0xB1,0xB0,
0xAF,0xAE,0xAD,0xAC,0xAB,0xAA,0xA9,0xA8,
0xA7,0xA6,0xA5,0xA4,0xA3,0xA2,0xA1,0xA0,
0x9F,0x9E,0x9D,0x9C,0x9B,0x9A,0x99,0x98,
0x97,0x96,0x95,0x94,0x93,0x92,0x91,0x90,
0x8F,0x8E,0x8D,0x8C,0x8B,0x8A,0x89,0x88,
0x87,0x86,0x85,0x84,0x83,0x82,0x81,0x80,
0x7F,0x7E,0x7D,0x7C,0x7B,0x7A,0x79,0x78,
0x77,0x76,0x75,0x74,0x73,0x72,0x71,0x70,
0x6F,0x6E,0x6D,0x6C,0x6B,0x6A,0x69,0x68,
0x67,0x66,0x65,0x64,0x63,0x62,0x61,0x60,
0x5F,0x5E,0x5D,0x5C,0x5B,0x5A,0x59,0x58,
0x57,0x56,0x55,0x54,0x53,0x52,0x51,0x50,
0x4F,0x4E,0x4D,0x4C,0x4B,0x4A,0x49,0x48,
0x47,0x46,0x45,0x44,0x43,0x42,0x41,0x40,
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,
0x48,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,
0x58,0x59,0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F
};
//方波段码
unsigned code squ[256]={
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};
//10ms延时函数
void delay_10ms(uchar i)
{
//毫秒级延时函数
//在12MHZ下晶振下,大约延时10ms/次
uchar a,b,c;
for(c = i; c > 0; --c)
for(b = 129; b > 0; b--)
for(a = 38; a > 0; a--);
}
//定时器0初始化
void Timer0Init(void)
{
unsigned int THHL;
TMOD &= 0xF0; //定时器1失效
TMOD |= 0x01; //定时器0,16位计数
THHL=65536-freq;
TL0 =THHL%256; //设置定时初值
TH0 =THHL/256; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1; //开定时器0中断
EA=1; //开总中断
PT0=0; //定时器0中断最优
}
void main()
{
/*指示灯初始化,全部熄灭*/
// triangular_wave_LED3 = 1;
// square_wave_LED2 = 1;
// sine_wave_LED1 = 1;
Timer0Init();//初始化定时器0
while(1)
{
if(change_wave_Key == 0)
{
delay_10ms(1);//延时消抖
if(change_wave_Key == 0)
{
while(!change_wave_Key);//松手检测
number++;
if(number == 5)
{
number = 0;
}
}
}
switch(number)
{
case 1: {DI=sin[n]; break;}//正弦波
case 2: {DI=thr[n]; break;}//三角波
case 3: {for(i=0;i<256;i++){DI=squ[i];} break;}//方波
case 4: {DI=256-n; break;}//锯齿波
default:{break;}
}
}
}
//定时器0中断处理函数
void Timer0_Routine() interrupt 1
{
THHL=65536-freq;
TL0=THHL%256;
TH0=THHL/256;
if(n>=255)
{n=0;}
else
{n++;}
}
电路仿真及代码例程
链接:https://share.weiyun.com/ALLrnKTu 密码:cchvph
(5)参考文献
基于51单片机的DAC0832波形发生器
由于作者水平有限,如有错误望指摘。