秒表的程序和编程思路,分享给大家共同学习
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg52.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- /********端口定义*********/
- #define DATA P0
- sbit seg_sel = P1^4;
- sbit bit_sel = P1^5;
- sbit start = P3^2;
- sbit store = P3^3;
- sbit diskey = P3^4;
- sbit reset = P3^5;
- sbit led = P1^0;
- /********变量定义*********/
- u16 tem_ms;
- u8 n_ten_ms,n_sec,n_min;
- u8 run_flag;//当run_flag为1,代表开始计数
- u8 temp_buf[5][3];
- u8 arr_pt;
- u8 code seg_tab[]={
- 0xc0,0xf9,0xa4,0xb0,
- 0x99,0x92,0x82,0xf8,
- 0x80,0x90,0x88,0x83,
- 0xc6,0xa1,0x86,0x8e
- };
- u8 code bit_tab[]={
- 0x10,0x20,0x40,0x80,
- 0x01,0x02,0x04,0x08
- };
- void delay(unsigned int y)
- {
- unsigned int x,z;
- for(x=y; x>0; x--)
- for(z=110; z>0; z--);
- }
- void display_led(u8 which_bit, u8 which_number)
- {
- bit_sel = 1;//Q[7..0]=D[7..0]
- DATA = bit_tab[which_bit];
- bit_sel = 0;//BIT[7..0]=0x80
- seg_sel = 1;//Q[7..0]=D[7..0]
- DATA = seg_tab[which_number];
- seg_sel = 0;
- }
- void display(u8 which_bit, u8 which_number)
- {
- u8 x,y;
- x = which_number/10;//分离十位
- y = which_number%10;//分离个位
- if (which_bit == 1)
- {
- display_led(1, x);
- delay(2);
- display_led(0, y);
- delay(2);
- }
- if (which_bit == 2)
- {
- display_led(4, x);
- delay(2);
- display_led(3, y);
- delay(2);
- }
- if (which_bit == 3)
- {
- display_led(7, x);
- delay(2);
- display_led(6, y);
- delay(2);
- }
- }
- void reflash (void)
- {
- display(1, n_ten_ms);
- display(2, n_sec);
- display(3, n_min);
- }
- void main (void)
- {
- TMOD = 0x01;//设置定时/计数器T0
- TH0 = (65535-10000) / 256;
- TL0 = (65535-10000) % 256;
- EA = 1;//开放总中断
- ET0 = 1;//开放定时器0的溢出中断
- //TR0 = 1;
- while (1)
- {
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
Keil.rar
(19.68 KB, 下载次数: 18)
|