EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
给大家分享一个51单片机+1602液晶屏显示 KY-040旋转编码器模块的程序和仿真
, j* v( l& Z, G- V
! f9 B- R d' Z$ u程序功能:旋转编码器顺时针旋转,显示数值增加
( R! E* ^+ b/ y/ ]) F1 x+ h$ i" T 旋转编码器逆时针旋转,显示数值减小
8 e! i; W) [) m3 `1 f/ r 旋转编码器按下,切换显示ON/OFF2 e) h$ P) }% e9 u- N
# |# |# \7 [' R+ v$ A; U
根据旋转编码器KY-040模块资料可知:
, R& x2 B. Z; [" P$ S. T2 e6 d/ l' x, N* x
% N. s$ k% u! A6 W+ b( E( ?2 X7 R
在下降触发模式下,A和B进行电平比较:! r1 i K( [+ n" }+ ~
对A触发的中断:同加异减
1 ?- I( U: c7 W对B触发的中断:同减异加7 A6 m5 y5 u) M$ I9 e9 W
反之亦然 / b- I2 z+ p5 R" A. D
那么可以通过手动模拟外部中断触发瞬间的电平状态来进行仿真, CLK(A) ---- P3^2 DT(B) ---- P3^3 SW(Z) ---- P3^4 外部中断0下降沿触发后,对B的电平进行判断即可知道旋转编码器 的旋转方向。 ![]() ![]() ![]()
$ }) ^4 b% K* g5 p- ^& R/ B仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)( ]+ V0 @* i- L' ?( n# Z3 s! g D
6 l9 |8 d! j6 R5 E" a
- \3 R1 N, q$ _% s单片机源程序如下:
1 j# e0 c$ W! M; J2 |! ]" M- #include<reg51.h>
- #include"lcd.h"
- #include<intrins.h>
- //外部中断的IO
- sbit K1=P3^2;
- sbit K2=P3^3;
- sbit K3=P3^4;
- void IntConfiguration();
- void Delay(unsigned int n);
- unsigned char KeyValue=0;
- unsigned char state=0;
- /*******************************************************************************
- * 函 数 名 : main
- * 函数功能 : 主函数
- * 输 入 : 无
- * 输 出 : 无
- *******************************************************************************/
- void main(void)
- {
- LcdInit();
- IntConfiguration();
- LCDWrite_String(2, 0, 10, "Bian ma qi");
- while(1)
- {
- if(K3==0)
- {
- Delay(1); //延时消抖
- if(K3==0)
- {
- state=!state;
- while(K3==0);
- }
- }
- if(state==1)
- {
- LcdWriteCom(0xc0+13);
- LcdWriteData('O');
- LcdWriteData('N');
- LcdWriteData(' ');
- }
- else
- {
- LcdWriteCom(0xc0+13);
- LcdWriteData('O');
- LcdWriteData('F');
- LcdWriteData('F');
- }
- LcdWriteCom(0xC0+4);
- LcdWriteData('0'+KeyValue/100%10);
- LcdWriteData('0'+KeyValue/10%10);
- LcdWriteData('0'+KeyValue%10);
- }
- }
- /*******************************************************************************
- * 函 数 名 : IntConfiguration()
- * 函数功能 : 设置外部中断
- * 输 入 : 无
- * 输 出 : 无
- *******************************************************************************/
- void IntConfiguration()
- {
- //设置INT0
- IT0=1;//跳变沿出发方式(下降沿)
- EX0=1;//打开INT0的中断允许。
- EA=1;//打开总中断
- }
- /*******************************************************************************
- * 函 数 名 : Delay(unsigned int n)
- * 函数功能 : 延时
- * 输 入 : n
- * 输 出 : 无
- *******************************************************************************/
- void Delay(unsigned int n) //延时50us误差 0us
- {
- unsigned char a,b;
- for(;n>0;n--)
- {
- for(b=1;b>0;b--)
- for(a=22;a>0;a--);
- }
- }
- /*******************************************************************************
- * 函 数 名 : Int0() interrupt 0
- * 函数功能 : 外部中断0的中断函数
- * 输 入 : 无
- * 输 出 : 无
- *******************************************************************************/
- void Int0() interrupt 0 //外部中断0的中断函数
- {
- if(K2==1)
- {
- KeyValue++;
- }
- if(K2==0)
- {
- KeyValue--;
- }
- if(KeyValue>200)
- KeyValue=200;
- if(KeyValue<1)
- KeyValue=1;
- }# N( d$ w. F" Q1 p& C" G. i
+ q0 R) E& @+ Z" @- V |