使用DAC0832作为信号发生器输出四种波形:方波、三角波、锯齿波、梯形波。
基本思路:
第一个按钮选择波形:方波、三角波、锯齿波、梯形波;
第二个按钮增加幅值;
第三个按钮减小幅值。仿真电路图:
示波器效果如下:
方波:
三角波:
锯齿波:
梯形波:
主程序<chengxu.c>:- #include "reg52.h"
- #include "init.h" //DAC0832初始化
- #include "single.h" //DAC0832用到的函数
- #include "delay.h" //延时
- #include "Key.h" //按键
- #define uchar unsigned char
- #define uint unsigned int
- int main(void)
- {
- uchar Model=0; //0-方波 1-三角波 2-锯齿波 3-梯形波
- uint Count=0; //计数器
- uint Squ_Per=256;
- uint Tri_Per=256;
- uint Saw_Per=256;
- uint Sin_Per=256;
- init();
- while(1)
- {
- while(Model==0) //方波
- {
- Square_wave(Squ_Per,&Count);
- Count+=4;
- Squ_Per=Key_Plus(Squ_Per);
- Squ_Per=Key_Subc(Squ_Per);
- Model=Key_Model(Model,&Squ_Per,&Count); //每次退出当前while时记得复原Period和Count的数据
- }
- while(Model==1) //三角波
- {
- Triangle_wave(Tri_Per,&Count);
- Count+=4;
- Tri_Per=Key_Plus(Tri_Per);
- Tri_Per=Key_Subc(Tri_Per);
- Model=Key_Model(Model,&Tri_Per,&Count);
- }
- while(Model==2) //锯齿波
- {
- Sawtooth_wave(Saw_Per,&Count);
- Count+=4;
- Saw_Per=Key_Plus(Saw_Per);
- Saw_Per=Key_Subc(Saw_Per);
- Model=Key_Model(Model,&Saw_Per,&Count);
- }
- while(Model==3) //波
- {
- Sin_wave(Sin_Per,&Count);
- Count+=4;
- Sin_Per=Key_Plus(Sin_Per);
- Sin_Per=Key_Subc(Sin_Per);
- Model=Key_Model(Model,&Sin_Per,&Count);
- }
- }
- return 0;
- }
复制代码 延时<delay.c>:- void delay(unsigned int r)
- {
- unsigned int i,j;
- for(i=r;i>0;i--)
- for(j=110;j>0;j--);
- }
复制代码 延时头文件<delay.h>:- #ifndef __DELAY_H__
- #define __DELAY_H__
- #include <intrins.h>
- #define NOP() _nop_()
- void delay(unsigned int r);
- #endif
复制代码 芯片初始化<init.c>:
- #include "reg52.h"
- sbit CS_DAC=P1^5; //DAC0832的片选端口
- sbit WR_DAC=P1^6; //DAC0832的数据写入端口
- extern void init(void)
- {
- P0=0xff;
- P1=0xff;
- P2=0xff;
- P3=0xff;
- CS_DAC=0;//一直片选中DAC0832,低电平有效啊~
- WR_DAC=0;//一直写入数据到DAC0832
- }
复制代码 芯片初始化头文件<init.h>:
- #ifndef __INIT_H__
- #define __INIT_H__
- extern void init(void);
- #endif
复制代码 按键识别<key.c>:
- #include "reg52.h"
- #include "Key.h"
- #include "delay.h"
- sbit key1=P3^2; //波形选择按键
- sbit key2=P3^3; //幅值增加按键
- sbit key3=P3^4; //幅值减少按键
- //波形选择
- unsigned char Key_Model(unsigned char Model,unsigned int *Pre,unsigned int *Count)
- {
- if(key1==0)
- {
- delay(10);
- if(key1==0)
- {
- Model=Model+1;
- *Pre=256;
- *Count=0;
- }
- }
- while(key1==0);
- if(Model>3)
- {
- Model=0;
- }
- return Model;
- }
- //幅值增加
- unsigned int Key_Plus(unsigned int Per)
- {
- if(key2==0)
- {
- delay(10);
- if(key2==0)
- {
- Per=Per+8;
- }
- }
- while(key2==0);
- if(Per>256)
- {
- Per=0;
- }
- return Per;
- }
- //幅值减少
- unsigned int Key_Subc(unsigned int Per)
- {
- if(key3==0)
- {
- delay(10);
- if(key3==0)
- {
- Per=Per-8;
- }
- }
- while(key3==0);
- if(Per<0)
- {
- Per=256;
- }
- return Per;
- }
复制代码 按键识别头文件<key.h>:
- #ifndef __KEY_H__
- #define __KEY_H__
- unsigned char Key_Model(unsigned char Model,unsigned int *Pre,unsigned int *Count);
- unsigned int Key_Plus(unsigned int Per);
- unsigned int Key_Subc(unsigned int Per);
- #endif
复制代码 绘制波形函数<single.c>:
- #include "reg52.h"
- #include "single.h"
- #include "delay.h"
- #define DATA P0
- //方波
- void Square_wave(unsigned int Per,unsigned int *Count)
- {
- if(*Count>=Per) *Count=0;
- if(*Count<Per/2)
- {
- DATA=0x00;
- }
- else
- {
- DATA=0xFF;
- }
- }
- //三角波
- void Triangle_wave(unsigned int Per,unsigned int *Count)
- {
- if(*Count>=Per) *Count=0;
- if(*Count<Per/2)
- {
- DATA=*Count;
- }
- else
- {
- DATA=Per-*Count;
- }
- }
- //锯齿波
- void Sawtooth_wave(unsigned int Per,unsigned int *Count)
- {
- if(*Count>=Per) *Count=0;
- if(*Count<Per)
- {
- DATA=*Count;
- }
- }
- //波
- void Sin_wave(unsigned int Per,unsigned int *Count)
- {
- if(*Count>Per) *Count=0;
- if(*Count<Per/2)
- {
- DATA=*Count;
- }
- else if(*Count==Per/2)
- {
- delay(100);
- }
- else if(*Count<Per)
- {
- DATA=Per-*Count;
- }
- else if(*Count==Per)
- {
- delay(100);
- }
- }
复制代码 绘制波形函数头文件<single.h>:- #ifndef __SINGLE_H__
- #define __SINGLE_H__
- void Square_wave(unsigned int Per,unsigned int *Count);
- void Triangle_wave(unsigned int Per,unsigned int *Count);
- void Sawtooth_wave(unsigned int Per,unsigned int *Count);
- void Sin_wave(unsigned int Per,unsigned int *Count);
- #endif
复制代码
资料51hei下载地址(Proteus仿真+代码):
利用模数转换DAC0832输出各种波形.7z
(578.99 KB, 下载次数: 357)
|