单片机==独立按键+数码管计数+扫描法(8)

来源:互联网 发布:网络工程和网络布线 编辑:程序博客网 时间:2024/06/06 02:26
#include <reg52.h>#include "delay.h"sbit key1 = P2^0;sbit seg_select = P1^0;sbit bit_select = P1^1;#define SEGPORT P0unsigned char tempdata[2];unsigned char segdata[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};void timer0_init(void){    EA = 1;    TMOD |= 0x01;    TH0 = (65536 - 10000) / 256;    TL0 = (65536 - 10000) % 256;    ET0 = 1;    TR0 = 1;}void display(){    static unsigned char i = 0;    SEGPORT = 0x0;    seg_select = 1;    seg_select = 0;    SEGPORT = 0xff;    bit_select = 1;    bit_select = 0;    SEGPORT = tempdata[i];    seg_select = 1;    seg_select = 0;    SEGPORT = bitdata[i];    bit_select = 1;    bit_select = 0;    i++;    if(2 == i)    {        i = 0;    }}void timer0_isr(void) interrupt 1{    TH0 = (65536 - 10000) / 256;    TL0 = (65536 - 10000) % 256;    display();}void main(){    unsigned char disdata = 0;;    timer0_init();    while(1)    {        delay_ms(10);        if(0 == key1)        {            while(!key1)            delay_ms(10);            while(!key1);            disdata++;            if(100 == disdata)            {                disdata = 0;            }        }        tempdata[0] = segdata[disdata / 10];        tempdata[1] = segdata[disdata % 10];    }}
0 0
原创粉丝点击