单片机C语言之中断

#include <reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;

//timer1
void timer1_int(){ 
    TMON |= 0x10;//M0 set 1
    TMON &= 0xdf;//M1 set 0
    TH1 = 0xfc;
    TL1 = 0x67;
    TR1 = 1;
}

//interrupt initialize
void int_init(){
    ET1 = 1;
    EA = 1;
}

//Digital Tube
code uint8 number[] = {0xc0,0xf9,0xa4,0xb0,
                       0x99,0x92,0x82,0xf8,
                       0x80,0x90,0x88,0x83,
                       0xa7,0xa1,0x86,0x8e}
uint8 a[6];
sbit ADDR0 = P1^0,ADDR1 = P1^1,ADDR2 = p1^2;//3-8 decoder
void refresh_led()
{
    static uint16 j = 0;
    switch(j){
        case 0:ADDR0=0;ADDR1=0;ADDR2=0;j++;P0=num[a[0]];break;
        case 1:ADDR0=1;ADDR1=0;ADDR2=0;j++;P0=num[a[1]];break;
        case 2:ADDR0=0;ADDR1=1;ADDR2=0;j++;P0=num[a[2]];break;
        case 3:ADDR0=0;ADDR1=1;ADDR2=1;j++;P0=num[a[3]];break;
        case 4:ADDR0=1;ADDR1=0;ADDR2=0;j++;P0=num[a[4]];break;
        case 5:ADDR0=1;ADDR1=0;ADDR2=1;j++;P0=num[a[5]];break;
        default:break;
    }
}

sbit ADDR3 = P1^3,ENLED = P1^4;
main(){
    ENLED =0;ADDR3=1;
    timer1_init();
    int_init();
    while(1);
}

//ET1 interrupt level is 3.vector address = 8 * interrupt level +3
uint16 counter = 0;
void interrupt_timer1() interrupt 3
{
    static uint32 sec=0;//static variable: only first assignment works
    //TF1 clear automatically when interrupt
    TH1 = 0xFC;
    TL1 = 0x67;
    counter++;
    if(1000 == counter){
        sec++;
        counter = 0;
        unint 32 tmp = sec;
        for(uint16 i=0;i<6;i++){
            a[i]=tmp%10;
            tmp /= 10;
        }
    }
    refresh_led();
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值