|
使用DAC0832作為信號(hào)發(fā)生器輸出四種波形:方波、三角波、鋸齒波、梯形波。
基本思路:
第一個(gè)按鈕選擇波形:方波、三角波、鋸齒波、梯形波;
第二個(gè)按鈕增加幅值;
第三個(gè)按鈕減小幅值。仿真電路圖:
51hei.gif (320.18 KB, 下載次數(shù): 42)
下載附件
2022-4-20 17:49 上傳
示波器效果如下:
方波:
方波.png (64.96 KB, 下載次數(shù): 99)
下載附件
2019-1-28 14:30 上傳
三角波:
三角波.png (67.05 KB, 下載次數(shù): 88)
下載附件
2019-1-28 14:30 上傳
鋸齒波:
鋸齒波.png (68.03 KB, 下載次數(shù): 87)
下載附件
2019-1-28 14:30 上傳
梯形波:
正弦波.png (64.84 KB, 下載次數(shù): 113)
下載附件
2019-1-28 14:30 上傳
主程序<chengxu.c>:- #include "reg52.h"
- #include "init.h" //DAC0832初始化
- #include "single.h" //DAC0832用到的函數(shù)
- #include "delay.h" //延時(shí)
- #include "Key.h" //按鍵
- #define uchar unsigned char
- #define uint unsigned int
- int main(void)
- {
- uchar Model=0; //0-方波 1-三角波 2-鋸齒波 3-梯形波
- uint Count=0; //計(jì)數(shù)器
- 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); //每次退出當(dāng)前while時(shí)記得復(fù)原Period和Count的數(shù)據(jù)
- }
- 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;
- }
復(fù)制代碼 延時(shí)<delay.c>:- void delay(unsigned int r)
- {
- unsigned int i,j;
- for(i=r;i>0;i--)
- for(j=110;j>0;j--);
- }
復(fù)制代碼 延時(shí)頭文件<delay.h>:- #ifndef __DELAY_H__
- #define __DELAY_H__
- #include <intrins.h>
- #define NOP() _nop_()
- void delay(unsigned int r);
- #endif
復(fù)制代碼 芯片初始化<init.c>:
- #include "reg52.h"
- sbit CS_DAC=P1^5; //DAC0832的片選端口
- sbit WR_DAC=P1^6; //DAC0832的數(shù)據(jù)寫(xiě)入端口
- extern void init(void)
- {
- P0=0xff;
- P1=0xff;
- P2=0xff;
- P3=0xff;
- CS_DAC=0;//一直片選中DAC0832,低電平有效啊~
- WR_DAC=0;//一直寫(xiě)入數(shù)據(jù)到DAC0832
- }
復(fù)制代碼 芯片初始化頭文件<init.h>:
- #ifndef __INIT_H__
- #define __INIT_H__
- extern void init(void);
- #endif
復(fù)制代碼 按鍵識(shí)別<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;
- }
復(fù)制代碼 按鍵識(shí)別頭文件<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
復(fù)制代碼 繪制波形函數(shù)<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);
- }
- }
復(fù)制代碼 繪制波形函數(shù)頭文件<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
復(fù)制代碼
資料51hei下載地址(Proteus仿真+代碼):
利用模數(shù)轉(zhuǎn)換DAC0832輸出各種波形.7z
(578.99 KB, 下載次數(shù): 359)
2022-4-20 17:49 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|