2051 comparator analog AD source program (C language)
/* io allocation: *
;* OUTPUT: *
;* P1.0 ...... analog input *
;* P1.1 ...... DA input comparison reference voltage *
;* P1.2~7..... R-2R DA resistor network *
;* P3.7 ...... LED analog brightness output *
;* CPU C LOC K EQU 6M */
//xiaoqi last edit in 2001.11
//#pragma SRC
#i nclude "AT89x051.h"
#i nclude
#i nclude
#i nclude
// Macro definition of variable type identifier, everyone likes to do this
#define Uchar unsigned char
#define Uint unsigned int
#define Ledlight() (P3 &= 0x7f)
#define Leddark() (P3 |= 0x80)
sbit P36 = P3^6; // internal judgment pin of comparator
sbit LED = P3^7; // a light emitting diode to observe brightness changes
//Internal flag bit definition
bit less; //Compare if it is greater than 1. Less than, 0. Greater than
// Global variable definition
Uchar timer1, // Universal delay counter
timer2, // Key buzzer response timer
adcdata, // ad conversion variable
PWM 1; // PWM output ratio
// Function list
void DelayMs(unsigned int number); // millisecond delay void timers0(); // Scan and display the digital tube
in the timer interrupt (ct0) void Initall(void); // System initialization void timers1(void); // TC1 timer interrupt is used to scan the display and keyboard Uchar adcread(void); // adc conversion program
void main(void) using 0
{
DelayMs(120);
Initall();
pwm1 = adcread();
LED=1;
while(1)
{
pwm1 = adcread();
timer2=10;
while (timer2);
}
}
// millisecond delay
void DelayMs(unsigned int number)
{
unsigned char temp;
for(;number!=0;number--)
for(temp=112;temp!=0;temp--);
}
/****************************************
Do PWM output of LED in timer interrupt
****************************************/
void timers0() interrupt 1 using 1
{
TH0 = 0xff;
TL0 = 0xd0;
timer1--;
if (timer1==pwm1)LED=0;
if (timer1==0){
LED=1;
timer1=0x40;
timer2--;
};
}
/****************
;* 6-bit ADC conversion
;****************/
Uchar adcread(void)
{
Uchar i=0x3f,temp=0;
P36 =1;
P1 = 3; _nop_();_nop_(); // Start from zero
while ((i--)&& (P36))
{
temp += 4;
P1 = temp|3;
_nop_();
}
temp >>= 2;
return temp;
}
/****************
;* System initialization
;********************/
void Initall(void)
{
TMOD = 0x11; // 0001 0001 hexadecimal counter
IP = 0x8; // 0000 1000 t1 priority
IE = 0x8A; // 1000 1010 t0,t1 interrupt enable
TCON = 5; // 0000 0101 external interrupt low level trigger
TR0 = 1; // Turn on the timer interrupt, which is already turned on in IE, and indicate it again
TR1 = 0;
ET0 = 1;
ET1 = 0;
P1 = 0xff;
}
/*************************************
TC1 timer interrupt is used to scan the display and keyboard (ct1)
*************************************/
void timers1(void) interrupt 3 using 2
{
_nop_(); //Not enabled in the experiment
}
Previous article:C51 independent key recognition sample program
Next article:How to display 8 digits in decimal on a seven segment display?
Recommended ReadingLatest update time:2024-11-16 14:34
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
- 1. When it comes to the conduction condition of NMOS, VGS>0 is sufficient. But after conduction, won't VS be greater than VG? 2. We...
- DIY slide table for electron microscope
- Introduction to the mobile station development board: ultra-low power python board
- XMC4800 Review (Part 3) -- USB Multiplexing Device CDC+HID
- 2G---5G and future antenna technology
- The 2019 Intel FPGA Technology Conference in Beijing invites you to attend!
- DSP28335 ADC software trigger
- Integrated power devices simplify FPGA and SoC design
- How to use the stepper motor double four beats, please teach me
- EEWORLD University Hall ---- Electrical Engineering North China Electric Power University Li Yonggang