Digital voltmeter using STC12C5204AD microcontroller

Publisher:CelestialSoulLatest update time:2015-06-16 Source: 51heiKeywords:STC12C5204AD Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

        This is a program for making a digital voltmeter using STC12C5204AD. P0.0-P0.3 is the common cathode digital tube bit drive end, P2 port is the segment drive port of the common cathode digital tube segment ag and dp, P0 and P2 ports are set to push-pull output mode, segment output adds 470 ohm current limiting resistor, AD is 8 bits, conversion voltage division 5/256=0.0195312V, shunt resistor is the measured resistance value, AD value * 0.0195312v/shunt resistor to ground, calculate shunt current, and then use shunt current * shunt resistor and current limiting resistor to display the input voltage value. For amateur use, the accuracy is enough, much more accurate than that small pointer. Please refer to the chip manual for corrections where port settings are involved.
#include //STC12C5204AD header file (6 K) Download times: 15
#include //_nop_();
#define uchar unsigned char
#define uint unsigned int
//sfr ADC_CONTR=0XBC;//adc enable bit.ADC power control bit
sfr ADC_RES=0XBD;//adc data high eight bits, that is, ADC value
sfr P1ASF=0X9D;//P1 each port ADC enable end
uchar led_bcd[]={0x3F,/*0*/ //Common cathode digital tube
          0x06,/*1*/
          0x5B,/*2*/
          0x4F,/*3*/
          0x66,/*4*/
          0x6D,/*5*/
          0x7D,/*6*/
          0x07,/*7*/
          0x7F,/*8*/
          0x6F,/*9*/
    0x00,/* */
    0x7c /*b*/
    }
    ,ad_data
    ;
uint data dis[5]={0x00,0x00,0x00,0x00,0x00},ZZ; //Define four display data units and one data storage unit
//dis[4,3,2,1,5] Display: 12.34 5 is the storage unit

/*******************************************************************/
/* Delay subroutine */
/* */
/* */
/*******************************************************************/
void delay(uint z) //Delay subfunction
{  
   uchar de1,de2;
  for(de1=z;de1>0;de1--)
  for(de2=100;de2>0;de2--);  
}
/***********************************************************/
//Display subfunction display(h1,h2,h3,h4)
//h1,h2,h3,h4 are four-digit LED digital tube display variables
//
//
//
/*******************************************************/
void display(uchar h1,uchar h2,uchar s1,uchar s2) ////LED display function (parameters: LED1,LED2,LED3,LED4)
{

// The first digital tube displays data
if (h1==0x3f) h1=0x00; //0 blanking statement
P2=h1;
  //delay(2);
P00=0;
  delay(2); //Parameter 2-10 The larger the value, the higher the brightness of the LED, but the stronger the flickering feeling; the smaller the value, the lower the brightness of the LED, but the smaller the flickering feeling.
P2=0X00;
P00=1;
/*******************************************************/
// The second digital tube displays data
P2=h2|0x80;
  //delay(2);
P01=0;
  delay(2); //Parameter 2-10 The larger the value, the higher the brightness of the LED, but the stronger the flickering feeling; the smaller the value, the lower the brightness of the LED, but the smaller the flickering feeling.
P2=0X00;
P01=1;
//delay(5);
/***********************************************************/
// The third digital tube displays data

P2=s1;
  //delay(2);
P02=0;
  delay(2); //Parameter 2-10: large values ​​mean high LED brightness but strong flickering, small values ​​mean low LED brightness but small flickering.
  P2=0X00;
    P02=1;
// delay(5);
/*******************************************************/
// The fourth digital tube displays data
P2=s2;
  //delay(2);
P03=0;
  delay(2); //Parameter 2-10: large values ​​mean high LED brightness but strong flickering, small values ​​mean low LED brightness but small flickering.
  P2=0X00;
P03=1;

//delay(5);

}
void init()//system initialization function
{
P2M1=0Xff; //Strong push-pull push ag dp
P2M0=0X00; //Strong push-pull push ag dp
P0M1=0X0f; //Strong push-pull push bit
P0M0=0X00; //Strong push-pull push bit
P0=0XFF; //Initialize
P2=0; //Initialize
/***Timer 0 initialization setting*****/  
TMOD = 0x01;
    TH0 = 0xFC; //Initial value 1ms
    TL0 = 0x18; //Initial value 1ms
    EA = 1; //Open total interrupt
    ET0 = 1; //Timer 0 interrupt open
    TR0 = 1; //Open timer to start timing
/****************************/
ZZ=0;
}
void initADC() //AD initialization
{
P1ASF=0x01;//Only use P1.0 port as ADC input.
ADC_RES=0;//ADC data register clears
ADC_CONTR=ADC_POWER|ADC_SPEEDLL; //Turn on ADC and set conversion rate See STC12C52.H for details
delay(2); //Turn on ADC and delay.
}
uchar readADC() //Read AD
{
ADC_CONTR=ADC_POWER|ADC_SPEEDLL|0|ADC_START;
_nop_();
_nop_();
_nop_();
_nop_();
while(!(ADC_CONTR&ADC_FLAG));//Wait for conversion completion flag
ADC_CONTR&=~ADC_FLAG;//close ADC
return ADC_RES;//Return ADC value.
}
void main()
{
init();
initADC();//Initialize ADC
ad_data=readADC();//Power on conversion
while(1)
{
  float j;
  uchar ad_data;
  while(ZZ>=500) //Read ADC every 500 milliseconds
  {
   ad_data=readADC();//Read ADC data
   ZZ=0;
  }
  j=ad_data*19.5312;//AD data*19.5312 (amplified 1000 times) = voltage after shunting
  j=j/9890; //j(voltage)/grounding resistance (measured) 9890 ohms
  j=j*60190;//j(current)*(input current limiting resistance (measured) 60190 ohms + grounding resistance (measured) 9890 ohms) calculate the actual input voltage If high-precision resistors are used, directly input the resistance value
  dis[4]=j/1000;
  dis[3]=dis[4]/10;
  dis[2]=dis[4]%10;
  //h=(h-(dis[3]*10+dis[2]))*100;
  dis[4]=j/10-dis[4]*100;
  //dis[4]=j%100;//The first decimal place
  dis[1]=dis[4]/10;
  dis[0]=dis[4]%10;//The second decimal place
  //delay(10000);
}
}
void Timer0Interrupt(void) interrupt 1
{
    ZZ++; //AD read interval time control
TH0 = 0xFC;
    TL0 = 0x18;
display(led_bcd[dis[3]],led_bcd[dis[2]],led_bcd[dis[1]],led_bcd[dis[0]]);
   }

Keywords:STC12C5204AD Reference address:Digital voltmeter using STC12C5204AD microcontroller

Previous article:32-light shake stick made by STC11F04 single chip microcomputer
Next article:Digital tube display STC89C54RD+DB18B20 temperature measurement board

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号