51单片机定时器控制LED闪烁

#include <reg51.h>

#define LED_P1_1 0x01
#define LED_P1_2 0x02
#define LED_P1_3 0x04
#define LED_P1_4 0x08
#define LED_P1_5 0x10
#define LED_P1_6 0x20
#define LED_P1_7 0x40
#define LED_P1_8 0x80

unsigned int count_D1_D2 = 0;
unsigned int count_D3_D4 = 0;
unsigned int count_D5_D6 = 0;
unsigned int count_D7_D8 = 0;

void init_timer(void)
{
    TMOD = 0x01; 
    TH0 = (65536 - 200) >> 8; 
    TL0 = (65536 - 200) & 0xFF; 
    TR0 = 1; 
    EA = 1; 
    ET0 = 1; 
}

void main()
{
    P1 = 0xFF; 
    init_timer();
    
    while(1)
    {
        
    }
}


void timer0_isr() interrupt 1
{
    TH0 = (65536 - 200) >> 8; 
    TL0 = (65536 - 200) & 0xFF; 
    
    
    count_D1_D2++;
    count_D3_D4++;
    count_D5_D6++;
    count_D7_D8++;
    
    
    if(count_D1_D2 >= 1000) 
    {
        count_D1_D2 = 0;
        P1 ^= LED_P1_1 | LED_P1_2; 
    }

    if(count_D3_D4 >= 2500) 
    {
        count_D3_D4 = 0;
        P1 ^= LED_P1_3 | LED_P1_4; 
    }

    if(count_D5_D6 >= 5000) 
    {
        count_D5_D6 = 0;
        P1 ^= LED_P1_5 | LED_P1_6; 
    }

    if(count_D7_D8 >= 10000) 
    {
        count_D7_D8 = 0;
        P1 ^= LED_P1_7 | LED_P1_8; 
    }
}
#include <reg52.h>  
sbit LED1 = P1^0;
sbit LED2 = P1^1;
sbit LED3 = P1^2;
sbit LED4 = P1^3;
sbit LED5 = P1^4;
sbit LED6 = P1^5;
sbit LED7 = P1^6;
sbit LED8 = P1^7;




unsigned int timer_count = 0; 


void Timer0_ISR(void) interrupt 1 {
    TH0 = 0xee;  
    TL0 = 0x00;         
    
    timer_count++;  
    
    if (timer_count ==200 ) {
P1=0xff;			
        LED1 = ~LED1;  
        LED2 = ~LED2; 
			
        			
    }
    if (timer_count  == 500) {  
 P1=0xff;
        LED3 = ~LED3;  
        LED4 = ~LED4;  
    }
		if (timer_count  == 1000) {  
			  P1=0xff;
 
			  LED1 = ~LED1;  
        LED2 = ~LED2;
        LED3 = ~LED3;  
        LED4 = ~LED4;  
			  LED5 = ~LED5;
			  LED6 = ~LED6;
			  
    }
		if (timer_count  == 2000) {  
			P1=0xff;
 
			  LED1 = ~LED1;  
        LED2 = ~LED2;
        LED3 = ~LED3;  
        LED4 = ~LED4;  
			  LED5 = ~LED5;
			  LED6 = ~LED6;
			  LED7 = ~LED7;
			  LED8 = ~LED8;
			  timer_count=0;
    }
   
}

void main(void) {
  
    TMOD |= 0x01;  
    

	P1=0xff;
    TH0 = 0x00;
    TL0 = 0x00;
    ET0 = 1;
    EA = 1;
    TR0=1;

    while (1) {

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值