12.STC15W408AS MCU Comparator

Publisher:春水碧于天Latest update time:2022-08-03 Source: csdnKeywords:STC15W408AS Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Internal structure of comparator

The STC15W408AS microcontroller has a built-in comparator function. The internal layout of the comparator is shown in the following figure:

The level of the comparator positive input terminal CMP+ can be compared with the level of the comparator negative input terminal CMP-, and can also be compared with the internal BandGap reference voltage (near 1.27V).


2. STC15W series special function registers related to comparators

2.1 Comparator Control Register 1: CMPCR1

CMPCR1: Comparator Control Register 1

CMPEN: Comparator module enable bit


        CMPEN=1, enable the comparator module;


        CMPEN=0, disables the comparator module and turns off the power supply of the comparator.


In the case where CMPEN is 1:


When the comparison result of the comparator changes from LOW to HIGH, if PIE is set to 1, then one of the built-in


A register called CMPIF_p will be set to 1;


When the comparison result of the comparator changes from HIGH to LOW, if NIE is set to 1, then one of the built-in


A register called CMPIF_n will be set to 1;


When the CPU reads the value of CMPIF, it will read (CMPIF_p || CMPIF_n);


When the CPU writes 0 to CMPIF, both CMPIF_p and CMPIF_n will be cleared to 0.


The condition for interrupt generation is [ (EA==1) && (((PIE==1)&&(CMPIF_p==1)) || ((NIE==1)&&(CM


PIF_n==1))) ]After the CPU receives an interrupt, it does not automatically clear the CMPIF flag. The user must use software to write "0" to clear it.


PIE: Comparator rising edge interrupt enable bit (Pos-edge Interrupt Enabling)


        PIE = 1, enable the comparator to change from LOW to HIGH event to set CMPIF_p/generate an interrupt;


        PIE = 0, disables the comparator LOW to HIGH event to set CMPIF_p/generate an interrupt.


NIE: Comparator falling edge interrupt enable bit (Neg-edge Interrupt Enabling)


        NIE = 1, enable the event of the comparator changing from HIGH to LOW to set CMPIF_n/generate an interrupt;


        NIE = 0, disables the comparator HIGH to LOW event to set CMPIF_n/generate an interrupt.


PIS: Comparator positive selection bit


        PIS = 1, select ADCIN selected by ADCIS[2:0] as the positive input source of the comparator


        PIS = 0, select external P5.5 as the positive input source of the comparator


NIS: Comparator negative selection bit


        NIS = 1, select external pin P5.4 as the negative input source of the comparator


        NIS = 0, select the internal BandGap voltage BGV as the negative input source of the comparator


CMPOE: comparison result output control bit


        CMPOE = 1, enables the comparison result of the comparator to be output to P1.2;


        CMPOE = 0, disable the comparator comparison result output


CMPRES: Comparator Result flag


        CMPRES = 1, the level of CMP+ is higher than the level of CMP- (or the level of the internal BandGap reference voltage);


        CMPRES = 0, the level of CMP+ is lower than the level of CMP- (or the level of the internal BandGap reference voltage)


This bit is a "read-only" bit; software writing to it has no meaning. The result of the software reading


It is the "result after ENLCCTL control", not the direct output result of the Analog comparator.


2.2 Comparator Control Register 2: CMPCR2

Comparator Control Register 2

INVCMPO: Inverse Comparator Output


        INVCMPO = 1, the comparator is inverted and then output to P1.2;


        INVCMPO = 0, comparator output is normal.


The output of the comparator uses the "result after ENLCCTL control" rather than the direct output result of the Analog comparator.


DISFLT: Remove the 0.1uS Filter from the comparator output


        DISFLT = 1, turn off the comparator output 0.1uS Filter (can slightly increase the comparator speed);


        DISFLT = 0, the comparator output has a 0.1uS filter.


LCDTY[5:0]: Comparator output level-change control filter length (Duty) selection


bbbbbb:=


When the comparator changes from LOW to HIGH, it must detect that the subsequent HIGH lasts for at least bbbbbb clocks.


The analog comparator output is determined to be changed from LOW to HIGH within bbbbbb clocks.


The output returns to LOW, and the chip circuit thinks nothing has happened, and regards the comparator output as always being LOW.


When the comparator changes from HIGH to LOW, it must detect that the subsequent LOW continues for at least bbbbbb clocks.


The analog comparator output is determined to be changed from HIGH to LOW within bbbbbb clocks.


The output returns to HIGH, and the chip circuit thinks nothing has happened, as if the comparator output has always been HIGH.


If it is set to 000000, it means there is no Level-Change Control.

3. Comparator Test Procedure

#include "stc15.h"

void CmpInit();

 

void main()

{

CmpInit();

EA = 1; // CPU opens interrupt

while (1);

}

// Initialize the comparator module

void CmpInit()

{

// P5.5 is the positive pole of the comparator

// P5.4 is the negative pole of the comparator

// P1.2 is the comparator result output port

CMPCR1 = 0; // Initialize the comparator

CMPCR1 |= 0x80; // Enable the comparator module

CMPCR1 |= 0x02; // Enable the comparison result of the comparator to be output to P1.2

CMPCR1 |= 0x20; // Enable comparator rising edge interrupt

}

//Comparator interrupt service function

void cmp_isr() interrupt 21 //Comparator interrupt vector entry

{

     CMPCR1 &= ~0x40; // Clear completion flag

     P12 = CMPCR1 & 0x01; // Output the comparator result CMPRES to the test port for display

}

Keywords:STC15W408AS Reference address:12.STC15W408AS MCU Comparator

Previous article:13.STC15W408AS MCU SPI
Next article:11.STC15W408AS MCU CCP/PCA/PWM Application

Recommended ReadingLatest update time:2024-11-15 07:37

74HC138 experimental routine based on 51 single chip microcomputer
**************************  Copyright(C)CaKe  ***************************************** ========================================================= ============================ ===============**File name: main.c ============== ==============**Author : CaKen ============== ===============**Version number: V1.0 ==
[Microcontroller]
Summary of issues related to crystal oscillators for 51 microcontrollers
Preface When learning the 51 microcontroller, there are always many questions about the crystal oscillator. In fact, the crystal oscillator is like the human heart, the pulse of the blood. After understanding the crystal oscillator problem of the microcontroller, other problems with the 51 microcontroller can be easil
[Microcontroller]
Single chip spot welding machine control complete program + PCB circuit diagram
Uploaded on 2019-8-25 15:20     The microcontroller source program is as follows: /*********************************************************************************** * * * Spot welding machine controller * -------------------------------------------------------------------------------- * Structure description: 51
[Microcontroller]
Single chip spot welding machine control complete program + PCB circuit diagram
51 MCU controls LED lights through WIFI module ESP8266
1. System Solution The mobile phone APP controls the switch of the LED light through the ESP8266 WIFI module and the 51 single-chip microcomputer. The lower computer is composed of the single-chip microcomputer, ESP8266 module and LED light, and the upper computer is the Android mobile phone APP. We send the switch
[Microcontroller]
51 MCU controls LED lights through WIFI module ESP8266
STM32 MCU serial port remapping
Hello everyone, starting from today, I will share and discuss the problems I encountered in my work and my learning experience with you. Let me tell you about the port remapping of the STM32 microcontroller, because I use myself as an example. Here is the remapping of USART1 as an example.                 Because I wa
[Microcontroller]
STM32 MCU serial port remapping
STC89C52 microcontroller experiment 1-light up a light-emitting diode
/*------------------------------------------------ -------------------------- REG51.H Header file for generic 80C51 and 80C31 microcontroller. Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc. All rights reserved. -------------------------------------------------- --------------------------*/ #i
[Microcontroller]
PIC16F877A MCU realizes HT1621 LCD display control
Introduction: This article uses the PIC16F877A microcontroller to implement the HT1621 LCD display control. The hardware circuit has few hardware, small size, simple structure, high stability, and can accurately display the leakage, overload, undervoltage and other indications of the multi-functional fully automatic i
[Microcontroller]
PIC16F877A MCU realizes HT1621 LCD display control
51 MCU interrupt control
The 51 series MCU has 5 interrupt sources and 2 priorities, which can realize the nested structure of two-level interrupt services. Now many 51-compatible MCUs have 4 priorities (or more) and more interrupt sources. --------------------- If you want to study the nesting of interrupts, you should start with the interru
[Microcontroller]
Latest Microcontroller Articles
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号