The software of this system is quite typical: including keyboard application, display application and DA converter application. In this design, there are three kinds of output waveforms: sine wave, square wave and triangle wave.
The output of square wave is the simplest, just change the output voltage according to the set period value.
The output of the triangle wave is also relatively simple. The output of the microcontroller only needs to complete the alternating increase and decrease of the digital quantity.
The output of sine wave is the most troublesome. If the voltage value of each output point is calculated in the software, a lot of CPU time will be wasted, so that the frequency requirement cannot be met. Usually the simplest method is to calculate the voltage value of each output point manually, and then give it in array form when writing the program. When needed, just output it in sequence. This method is faster than the operation method and the shape of the curve can be modified flexibly. In this design, 360 degrees is divided into 256 points, and the interval between each two points is 1.4 degrees, and then the digital value corresponding to the voltage of each point is calculated. As long as this set of data is repeatedly output to DAC0832, the desired sine wave can be obtained at the output end of the system.
The specific procedures are as follows:
#include#define uchar unsigned char #define uint unsigned int #define DAdata P0 uchar code Sinetab[256]= { 0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e, 0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e, 0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xab,0xad, 0xaf,0xb1,0xb2,0xb4,0xb6,0xb7,0xb9,0xba, 0xbc,0xbd,0xbf,0xc0,0xc1,0xc3,0xc4,0xc5, 0xc6,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce, 0xce,0xcf,0xd0,0xd1,0xd1,0xd2,0xd2,0xd3, 0xd3,0xd3,0xd2,0xd2,0xd1,0xd1,0xd0,0xcf, 0xce,0xce,0xcd,0xcc,0xcb,0xca,0xc9,0xc8, 0xc6,0xc5,0xc4,0xc3,0xc1,0xc0,0xbf,0xbd, 0xbc,0xba,0xb9,0xb7,0xb6,0xb4,0xb2,0xb1, 0xaf,0xad,0xab,0xaa,0xa8,0xa6,0xa4,0xa2, 0xa0,0x9e,0x9c,0x9a,0x98,0x96,0x94,0x92, 0x90,0x8e,0x8c,0x8a,0x88,0x86,0x84,0x82, 0x80,0x7d,0x7b,0x79,0x77,0x75,0x73,0x71, 0x6f,0x6d,0x6b,0x69,0x67,0x65,0x63,0x61, 0x5f,0x5d,0x5b,0x59,0x57,0x55,0x54,0x52, 0x50,0x4e,0x4d,0x4b,0x49,0x48,0x46,0x45, 0x43,0x42,0x40,0x3f,0x3e,0x3c,0x3b,0x3a, 0x39,0x37,0x36,0x35,0x34,0x33,0x32,0x31, 0x31,0x30,0x2f,0x2e,0x2e,0x2d,0x2d,0x2c, 0x2c,0x2b,0x2b,0x2b,0x2b,0x2a,0x2a,0x2a, 0x2a,0x2a,0x2a,0x2a,0x2b,0x2b,0x2b,0x2b, 0x2c,0x2c,0x2d,0x2d,0x2e,0x2e,0x2f,0x30, 0x31,0x31,0x32,0x33,0x34,0x35,0x36,0x37, 0x39,0x3a,0x3b,0x3c,0x3e,0x3f,0x40,0x42, 0x43,0x45,0x46,0x48,0x49,0x4b,0x4d,0x4e, 0x50,0x52,0x54,0x55,0x57,0x59,0x5b,0x5d, 0x5f,0x61,0x63,0x65,0x67,0x69,0x6b,0x6d, 0x6f,0x71,0x73,0x75,0x77,0x79,0x7b,0x7d, }; uchar code Triangletab[58]= { 0x1a,0x21,0x28,0x2f,0x36,0x3d,0x44,0x4b, 0x52,0x59,0x60,0x67,0x6e,0x75,0x7c,0x83, 0x8a,0x91,0x98,0x9f,0xa6,0xad,0xb4,0xbb, 0xc2,0xc9,0xd0,0xd7,0xde,0xe5, 0xde,0xd7,0xd0,0xc9,0xc2,0xbb,0xb4,0xad, 0xa6,0x9f,0x98,0x91,0x8a,0x83,0x7c,0x75, 0x6e,0x67,0x60,0x59,0x52,0x4b,0x44,0x3d, 0x36,0x2f,0x28,0x21, }; uchar code Squaretab[2]={0x56,0xaa}; uchar code disp1[]= { "Sine Wave" "Triangle Wale" "Square Wave" }; uchar idata disp2[16]={"Frequency: Hz"}; uchar code Coef[3]={10,100,200}; uchar idata WaveFre[3]={1,1,1}; uchar code WaveTH[]= { 0xfc,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xfc,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, }; uchar code WaveTL[]= { 0xf2,0x78,0xfb,0x3c,0x63,0x7d,0x8f,0x9d,0xa8,0xb1, 0x17,0x0b,0xb2,0x05,0x37,0x58,0x70,0x82,0x90,0x9b, 0x4d,0xa7,0xc4,0xd3,0xdc,0xe2,0xe6,0xea,0xec,0xee }; uchar Wavecount,THtemp,TLtemp; uchar Waveform; sbit rs=P2^5; sbit rw=P2^6; sbit e=P2^7; sbit DA=P2^0; sbit KEY=P3^2; void delay(uchar i) { uchar j; for(;i>0;i--) for(j=20;j>0;j--); } void busy() { uchar temp; temp=0x00; rs=0; rw=1; while((temp&0x80)==0x80) { P0=0xff; e=1; temp=P0; e=0; } } void WR_Com(uchar temp) { busy(); rs=0; rw=0; P0=temp; e=1; e=0; } void WR_Data(uchar num) { busy(); rs=1; rw=0; P0=num; e=1; e=0; } void disp_lcd(uchar addr,uchar *temp1) { uchar i; WR_Com(addr); delay(100); for(i=0;i<16;i++) { WR_Data(temp1[i]); delay(100); } } void lcd_ini() { char i; for(i=3;i>0;i--) { P0=0x30; rs=0; rw=0; e=1; e=0; delay(100); } P0=0x38; rs=0; rw=0; e=1; e=0; delay(100); } void lcd_Reset() { WR_Com(0x01); delay(100); WR_Com(0x06); delay(100); WR_Com(0x0c); delay(100); } void SineOUT(uchar Wavecount) { DAdata=Sinetab[Wavecount++]; Wavecount=0; DA=0; DA=1; } void TriangleOUT(uchar Wavecount) { DAdata=Triangletab[Wavecount++]; if(Wavecount>57) Wavecount=0; DA=0; DA=1; } void SquareOUT(uchar Wavecount) { DAdata=Squaretab[Wavecount++]; if(Wavecount>1) Wavecount=0; DA=0; DA=1; } void timer() interrupt 1 { TH0=THtemp; TL0=THtemp; if(Waveform==0) SineOUT(Wavecount); else if(Waveform==1) TriangleOUT(Wavecount); else if(Waveform==2) SquareOUT(Wavecount); } void key_int() interrupt 0 { uchar keytemp,keytemp1; uint WaveCoef; EA=0; TR0=0; keytemp1=0; delay(10); while(!KEY); keytemp=~P2&0x1e; keytemp>>=1; while(keytemp!=8) { keytemp=~P2&0x1e; keytemp>>=1; if(keytemp!=keytemp1) { keytemp1=keytemp; switch(keytemp) { case 1: if(++Waveform==3) Waveform=0; break; case 2: if(++WaveFre[Waveform]==11) WaveFre[Waveform]=1; break; case 4: if (--WaveFre[Waveform]==0) WaveFre[Waveform]=10; break; } THtemp=WaveTH[Waveform*16+(WaveFre[Waveform]-1)]; TLtemp=WaveTL[Waveform*16+(WaveFre[Waveform]-1)]; WaveCoef=WaveFre[Waveform]*Coef[Waveform]; disp2[13]=WaveCoef%10+0x30; WaveCoef/=10; disp2[12]=WaveCoef%10+0x30; WaveCoef/=10; disp2[11]=WaveCoef%10+0x30; WaveCoef/=10; disp2[10]=WaveCoef%10+0x30; WaveCoef/=10; disp_lcd(0x80,&disp1[Waveform*16]); disp_lcd(0xc0,disp2); } } TH0=THtemp; TL0=THtemp; Wavecount=0; TR0=1; } void main() { uint WaveCoef; uchar i; lcd_ini(); lcd_Reset(); WaveCoef=WaveFre[Waveform]*Coef[Waveform]; disp2[13]=WaveCoef%10+0x30; WaveCoef/=10; disp2[12]=WaveCoef%10+0x30; WaveCoef/=10; disp2[11]=WaveCoef%10+0x30; WaveCoef/=10; disp2[10]=WaveCoef%10+0x30; WaveCoef/=10; disp_lcd(0x80,&disp1[Waveform*16]); disp_lcd(0xc0,disp2); i=0; DAdata=0x00; DA=0; TMOD=0x01; IT0=1; ET0=1; EX0=1; EA=1; while(1); }
Previous article:Design of 12864 LCD animation effect based on single chip microcomputer
Next article:About C51 assembly ADD and SUBB instructions
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Multi-tabbed putty - MTPuTTY
- There are always reports of layoffs at major domestic companies. Is the domestic economic environment really getting worse?
- Division of the electromagnetic spectrum
- China's broadband household users will reach 130 million in 2010
- Calculation of voltage amplification factor under deep negative feedback
- DIY vertical milling machine, engraving machine
- I made a board that supports MicroPython. I'll show it to you.
- Why is the DCDC step-down chip easily damaged?
- Answers to some questions raised in the review of Espressif's ESP32-S2-Kaluga-1 development board
- ST MEMS Device Resource Library-Device Application Guide