0-99V digital voltmeter made with STC12C2052AD microcontroller

Publisher:HeavenlyLoveLatest update time:2015-07-13 Source: 51heiKeywords:STC12C2052AD Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The 0-99V digital voltmeter made with STC12C2052AD microcontroller comes from Digital Home_abenyao. I think this circuit is quite useful. The principle is simple. It directly uses the AD with STC12C2052AD microcontroller. The digital tube uses a four-digit common anode digital tube. There is a little jump after the decimal point. The circuit is built on a breadboard. The reference voltage is generated by TL431. The following is a real picture:
*************************************************** *********************
0-99V digital voltmeter source program made with STC12C2052AD microcontroller
First published by Digital Home. ID: abenyao
P1.6 port is 0-5V analog input terminal, P1.5 port is connected to 2.5V reference power supply output by TL431l, 4-bit serial LED digital tube display
*************************************************** ************************/
#include  //MCU header file
#include  //51 basic operations (including _nop_ empty function)
#define uchar unsigned char
#define uint unsigned int
#define LEDBus P3
//a3.0-b3.1-c3.2-d3.3-e3.4-f3.5-g3.7-dp1.0
#define ON 1 //define 0 as on
#define OFF 0 //define 1 to turn off


sbit ge=P1^4; //bit selection
sbit shi=P1^3; //Tens bit selection
sbit bai=P1^2; //hundreds digit selection
sbit qian=P1^1; //Thousands digit selection
sbit db=P1^0;
char d[5];
uint R,M,N; //If defined as uchar type, it can only display values ​​below 2.5V
uchar code LEDTab[]={0xc0,0xf9,0x64,0x70,0x59,0x52,0x42,0xf8,0x40,0x58};
/****************************************************** ****************
Function name: millisecond CPU delay function
Call: delay (?);
Parameter: 1~65535 (parameter cannot be 0)
Return value: None
Result: The CPU usage delay is the same as the parameter value in milliseconds
Note: When applied to 1T MCU, i<600; when applied to 12T MCU, i<125
/****************************************************** *******************/
void delay(uint t)
{
uint i; //define variables
for(;t>0;t--) //If t is greater than 0, t minus 1 (outer loop)
  for(i=600;i>0;i--); //i is equal to 124, if i is greater than 0, i minus 1
}
/****************************************************** ******************
Function name: ADC initialization and 8-bit A/D conversion function
Return value: 8-bit ADC data
Result: Read the A/D conversion value of the specified ADC interface and return the value
Note: Applicable to STC12C2052AD series MCU (must use STC12C2052AD.h header file)
*************************************************** *******************/
uchar Read (uchar CHA) {
uchar AD_FIN=0; //Store A/D conversion flag; if this variable is defined outside the function, the display of continuously changing analog quantity cannot be obtained
/******The following is the ADC initialization program********************************/
    CHA &= 0x07; //Select one of the 8 interfaces of ADC (0000 0111 clear the high 5 bits)
    ADC_CONTR = 0x60; //ADC conversion speed (0XX0 0000, XX controls the speed, please set according to the data sheet)
    _nop_();
    ADC_CONTR |= CHA; //Select the current A/D channel
    _nop_();
    ADC_CONTR |= 0x80; //Start A/D power supply
    delay(1); //Let the input voltage reach stability (1ms is enough?
/******The following is the ADC execution program********************************/
    ADC_CONTR |= 0x08; //Start A/D conversion (0000 1000 ADCS = 1)
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    while (AD_FIN ==0){ //Wait for A/D conversion to end
    AD_FIN = (ADC_CONTR & 0x10); //0001 0000 Test whether A/D conversion is finished
    }
    ADC_CONTR &= 0xE7; //1111 0111 clear ADC_FLAG bit, turn off A/D conversion,
return (ADC_DATA); //Return A/D conversion result (8 bits)
}
/****************************************************** *****************
Displays the function conversion function:
M = analog sampling value, N = reference voltage source sampling value (2.5V in this example), R = analog input value (to be displayed)
N=256*2.5/Vcc; after deformation, Vcc=256*2.5/N; Substitute M=256*R/Vcc; get M=R*N/2.5; after deformation, R=M*2.5/N
1.105 is the voltage divider ratio at the input.
*************************************************** ****************/
void transfer(void){
M=Read(6); //P1.6 port analog conversion
N=Read(5); //P1.5 port 2.5V reference voltage source sampling (conversion)
R=((M*2.5)/N)*1.105*1000; //Convert the input analog quantity and amplify it 1000 times;
/***The following is a 3-digit display conversion***/
d[3]=R/1000;
R=R%1000;
d[2]=R/100;
R=R%100;
d[1]=R/10;
d[0]=R%10;
}
/**********Display function (not optimized)****************************************************/
void xian_shi (void)
{
LEDBus=LEDTab[d[0]]; //Send the unit digit to the digital tube for display
ge=ON; //Open the unit digit selection
delay(2); //1 means display for 1 millisecond, 0 shortens the display time and reduces the brightness by half
ge=OFF; //Close the ones digit selection
LEDBus=LEDTab[d[1]]; //The ten digit is sent to the digital tube for display
shi=ON; //Open the tens digit selection


delay(2); //1 means display for 1 millisecond, 0 shortens the display time and reduces the brightness by half
shi=OFF; //Disable the tens digit selection


LEDBus=LEDTab[d[2]]; //Hundreds digit is sent to digital tube for display
bai=ON; //Open the hundreds digit selection
db=0; //Display decimal point
delay(2); //1 means display for 1 millisecond, 0 shortens the display time and reduces the brightness by half
bai=OFF; //Disable hundreds digit selection
db=1; //Turn off decimal point display
LEDBus=LEDTab[d[3]]; //Thousands digit is sent to digital tube for display
qian=ON; //Open the thousands digit selection
delay(2); //1 means display for 1 millisecond, 0 shortens the display time and reduces the brightness by half
qian=OFF; //Close the thousands digit selection
}
/****************************************************** *****************
Function name: main function
Call: None
Parameters: None
Return value: None
Result: Infinite loop at the beginning of the program
Remark:
*************************************************** ****************/
void main (void)
{
P1M0 = 0x60; //P1.0/P1.1: 0000 0011 (high impedance) //Note: When changing the ADC channel, the corresponding IO interface must also be changed to high impedance input.
P1M1 = 0x00; //P1.0/P1.1: 0000 0000
while(1){
uchar i;
delay(1);
i++;
if (i==30)
{
transfer();
i=0;
}
xian_shi();
//delay(10);
}
}  
Keywords:STC12C2052AD Reference address:0-99V digital voltmeter made with STC12C2052AD microcontroller

Previous article:The unknown mystery of 51 MCU's IO port
Next article:Electronic clock based on AT89C51

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号