2051 comparator analog AD source program (C language)

Publisher:phi31Latest update time:2018-03-12 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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
}


Reference address:2051 comparator analog AD source program (C language)

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

Fast charger circuit designed by AT89C2051
Fast charger circuit designed by AT89C2051
[Power Management]
Fast charger circuit designed by AT89C2051
Design of infrared remote control LED electronic clock based on AT89C2051
1. Introduction Homemade LED electronic clocks can be seen in many electronic newspapers and magazines, but most of them need to reset the time and other parameters after power failure, which brings a lot of inconvenience to use. There are also backup batteries as backup power sources, but they are often large in si
[Microcontroller]
Design of infrared remote control LED electronic clock based on AT89C2051
Design of decoder based on AT89C2051
At present, the most widely used codec in my country is the PT2262 encoding chip with 531441 (312) states and the matching PT2272 decoding chip produced by Princeton. However, one PT2262 can only decode 15 states of PT2272 with one address. For larger codec security and anti-theft control systems, multiple PT2272 co
[Microcontroller]
Design of decoder based on AT89C2051
Analysis of a New Current Limit Comparator
1. Introduction The limiting current comparator is a very important part of the current mode control circuit. It generates different limiting currents for different load conditions to limit the peak current or average current on the inductor, thereby minimizing the output voltage ripple and improving power effi
[Analog Electronics]
Analysis of a New Current Limit Comparator
MAX931 comparator is used to monitor the main power supply voltage circuit
The main power supply and backup battery are connected to the load through a simple diode "OR" logic circuit. However, when the battery voltage exceeds the main power supply voltage, the diode "OR" logic circuit will connect the battery power supply and cannot reasonably select the main power supply. The circu
[Power Management]
MAX931 comparator is used to monitor the main power supply voltage circuit
Design of digital thermometer based on AT89C2051
Single-chip microcomputer control has become one of the goals pursued by electronic design today. This article applies this control technology to temperature measurement. AT89C2051 is a low-voltage, high-performance CMOS 8-bit single-chip microcomputer produced by Atmel Corporation of the United States. It contains
[Microcontroller]
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号