①Use the STC15F2K60S2 microcontroller board to design a digital voltmeter, which can only be implemented using the internal ADC of the microcontroller;
② The DC voltage range is required to be 0-4.55V, the measurement error is less than 0.005V, and the measurement result is retained to three decimal places;
③The voltmeter has an automatic over-range alarm function. When the measured voltage exceeds 4.55V, the buzzer will be driven to alarm. When the voltage is less than 4.55V, the buzzer will be automatically turned off.
④ The voltage measurement calibration and evaluation shall be based on the multimeter in hand on the test day. The display device is optional and the internal reference voltage is required to be used as the reference voltage;
⑤ The collected results are uploaded to the PC via the USB to serial port cable, and the display format is: "N-way voltage: X.XXXV";
⑥The serial port transmission baud rate is 57600, the crystal oscillator uses the internal 22.1184M, and the reset pin cannot be used as I/O;
⑦The voltmeter has an automatic sleep function. It automatically enters sleep mode 15 seconds after startup and continues to work normally after waking up.
The microcontroller source program is as follows:
#include"STC15F2K60S2.h" //header file
#include"JLX12864G-086S-ZK.h" //header file
#include"ADC.h" //header file
#include float VCC; //power supply voltage unsigned int a; //Measure the ADC digital value of the ninth channel float Va; //9th channel voltage value char temp[16]; void UART_int() //Serial port initialization function { TMOD|=0x20; //T1 works in mode 2, 8-bit auto-reload mode TH1=TL1=256-22118400/57600/384; //TH1: reload value 9600 baud rate, crystal oscillator 11.0592MHz; TR1=1; AUXR=0x00; //Use timer 1 to generate baud rate, S1ST2=0 SCON=0x50; //SCON: Mode 1, 8-bit UART, enable reception TI=1; } void main() { float Vin,V_temp=0; unsigned char i; InitADC(); UART_int(); initial_lcd(); clear_screen(); display_GB2312_string(1,1,"Design of digital voltmeter"); display_GB2312_string(3,1,"Voltage:"); while(1) { //Measure the voltage value of the ninth channel (internal reference voltage) P1ASF=0x00; VCC=(1.24612/ADC_Read(ADC_CH0))*1023; //The internal reference voltage is 1.23611V(1.27), and VCC is calculated by reverse calculation P1ASF=0xFF; // for(i=0;i<30;i++)V_temp=V_temp+VCC/1023*ADC_Read(ADC_CH0); // Vin=V_temp/30; //Calculate the average value for calibration accuracy // V_temp=0; Vin=VCC/1023*ADC_Read(ADC_CH0); sprintf(temp,"%.3fV",Vin); display_GB2312_string(3,41,temp); if(Vin>4.55)P34=!P34; else {P34=1;} printf("0-way voltage:%.3fVn",Vin); } } #include"STC15F2K60S2.h" //header file #include //ADC control bit definition ADC_CONTR #define ADC_POWER 0x80 //ADC power control bit #define ADC_FLAG 0x10 //ADC conversion completed flag #define ADC_START 0x08 //ADC starts conversion #define ADC_SPEEDLL 0x00 //ADC conversion rate 540 clock #define ADC_SPEEDL 0x20 //ADC conversion rate 360 clocks #define ADC_SPEEDH 0x40 //ADC conversion rate 180 clock #define ADC_SPEEDHH 0x60 //ADC conversion rate 90 clock #define ADC_CH0 0x00 //Conversion channel P1.0 #define ADC_CH1 0x01 //Conversion channel P1.1 #define ADC_CH2 0x02 //Conversion channel P1.2 #define ADC_CH3 0x03 //Conversion channel P1.3 #define ADC_CH4 0x04 //Conversion channel P1.4 #define ADC_CH5 0x05 //Conversion channel P1.5 #define ADC_CH6 0x06 //Conversion channel P1.6 #define ADC_CH7 0x07 //Conversion channel P1.7 //ADC port analog function settings P1ASF #define P1ASF_0 0x01 //Set P1.0 port to ADC port #define P1ASF_1 0x02 //Set P1.1 port to ADC port #define P1ASF_2 0x04 //Set P1.2 port as ADC port #define P1ASF_3 0x08 //Set P1.3 port as ADC port #define P1ASF_4 0x10 //Set P1.4 port as ADC port #define P1ASF_5 0x20 //Set P1.5 port as ADC port #define P1ASF_6 0x40 //Set P1.6 port as ADC port #define P1ASF_7 0x80 //Set P1.7 port as ADC port float VCC; //power supply voltage void delay_nus(unsigned int t){while(t--);}//microsecond delay function void delay_nms(unsigned int t) //millisecond delay function { unsigned int a; while(t--)for(a=0;a<80;a++); } void InitADC() { P1ASF=0xFF; //Open P1.0~P1.7 as analog input channel ADC_CONTR=ADC_POWER|ADC_SPEEDLL; delay_nms(1); //delay 1mS } unsigned int ADC_Read(unsigned char ADC_CH)//ADC sampling initialization function { unsigned int AD_Dat; ADC_CONTR|=ADC_POWER|ADC_SPEEDLL|ADC_CH|ADC_START; //Configure ADC control register //Turn on power, maximum speed, select channel delay_nus(100); //delay 100uS while((ADC_CONTR&ADC_FLAG)==0); //Wait for the conversion to end 0x10=0001 0000 ADC_CONTR &=~ADC_FLAG; //Clear conversion end ADC_FLAG AD_Dat=(ADC_RES<<2)+(ADC_RESL&0x03); //Integrate the data into 10 digits and do calculations ADC_CONTR=0x00; //Practice has proved that adding this code is necessary to perform multi-channel voltage acquisition return AD_Dat; //Return voltage digital value }
Previous article:Schematic diagram of serial communication between two microcontrollers
Next article:Chinese Characters Simulation Input Based on SC95F8616 Single Chip Microcomputer
- 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
- Misuse of "termination" will cause signal failure
- Raspberry Pi Pico is out~ I originally said that I would not review it before the New Year, but I am here to recommend this board to you~~
- PyBoard W5500 firmware compilation issue
- Discussion and help
- Causes of thermal resistor soft breakdown and troubleshooting of thermal resistor soft breakdown
- "Playing with the board" + lighting up the SPI touch screen of Xintao
- Ultrasonic flaw detection system using high-speed and high-resolution signal acquisition card
- 2020-7-5-Abdominal breathing training trainer
- ST MEMS Device Resource Library - Hardware Design Guide
- &FF0000 and &880000 are both red. What is the difference between these two reds?