当前位置:网站首页>【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
2022-04-23 14:26:00 【perseverance52】
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
STC8GK2比较器简介
STC8G 系列单片机内部集成了一个比较器。比较器的正极可以是 P3.7 端口或者 ADC 的模拟输入通道,而负极可以 P3.6 端口或者是内部 BandGap 经过 OP 后的 REFV 电压(内部固定比较电压)。通过多路选择器和分时复用可实现多个比较器的应用.
比较器内部有可程序控制的两级滤波:模拟滤波和数字滤波。模拟滤波可以过滤掉比较输入信号中的毛刺信号,数字滤波可以等待输入信号更加稳定后再进行比较。比较结果可直接通过读取内部寄存器位获得,也可将比较器结果正向或反向输出到外部端口。将比较结果输出到外部端口可用作外部事件的触发信号和反馈信号,可扩大比较的应用范围。
- 比较器内部结构图

- 比较器相关的寄存器

- 比较器控制寄存器 1(CMPCR1)


- 比较器控制寄存器 2(CMPCR2)



- 接线说明

比较器的使用(中断方式)
/*STC8G2单片机中断方式获取P36引脚电压值与内部1.19V电压进行比较,将结果输出到P10, 当P36引脚电压高于1.19v时,则P10输出高电平,低于1.19V则触发中断,P10输出低电平*/
#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;//手动清除清除中断标志,1011,1111
P10 = (CMPCR1 & 0x01); //将比较器结果CMPRES输出到测试口显示
}
void main()
{
P0M0 = 0x00;//设置个IO端口为准双向口
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P10 =0;//初始P10端口为低电平
CMPCR2 = 0x00;
CMPCR2 &= ~0x80; //比较器正向输出
// CMPCR2 |= 0x80; //比较器反向输出
CMPCR2 &= ~0x40; //使能 0.1us 滤波
// CMPCR2 |= 0x40; //禁止 0.1us 滤波
// CMPCR2 &= ~0x3f; //比较器结果直接输出
CMPCR2 |= 0x10; //比较器结果经过 16 个去抖时钟后输出
CMPCR1 = 0x00;
CMPCR1 |= 0x30; //使能比较器边沿中断
// CMPCR1 &= ~0x20; //禁止比较器上升沿中断
// CMPCR1 |= 0x20; //使能比较器上升沿中断
// CMPCR1 &= ~0x10; //禁止比较器下降沿中断
// CMPCR1 |= 0x10; //使能比较器下降沿中断
CMPCR1 &= ~0x08; //P3.7 为 CMP+输入脚
// CMPCR1 |= 0x08; //ADC 输入脚为 CMP+输入脚
CMPCR1 &= ~0x04; //内部 1.19V 参考信号源为 CMP-输入脚
// CMPCR1 |= 0x04; //P3.6 为 CMP-输入脚
// CMPCR1 &= ~0x02; //禁止比较器输出
CMPCR1 |= 0x02; //使能比较器输出
CMPCR1 |= 0x80; //使能比较器模块
EA =1;
while (1)
{
}
}
比较器的使用(查询方式)
查询方式的话,就是在while循环里面不断查询对应寄存器的状态来确定当前比较器输入引脚(P37口)的电压值。
/*STC8G2单片机中断方式获取P36引脚电压值与内部1.19V电压进行比较,将结果输出到P10, 当P36引脚电压高于1.19v时,则P10输出高电平,低于1.19V则触发中断,P10输出低电平*/
#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;//手动清除清除中断标志,1011,1111
// P10 = (CMPCR1 & 0x01); //将比较器结果CMPRES输出到测试口显示
//}
void main()
{
P0M0 = 0x00;//设置个IO端口为准双向口
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P10 =0;//初始P10端口为低电平
CMPCR2 = 0x00;
CMPCR2 &= ~0x80; //比较器正向输出
// CMPCR2 |= 0x80; //比较器反向输出
CMPCR2 &= ~0x40; //使能 0.1us 滤波
// CMPCR2 |= 0x40; //禁止 0.1us 滤波
// CMPCR2 &= ~0x3f; //比较器结果直接输出
CMPCR2 |= 0x10; //比较器结果经过 16 个去抖时钟后输出
CMPCR1 = 0x00;
CMPCR1 |= 0x30; //使能比较器边沿中断
// CMPCR1 &= ~0x20; //禁止比较器上升沿中断
// CMPCR1 |= 0x20; //使能比较器上升沿中断
// CMPCR1 &= ~0x10; //禁止比较器下降沿中断
// CMPCR1 |= 0x10; //使能比较器下降沿中断
CMPCR1 &= ~0x08; //P3.7 为 CMP+输入脚
// CMPCR1 |= 0x08; //ADC 输入脚为 CMP+输入脚
CMPCR1 &= ~0x04; //内部 1.19V 参考信号源为 CMP-输入脚
// CMPCR1 |= 0x04; //P3.6 为 CMP-输入脚
// CMPCR1 &= ~0x02; //禁止比较器输出
CMPCR1 |= 0x02; //使能比较器输出
CMPCR1 |= 0x80; //使能比较器模块
// EA =1;
while (1)
{
// CMPCR1 &= 0xbf;//手动清除清除中断标志,1011,1111
P10= (CMPCR1 & 0x01); //将比较器结果CMPRES输出到测试口显示
}
}
作为掉电检测的话,最好使用中断方式,来处理事件,将P37作为外部电压检测口,如果使用内部电压进行比较的话,就是1.19V作为比较值。建议使用P36作为CMP-输入脚,接到3.3V电压上,单片机采用5V供电,P37引脚检测5V电压值,当检测电压低于3.3V时,就会触发中断,相当于将比较的基准电压提高到了3.3V,实现相关代码如下:CMPCR1 |= 0x04; //P3.6 为 CMP-输入脚
- P36 作为CMP-输入脚代码
/*STC8G2单片机中断方式获取P36引脚电压值与内部1.19V电压进行比较,将结果输出到P10, 当P36引脚电压高于1.19v时,则P10输出高电平,低于1.19V则触发中断,P10输出低电平*/
#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;//手动清除清除中断标志,1011,1111
P10 = (CMPCR1 & 0x01); //将比较器结果CMPRES输出到测试口显示
}
void main()
{
P0M0 = 0x00;//设置个IO端口为准双向口
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P10 =0;//初始P10端口为低电平
CMPCR2 = 0x00;
CMPCR2 &= ~0x80; //比较器正向输出
// CMPCR2 |= 0x80; //比较器反向输出
CMPCR2 &= ~0x40; //使能 0.1us 滤波
// CMPCR2 |= 0x40; //禁止 0.1us 滤波
// CMPCR2 &= ~0x3f; //比较器结果直接输出
CMPCR2 |= 0x10; //比较器结果经过 16 个去抖时钟后输出
CMPCR1 = 0x00;
CMPCR1 |= 0x30; //使能比较器边沿中断
// CMPCR1 &= ~0x20; //禁止比较器上升沿中断
// CMPCR1 |= 0x20; //使能比较器上升沿中断
// CMPCR1 &= ~0x10; //禁止比较器下降沿中断
// CMPCR1 |= 0x10; //使能比较器下降沿中断
CMPCR1 &= ~0x08; //P3.7 为 CMP+输入脚
// CMPCR1 |= 0x08; //ADC 输入脚为 CMP+输入脚
// CMPCR1 &= ~0x04; //内部 1.19V 参考信号源为 CMP-输入脚
CMPCR1 |= 0x04; //P3.6 为 CMP-输入脚
// CMPCR1 &= ~0x02; //禁止比较器输出
CMPCR1 |= 0x02; //使能比较器输出
CMPCR1 |= 0x80; //使能比较器模块
EA =1;
while (1)
{
// P10= (CMPCR1 & 0x01); //将比较器结果CMPRES输出到测试口显示
}
}
版权声明
本文为[perseverance52]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_42880082/article/details/124360835
边栏推荐
- Qt实战:云曦聊天室篇
- After entering the new company, the operation and maintenance engineer can understand the deployment of the system from the following items
- On the insecurity of using scanf in VS
- 想要成为架构师?夯实基础最重要
- Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
- 外包干了四年,废了...
- 8.5 循环神经网络简洁实现
- KVM learning resources
- 51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
- 顺序表的操作,你真的学会了吗?
猜你喜欢

API Gateway/API 网关(三) - Kong的使用 - 限流rate limiting(redis)

Qt界面优化:Qt去边框与窗体圆角化

ASEMI超快恢复二极管与肖特基二极管可以互换吗

Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code

Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)

51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code

Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data

单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序

LotusDB 设计与实现—1 基本概念

KVM learning resources
随机推荐
TUN 设备原理
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)
Four ways of SSH restricting login
ssh限制登录的四种手段
Debug on TV screen
Detailed explanation of C language knowledge points -- first knowledge of C language [1]
QT interface optimization: QT border removal and form rounding
ASEMI三相整流桥和单相整流桥的详细对比
SSH 通过跳板机连接远程主机
First acquaintance with STL
流程控制之分支语句
常见存储类型和FTP主被动模式解析
爬虫练习题(一)
8.2 文本预处理
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码
Uni app message push
Tongxin UOS php7 2.3 upgrade to php7.0 two point two four