当前位置:网站首页>[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
[stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
2022-04-23 14:45:00 【perseverance52】
【STC8G2K64S4】 Comparator introduction and comparator power down detection example program
STC8GK2 Introduction to comparator
STC8G A comparator is integrated in the series MCU . The positive pole of the comparator can be P3.7 Port or ADC Analog input channel , The negative electrode can P3.6 Or internal port BandGap after OP After REFV voltage ( Internal fixed comparison voltage ). The application of multiple comparators can be realized through multiplexer and time-sharing multiplexing .
There is a programmable two-stage filter inside the comparator : Analog filtering and digital filtering . Analog filtering can filter out the burr signal in the comparison input signal , Digital filtering can wait for the input signal to be more stable before comparison . The comparison result can be obtained directly by reading the internal register bit , The result of the comparator can also be output to the external port in the forward or reverse direction . The comparison result is output to the external port, which can be used as trigger signal and feedback signal of external events , It can expand the application scope of comparison .
- Internal structure diagram of comparator
- Comparator related registers
- Comparator control register 1(CMPCR1)
- Comparator control register 2(CMPCR2)
- Wiring instructions
Use of comparator ( Interrupt mode )
/*STC8G2 MCU interrupt mode to obtain P36 Pin voltage value and internal voltage 1.19V Compare the voltage , Output the result to P10, When P36 Pin voltage is higher than 1.19v when , be P10 Output high level , lower than 1.19V An interrupt is triggered ,P10 Output low level */
#include "reg51.h"
#include "intrins.h"
sfr CMPCR1 = 0xe6;
sfr CMPCR2 = 0xe7;
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P4M1 = 0xb3;
sfr P4M0 = 0xb4;
sfr P5M1 = 0xc9;
sfr P5M0 = 0xca;
sbit P10 = P1^0;
sbit P11 = P1^1;
void cmp() interrupt 21 using 1
{
CMPCR1 &= 0xbf;// Manually clear the interrupt flag ,1011,1111
P10 = (CMPCR1 & 0x01); // Compare the result of the comparator CMPRES Output to the test port to display
}
void main()
{
P0M0 = 0x00;// Set up a IO The port is a quasi two-way port
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P10 =0;// initial P10 The port is low
CMPCR2 = 0x00;
CMPCR2 &= ~0x80; // Comparator forward output
// CMPCR2 |= 0x80; // Comparator reverse output
CMPCR2 &= ~0x40; // Can make 0.1us wave filtering
// CMPCR2 |= 0x40; // prohibit 0.1us wave filtering
// CMPCR2 &= ~0x3f; // The result of the comparator is output directly
CMPCR2 |= 0x10; // The comparator result passes through 16 Output after de dithering the clock
CMPCR1 = 0x00;
CMPCR1 |= 0x30; // Enable comparator edge interrupt
// CMPCR1 &= ~0x20; // Disable comparator rising edge interrupt
// CMPCR1 |= 0x20; // Enable the rising edge of the comparator to interrupt
// CMPCR1 &= ~0x10; // Disable the falling edge interrupt of the comparator
// CMPCR1 |= 0x10; // Enable the falling edge of the comparator to interrupt
CMPCR1 &= ~0x08; //P3.7 by CMP+ Input foot
// CMPCR1 |= 0x08; //ADC Input pin is CMP+ Input foot
CMPCR1 &= ~0x04; // Inside 1.19V The reference signal source is CMP- Input foot
// CMPCR1 |= 0x04; //P3.6 by CMP- Input foot
// CMPCR1 &= ~0x02; // Disable comparator output
CMPCR1 |= 0x02; // Enable comparator output
CMPCR1 |= 0x80; // Enable comparator module
EA =1;
while (1)
{
}
}
Use of comparator ( A query )
If the query method is , Is in the while In the loop, the status of the corresponding register is continuously queried to determine the current comparator input pin (P37 mouth ) The voltage value of .
/*STC8G2 MCU interrupt mode to obtain P36 Pin voltage value and internal voltage 1.19V Compare the voltage , Output the result to P10, When P36 Pin voltage is higher than 1.19v when , be P10 Output high level , lower than 1.19V An interrupt is triggered ,P10 Output low level */
#include "reg51.h"
#include "intrins.h"
sfr CMPCR1 = 0xe6;
sfr CMPCR2 = 0xe7;
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P4M1 = 0xb3;
sfr P4M0 = 0xb4;
sfr P5M1 = 0xc9;
sfr P5M0 = 0xca;
sbit P10 = P1^0;
sbit P11 = P1^1;
//void cmp() interrupt 21 using 1
//{
// CMPCR1 &= 0xbf;// Manually clear the interrupt flag ,1011,1111
// P10 = (CMPCR1 & 0x01); // Compare the result of the comparator CMPRES Output to the test port to display
//}
void main()
{
P0M0 = 0x00;// Set up a IO The port is a quasi two-way port
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P10 =0;// initial P10 The port is low
CMPCR2 = 0x00;
CMPCR2 &= ~0x80; // Comparator forward output
// CMPCR2 |= 0x80; // Comparator reverse output
CMPCR2 &= ~0x40; // Can make 0.1us wave filtering
// CMPCR2 |= 0x40; // prohibit 0.1us wave filtering
// CMPCR2 &= ~0x3f; // The result of the comparator is output directly
CMPCR2 |= 0x10; // The comparator result passes through 16 Output after de dithering the clock
CMPCR1 = 0x00;
CMPCR1 |= 0x30; // Enable comparator edge interrupt
// CMPCR1 &= ~0x20; // Disable comparator rising edge interrupt
// CMPCR1 |= 0x20; // Enable the rising edge of the comparator to interrupt
// CMPCR1 &= ~0x10; // Disable the falling edge interrupt of the comparator
// CMPCR1 |= 0x10; // Enable the falling edge of the comparator to interrupt
CMPCR1 &= ~0x08; //P3.7 by CMP+ Input foot
// CMPCR1 |= 0x08; //ADC Input pin is CMP+ Input foot
CMPCR1 &= ~0x04; // Inside 1.19V The reference signal source is CMP- Input foot
// CMPCR1 |= 0x04; //P3.6 by CMP- Input foot
// CMPCR1 &= ~0x02; // Disable comparator output
CMPCR1 |= 0x02; // Enable comparator output
CMPCR1 |= 0x80; // Enable comparator module
// EA =1;
while (1)
{
// CMPCR1 &= 0xbf;// Manually clear the interrupt flag ,1011,1111
P10= (CMPCR1 & 0x01); // Compare the result of the comparator CMPRES Output to the test port to display
}
}
As a power down detection , It is best to use interrupt mode , To deal with Events , take P37
As an external voltage detection port , If the internal voltage is used for comparison , Namely 1.19V As a comparative value . It is recommended to use P36
As CMP-
Input foot , Receive 3.3V
On voltage , Single chip microcomputer adopts 5V Power supply ,P37 Pin detection 5V Voltage value , When the detection voltage is lower than 3.3V when , The interrupt will be triggered , It is equivalent to increasing the reference voltage of the comparison to 3.3V, The implementation related code is as follows :CMPCR1 |= 0x04; //P3.6 by CMP- Input foot
- P36 As CMP- Enter the pin code
/*STC8G2 MCU interrupt mode to obtain P36 Pin voltage value and internal voltage 1.19V Compare the voltage , Output the result to P10, When P36 Pin voltage is higher than 1.19v when , be P10 Output high level , lower than 1.19V An interrupt is triggered ,P10 Output low level */
#include "reg51.h"
#include "intrins.h"
sfr CMPCR1 = 0xe6;
sfr CMPCR2 = 0xe7;
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P4M1 = 0xb3;
sfr P4M0 = 0xb4;
sfr P5M1 = 0xc9;
sfr P5M0 = 0xca;
sbit P10 = P1^0;
sbit P11 = P1^1;
void cmp() interrupt 21 using 1
{
CMPCR1 &= 0xbf;// Manually clear the interrupt flag ,1011,1111
P10 = (CMPCR1 & 0x01); // Compare the result of the comparator CMPRES Output to the test port to display
}
void main()
{
P0M0 = 0x00;// Set up a IO The port is a quasi two-way port
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P10 =0;// initial P10 The port is low
CMPCR2 = 0x00;
CMPCR2 &= ~0x80; // Comparator forward output
// CMPCR2 |= 0x80; // Comparator reverse output
CMPCR2 &= ~0x40; // Can make 0.1us wave filtering
// CMPCR2 |= 0x40; // prohibit 0.1us wave filtering
// CMPCR2 &= ~0x3f; // The result of the comparator is output directly
CMPCR2 |= 0x10; // The comparator result passes through 16 Output after de dithering the clock
CMPCR1 = 0x00;
CMPCR1 |= 0x30; // Enable comparator edge interrupt
// CMPCR1 &= ~0x20; // Disable comparator rising edge interrupt
// CMPCR1 |= 0x20; // Enable the rising edge of the comparator to interrupt
// CMPCR1 &= ~0x10; // Disable the falling edge interrupt of the comparator
// CMPCR1 |= 0x10; // Enable the falling edge of the comparator to interrupt
CMPCR1 &= ~0x08; //P3.7 by CMP+ Input foot
// CMPCR1 |= 0x08; //ADC Input pin is CMP+ Input foot
// CMPCR1 &= ~0x04; // Inside 1.19V The reference signal source is CMP- Input foot
CMPCR1 |= 0x04; //P3.6 by CMP- Input foot
// CMPCR1 &= ~0x02; // Disable comparator output
CMPCR1 |= 0x02; // Enable comparator output
CMPCR1 |= 0x80; // Enable comparator module
EA =1;
while (1)
{
// P10= (CMPCR1 & 0x01); // Compare the result of the comparator CMPRES Output to the test port to display
}
}
版权声明
本文为[perseverance52]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231426130235.html
边栏推荐
- 555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等
- Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
- Master in minutes --- ternary operator (ternary operator)
- Arduino for esp8266串口功能简介
- 51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
- One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
- Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
- MySQL error packet out of order
- PWM speed regulation control system of DC motor based on 51 single chip microcomputer (with complete set of data such as Proteus simulation + C program)
- 单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
猜你喜欢
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
【JZ46 把数字翻译成字符串】
面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
ASEMI整流模块MDQ100-16在智能开关电源中的作用
51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
Find daffodils - for loop practice
AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等
Swift Protocol 关联对象 资源名称管理 多线程GCD 延迟 once
A good tool: aardio
We reference My97DatePicker to realize the use of time plug-in
随机推荐
epoll 的 ET,LT工作模式———实例程序
【JZ46 把数字翻译成字符串】
AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等
Parameter stack pressing problem of C language in structure parameter transmission
电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
一个月把字节,腾讯,阿里都面了,写点面经总结……
Achievements in science and Technology (21)
详解TCP的三次握手
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
PCIe X1 插槽的主要用途是什么?
QT actual combat: Yunxi chat room
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
we引用My97DatePicker 实现时间插件使用
The initial C language framework is suitable for review and preliminary understanding
MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
多语言通信基础 06 go实现grpc的四种数据流模式实现
自动化的艺术
51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
Model location setting in GIS data processing -cesium