单片机==独立按键+数码管计数+中断法(9)

来源:互联网 发布:建筑软件自学网 编辑:程序博客网 时间:2024/06/05 14:09
#include <reg52.h>#include "delay.h"sbit key1 = P3^2;sbit seg_select = P1^0;sbit bit_select = P1^1;#define SEGPORT P0unsigned char tempdata[2] = {0x3f,0x3f};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 int0_init(){    EA = 1;    IT0 = 1;    EX0 = 1;}void int0_isr(void) interrupt 0{    static unsigned char disdata = 0;    EX0 = 0;    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];        EX0 = 1;}void main(){    timer0_init();  int0_init();    while(1);}
0 0
原创粉丝点击