|
下面是中断定时处理按键的proteus仿真原理图(工程文件可到本帖附件中下载):
单片机中断定时处理按键源程序如下:
- //#include <reg51.h>
- # include <STC12C5A60S2.h>
- # define uchar unsigned char
- # define vtime 3000 //定时3ms,一帧8*3=24ms,频率=40Hz
- //共阴数码管段码表
- uchar code distable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- //位选码表
- uchar code numi[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- //显示缓存
- uchar V_ram[]={1,2,3,4,5,6,7,8};
- uchar wi=0; //位选循环变量
- main() //m1:m0 00=标准; 01=推挽; 10=输入; 11=开漏输出
- {
- //m1:m0 00=标准; 01=推挽; 10=输入; 11=开漏输出
- P0M1 = 0X00;
- P0M0 = 0Xff;
- P2M1 = 0X00;
- P2M0 = 0Xff; //设定P0,P2推挽输出
- TMOD = 0X01; //设定定时器0为16位计数方式
- TH0 = (65536-vtime )/256;
- TL0 = (65536-vtime )%256; //赋定时器0初值
- ET0 = 1; //开定时器0中断
- EA = 1; //开总中断
- TR0 = 1; //启动定时器0计数
- while(1);
- }
- void t0_isp() interrupt 1
- {
- uchar dm,wx;
- TH0 = (65536-vtime )/256;
- TL0 = (65536-vtime )%256; //赋定时器0初值
- dm=distable[V_ram[wi]]; //取显示段码
- wx=numi[wi]; //取位选码
- P2=0x00; //关显示
- P0=dm; //段码赋给P0口
- P2=wx; //点亮位选的那个数码管
- wi++;
- if(wi==8)wi=0;
- }
-
复制代码
单片机中断定时实现数码管扫描原理图:
- //#include <reg51.h>
- # include <STC12C5A60S2.h>
- # define uchar unsigned char
- # define vtime 3000 //定时3ms,一帧8*3=24ms,频率=40Hz
- //共阴数码管段码表
- uchar code distable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- //位选码表
- uchar code numi[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- //显示缓存
- uchar V_ram[]={1,2,3,4,5,6,7,8};
- uchar wi=0; //位选循环变量
- main() //m1:m0 00=标准; 01=推挽; 10=输入; 11=开漏输出
- {
- //m1:m0 00=标准; 01=推挽; 10=输入; 11=开漏输出
- P0M1 = 0X00;
- P0M0 = 0Xff;
- P2M1 = 0X00;
- P2M0 = 0Xff; //设定P0,P2推挽输出
- TMOD = 0X01; //设定定时器0为16位计数方式
- TH0 = (65536-vtime )/256;
- TL0 = (65536-vtime )%256; //赋定时器0初值
- ET0 = 1; //开定时器0中断
- EA = 1; //开总中断
- TR0 = 1; //启动定时器0计数
- while(1);
- }
- void t0_isp() interrupt 1
- {
- uchar dm,wx;
- TH0 = (65536-vtime )/256;
- TL0 = (65536-vtime )%256; //赋定时器0初值
- dm=distable[V_ram[wi]]; //取显示段码
- wx=numi[wi]; //取位选码
- P2=0x00; //关显示
- P0=dm; //段码赋给P0口
- P2=wx; //点亮位选的那个数码管
- …………限于本文篇幅 余下代码请从51黑下载附件…………
-
复制代码
最后给大家分享一些我们老师给的一些经典的单片机程序源码, 一共有十多个.都有详细的注释,然大家快速的理解每一行代码的意思。而且有proteus仿真原理图。大家可以直接验证程序的对错.
本系列所有源码打包下载地址(含proteus仿真工程文件和源程序):
http://www.51hei.com/bbs/dpj-82474-1.html
本例程下载:
中断定时keytest.rar
(52.86 KB, 下载次数: 30)
中断定时scan_seg.rar
(52.94 KB, 下载次数: 26)
|
|