Program source code
#include #include #include #define uchar unsigned char #define uint unsigned int unsigned long Result,i; sbit SDA=P1^1; //PCF8591 interface sbit SCL=P1^0; unsigned int a=0; // waveform sampling point value unsigned int b=0; unsigned int c=0; unsigned int bx_chang=0; unsigned int n=40; // Frequency calculation value unsigned char TH; unsigned char TL; unsigned int mode=0; //0 is the adjustment amplitude, 1 is the adjustment frequency unsigned int fd=6; // Amplitude initial value 3.0V unsigned int x; // sampling point interval unsigned int u; //LCD screen refresh variable //************* sbit CS =P3^5; //LCD interface sbit SID=P3^6; sbit SCLK=P3^7; sbit PSB=P1^5; //************* sbit p20=P2^0; // waveform adjustment sbit p21=P2^1; // Increase frequency and amplitude sbit p22=P2^2; // Reduce frequency and amplitude sbit p32=P3^2; // Frequency amplitude selection //sin waveform array flying code tosin[256]={ 0x80,0x83,0x86,0x89,0x8D,0x90,0x93,0x96,0x99,0x9C,0x9F,0xA2,0xA5,0xA8,0xAB,0xAE, 0xB1,0xB4,0xB7,0xBA,0xBC,0xBF,0xC2,0xC5,0xC7,0xCA,0xCC,0xCF,0xD1,0xD4,0xD6,0xD8, 0xDA,0xDD,0xDF,0xE1,0xE3,0xE5,0xE7,0xE9,0xEA,0xEC,0xEE,0xEF,0xF1,0xF2,0xF4,0xF5, 0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFD,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFD,0xFC,0xFB,0xFA,0xF9,0xF8,0xF7,0xF6, 0xF5,0xF4,0xF2,0xF1,0xEF,0xEE,0xEC,0xEA,0xE9,0xE7,0xE5,0xE3,0xE1,0xDF,0xDD,0xDA, 0xD8,0xD6,0xD4,0xD1,0xCF,0xCC,0xCA,0xC7,0xC5,0xC2,0xBF,0xBC,0xBA,0xB7,0xB4,0xB1, 0xAE,0xAB,0xA8,0xA5,0xA2,0x9F,0x9C,0x99,0x96,0x93,0x90,0x8D,0x89,0x86,0x83,0x80, 0x80,0x7C,0x79,0x76,0x72,0x6F,0x6C,0x69,0x66,0x63,0x60,0x5D,0x5A,0x57,0x55,0x51, 0x4E,0x4C,0x48,0x45,0x43,0x40,0x3D,0x3A,0x38,0x35,0x33,0x30,0x2E,0x2B,0x29,0x27, 0x25,0x22,0x20,0x1E,0x1C,0x1A,0x18,0x16,0x15,0x13,0x11,0x10,0x0E,0x0D,0x0B,0x0A, 0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09, 0x0A,0x0B,0x0D,0x0E,0x10,0x11,0x13,0x15,0x16,0x18,0x1A,0x1C,0x1E,0x20,0x22,0x25, 0x27,0x29,0x2B,0x2E,0x30,0x33,0x35,0x38,0x3A,0x3D,0x40,0x43,0x45,0x48,0x4C,0x4E, 0x51,0x55,0x57,0x5A,0x5D,0x60,0x63,0x66,0x69,0x6C,0x6F,0x72,0x76,0x79,0x7C,0x80 }; //*********************************************** void delay(unsigned int z) // delay function { unsigned int x,y; for(x=z;x>0;x--) for(y=125;y>0;y--) ; } //*******************************************************LCD display function group void SendByte(unsigned char Dbyte) //LCD byte transmission { unsigned char i; CS=1; for(i=0;i<8;i++) { SCLK = 0; if((Dbyte< SID=1; else SID=0; SCLK = 1; SCLK = 0; } CS=0; } void Lcd_WriteCmd(unsigned char Cbyte ) //LCD data and command transmission { delay(10); SendByte(0xf8); SendByte(0xf0&Cbyte); SendByte(0xf0&(Cbyte<<4)); } void Lcd_WriteData(unsigned char Dbyte ) { delay(10); SendByte(0xfa); SendByte(0xf0&Dbyte); SendByte(0xf0&(Dbyte<<4)); } void InitLCD() //LCD initialization { Lcd_WriteCmd(0x30); Lcd_WriteCmd(0x06); Lcd_WriteCmd(0x0c); Lcd_WriteCmd(0x04); Lcd_WriteCmd(0x01); Lcd_WriteCmd(0x02); Lcd_WriteCmd(0x80); } void xianshi(unsigned char x,unsigned char y,unsigned char *stri) //LCD data transmission address { if(x==1) Lcd_WriteCmd(0x80+y-1); else if(x==2) Lcd_WriteCmd(0x90+y-1); else if(x==3) Lcd_WriteCmd(0x88+y-1); else if(x==4) Lcd_WriteCmd(0x98+y-1); while(*stri>0) { Lcd_WriteData(*stri); stri++; } } //**************************************************** void delayp() // delay function {;;} void delay_1ms(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--) ; } //********************************************I2C protocol void start()// start signal { SDA=1; delayp(); SCL=1; delayp(); SDA=0; delayp(); } void stop() // Stop signal { SDA=0; delayp(); SCL=1; delayp(); SDA=1; delayp(); } void respons()// Response is equivalent to an intelligent delay function { flying i; SCL=1; delayp(); while((SDA==1)&&(i<250)) i++; SCL=0; delayp(); } void init() // initialization { SDA=1; delayp(); SCL=1; delayp(); } void write_byte(uchar date) // Write one byte of data { flying i,temp; temp=date; for(i=0;i<8;i++) { temp=temp<<1; // Left shift one bit out of the position in CY SCL=0; // sda can change value only when scl =0 delayp(); SDA=CY; delayp(); SCL=1; delayp(); } SCL=0; delayp(); SDA=1; delayp(); } void write_add(uchar date) //D/A转换 { start(); write_byte(0x90); response(); write_byte(0x40); response(); write_byte(date); response(); stop(); } //**************************************************** int main()//**************************************************** Main function { TMOD = 0x01; TH0 = (65536-99000/n)/256; // timing time TL0 = (65536-99000/n)%256; TH1 = (65536-5000)/256; TL1 = (65536-5000)%256; EA = 1; ET0 = 1; ET1 = 1; TR0 = 1; TR1 = 1; heat(); while(1) { PSB=0; InitLCD(); //*********************************************** Display module for(u=0;u<9;u++) { xianshi(1,1," Signal generator "); xianshi(2,1," Waveform: "); if(bx_chang==0) xianshi(2,4,"sin"); if(bx_chang==1) xianshi(2,4,"Square"); if(bx_chang==2) xianshi(2,4,"Triangle"); if(mode==0) { xianshi(3,1,"幅度:"); Lcd_WriteData(0x30+(fd*5/10)); xianshi(3,5,"."); Lcd_WriteData(0x30+(fd*5%10)); xianshi(3,6,"V"); } if(mode==1) { xianshi(3,1,"频率:"); Lcd_WriteData(0x30+(n/2/100)); Lcd_WriteData(0x30+(n/2/10)); Lcd_WriteData(0x30+(n/2%10)); xianshi(3,8,"HZ"); } } } } //**************************************************************8 void refresh_f( void ) interrupt 1 // timer interrupt { if(n>=0&&n<40) { x=14; TH0 = (65536-92900/n)/256; TL0 = (65536-92900/n)%256; } else if (n>=40&&n<80) { x=15; TH0 = (65536-97920/n)/256; TL0 = (65536-97920/n)%256; } //**************************************** Sine waveform a=a+x; if(a<256&&bx_chang==0) { write_add(tosin[a]*0.1*fd); } if(a>=256) { a=0; } //************************************ Square wave waveform b=b+x; if(b<128&&bx_chang==1) { write_add(0x00*0.1*fd); } if(b>=128&&b<256&&bx_chang==1) write_add(0xff*0.1*fd); if(b>=256) { b=0; } //**************************************** Triangle wave shape c=c+x; if(c<128&&bx_chang==2) { write_add(c*0.2*fd); } if(c>=128&&c<256&&bx_chang==2) write_add((-c+256)*0.2*fd); if(c>=256) { c=0; } } //************************************************ Timer interrupt key interrupt void refresh_zd( void ) interrupt 3 { TH1 = (65536-5000)/256; //5000us TL1 = (65536-5000)%256; //*******************************************8 if(p32==0) // Frequency or amplitude adjustment selection { delay_1ms(1000); if(p32==0) mode=mode+1; if(mode>=2) mode=0; } if(p20==0) // waveform selection { delay_1ms(1000); bx_chang=bx_chang+1; if(bx_chang>=3) bx_chang=0; } //******************************************* Frequency adjustment
Previous article:Eight-string laser harp circuit diagram and single-chip source code
Next article:MCU external interrupt program (give P3.2 pin low level, LED will show the inverted state)
- 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
- Is the IRFP460 I bought a fake tube?
- How to prevent dead waiting programs from causing other tasks to fail to run during development
- Summary of 10 major issues with ZigBee technology
- [Mil MYD-YA15XC-T Review] +RGB dot screen
- BQ24133 charging current accuracy
- 3G will achieve unprecedented success in China [Reprint]
- PIC16C712 functions, features, and application areas
- MCU Development Tools
- Power supply project
- UART to Wi-Fi Bridge Adds Connectivity to Existing Hardware