这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界» 论坛首页» DIY与开源设计» 电子DIY» 1.8TFT+ PS2键盘+1602+18B20+数码管+秒表+按键

共24条 3/3 1 2 3 跳转至
工程师
2011-12-26 21:53:13 打赏
21楼

#include //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义

code unsigned char tab[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//共阴数码管 0-9

unsigned char Dis_Shiwei; //定义十位
unsigned char Dis_Gewei; //定义个位
unsigned char S_Dis_Shiwei;//定义秒十位
unsigned char S_Dis_Gewei; //定义秒个位
unsigned char second,msecond;
/******************************************************************/
/* 函数声明 */
/******************************************************************/
void CLR(void);
/******************************************************************/
/* 延时函数 */
/******************************************************************/
void delay(unsigned int cnt)
{
while(--cnt);
}
/******************************************************************/
/* 主函数 */
/******************************************************************/
main()
{
EX0=1;//外部中断0设置
IT0=1;

EX1=1;//外部中断1设置
IT1=1;

TMOD |=0x01;//定时器设置 10ms in 12M crystal
TH0=0xd8;
TL0=0xf0;
ET0=1; //打开中断
TR0=0;

EA=1;
CLR();
while(1)
{
P0=S_Dis_Shiwei;//显示秒十位
P2=1;
delay(300); //短暂延时
P0=S_Dis_Gewei; //显示秒个位
P2=2;
delay(300);
P0=0x40; //显示秒个位
P2=3;
delay(300);
P0=Dis_Shiwei; //显示十位
P2=4;
delay(300); //短暂延时
P0=Dis_Gewei; //显示个位
P2=5;
delay(300);
}
}
/******************************************************************/
/* 定时器中断函数 */
/******************************************************************/
void tim(void) interrupt 1 using 1
{

TH0=0xd8;//重新赋值
TL0=0xf0;
msecond++;
if (msecond==100)
{
msecond=0;
second++;//秒加1
if(second==100)
second=0;
S_Dis_Shiwei=tab[second/10];//十位显示值处理
S_Dis_Gewei=tab[second%10]; //个位显示处理

}
Dis_Shiwei=tab[msecond/10];//十位显示值处理
Dis_Gewei=tab[msecond%10]; //个位显示处理

}
/******************************************************************/
/* 外部中断函数 */
/******************************************************************/
void ISR_INT0(void) interrupt 0 using 1
{
TR0=!TR0; //利用外部中断打开和关闭定时器0 用于开始和停止计时

}
void ISR_INT1(void) interrupt 2 using 1
{
if(TR0==0)//停止时才可以清零
CLR();
}

void CLR(void)
{
second=0; //利用外部中断清零
msecond=0;
Dis_Shiwei=tab[msecond/10];//十位显示值处理
Dis_Gewei=tab[msecond%10]; //个位显示处理
S_Dis_Shiwei=tab[second/10];//十位显示值处理
S_Dis_Gewei=tab[second%10]; //个位显示处理
}

工程师
2011-12-26 21:55:10 打赏
22楼
好了吧,我的MCU DIY学习过程到此结束了,随后把我写的几十个实验程序发上来,这几天正在考试,呵呵,马上放假了

菜鸟
2012-01-28 14:46:15 打赏
23楼

菜鸟
2015-11-22 21:42:25 打赏
24楼
楼主威武,学习一下

共24条 3/3 1 2 3 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册]