Implementation of internal ADC in STC15F2K60S2 MCU digital voltmeter program

Publisher:Tiger8Latest update time:2020-09-08 Source: elecfansKeywords:STC15F2K60S2 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

①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 //Header file used by printf function

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 //Header file used by printf function


//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

}

Keywords:STC15F2K60S2 Reference address:Implementation of internal ADC in STC15F2K60S2 MCU digital voltmeter program

Previous article:Schematic diagram of serial communication between two microcontrollers
Next article:Chinese Characters Simulation Input Based on SC95F8616 Single Chip Microcomputer

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号