First, paste the simulated circuit diagram below (the simulation software is Protuse, upload a larger diagram so that everyone can see it clearly):
Original parts list: STC89C52 microcontroller . Simulation requires these original parts. The specific hardware design is finalized and is being produced~
Upload the C program below~ (The IDE environment used is Keil 4, and the language is C language)
Source file (.c file):
1main.c file:
1 #include “reg52.h”
2 #include "init.h"
3 #include "single.h"
4 #include "delay.h"
5 #include "Key.h"
6 int main(void)
7 {
8 unsigned char Model=0;//0-square wave 1-triangular wave 2-sawtooth wave 3-sine wave
9 unsigned int Count=0;//Counter
10 unsigned int Squ_Per=256;
11 unsigned int Tri_Per=256;
12 unsigned int Saw_Per=256;
13 unsigned int Sin_Per=256;
14 init();
15 while(1)
16 {
17 while(Model==0)
18 {
19 Square_wave(Squ_Per,&Count);
20 Count+=4;
21 Squ_Per=Key_Plus(Squ_Per);
22 Squ_Per=Key_Subc(Squ_Per);
23 Model=Key_Model(Model, &Squ_Per, &Count);//Remember to restore the Period and Count data each time you exit the current while
twenty four }
25 while(Model==1)
26 {
27 Triangle_wave(Tri_Per,&Count);
28 Count+=4;
29 Tri_Per=Key_Plus(Tri_Per);
30 Tri_Per=Key_Subc(Tri_Per);
31 Model=Key_Model(Model,&Tri_Per,&Count);
32}
33 while(Model==2)
34 {
35 Sawtooth_wave(Saw_Per,&Count);
36 Count+=4;
37 Saw_Per=Key_Plus(Saw_Per);
38 Saw_Per=Key_Subc(Saw_Per);
39 Model=Key_Model(Model,&Saw_Per,&Count);
40}
41 while(Model==3)
42 {
43 Sin_wave(Sin_Per,&Count);
44 Count+=4;
45 Sin_Per=Key_Plus(Sin_Per);
46 Sin_Per=Key_Subc(Sin_Per);
47 Model=Key_Model(Model, &Sin_Per, &Count);
48 }
49 }
50 return 0;
51 }
2init.c file:
1 #include “reg52.h”
2 sbit CS_DAC=P1^5;//Chip select port of DAC0832
3 sbit WR_DAC=P1^6; //Data write port of DAC0832
4 extern void init(void)
5 {
6 P0=0xff;
7 P1=0xff;
8 P2=0xff;
9 P3=0xff;
10 CS_DAC=0;//The chip selects DAC0832, low level is active~
11 WR_DAC=0;//Write data to DAC0832
12}
3single.c file
1 #include “reg52.h”
2 #include "single.h"
3 #include "delay.h"
4 #define DATA P0
5 void Square_wave(unsigned int Per, unsigned int *Count)
6 {
7 if(*Count》=Per) *Count=0;
8 if(*Count
9 {
10 DATA=0x00;
11 }
12 else
13 {
14 DATA=0xFF;
15}
16}
17 void Triangle_wave(unsigned int Per, unsigned int *Count)
18 {
19 if(*Count》=Per) *Count=0;
20 if(*Count
twenty one {
22 DATA=*Count;
twenty three }
24 else
25 {
26 DATA=Per-*Count;
27}
28 }
29 void Sawtooth_wave(unsigned int Per, unsigned int *Count)
30 {
31 if(*Count》=Per) *Count=0;
32 if(*Count
33 {
34 DATA=*Count;
35}
36}
37 void Sin_wave(unsigned int Per, unsigned int *Count)
38 {
39 if(*Count》Per) *Count=0;
40 if(*Count
41 {
42 DATA=*Count;
43}
44 else if(*Count==Per/2)
45 {
46 delay(100);
47 }
48 else if(*Count
49 {
50 DATA=Per-*Count;
51 }
52 else if(*Count==Per)
53 {
54 delay(100);
55 }
56 }
4Key.c file:
1 #include "Key.h"
2 #include "delay.h"
3 sbit key2=P3^3; //wave Change
4 sbit key3=P3^4; //Fre plus
5 sbit key4=P3^5; //Fre subc
6 unsigned char Key_Model (unsigned char Model, unsigned int *Pre, unsigned int *Count)
7 {
8 if(key2==0)
9 {
10 delay(10);
11 if(key2==0)
12 {
13 Model=Model+1;
14 *Pre=256;
15 *Count=0;
16}
17}
18 while(key2==0);
19 if(Model》3)
20 {
21 Model=0;
twenty two }
23 return Model;
twenty four }
25 unsigned int Key_Plus (unsigned int Per)
26 {
27 if(key3==0)
28 {
29 delay(10);
30 if(key3==0)
31 {
32Per=Per+8;
33}
34}
35 while(key3==0);
36 if (Per》256)
37 {
38Per=0;
39 }
40 returnPer;
41 }
42 unsigned int Key_Subc (unsigned int Per)
43 {
44 if(key4==0)
45 {
46 delay(10);
47 if(key4==0)
48 {
49Per=Per-8;
50 }
51 }
52 while(key4==0);
53 if(Per《0)
54 {
55Per=256;
56 }
57 returnPer;
58 }
5delay.c file:
1 void delay(unsigned int r)
2 {
3 unsigned int i,j;
4 for(i=r;i》0;i--)
5 for(j=110;j》0;j--);
6}
Header file (.h file):
1init.h file:
1 extern void init(void);
2single.h file:
1 void Square_wave(unsigned int Per, unsigned int *Count);
2 void Triangle_wave(unsigned int Per, unsigned int *Count);
3 void Sawtooth_wave(unsigned int Per, unsigned int *Count);
4 void Sin_wave(unsigned int Per, unsigned int *Count);
3Key.h file:
1 #include “reg52.h”
2 unsigned char Key_Model(unsigned char Model, unsigned int *Pre, unsigned int *Count);
3 unsigned int Key_Plus(unsigned int Per);
4 unsigned int Key_Subc(unsigned int Per);
4delay.h file:
1 #include
2 void delay(unsigned int r);
3 #define NOP() _nop_()
I have uploaded all the project files used~ Let’s take a look at the simulation results: (You can perform filtering when designing the circuit, and then amplify the signal. In this case, the effect may be better~)
Square wave: Triangular wave: Sawtooth wave: Trapezoidal wave:
Please note that there is a small problem with the circuit. Due to my mistake, the output interfaces of Iout1 and Iout2 of DAC0832 should be connected as follows:
The areas that need to be modified are:
1. Change the power supply to dual power supply
2. The Iout interface needs to be grounded
After the modification is completed, the result will be more perfect. Upload a triangle wave waveform below.
Previous article:STC89C52RC serial port baud rate program
Next article:Design plan for communication between AT89C52 microcontroller and SD card
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- The chip I use is STC12C5410AD. Can you guys see if it is correct?
- To our members and visitors!!
- Cost-effective EMI pre-compliance test system for automotive electronics
- STM32 Learning Tour ① Development Environment Construction
- Several Notes on Asynchronous Serial Port Expansion of DSPs
- EEWORLD University Hall----Application of TI Jacinto series products in ADAS
- EEWORLD University ---- ADC and DAC
- 120kHz PWM switching power supply
- Please help, what kind of sensor is needed to complete this function?
- How does the STM32 interrupt behavior jump to the EXTI9_5_IRQHandler function?