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
#include
#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]]);
}
Previous article:32-light shake stick made by STC11F04 single chip microcomputer
Next article:Digital tube display STC89C54RD+DB18B20 temperature measurement board
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- CPU reads and writes memory
- TI C2000 LaunchPad uses official routines to compile and debug
- How to configure TMS570 HAL CODEGEN
- ISA bus interface
- MicroPython now runs on the LEGO MINDSTORMS Inventor Hub
- How to suppress the output ripple of switching power supply?
- What is the file with the suffix jed? What is its function?
- EEWORLD University ---- ESP32 MicroPython Video Tutorial
- Relationship between PN conduction voltage drop and current and temperature
- [GD32L233C-START Review] 15. RT-Thread message queue, multi-threaded use