定时器2
作者:互联网
去掉鬼影和数字变换抖动的定时器
数码管会有两个现象,
1、不显示的段有荧光,俗称鬼影
原因,38译码器切换的时候,由于运行速度太快,上一条指令的瞬时值未消除产生的。
解决方法:在数码管显示值前,清0
2、数码管变化的时候,其它抖动
原因:程序在计算的时候浪费了时间,数码管刷新不是以1毫秒进行的,时间长数码管显示拉动。
解决方法:使用中断优先级
EA:启动中断
ET0:打开定时器中断
中断函数的说明:
void InterruptTimer0() interrupt 1
interrupt:中断函数,
1:中断函数的编号。
中断函数的编号x, 计算方法,x*8+3 = 中断向量地址、
#include <reg52.h> /*38译码器选位输入*/ sbit ADDR0 = P2^5; sbit ADDR1 = P2^6; sbit ADDR2 = P2^7; unsigned int conter; unsigned char l; /*数码管真值表*/ unsigned char led_display[16] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71}; /*存储LED变化时的值*/ unsigned char led_buffer[10] = {0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00}; unsigned char vector_38[8] = {0x00, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xE0}; void main() { unsigned int sec; /*打开定时器*/ TR0 = 1; TMOD = 0x01; /*打开中断*/ EA = 1; ET0 = 1; TH0 = 0xFC; //1us TL0 = 0x67; conter = 0; /*赋初值*/ while(1) { if (conter >= 1000) { /*更改数码管*/ sec++; conter = 0; led_buffer[7] = led_display[sec%10]; led_buffer[6] = led_display[sec/10%10]; led_buffer[5] = led_display[sec/100%10]; led_buffer[4] = led_display[sec/1000%10]; led_buffer[3] = led_display[sec/10000%10]; led_buffer[2] = led_display[sec/100000%10]; led_buffer[1] = led_display[sec/1000000%10]; led_buffer[0] = led_display[sec/10000000%10]; } } } void InterruptTimer0() interrupt 1 { TH0 = 0xFC; //1us TL0 = 0x67; conter++; P0 = 0x00; if (l == 0) {ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;l++;P1=led_buffer[0];} else if (l == 1) {ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;l++;P1=led_buffer[1];} else if (l == 2) {ADDR0 = 0;ADDR1 = 1;ADDR2 = 0;l++;P1=led_buffer[2];} else if (l == 3) {ADDR0 = 1;ADDR1 = 1;ADDR2 = 0;l++;P1=led_buffer[3];} else if (l == 4) {ADDR0 = 0;ADDR1 = 0;ADDR2 = 1;l++;P1=led_buffer[4];} else if (l == 5) {ADDR0 = 1;ADDR1 = 0;ADDR2 = 1;l++;P1=led_buffer[5];} else if (l == 6) {ADDR0 = 0;ADDR1 = 1;ADDR2 = 1;l++;P1=led_buffer[6];} else if (l == 7) {ADDR0 = 1;ADDR1 = 1;ADDR2 = 1;l++;P1=led_buffer[7];} if (l>7){l=0;} }
标签:定时器,led,buffer,0x00,++,ADDR2,ADDR0 来源: https://blog.51cto.com/yishi/2772693