一. 硬件设计详解 说明: 选通一个 分两步, 第一步是位选,即选择哪个数码管亮, 这里位选是通过锁存器U2的WE1~WE6来选择第一个数码管到第六个数码管,由于数码管为共阴极(相对于8位数码管),那么这里WE端为0代表位选选通, 第二步是段选,即选择数码管的哪位亮, 这里段选是通过锁存器U1的a~h来选择数码管的8位管子,这里DUAN端为1代表段选选通. 二. 程序设计详解 1. 让第一个数码管显示8 #include <reg52.h> sbit duan = P2 ^ 6; sbit wela = P2 ^ 7; void main() { wela = 1; P0 = 0xfe; wela = 0; duan = 1; P0 = 0x7f; duan = 0; while (1); } 2. 让6个数码管显示反复显示0~F #include <reg52.h> #define uchar unsigned char #define uint unsigned int sbit duan = P2 ^ 6; sbit wela = P2 ^ 7; uchar code table[] = { 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71 }; void delayms(uint xms); void main() { uchar num; wela = 1; P0 = 0xc0; wela = 0; while (1) { for (num = 0; num < 16; num++) { duan = 1; P0 = table[num]; duan = 0; delayms(500); } } } void delayms(uint xms) { uint i, j; for (i = xms; i > 0; i--) for (j = 110; j > 0; j--) ; } 3. 第一个数码管显示1,时间为0.5s,然后关闭它, 第二个数码管显示2,时间为0.5s,然后关闭它, … 第六个数码管显示6,时间为0.5s,然后关闭它, 依次循环. #include <reg52.h> #define uchar unsigned char #define uint unsigned int sbit duan = P2 ^ 6; sbit wela = P2 ^ 7; uchar code we_table[] = { 0xfe,0xfd,0xfb,0xf7,0xef,0xdf }; uchar code du_table[] = { 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71 }; void delayms(uint xms); void main() { uchar num; while (1) { for (num = 0; num < 6; num++) { wela = 1; P0 = we_table[num]; wela = 0; P0 = 0xff; duan = 1; P0 = du_table[num+1]; duan = 0; delayms(1); } } } void delayms(uint xms) { uint i, j; for (i = xms; i > 0; i--) for (j = 110; j > 0; j--) ; } 说明: 只要将延时时间调整为1s,我们可以看到数码管一起显示123456. P0 = 0xff; 是用来消影, 即消除段选与位选之间的影响. 4. 定时器0的方式1实现第一个发光二极管以200ms间隔闪烁, 定时器1的方式1实现数码管前两位59s循环计时 #include <reg52.h> #define uchar unsigned char #define uint unsigned int sbit dula = P2 ^ 6; sbit wela = P2 ^ 7; sbit 1 = P1 ^ 0; uchar code table[] = { 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71 }; void delayms(uint); void display(uint); uchar num, num1, num2; void main() { TMOD = 0x11; TH0 = (65536 - 45872) / 256; TL0 = (65536 - 45872) % 256; TH1 = (65536 - 45872) / 256; TL1 = (65536 - 45872) % 256; EA = 1; ET0 = 1; ET1 = 1; TR0 = 1; TR1 = 1; while (1) { display(num); } } void display(uint num) { dula = 1; P0 = table[num / 10]; dula = 0; P0 = 0xff; wela = 1; P0 = 0xfe; wela = 0; delayms(5); dula = 1; P0 = table[num % 10]; dula = 0; P0 = 0xff; wela = 1; P0 = 0xfd; wela = 0; delayms(5); } void delayms(uint xms) { uint i, j; for (i = xms; i > 0; i--) for (j = 110; j > 0; j--) ; } void T0_time() interrupt 1 { TH0 = (65536 - 45872) / 256; TL0 = (65536 - 45872) % 256; num1++; if (num1 == 4) { num1 = 0; led1 = ~led1; } } void T1_time() interrupt 3 { TH1 = (65536 - 45872) / 256; TL1 = (65536 - 45872) % 256; num2++; if (num2 == 20) { num2 = 0; num++; if (num == 60) num = 0; } } 5. 用定时器以间隔500MS在6位数码管上依次显示0、1、2、3….C、D、E、F,重复 #include <reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char sbit dula = P2 ^ 6; sbit wela = P2 ^ 7; uchar num, tt; uchar code table[] = { 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71 }; void main() { num = 0; tt = 0; TMOD = 0x01; TH0 = (65536 - 45872) / 256; TL0 = (65536 - 45872) % 256; EA = 1; ET0 = 1; TR0 = 1; dula = 1; P0 = 0x3f; dula = 0; wela = 1; P0 = 0xc0; wela = 0; while (1) { if (tt == 10) { tt = 0; num++; if (num == 16) num = 0; dula = 1; P0 = table[num]; dula = 0; } } } void T0_timer() interrupt 1 { TH0 = (65536 - 45872) / 256; TL0 = (65536 - 45872) % 256; tt++; } 6. 利用动态扫描方法在六位数码管上显示出稳定的654321 #include <reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char sbit dula = P2 ^ 6; sbit wela = P2 ^ 7; uchar code du_table[] = { 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71 }; uchar code we_table[] = { 0xfe,0xfd,0xfb,0xf7,0xef,0xdf }; void delayms(uint); void main() { uchar num; for (num = 6; num > 0; num--) { dula = 1; P0 = du_table[num]; dula = 0; P0 = 0xff; wela = 1; P0 = we_table[6 - num]; wela = 0; delayms(1); } } void delayms(uint xms) { uint i, j; for (i = xms; i > 0; i--) for (j = 110; j > 0; j--) ; } 7. 用动态扫描方法和定时器1在数码管的前三位显示出秒表,精确到1%秒,即后两位显示1%秒,一直循环下去 #include <reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char sbit dula = P2 ^ 6; sbit wela = P2 ^ 7; uint tt; uchar code du_table[] = { 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71 }; void display(uint); void delayms(uint); void main() { TMOD = 0x10; TH1 = (65536 - 10000) / 256; TL1 = (65536 - 10000) % 256; EA = 1; ET1 = 1; TR1 = 1; while (1) { display(tt); } } void T1_timer() interrupt 3 { TH1 = (65536 - 10000) / 256; TL1 = (65536 - 10000) % 256; tt++; if (tt == 1000) tt = 0; } void display(uint num) { dula = 1; P0 = du_table[num / 100]; dula = 0; P0 = 0xff; wela = 1; P0 = 0xfe; wela = 0; delayms(1); dula = 1; P0 = du_table[num % 100 / 10]; dula = 0; P0 = 0xff; wela = 1; P0 = 0xfd; wela = 0; delayms(1); dula = 1; P0 = du_table[num % 100 % 10]; dula = 0; P0 = 0xff; wela = 1; P0 = 0xfb; wela = 0; delayms(1); } void delayms(uint xms) { uint i, j; for (i = xms; i > 0; i--) for (j = 110; j > 0; j--) ; } 8. 利用动态扫描和定时器1在数码管上显示出从765432开始以1/10秒的速度往下递减直至765398并保持显示此数,与此同时利用定时器0以500MS速度进行流水灯从上至下移动,当数码管上数减到停止时,实验板上流水灯也停止然后全部开始闪烁,3秒后(用T0定时)流水灯全部关闭、数码管上显示出“HELLO”。到此保持住 #include <reg52.h> #include <intrins.h> #define uint unsigned int #define uchar unsigned char sbit dula = P2 ^ 6; sbit wela = P2 ^ 7; uchar code table[] = { 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71, 0x76,0x79,0x38,0x3f,0 }; uchar temp, t0, t1, bai, shi, ge, flag, flag1; uint shu; void init(); void display(uchar, uchar, uchar, uchar, uchar, uchar); void delayms(uint); void main() { init(); while (1) { if (flag1 != 1) display(7,6,5,bai,shi,ge); else display(16,17,18,18,19,20); } } void init() { shu = 432; temp = 0xfe; P1 = temp; TMOD = 0x11; TH0 = (65536 - 50000) / 256; TL0 = (65536 - 50000) % 256; TH1 = (65536 - 50000) / 256; TL1 = (65536 - 50000) % 256; EA = 1; ET0 = 1; ET1 = 1; TR0 = 1; TR1 = 1; } void T0_timer() interrupt 1 { TH0 = (65536 - 50000) / 256; TL0 = (65536 - 50000) % 256; t0++; if (flag != 1) { if (t0 == 10) { t0 = 1; temp = _crol_(temp,1); P1 = temp; } } else { if (t0 % 4 == 0) { P1 = ~P1; } if (t0 == 60) { TR0 = 0; P1 = 0xff; flag1 = 1; } } } void T1_timer() interrupt 3 { TH1 = (65536 - 50000) / 256; TL1 = (65536 - 50000) % 256; t1++; if (t1 == 2) { t1 = 0; shu--; bai = shu / 100; shi = shu % 100 / 10; ge = shu % 10; if (shu == 398) { TR0 = 0; TH0 = (65536 - 50000) / 256; TL0 = (65536 - 50000) % 256; TR0 = 1; flag = 1; t0 = 0; P1 = 0xff; TR1 = 0; } } } void display(uchar aa, uchar bb, uchar cc, uchar bai, uchar shi, uchar ge) { dula = 1; P0 = table[aa]; dula = 0; P0 = 0xff; wela = 1; P0 = 0xfe; wela = 0; delayms(1); dula = 1; P0 = table[bb]; dula = 0; P0 = 0xff; wela = 1; P0 = 0xfd; wela = 0; delayms(1); dula = 1; P0 = table[cc]; dula = 0; P0 = 0xff; wela = 1; P0 = 0xfb; wela = 0; delayms(1); dula = 1; P0 = table[bai]; dula = 0; P0 = 0xff; wela = 1; P0 = 0xf7; wela = 0; delayms(1); dula = 1; P0 = table[shi]; dula = 0; P0 = 0xff; wela = 1; P0 = 0xef; wela = 0; delayms(1); dula = 1; P0 = table[ge]; dula = 0; P0 = 0xff; wela = 1; P0 = 0xdf; wela = 0; delayms(1); } 原文链接: |